Compare commits

..

12 Commits

13 changed files with 493 additions and 102 deletions

View File

@ -2,3 +2,5 @@ BasedOnStyle: Microsoft
IndentCaseLabels: true
UseTab: true
IndentWidth: 4
AlignArrayOfStructures: Left
AlignConsecutiveAssignments: true

View File

@ -1,21 +1,29 @@
cmake_minimum_required(VERSION 3.25)
include(FetchContent)
project(JavaBytecodeParser)
include_directories(include)
include_directories(${CMAKE_BINARY_DIR})
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CXX_STANDARD 23)
set(BUILD_TESTS OFF)
set(BUILD_BENCHMARK OFF)
set(CAVM_RUNTIME_CHECK_SUPPORT ON)
add_subdirectory(src)
configure_file(include/Config.in ${CMAKE_BINARY_DIR}/Config.h)
set(CONSTANT_POOL_TAG_SRC ${CONSTANT_POOL_TAG_SRC})
message("src " ${CONSTANT_POOL_TAG_SRC})
add_executable(parser
src/Main.cpp
${CONSTANT_POOL_TAG_SRC}
)
set(CMAKE_CXX_FLAGS "-std=c++${CXX_STANDARD} -O0 -g -Wpedantic")

109
include/Attributes.h Normal file
View File

@ -0,0 +1,109 @@
#ifndef __CAVM_ATTRIBUTES_H
#define __CAVM_ATTRIBUTES_H
#include <ClassFormat.h>
#include <SpecTypes.h>
#include <Utils/CRC32_CompileTime.hpp>
#include <cstdint>
namespace Attributes
{
enum class ClassAttributes : uint32_t
{
SOURCE_FILE = "SourceFile"_crc32,
INNER_CLASSES = "InnerClasses"_crc32,
ENCLOSING_METHOD = "EnclosingMethod"_crc32,
SOURCE_DEBUG_EXTENSION = "SourceDebugExtension"_crc32,
BOOTSTRAP_METHODS = "BootstrapMethods"_crc32,
MODULE = "Module"_crc32,
MODULE_PACKAGES = "ModulePackages"_crc32,
MODULE_MAIN_CLASS = "ModuleMainClass"_crc32,
NEST_HOST = "NestHost"_crc32,
NEST_MEMBERS = "NestMembers"_crc32,
RECORD = "Record"_crc32,
PERMITTED_SUBCLASSES = "PermittedSubclasses"_crc32,
SYNTHETIC = "Synthetic"_crc32,
DEPRECATED = "Deprecated"_crc32,
SIGNATURE = "Signature"_crc32,
RUNTIME_VISIBLE_ANNOTATIONS = "RuntimeVisibleAnnotations"_crc32,
};
enum class FieldAttributes : uint32_t
{
CONSTANT_VALUE = "ConstantValue"_crc32,
SYNTHETIC = "Synthetic"_crc32,
DEPRECATED = "Deprecated"_crc32,
SIGNATURE = "Signature"_crc32,
RUNTIME_VISIBLE_ANNOTATIONS = "RuntimeVisibleAnnotations"_crc32,
RUNTIME_INVISIBLE_ANNOTATIONS = "RuntimeInvisibleAnnotations"_crc32,
RUNTIME_VISIBLE_TYPE_ANNOTATIONS = "RuntimeVisibleTypeAnnotations"_crc32,
RUNTIME_INVISIBLE_TYPE_ANNOTATIONS = "RuntimeInvisibleTypeAnnotations"_crc32,
};
enum class MethodAttributes : uint32_t
{
CODE = "Code"_crc32,
EXCEPTIONS = "Exceptions"_crc32,
RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS = "RuntimeVisibleParameterAnnotations"_crc32,
RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS = "RuntimeInvisibleParameterAnnotations"_crc32,
ANNOTATION_DEFAULT = "AnnotationDefault"_crc32,
METHOD_PARAMETERS = "MethodParameters"_crc32,
SYNTHETIC = "Synthetic"_crc32,
DEPRECATED = "Deprecated"_crc32,
SIGNATURE = "Signature"_crc32,
RUNTIME_VISIBLE_ANNOTATIONS = "RuntimeVisibleAnnotations"_crc32,
RUNTIME_INVISIBLE_ANNOTATIONS = "RuntimeInvisibleAnnotations"_crc32,
RUNTIME_VISIBLE_TYPE_ANNOTATIONS = "RuntimeVisibleTypeAnnotations"_crc32,
RUNTIME_INVISIBLE_TYPE_ANNOTATIONS = "RuntimeInvisibleTypeAnnotations"_crc32,
};
enum class RecordComponentAttributes : uint32_t
{
RUNTIME_VISIBLE_ANNOTATIONS = "RuntimeVisibleAnnotations"_crc32,
RUNTIME_INVISIBLE_ANNOTATIONS = "RuntimeInvisibleAnnotations"_crc32,
RUNTIME_VISIBLE_TYPE_ANNOTATIONS = "RuntimeVisibleTypeAnnotations"_crc32,
RUNTIME_INVISIBLE_TYPE_ANNOTATIONS = "RuntimeInvisibleTypeAnnotations"_crc32,
};
enum class CodeAttributes : uint32_t
{
LINE_NUMBER_TABLE = "LineNumberTable"_crc32,
LOCAL_VARIABLE_TABLE = "LocalVariableTable"_crc32,
LOCAL_VARIABLE_TYPE_TABLE = "LocalVariableTypeTable"_crc32,
STACK_MAP_TABLE = "StackMapTable"_crc32,
RUNTIME_VISIBLE_TYPE_ANNOTATIONS = "RuntimeVisibleTypeAnnotations"_crc32,
RUNTIME_INVISIBLE_TYPE_ANNOTATIONS = "RuntimeInvisibleTypeAnnotations"_crc32,
};
struct CODE_Attribute : BaseAttribute
{
private:
typedef struct
{
u2 start_pc;
u2 end_pc;
u2 handler_pc;
u2 catch_type;
} exception_table;
public:
u2 max_stack;
u2 max_locals;
u4 code_length;
std::vector<uint8_t> code;
u2 exception_table_length;
std::vector<exception_table> exceptions;
u2 attributes_count;
std::vector<attribute_info> attributes;
};
struct SOURCE_FILE_Attribute : BaseAttribute
{
u2 sourcefile_index;
};
attribute_info AttributesParser(JavaClassFormat &jc);
} // namespace Attributes
#endif

