Compare commits

...

5 Commits

8 changed files with 50 additions and 25 deletions

View File

@ -10,9 +10,6 @@ include_directories(${CMAKE_BINARY_DIR})
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CXX_STANDARD 23) set(CXX_STANDARD 23)
set(BUILD_TESTS OFF)
set(BUILD_BENCHMARK OFF)
set(CAVM_RUNTIME_CHECK_SUPPORT ON) set(CAVM_RUNTIME_CHECK_SUPPORT ON)
add_subdirectory(src) add_subdirectory(src)
@ -26,4 +23,6 @@ message("src " ${CONSTANT_POOL_TAG_SRC})
add_executable(parser add_executable(parser
${CONSTANT_POOL_TAG_SRC} ${CONSTANT_POOL_TAG_SRC}
) )
if (UNIX)
set(CMAKE_CXX_FLAGS "-std=c++${CXX_STANDARD} -O0 -g -Wpedantic") set(CMAKE_CXX_FLAGS "-std=c++${CXX_STANDARD} -O0 -g -Wpedantic")
endif()

View File

@ -4,6 +4,17 @@
#include <Types.h> #include <Types.h>
#include <cstddef> #include <cstddef>
#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_bswap16 _byteswap_ushort
#define __builtin_bswap32 _byteswap_ulong
#define __builtin_bswap64 _byteswap_uint64
#endif
template <typename T> template <typename T>
class BigEndian class BigEndian
{ {
@ -33,8 +44,8 @@ class BigEndian
return __builtin_bswap32(val); return __builtin_bswap32(val);
case 8: case 8:
return __builtin_bswap64(val); return __builtin_bswap64(val);
// Clang doesn't support __builtin_bswap128, so we fall back to a generic byte-order swap implementation // Only gcc has a 128-bit byte-order swap builtin, so we fall back to a generic byte-order swap implementation for clang and for msvc
#ifndef __clang__ #if not defined(__clang__) && not defined(_MSC_VER)
case 16: case 16:
return __builtin_bswap128(val); return __builtin_bswap128(val);
#endif #endif

View File

@ -58,6 +58,12 @@ enum class ACCESS_FLAGS
}; };
} // namespace Method } // namespace Method
struct BaseAttribute
{
u2 attribute_name_index;
u4 attribute_length;
};
struct attribute_info struct attribute_info
{ {
u2 attribute_name_index; u2 attribute_name_index;
@ -65,12 +71,6 @@ struct attribute_info
std::vector<u1> info; std::vector<u1> info;
}; };
struct BaseAttribute
{
u2 attribute_name_index;
u4 attribute_length;
};
struct method_info struct method_info
{ {
u2 access_flags; u2 access_flags;

View File

@ -4,7 +4,7 @@
#include <SpecTypes.h> #include <SpecTypes.h>
#include <cstdint> #include <cstdint>
#include <cstdlib> #include <cstdlib>
#include <string>
struct JavaClassFormat; struct JavaClassFormat;
@ -47,7 +47,7 @@ struct ConstantUtf8Info : ConstantPoolInfo
{ {
ConstantPoolTags::Tags tag; ConstantPoolTags::Tags tag;
u2 length; u2 length;
unsigned char *bytes; std::string bytes;
// std::vector<unsigned char> bytes; // std::vector<unsigned char> bytes;
}; };

View File

@ -1,4 +1,4 @@
#include "ConstantPoolTags/Tags.h" #include <ConstantPoolTags/Tags.h>
#include <Globals.h> #include <Globals.h>
#include <Utils/Utils.h> #include <Utils/Utils.h>
#include <Attributes.h> #include <Attributes.h>
@ -8,6 +8,7 @@
#include <cassert> #include <cassert>
#include <string> #include <string>
#include <Utils/CRC32_CompileTime.hpp> #include <Utils/CRC32_CompileTime.hpp>
#include <algorithm>
attribute_info Attributes::AttributesParser(JavaClassFormat& jc) attribute_info Attributes::AttributesParser(JavaClassFormat& jc)
{ {
@ -28,7 +29,7 @@ attribute_info Attributes::AttributesParser(JavaClassFormat& jc)
int idx = ret.info.data()->ToHostFormat(); int idx = ret.info.data()->ToHostFormat();
ASSERT(VEC_ELEM_TYPE(jc.constant_pool[ret.attribute_name_index.ToHostFormat() - 1]) == TYPE(ConstantPoolTags::ConstantUtf8Info)); 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); std::string str = std::any_cast<ConstantPoolTags::ConstantUtf8Info>(jc.constant_pool[ret.attribute_name_index.ToHostFormat() - 1]).bytes;
switch ((ClassAttributes)crc32_rec(CRC32, str.c_str())) switch ((ClassAttributes)crc32_rec(CRC32, str.c_str()))
{ {
@ -39,6 +40,7 @@ attribute_info Attributes::AttributesParser(JavaClassFormat& jc)
<< std::any_cast<ConstantPoolTags::ConstantUtf8Info>(jc.constant_pool[idx - 1]).bytes << std::any_cast<ConstantPoolTags::ConstantUtf8Info>(jc.constant_pool[idx - 1]).bytes
<< std::endl; << std::endl;
SOURCE_FILE_Attribute attr; SOURCE_FILE_Attribute attr;
attr.sourcefile_index = idx;
// attr. // attr.
break; break;
} }

View File

@ -14,31 +14,34 @@ void ConstantPoolTags::ConstantPoolParser(ConstantPoolTags::Tags t, JavaClassFor
case Tags::Utf8: { case Tags::Utf8: {
// std::cout << "FIXME: Implement Utf8 Constant Pool Tag" << std::endl; // std::cout << "FIXME: Implement Utf8 Constant Pool Tag" << std::endl;
ConstantUtf8Info ref; ConstantUtf8Info ref;
std::vector<uint8_t> length = Utils::ReadFromStart(Globals::buffer(), 2); ref.length = Utils::ReadFromStartIntoVal<uint16_t>(Globals::buffer(), 2);
// std::vector<uint8_t> length = Utils::ReadFromStart(Globals::buffer(), 2);
ref.tag = t; ref.tag = t;
ref.length = Utils::vecToVal<uint16_t>(length); // ref.length = Utils::vecToVal<uint16_t>(length);
unsigned char x = Utils::ReadFromStartIntoVal<unsigned char>(Globals::buffer(), 1); unsigned char x = Utils::ReadFromStartIntoVal<unsigned char>(Globals::buffer(), 1);
bool valid = true; bool valid = true;
ref.bytes = new unsigned char[ref.length.ToHostFormat()]; unsigned char* bytes = new unsigned char[ref.length.ToHostFormat()];
int len = 0;
for (int i = 0; i < ref.length.ToHostFormat(); i++) for (int i = 0; i < ref.length.ToHostFormat(); i++)
{ {
if ((x & 0x80) == 0) if ((x & 0x80) == 0)
{ {
ref.bytes[i] = x; bytes[i] = x;
} }
else if ((x & 0x800) == 0) else if ((x & 0x800) == 0)
{ {
unsigned char y = Utils::ReadFromStartIntoVal<unsigned char>(Globals::buffer(), 1); unsigned char y = Utils::ReadFromStartIntoVal<unsigned char>(Globals::buffer(), 1);
ref.bytes[i] = ((x & 0x1f) << 6) + (y & 0x3f); bytes[i] = ((x & 0x1f) << 6) + (y & 0x3f);
} }
else if ((x & 0x10000) == 0) else if ((x & 0x10000) == 0)
{ {
unsigned char y = Utils::ReadFromStartIntoVal<unsigned char>(Globals::buffer(), 1); unsigned char y = Utils::ReadFromStartIntoVal<unsigned char>(Globals::buffer(), 1);
unsigned char z = Utils::ReadFromStartIntoVal<unsigned char>(Globals::buffer(), 1); unsigned char z = Utils::ReadFromStartIntoVal<unsigned char>(Globals::buffer(), 1);
ref.bytes[i] = ((x & 0xf) << 12) + ((y & 0x3f) << 6) + (z & 0x3f); bytes[i] = ((x & 0xf) << 12) + ((y & 0x3f) << 6) + (z & 0x3f);
} }
else else
{ {
@ -46,11 +49,20 @@ void ConstantPoolTags::ConstantPoolParser(ConstantPoolTags::Tags t, JavaClassFor
exit(1); exit(1);
} }
if (i != ref.length.ToHostFormat() - 1) if (i < ref.length.ToHostFormat() - 1)
x = Utils::ReadFromStartIntoVal<unsigned char>(Globals::buffer(), 1); x = Utils::ReadFromStartIntoVal<unsigned char>(Globals::buffer(), 1);
len += 1;
} }
std::string bytes1;
bytes1.append(reinterpret_cast<const char*>(bytes), 0, len);
ref.bytes = bytes1;
jc.constant_pool.push_back(ref); jc.constant_pool.push_back(ref);
ref.length = len;
break; break;
} }
case Tags::Integer: case Tags::Integer:

View File

@ -163,7 +163,8 @@ int main(int argc, char **argv)
if (x == TYPE(ConstantPoolTags::ConstantUtf8Info)) if (x == TYPE(ConstantPoolTags::ConstantUtf8Info))
{ {
std::cout << "utf8:\t" << std::any_cast<ConstantPoolTags::ConstantUtf8Info>(jc.constant_pool[i]).bytes << std::endl; ConstantPoolTags::ConstantUtf8Info ref = std::any_cast<ConstantPoolTags::ConstantUtf8Info>(jc.constant_pool[i]);
std::cout << "utf8:\t" << "(length): " << ref.length.ToHostFormat() << ",\t(str): " << ref.bytes << std::endl;
} }
else if (x == TYPE(ConstantPoolTags::ConstantClassInfo)) else if (x == TYPE(ConstantPoolTags::ConstantClassInfo))
{ {

View File

@ -32,7 +32,7 @@ void Methods::MethodParser(method_info &p, JavaClassFormat &jc)
ASSERT(VEC_ELEM_TYPE(v) == TYPE(ConstantPoolTags::ConstantUtf8Info)); ASSERT(VEC_ELEM_TYPE(v) == TYPE(ConstantPoolTags::ConstantUtf8Info));
ConstantPoolTags::ConstantUtf8Info str = std::any_cast<ConstantPoolTags::ConstantUtf8Info>(v); ConstantPoolTags::ConstantUtf8Info str = std::any_cast<ConstantPoolTags::ConstantUtf8Info>(v);
ASSERT(strcmp(reinterpret_cast<const char *>(str.bytes), "Code") == 0); ASSERT(strcmp(str.bytes.c_str(), "Code") == 0);
u4 attribute_length = Utils::ReadFromStartIntoVal<uint32_t>(Globals::buffer()); u4 attribute_length = Utils::ReadFromStartIntoVal<uint32_t>(Globals::buffer());