View File

@ -2,6 +2,7 @@
#define __JAVA_PARSER_CLASS_FORMAT_H
#include <SpecTypes.h>
#include <any>
#include <cstdlib>
#include <vector>
#include <ConstantPoolTags/Tags.h>
@ -57,30 +58,36 @@ enum class ACCESS_FLAGS
};
} // namespace Method
typedef struct
struct attribute_info
{
u2 attribute_name_index;
u4 attribute_length;
std::vector<u1> info;
} attribute_info;
};
typedef struct
struct BaseAttribute
{
u2 attribute_name_index;
u4 attribute_length;
};
struct method_info
{
u2 access_flags;
u2 name_index;
u2 descriptor_index;
u2 attributes_count;
std::vector<attribute_info> attributes;
} method_info;
};
typedef struct
struct field_info
{
u2 access_flags;
u2 name_index;
u2 descriptor_index;
u2 attributes_count;
std::vector<attribute_info> attributes;
} field_info;
};
struct JavaClassFormat
{
@ -88,7 +95,7 @@ struct JavaClassFormat
u2 minor;
u2 major;
u2 constant_pool_count;
std::vector<ConstantPoolTags::ConstantPool> constant_pool;
std::vector<std::any> constant_pool;
u2 access_flags;
u2 this_class;
u2 super_class;

6
include/Config.in Normal file
View File

@ -0,0 +1,6 @@
#ifndef __CAVM_CONFIG_H
#define __CAVM_CONFIG_H
#define CAVM_RUNTIME_CHECK_SUPPORT @CAVM_RUNTIME_CHECK_SUPPORT@
#endif

View File

@ -84,7 +84,7 @@ struct ConstantNameAndTypeInfo : ConstantPoolInfo
u2 descriptor_index;
};
void ConstantPoolParser(ConstantPool &p, JavaClassFormat &jc);
void ConstantPoolParser(ConstantPoolTags::Tags t, JavaClassFormat &jc);
}
#endif

View File

@ -0,0 +1,104 @@
#ifndef __CAVM_CRC32_COMPILETIME_HPP
#define __CAVM_CRC32_COMPILETIME_HPP
// Source: https://gist.github.com/oktal/5573082
#include <stdlib.h>
#define CRC32 0xFFFFFFFF
constexpr unsigned int crc32_table[] = {
0, 0x77073096, 0xEE0E612C, 0x990951BA,
0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3,
0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988,
0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91,
0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE,
0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC,
0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5,
0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172,
0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B,
0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940,
0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59,
0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116,
0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F,
0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924,
0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D,
0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A,
0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433,
0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818,
0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01,
0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E,
0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457,
0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C,
0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65,
0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2,
0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB,
0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0,
0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9,
0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086,
0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4,
0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD,
0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A,
0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683,
0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8,
0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1,
0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE,
0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7,
0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC,
0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5,
0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252,
0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B,
0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60,
0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79,
0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236,
0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F,
0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04,
0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D,
0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A,
0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713,
0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38,
0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21,
0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E,
0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777,
0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C,
0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45,
0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2,
0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB,
0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0,
0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6,
0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF,
0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94,
0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D
};
template<unsigned int CRC, char ...Chars> struct Crc32Impl {
};
template<unsigned int CRC, char Head, char ...Tail> struct Crc32Impl<CRC, Head, Tail...> {
static constexpr unsigned int value = Crc32Impl<
crc32_table[static_cast<unsigned char>(CRC) ^ static_cast<unsigned char>(Head)]
^ (CRC >> 8), Tail...>::value;
};
template<unsigned int CRC> struct Crc32Impl<CRC> {
static constexpr unsigned int value = CRC ^ 0xFFFFFFFF;
};
template<char ...Chars> using Crc32 = Crc32Impl<0xFFFFFFFF, Chars...>;
constexpr unsigned int crc32_rec(unsigned int crc, const char *s) {
return *s == 0 ? crc ^ 0xFFFFFFFF :
crc32_rec(crc32_table[static_cast<unsigned char>(crc) ^
static_cast<unsigned char>(*s)]
^ (crc >> 8), s + 1);
}
constexpr unsigned long long operator "" _crc32(const char *s, size_t len) {
return crc32_rec(0xFFFFFFFF, s);
}
#endif

View File

@ -1,24 +1,32 @@
#ifndef __JAVA_PARSER_UTILS_UTILS_H
#define __JAVA_PARSER_UTILS_UTILS_H
#ifndef __CAVM_UTILS_UTILS_H
#define __CAVM_UTILS_UTILS_H
#include <vector>
#include <Config.h>
#include <SpecTypes.h>
#include <cassert>
#include <cstdint>
#include <stdexcept>
#include <SpecTypes.h>
#include <typeinfo>
#include <vector>
#define TYPE(x) typeid(x).name()
#define VEC_ELEM_TYPE(x) x.type().name()
#ifdef CAVM_RUNTIME_CHECK_SUPPORT
#define ASSERT(x) assert(x)
#else
#define ASSERT(x)
#endif
namespace Utils
{
template <typename T, typename X>
ALWAYS_INLINE T M_POW(T x, X power)
template <typename T, typename X> ALWAYS_INLINE T M_POW(T x, X power)
{
T ret = 1;
if (power == 0)
return 1;
for (int i = 0; i < power; i++)
{
ret *= x;
@ -35,8 +43,7 @@ ALWAYS_INLINE T M_POW(T x, X power)
return ret;
}
template<typename T = unsigned int>
ALWAYS_INLINE T Hex2Int(T x)
template <typename T = unsigned int> ALWAYS_INLINE T Hex2Int(T x)
{
T ret = 0;
int digits = 0;
@ -52,8 +59,7 @@ ALWAYS_INLINE T Hex2Int(T x)
return ret;
}
template <typename T>
ALWAYS_INLINE const char* Int2Hex(T x)
template <typename T> ALWAYS_INLINE const char *Int2Hex(T x)
{
constexpr const char *digits[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};
@ -79,8 +85,7 @@ ALWAYS_INLINE const char* Int2Hex(T x)
return data;
}
template <typename T>
ALWAYS_INLINE std::vector<T> Read(std::vector<T> &vec, int start, int end)
template <typename T> ALWAYS_INLINE std::vector<T> Read(std::vector<T> &vec, int start, int end)
{
std::vector<T> ret;
@ -92,8 +97,7 @@ ALWAYS_INLINE std::vector<T> Read(std::vector<T> &vec, int start, int end)
return ret;
}
template <typename T>
ALWAYS_INLINE std::vector<T> ReadFromStart(std::vector<T> &vec, int end)
template <typename T> ALWAYS_INLINE std::vector<T> ReadFromStart(std::vector<T> &vec, int end)
{
std::vector<T> ret = Read(vec, 0, end);
for (int i = 0; i < end; i++)
@ -101,9 +105,7 @@ ALWAYS_INLINE std::vector<T> ReadFromStart(std::vector<T> &vec, int end)
return ret;
}
template <typename T, typename vec_type=uint8_t>
ALWAYS_INLINE T vecToVal(std::vector<vec_type> &vec)
template <typename T, typename vec_type = uint8_t> ALWAYS_INLINE T vecToVal(std::vector<vec_type> &vec)
{
if (vec.size() > sizeof(T))
{
@ -120,19 +122,17 @@ ALWAYS_INLINE T vecToVal(std::vector<vec_type> &vec)
return ret;
}
template <typename T, typename vec_type>
ALWAYS_INLINE T ReadFromStartIntoVal(std::vector<vec_type> &vec, int end)
template <typename T, typename vec_type> ALWAYS_INLINE T ReadFromStartIntoVal(std::vector<vec_type> &vec, int end)
{
std::vector<uint8_t> val = ReadFromStart<vec_type>(vec, end);
return vecToVal<T>(val);
}
template <typename T, typename vec_type>
ALWAYS_INLINE T ReadFromStartIntoVal(std::vector<vec_type> &vec)
template <typename T, typename vec_type> ALWAYS_INLINE T ReadFromStartIntoVal(std::vector<vec_type> &vec)
{
std::vector<uint8_t> val = ReadFromStart<vec_type>(vec, sizeof(T));
return vecToVal<T>(val);
}
}
} // namespace Utils
#endif

52
src/AttributeParser.cpp Normal file
View File

@ -0,0 +1,52 @@
#include "ConstantPoolTags/Tags.h"
#include <Globals.h>
#include <Utils/Utils.h>
#include <Attributes.h>
#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <cassert>
#include <string>
#include <Utils/CRC32_CompileTime.hpp>
attribute_info Attributes::AttributesParser(JavaClassFormat& jc)
{
attribute_info ret;
ret.attribute_name_index = Utils::ReadFromStartIntoVal<uint16_t>(Globals::buffer());
ret.attribute_length = Utils::ReadFromStartIntoVal<uint32_t>(Globals::buffer());
ASSERT(ret.attribute_length.ToHostFormat() > 0);
for (int i = 0; i < ret.attribute_length.ToHostFormat(); i++)
{
ret.info.push_back(Utils::ReadFromStartIntoVal<uint8_t>(Globals::buffer(), 1));
}
std::reverse(ret.info.begin(), ret.info.end());
int idx = ret.info.data()->ToHostFormat();
ASSERT(VEC_ELEM_TYPE(jc.constant_pool[ret.attribute_name_index.ToHostFormat() - 1]) == TYPE(ConstantPoolTags::ConstantUtf8Info));
std::string str = reinterpret_cast<const char*>(std::any_cast<ConstantPoolTags::ConstantUtf8Info>(jc.constant_pool[ret.attribute_name_index.ToHostFormat() - 1]).bytes);
switch ((ClassAttributes)crc32_rec(CRC32, str.c_str()))
{
case ClassAttributes::SOURCE_FILE:
{
ASSERT(VEC_ELEM_TYPE(jc.constant_pool[idx - 1]) == TYPE(ConstantPoolTags::ConstantUtf8Info));
std::cout << "Source File: "
<< std::any_cast<ConstantPoolTags::ConstantUtf8Info>(jc.constant_pool[idx - 1]).bytes
<< std::endl;
SOURCE_FILE_Attribute attr;
// attr.
break;
}
default:
std::cerr << "Unknown attribute: \"" << str << "\"" << std::endl;
exit(EXIT_FAILURE);
}
// switch ()
return ret;
}

View File

@ -2,7 +2,11 @@ add_subdirectory(ConstantPoolTags)
add_subdirectory(FieldInfo)
add_subdirectory(Method)
set(CWD ${CMAKE_CURRENT_LIST_DIR})
set(CONSTANT_POOL_TAG_SRC
${CWD}/Main.cpp
${CWD}/AttributeParser.cpp
${CONSTANT_POOL_TAG_SRC}
PARENT_SCOPE

View File

@ -7,15 +7,15 @@
#include <ClassFormat.h>
#include <ostream>
void ConstantPoolTags::ConstantPoolParser(ConstantPool &p, JavaClassFormat &jc)
void ConstantPoolTags::ConstantPoolParser(ConstantPoolTags::Tags t, JavaClassFormat &jc)
{
switch (p.tag)
switch (t)
{
case Tags::Utf8: {
// std::cout << "FIXME: Implement Utf8 Constant Pool Tag" << std::endl;
ConstantUtf8Info ref = static_cast<ConstantUtf8Info>(p.info);
ConstantUtf8Info ref;
std::vector<uint8_t> length = Utils::ReadFromStart(Globals::buffer(), 2);
ref.tag = p.tag;
ref.tag = t;
ref.length = Utils::vecToVal<uint16_t>(length);
unsigned char x = Utils::ReadFromStartIntoVal<unsigned char>(Globals::buffer(), 1);
@ -49,9 +49,7 @@ void ConstantPoolTags::ConstantPoolParser(ConstantPool &p, JavaClassFormat &jc)
if (i != ref.length.ToHostFormat() - 1)
x = Utils::ReadFromStartIntoVal<unsigned char>(Globals::buffer(), 1);
}
p.info = static_cast<ConstantPoolInfo>(ref);
jc.constant_pool.push_back(p);
jc.constant_pool.push_back(ref);
break;
}
@ -69,44 +67,40 @@ void ConstantPoolTags::ConstantPoolParser(ConstantPool &p, JavaClassFormat &jc)
exit(EXIT_SUCCESS);
case Tags::Class: {
// std::cout << "FIXME: Implement Class Constant Pool Tag" << std::endl;
ConstantClassInfo ref = static_cast<ConstantClassInfo>(p.info);
ConstantClassInfo ref;
// std::vector<uint8_t> name_index = Utils::ReadFromStart(Globals::buffer(), 2);
ref.tag = p.tag;
ref.tag = t;
ref.name_index = Utils::ReadFromStartIntoVal<uint16_t>(Globals::buffer(), 2);
p.info = static_cast<ConstantPoolInfo>(ref);
jc.constant_pool.push_back(p);
jc.constant_pool.push_back(ref);
// exit(EXIT_SUCCESS);
break;
}
case Tags::String: {
// std::cout << "FIXME: Implement String Constant Pool Tag" << std::endl;
ConstantStringInfo ref = static_cast<ConstantStringInfo>(p.info);
ref.tag = p.tag;
ConstantStringInfo ref;
ref.tag = t;
// std::cout << std::dec << Globals::buffer().size() << std::endl;
ref.string_index = Utils::ReadFromStartIntoVal<uint16_t>(Globals::buffer(), 2);
// std::cout << std::dec << ref.string_index.ToHostFormat() << std::endl;
p.info = static_cast<ConstantPoolInfo>(ref);
// std::cout << Globals::buffer().size() << std::endl;
jc.constant_pool.push_back(p);
jc.constant_pool.push_back(ref);
// exit(EXIT_SUCCESS);
break;
}
case Tags::Fieldref: {
// std::cout << "FIXME: Implement Fieldref Constant Pool Tag" << std::endl;
ConstantFieldrefInfo ref = static_cast<ConstantFieldrefInfo>(p.info);
ref.tag = p.tag;
ConstantFieldrefInfo ref;
ref.tag = t;
ref.class_index = Utils::ReadFromStartIntoVal<uint16_t>(Globals::buffer(), 2);
ref.name_and_type_index = Utils::ReadFromStartIntoVal<uint16_t>(Globals::buffer(), 2);
p.info = static_cast<ConstantPoolInfo>(ref);
jc.constant_pool.push_back(p);
jc.constant_pool.push_back(ref);
// exit(EXIT_SUCCESS);
break;
@ -115,15 +109,13 @@ void ConstantPoolTags::ConstantPoolParser(ConstantPool &p, JavaClassFormat &jc)
{
// std::cout << "FIXME: Implement Methodref Constant Pool Tag" << std::endl;
// ConstantMethodrefInfo ref = *reinterpret_cast<ConstantMethodrefInfo*>(p->info);
ConstantMethodrefInfo ref = static_cast<ConstantMethodrefInfo>(p.info);
ConstantMethodrefInfo ref;
ref.tag = p.tag;
ref.tag = t;
ref.class_index = Utils::ReadFromStartIntoVal<uint16_t>(Globals::buffer(), 2);
ref.name_and_type_index = Utils::ReadFromStartIntoVal<uint16_t>(Globals::buffer(), 2);
p.info = static_cast<ConstantPoolInfo>(ref);
jc.constant_pool.push_back(p);
jc.constant_pool.push_back(ref);
// exit(EXIT_SUCCESS);
break;
@ -133,14 +125,13 @@ void ConstantPoolTags::ConstantPoolParser(ConstantPool &p, JavaClassFormat &jc)
exit(EXIT_SUCCESS);
case Tags::NameAndType: {
// std::cout << "FIXME: Implement NameAndType Constant Pool Tag" << std::endl;
ConstantNameAndTypeInfo ref = static_cast<ConstantNameAndTypeInfo>(p.info);
ConstantNameAndTypeInfo ref;
ref.tag = p.tag;
ref.tag = t;
ref.name_index = Utils::ReadFromStartIntoVal<uint16_t>(Globals::buffer(), 2);
ref.descriptor_index = Utils::ReadFromStartIntoVal<uint16_t>(Globals::buffer(), 2);
p.info = static_cast<ConstantPoolInfo>(ref);
jc.constant_pool.push_back(p);
jc.constant_pool.push_back(ref);
// exit(EXIT_SUCCESS);
break;
@ -164,7 +155,7 @@ void ConstantPoolTags::ConstantPoolParser(ConstantPool &p, JavaClassFormat &jc)
std::cout << "FIXME: Implement Package Constant Pool Tag" << std::endl;
exit(EXIT_SUCCESS);
default:
std::cerr << "Unknown tag: " << std::hex << (int)p.tag << " @ byte " << std::dec << Globals::buffer().size() << " from end of class file" << std::endl;
std::cerr << "Unknown tag: " << std::hex << (int)t << " @ byte " << std::dec << Globals::buffer().size() << " from end of class file" << std::endl;
exit(EXIT_FAILURE);
}
}

View File

@ -2,6 +2,7 @@
#include "Methods.h"
#include <ConstantPoolTags/Tags.h>
#include <SpecTypes.h>
#include <any>
#include <cassert>
#include <cstdint>
#include <cstdlib>
@ -10,8 +11,13 @@
#include <Utils/Utils.h>
#include <iostream>
#include <Globals.h>
#include <typeindex>
#include <typeinfo>
#include <Attributes.h>
#define JVM_STACK_SIZE 4096
#define KB 1024
#define MB 1024 * KB
#define JVM_STACK_SIZE 16 * MB
int stack[JVM_STACK_SIZE];
@ -47,7 +53,7 @@ int main(int argc, char **argv)
jc.magic = Utils::ReadFromStartIntoVal<uint32_t>(Globals::buffer(), 4);
assert(jc.magic.ToHostFormat() == 0xcafebabe);
ASSERT(jc.magic.ToHostFormat() == 0xCAFEBABE);
jc.minor = Utils::ReadFromStartIntoVal<uint16_t>(Globals::buffer(), 2);
@ -63,21 +69,23 @@ int main(int argc, char **argv)
for (int i = 0; i < jc.constant_pool_count.ToHostFormat() - 1; i++)
{
ConstantPoolTags::ConstantPool cp;
cp.tag = (ConstantPoolTags::Tags)Utils::ReadFromStartIntoVal<int>(Globals::buffer(), 1);
ConstantPoolTags::Tags tag = (ConstantPoolTags::Tags)Utils::ReadFromStartIntoVal<int>(Globals::buffer(), 1);
ConstantPoolTags::ConstantPoolParser(cp, jc);
ConstantPoolTags::ConstantPoolParser(tag, jc);
}
jc.access_flags = Utils::ReadFromStartIntoVal<uint16_t>(Globals::buffer(), 2);
jc.this_class = Utils::ReadFromStartIntoVal<uint16_t>(Globals::buffer(), 2);
jc.super_class = Utils::ReadFromStartIntoVal<uint16_t>(Globals::buffer(), 2);
jc.interfaces_count = Utils::ReadFromStartIntoVal<uint16_t>(Globals::buffer(), 2);
assert(jc.this_class.ToHostFormat() >= 0);
assert(jc.super_class.ToHostFormat() >= 0);
ASSERT(jc.this_class.ToHostFormat() >= 0);
ASSERT(jc.super_class.ToHostFormat() >= 0);
assert(jc.constant_pool.size() > jc.this_class.ToHostFormat());
assert(jc.constant_pool[jc.this_class.ToHostFormat() - 1].tag == ConstantPoolTags::Tags::Class);
ASSERT(jc.constant_pool.size() > jc.this_class.ToHostFormat());
// std::cout << (int)(jc.constant_pool[jc.this_class.ToHostFormat() - 1]).tag << std::endl;
std::cout << std::dec << (int)std::any_cast<ConstantPoolTags::ConstantClassInfo>(jc.constant_pool[jc.this_class.ToHostFormat() - 1]).tag << std::endl;
// ASSERT(jc.constant_pool[jc.this_class.ToHostFormat() - 1].type().name());
if (jc.super_class.ToHostFormat() != 0)
{
@ -90,8 +98,8 @@ int main(int argc, char **argv)
// item of its ClassFile structure.
// FIXME: Finish implementing the above comment.
assert(jc.constant_pool.size() > jc.super_class.ToHostFormat());
assert(jc.constant_pool[jc.super_class.ToHostFormat() - 1].tag == ConstantPoolTags::Tags::Class);
ASSERT(jc.constant_pool.size() > jc.super_class.ToHostFormat());
ASSERT(jc.constant_pool[jc.super_class.ToHostFormat() - 1].type() == std::type_index(typeid(ConstantPoolTags::ConstantClassInfo)));
}
else
{
@ -105,12 +113,11 @@ int main(int argc, char **argv)
if (jc.access_flags.ToHostFormat() >= (int)JavaClass::ACCESS_FLAGS::ACC_MODULE)
{
assert(jc.access_flags.ToHostFormat() == (int)JavaClass::ACCESS_FLAGS::ACC_MODULE);
ASSERT(jc.access_flags.ToHostFormat() == (int)JavaClass::ACCESS_FLAGS::ACC_MODULE);
// Conditions for when jc.access_flags == JavaClass::ACCESS_FLAGS::ACC_MODULE
// major_version, minor_version: ≥ 53.0 (i.e., Java SE 9 and above)
assert(jc.major.ToHostFormat() >= 53);
assert(jc.minor.ToHostFormat() >= 0);
ASSERT(jc.major.ToHostFormat() >= 53);
ASSERT(jc.minor.ToHostFormat() >= 0);
}
// std::cout << std::dec << (int)(jc.constant_pool[jc.this_class.ToHostFormat()].info.tag) << std::endl;
@ -143,12 +150,64 @@ int main(int argc, char **argv)
// std::cout << std::hex << size - Globals::buffer().size() << std::endl;
}
jc.attributes_count = Utils::ReadFromStartIntoVal<uint16_t>(Globals::buffer());
Attributes::AttributesParser(jc);
std::cout << std::dec << "Attributes Count: " << jc.attributes_count.ToHostFormat() << std::endl;
for (int i = 0; i < jc.constant_pool_count.ToHostFormat() - 1; i++)
{
std::cout << "(" << i << ") " << (int)jc.constant_pool[i].tag << std::endl;
auto x = jc.constant_pool[i].type().name();
std::cout << "(" << i << ") ";
if (x == TYPE(ConstantPoolTags::ConstantUtf8Info))
{
std::cout << "utf8:\t" << std::any_cast<ConstantPoolTags::ConstantUtf8Info>(jc.constant_pool[i]).bytes << std::endl;
}
else if (x == TYPE(ConstantPoolTags::ConstantClassInfo))
{
std::cout << "class" << std::endl;
}
else if (x == TYPE(ConstantPoolTags::ConstantStringInfo))
{
ASSERT((jc.constant_pool[std::any_cast<ConstantPoolTags::ConstantStringInfo>(jc.constant_pool[i])
.string_index.ToHostFormat() -
1])
.type()
.name()
== TYPE(ConstantPoolTags::ConstantUtf8Info));
std::cout
<< "string index:\t" << i << "\t(utf-8 str):\t"
<< std::any_cast<ConstantPoolTags::ConstantUtf8Info>(jc.constant_pool[std::any_cast<ConstantPoolTags::ConstantStringInfo>(jc.constant_pool[i])
.string_index.ToHostFormat() -
1]).bytes
<< std::endl;
}
else if (x == TYPE(ConstantPoolTags::ConstantFieldrefInfo))
{
std::cout << "fieldref" << std::endl;
}
else if (x == TYPE(ConstantPoolTags::ConstantMethodrefInfo))
{
std::cout << "methodref" << std::endl;
}
else if (x == TYPE(ConstantPoolTags::ConstantNameAndTypeInfo))
{
std::cout << "nameandtype" << std::endl;
}
else
{
std::cout << "Unimplemented tag " << jc.constant_pool[i].type().name() << std::endl;
exit(EXIT_FAILURE);
}
}
std::cout << &jc.constant_pool[30].info << std::endl;
// std::cout << std::any_cast<ConstantPoolTags::ConstantUtf8Info>(jc.constant_pool[29]).bytes << std::endl;
std::cout << Globals::buffer().size() << std::endl;
// std::cout << &std::any_cast<ConstantPoolTags::ConstantPool>(jc.constant_pool[30]).info << std::endl;
// delete[] ((ConstantPoolTags::ConstantUtf8Info*)(&jc.constant_pool[30].info))->bytes;
// std::cout << jc.methods_count.ToHostFormat() << std::endl;

View File

@ -1,13 +1,16 @@
#include "ConstantPoolTags/Tags.h"
#include <Globals.h>
#include <Utils/Utils.h>
#include "Attributes.h"
#include <ClassFormat.h>
#include <SpecTypes.h>
#include <ConstantPoolTags/Tags.h>
#include <Globals.h>
#include <Methods.h>
#include <SpecTypes.h>
#include <Utils/Utils.h>
#include <any>
#include <cassert>
#include <cstdint>
#include <string>
#include <cstring>
#include <iostream>
#include <string>
void Methods::MethodParser(method_info &p, JavaClassFormat &jc)
{
@ -22,17 +25,63 @@ void Methods::MethodParser(method_info &p, JavaClassFormat &jc)
for (int i = 0; i < p.attributes_count.ToHostFormat(); i++)
{
attribute_info a;
Attributes::CODE_Attribute a;
a.attribute_name_index = Utils::ReadFromStartIntoVal<uint16_t>(Globals::buffer());
a.attribute_length = Utils::ReadFromStartIntoVal<uint32_t>(Globals::buffer());
for (int j = 0; j < a.attribute_length.ToHostFormat(); j++)
auto v = jc.constant_pool[a.attribute_name_index.ToHostFormat() - 1];
ASSERT(VEC_ELEM_TYPE(v) == TYPE(ConstantPoolTags::ConstantUtf8Info));
ConstantPoolTags::ConstantUtf8Info str = std::any_cast<ConstantPoolTags::ConstantUtf8Info>(v);
ASSERT(strcmp(reinterpret_cast<const char *>(str.bytes), "Code") == 0);
u4 attribute_length = Utils::ReadFromStartIntoVal<uint32_t>(Globals::buffer());
std::cout << "(attr)" << a.attribute_name_index.ToHostFormat() << std::endl;
a.max_stack = Utils::ReadFromStartIntoVal<uint16_t>(Globals::buffer());
a.max_locals = Utils::ReadFromStartIntoVal<uint16_t>(Globals::buffer());
a.code_length = Utils::ReadFromStartIntoVal<uint32_t>(Globals::buffer());
for (int i = 0; i < a.code_length.ToHostFormat(); i++)
{
a.info.push_back(Utils::ReadFromStartIntoVal<uint8_t>(Globals::buffer()));
a.code.push_back(Utils::ReadFromStartIntoVal<uint8_t>(Globals::buffer()));
}
a.exception_table_length = Utils::ReadFromStartIntoVal<uint16_t>(Globals::buffer());
for (int i = 0; i < a.exception_table_length.ToHostFormat(); i++)
{
a.exceptions.push_back({.start_pc = Utils::ReadFromStartIntoVal<uint16_t>(Globals::buffer()),
.end_pc = Utils::ReadFromStartIntoVal<uint16_t>(Globals::buffer()),
.handler_pc = Utils::ReadFromStartIntoVal<uint16_t>(Globals::buffer()),
.catch_type = Utils::ReadFromStartIntoVal<uint16_t>(Globals::buffer())});
}
a.attributes_count = Utils::ReadFromStartIntoVal<uint16_t>(Globals::buffer());
for (int i = 0; i < a.attributes_count.ToHostFormat(); i++)
{
u2 attribute_name_index = Utils::ReadFromStartIntoVal<uint16_t>(Globals::buffer());
u4 attribute_length = Utils::ReadFromStartIntoVal<uint32_t>(Globals::buffer());
for (int i = 0; i < attribute_length.ToHostFormat(); i++)
{
Utils::ReadFromStartIntoVal<uint8_t>(Globals::buffer());
}
// std::cout << attribute_name_index.ToHostFormat() << std::endl;
}
// for (int j = 0; j < attribute_length.ToHostFormat() - 8; j++)
// {
// uint8_t data = Utils::ReadFromStartIntoVal<uint8_t>(Globals::buffer());
// std::cout << std::dec << std::to_string(data) << std::endl;
// // a.info.push_back(Utils::ReadFromStartIntoVal<uint8_t>(Globals::buffer()));
// }
std::cout << a.attribute_name_index.ToHostFormat() << std::endl;
// std::cout << a.info.size() << std::endl;
// std::cout << info.size() << std::endl;
std::cout << std::endl;
}
// if ((p.access_flags.ToHostFormat() & (int)Method::ACCESS_FLAGS::ACC_NATIVE) == 0 &&