From ca277bbec0be83f94a30c2f813a9382dae3ca7af Mon Sep 17 00:00:00 2001 From: yomaytk Date: Wed, 28 Feb 2024 17:56:53 +0900 Subject: [PATCH 01/21] Improve the TranslateVMA function. --- runtime/Entry.cpp | 7 ++++--- runtime/Memory.cpp | 8 +++++--- runtime/Memory.h | 8 +++++++- runtime/Syscall.cpp | 4 ++-- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/runtime/Entry.cpp b/runtime/Entry.cpp index 56c6bb5..b3d1f9b 100644 --- a/runtime/Entry.cpp +++ b/runtime/Entry.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -16,9 +17,9 @@ int main(int argc, char *argv[]) { std::vector mapped_memorys; /* allocate Stack */ - mapped_memorys.push_back(MappedMemory::VMAStackEntryInit(argc, argv, &g_state)); + auto stack_memory = MappedMemory::VMAStackEntryInit(argc, argv, &g_state); /* allocate Heap */ - mapped_memorys.push_back(MappedMemory::VMAHeapEntryInit()); + auto heap_memory = MappedMemory::VMAHeapEntryInit(); /* allocate every sections */ for (int i = 0; i < __g_data_sec_num; i++) { // remove covered section (FIXME) @@ -39,7 +40,7 @@ int main(int argc, char *argv[]) { g_state.sr.ctr_el0 = {.qword = 0x80038003}; g_state.sr.dczid_el0 = {.qword = 0x4}; /* set global RuntimeManager */ - g_run_mgr = new RuntimeManager(mapped_memorys); + g_run_mgr = new RuntimeManager(mapped_memorys, stack_memory, heap_memory); g_run_mgr->heaps_end_addr = HEAPS_START_VMA + HEAP_SIZE; /* set lifted function pointer table */ for (int i = 0; __g_fn_vmas[i] && __g_fn_ptr_table[i]; i++) { diff --git a/runtime/Memory.cpp b/runtime/Memory.cpp index c9fcd95..6efec9e 100644 --- a/runtime/Memory.cpp +++ b/runtime/Memory.cpp @@ -96,11 +96,13 @@ void MappedMemory::DebugEmulatedMemory() { void *RuntimeManager::TranslateVMA(addr_t vma_addr) { /* search in every mapped memory */ - // std::cout << "vma: " << std::hex << "0x" << vma_addr << std::endl; + if (vma_addr >= stack_memory->vma) + return reinterpret_cast(stack_memory->bytes + (vma_addr - stack_memory->vma)); + if (vma_addr >= heap_memory->vma) + return reinterpret_cast(heap_memory->bytes + (vma_addr - heap_memory->vma)); for (auto &memory : mapped_memorys) { - if (memory->vma <= vma_addr && vma_addr < memory->vma_end) { + if (memory->vma <= vma_addr && vma_addr < memory->vma_end) return reinterpret_cast(memory->bytes + (vma_addr - memory->vma)); - } } debug_state_machine(); /* not exist sections which includes the vma_addr. */ diff --git a/runtime/Memory.h b/runtime/Memory.h index c7289ff..593d33c 100644 --- a/runtime/Memory.h +++ b/runtime/Memory.h @@ -87,6 +87,7 @@ class MappedMemory { bytes(__bytes), upper_bytes(__upper_bytes), bytes_on_heap(__bytes_on_heap) {} + MappedMemory() {} ~MappedMemory() { if (bytes_on_heap) free(bytes); @@ -110,8 +111,11 @@ class MappedMemory { class RuntimeManager { public: - RuntimeManager(std::vector __mapped_memorys) + RuntimeManager(std::vector __mapped_memorys, MappedMemory *__stack_memory, + MappedMemory *__heap_memory) : mapped_memorys(__mapped_memorys), + stack_memory(__stack_memory), + heap_memory(__heap_memory), addr_fn_map({}) {} RuntimeManager() {} ~RuntimeManager() { @@ -126,6 +130,8 @@ class RuntimeManager { } std::vector mapped_memorys; + MappedMemory *stack_memory; + MappedMemory *heap_memory; /* heap area manage */ addr_t heaps_end_addr; std::unordered_map addr_fn_map; diff --git a/runtime/Syscall.cpp b/runtime/Syscall.cpp index 32923be..1fe407d 100644 --- a/runtime/Syscall.cpp +++ b/runtime/Syscall.cpp @@ -305,7 +305,7 @@ void __svc_call(void) { break; case AARCH64_SYS_BRK: /* brk (unsigned long brk) */ { - auto heap_memory = g_run_mgr->mapped_memorys[1]; + auto heap_memory = g_run_mgr->heap_memory; if (state_gpr.x0.qword == 0) { /* init program break (FIXME) */ state_gpr.x0.qword = heap_memory->heap_cur; @@ -325,7 +325,7 @@ void __svc_call(void) { case AARCH64_SYS_MMAP: /* mmap (void *start, size_t lengt, int prot, int flags, int fd, off_t offset) */ /* TODO */ { - auto heap_memory = g_run_mgr->mapped_memorys[1]; + auto heap_memory = g_run_mgr->heap_memory; if (state_gpr.x4.dword != -1) elfconv_runtime_error("Unsupported mmap (X4=0x%08x)\n", state_gpr.x4.dword); if (state_gpr.x5.dword != 0) From 40c33b4b94c43af6f6076c3acf2bf3e4cb27a4e1 Mon Sep 17 00:00:00 2001 From: yomaytk Date: Thu, 29 Feb 2024 16:02:03 +0900 Subject: [PATCH 02/21] Fix openat syscall and custom handler for SIGSEGV. --- backend/remill/include/remill/BC/HelperMacro.h | 2 +- runtime/Entry.cpp | 14 ++++++++++++++ runtime/Syscall.cpp | 4 +++- utils/elfconv.cpp | 10 ++++++++++ utils/elfconv.h | 7 +++++++ 5 files changed, 35 insertions(+), 2 deletions(-) diff --git a/backend/remill/include/remill/BC/HelperMacro.h b/backend/remill/include/remill/BC/HelperMacro.h index 8073171..b593021 100644 --- a/backend/remill/include/remill/BC/HelperMacro.h +++ b/backend/remill/include/remill/BC/HelperMacro.h @@ -1,6 +1,6 @@ #pragma once -// #define LIFT_DEBUG 1 +#define LIFT_DEBUG 1 // #define LIFT_CALLSTACK_DEBUG 1 // #define LIFT_INSN_DEBUG 1 // #define ELFCONV_SYSCALL_DEBUG 1 diff --git a/runtime/Entry.cpp b/runtime/Entry.cpp index b3d1f9b..f6c329c 100644 --- a/runtime/Entry.cpp +++ b/runtime/Entry.cpp @@ -2,6 +2,12 @@ #include #include +#include +#if defined(LIFT_DEBUG) +# include +# include +# include +#endif #include #include #include @@ -16,6 +22,14 @@ int main(int argc, char *argv[]) { std::vector mapped_memorys; +#if defined(LIFT_DEBUG) + struct sigaction segv_action = {0}; + segv_action.sa_flags = SA_SIGINFO; + segv_action.sa_sigaction = segv_debug_state_machine; + if (sigaction(SIGSEGV, &segv_action, NULL) < 0) + elfconv_runtime_error("sigaction for SIGSEGV failed.\n"); +#endif + /* allocate Stack */ auto stack_memory = MappedMemory::VMAStackEntryInit(argc, argv, &g_state); /* allocate Heap */ diff --git a/runtime/Syscall.cpp b/runtime/Syscall.cpp index 1fe407d..5d36db0 100644 --- a/runtime/Syscall.cpp +++ b/runtime/Syscall.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #if defined(ELFC_RUNTIME_SYSCALL_DEBUG) # define EMPTY_SYSCALL(sysnum) printf("[WARNING] syscall \"" #sysnum "\" is empty now.\n"); @@ -171,8 +172,9 @@ void __svc_call(void) { case AARCH64_SYS_OPENAT: /* openat (int dfd, const char* filename, int flags, umode_t mode) */ state_gpr.x0.dword = openat( state_gpr.x0.dword, (char *) _ecv_translate_ptr(state_gpr.x1.qword), state_gpr.x2.dword); + break; case AARCH64_SYS_READ: /* read (unsigned int fd, char *buf, size_t count) */ - state_gpr.x0.qword = read(state_gpr.x0.dword, (char *) _ecv_translate_ptr(state_gpr.x1.qword), + state_gpr.x0.qword = read(3, (char *) _ecv_translate_ptr(state_gpr.x1.qword), static_cast(state_gpr.x2.qword)); break; case AARCH64_SYS_WRITE: /* write (unsigned int fd, const char *buf, size_t count) */ diff --git a/utils/elfconv.cpp b/utils/elfconv.cpp index 3bdcd56..66c9549 100644 --- a/utils/elfconv.cpp +++ b/utils/elfconv.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #define PRINT_GPR(index) \ std::cout << std::hex << "x" << #index << ": 0x" << g_state.gpr.x##index.qword << std::endl; @@ -75,3 +76,12 @@ extern "C" void debug_insn() { << gpr.x2.qword << ", x3: 0x" << gpr.x3.qword << ", x4: 0x" << gpr.x4.qword << ", x5: 0x" << gpr.x5.qword << std::endl; } + +#if defined(LIFT_DEBUG) +extern "C" void segv_debug_state_machine(int sig, siginfo_t *info, void *ctx) { + std::cout << "[ERROR] Segmantation Fault." << std::endl; + std::cout << "signo: " << info->si_signo << " code: " << info->si_code << std::endl; + debug_state_machine(); + exit(0); +} +#endif \ No newline at end of file diff --git a/utils/elfconv.h b/utils/elfconv.h index 9c3f967..4d3e2dd 100644 --- a/utils/elfconv.h +++ b/utils/elfconv.h @@ -1,8 +1,15 @@ #pragma once #include +#include +#if defined(LIFT_DEBUG) +# include +#endif /* debug function */ extern "C" void debug_state_machine(); extern "C" void debug_state_machine_vectors(); extern "C" void debug_insn(); +#if defined(LIFT_DEBUG) +extern "C" void segv_debug_state_machine(int sig, siginfo_t *info, void *ctx); +#endif \ No newline at end of file From deca44eea0ce2428af5004b3744742c141a48213 Mon Sep 17 00:00:00 2001 From: yomaytk Date: Fri, 1 Mar 2024 16:43:13 +0900 Subject: [PATCH 03/21] Minor fix. --- lifter/Lift.cpp | 4 ++-- lifter/TraceManager.cpp | 2 +- scripts/dev.sh | 2 +- utils/elfconv.cpp | 19 ++++++++++--------- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/lifter/Lift.cpp b/lifter/Lift.cpp index 4dd5aa3..4c7b270 100644 --- a/lifter/Lift.cpp +++ b/lifter/Lift.cpp @@ -64,8 +64,8 @@ int main(int argc, char *argv[]) { if (!FLAGS_dbg_fun_cfg.empty()) { for (auto &[fn_addr, dasm_func] : manager.disasm_funcs) { /* append the address of necesarry debug function */ - if (strncmp(dasm_func.func_name.substr(0, FLAGS_dbg_fun_cfg.length()).c_str(), - FLAGS_dbg_fun_cfg.c_str(), FLAGS_dbg_fun_cfg.length()) == 0) { + if (strncmp(dasm_func.func_name.substr(0, FLAGS_dbg_fun_cfg.length() + 4).c_str(), + (FLAGS_dbg_fun_cfg + "_____").c_str(), FLAGS_dbg_fun_cfg.length() + 4) == 0) { control_flow_debug_list[fn_addr] = true; break; } diff --git a/lifter/TraceManager.cpp b/lifter/TraceManager.cpp index 6ffe7f2..1952cd9 100644 --- a/lifter/TraceManager.cpp +++ b/lifter/TraceManager.cpp @@ -45,7 +45,7 @@ bool AArch64TraceManager::isFunctionEntry(uint64_t addr) { std::string AArch64TraceManager::GetUniqueLiftedFuncName(std::string func_name, uint64_t vma_s) { std::stringstream lifted_fn_name; - lifted_fn_name << func_name << "_" << to_string(unique_i64++) << "_" << std::hex << vma_s; + lifted_fn_name << func_name << "_____" << to_string(unique_i64++) << "_" << std::hex << vma_s; return lifted_fn_name.str(); } diff --git a/scripts/dev.sh b/scripts/dev.sh index 7a04829..8b40e1a 100755 --- a/scripts/dev.sh +++ b/scripts/dev.sh @@ -16,7 +16,7 @@ setting() { BUILD_LIFTER_DIR=${BUILD_DIR}/lifter BUILD_TESTS_AARCH64_DIR=${BUILD_DIR}/tests/aarch64 CXX=clang++-16 - OPTFLAGS="-O3" + OPTFLAGS="-O0" CLANGFLAGS="${OPTFLAGS} -static -I${ROOT_DIR}/backend/remill/include -I${ROOT_DIR}" CXXX64=x86_64-linux-gnu-g++-11 CROSS_COMPILE_FLAGS_X64="-static --target=x86-64-linux-gnu nostdin_linpack.c -fuse-ld=lld -pthread;" diff --git a/utils/elfconv.cpp b/utils/elfconv.cpp index 66c9549..562e0b7 100644 --- a/utils/elfconv.cpp +++ b/utils/elfconv.cpp @@ -50,11 +50,12 @@ extern "C" void debug_state_machine() { << std::endl; auto sr = g_state.sr; std::cout << "State.SR" << std::dec << std::endl; - std::cout << "tpidr_el0: " << sr.tpidr_el0.qword << ", tpidrro_el0: " << sr.tpidrro_el0.qword - << ", ctr_el0: " << sr.ctr_el0.qword << ", dczid_el0: " << sr.dczid_el0.qword - << ", midr_el1: " << sr.midr_el1.qword << ", n: " << sr.n << ", z: " << sr.z - << ", c: " << sr.c << ", v: " << sr.v << ", ixc: " << sr.ixc << ", ofc: " << sr.ofc - << ", ufc: " << sr.ufc << ", idc: " << sr.idc << ", ioc: " << sr.ioc << std::endl; + std::cout << std::hex << "tpidr_el0: 0x" << sr.tpidr_el0.qword << ", tpidrro_el0: 0x" + << sr.tpidrro_el0.qword << ", ctr_el0: 0x" << sr.ctr_el0.qword << ", dczid_el0: 0x" + << sr.dczid_el0.qword << ", midr_el1: 0x" << sr.midr_el1.qword << std::dec + << ", n: " << sr.n << ", z: " << sr.z << ", c: " << sr.c << ", v: " << sr.v + << ", ixc: " << sr.ixc << ", ofc: " << sr.ofc << ", ufc: " << sr.ufc + << ", idc: " << sr.idc << ", ioc: " << sr.ioc << std::endl; } extern "C" void debug_state_machine_vectors() { @@ -71,10 +72,10 @@ extern "C" void debug_state_machine_vectors() { extern "C" void debug_insn() { auto gpr = g_state.gpr; std::cout << "[DEBUG INSN]" << std::endl; - std::cout << std::hex << "PC: 0x" << std::setw(16) << std::setfill('0') << gpr.pc.qword - << ", x0: 0x" << gpr.x0.qword << ", x1: 0x" << gpr.x1.qword << ", x2: 0x" - << gpr.x2.qword << ", x3: 0x" << gpr.x3.qword << ", x4: 0x" << gpr.x4.qword - << ", x5: 0x" << gpr.x5.qword << std::endl; + std::cout << std::hex << "PC: 0x" << gpr.pc.qword << ", SP: 0x" << gpr.sp.qword << ", x19: 0x" + << gpr.x19.qword << ", x20: 0x" << gpr.x20.qword << ", x21: 0x" << gpr.x21.qword + << ", x22: 0x" << gpr.x22.qword << ", x29: 0x" << gpr.x29.qword << ", x30: 0x" + << gpr.x30.qword << std::endl; } #if defined(LIFT_DEBUG) From 3eaa421deebf9efb2c0741f3e7e67d98d0d5eb6c Mon Sep 17 00:00:00 2001 From: yomaytk Date: Mon, 4 Mar 2024 16:20:32 +0900 Subject: [PATCH 04/21] Add debug_memory to lifted LLVM bitcode. --- .../include/remill/BC/InstructionLifter.h | 1 + .../remill/include/remill/BC/TraceLifter.h | 2 ++ backend/remill/lib/BC/InstructionLifter.cpp | 27 ++++++++++--------- lifter/MainLifter.cpp | 3 +++ utils/elfconv.cpp | 21 ++++++++++++++- utils/elfconv.h | 1 + 6 files changed, 41 insertions(+), 14 deletions(-) diff --git a/backend/remill/include/remill/BC/InstructionLifter.h b/backend/remill/include/remill/BC/InstructionLifter.h index ae15949..7d435ae 100644 --- a/backend/remill/include/remill/BC/InstructionLifter.h +++ b/backend/remill/include/remill/BC/InstructionLifter.h @@ -180,6 +180,7 @@ class InstructionLifter : public InstructionLifterIntf { class Impl; const std::unique_ptr impl; + const std::string debug_memory_name; const std::string debug_insn_name; }; diff --git a/backend/remill/include/remill/BC/TraceLifter.h b/backend/remill/include/remill/BC/TraceLifter.h index 45cffb6..74bad12 100644 --- a/backend/remill/include/remill/BC/TraceLifter.h +++ b/backend/remill/include/remill/BC/TraceLifter.h @@ -145,6 +145,7 @@ class TraceLifter::Impl { max_inst_bytes(arch->MaxInstructionSize(arch->CreateInitialContext())), indirectbr_block_name("L_indirectbr"), g_get_jmp_block_address_func_name("__g_get_indirectbr_block_address"), + debug_memory_name("debug_memory"), debug_insn_name("debug_insn"), debug_call_stack_name("debug_call_stack") { inst_bytes.reserve(max_inst_bytes); @@ -234,6 +235,7 @@ class TraceLifter::Impl { std::string indirectbr_block_name; std::string g_get_jmp_block_address_func_name; + std::string debug_memory_name; std::string debug_insn_name; std::string debug_call_stack_name; }; diff --git a/backend/remill/lib/BC/InstructionLifter.cpp b/backend/remill/lib/BC/InstructionLifter.cpp index b9791a6..15f40c1 100644 --- a/backend/remill/lib/BC/InstructionLifter.cpp +++ b/backend/remill/lib/BC/InstructionLifter.cpp @@ -63,6 +63,7 @@ InstructionLifter::~InstructionLifter(void) {} InstructionLifter::InstructionLifter(const Arch *arch_, const IntrinsicTable *intrinsics_) : impl(new Impl(arch_, intrinsics_)), + debug_memory_name("debug_memory"), debug_insn_name("debug_insn") {} // Lift a single instruction into a basic block. `is_delayed` signifies that @@ -102,8 +103,9 @@ LiftStatus InstructionLifter::LiftIntoBlock(Instruction &arch_inst, llvm::BasicB isel_func = impl->unsupported_instruction; arch_inst.operands.clear(); status = kLiftedUnsupportedInstruction; - printf("[WARNING] Unsupported instruction at address: 0x%08lx (SemanticsFunction)\n", - arch_inst.pc); + printf( + "[WARNING] Unsupported instruction at address: 0x%08lx (SemanticsFunction), instForm: %s\n", + arch_inst.pc, arch_inst.function.c_str()); } llvm::IRBuilder<> ir(block); @@ -122,17 +124,6 @@ LiftStatus InstructionLifter::LiftIntoBlock(Instruction &arch_inst, llvm::BasicB ir.CreateStore(ir.CreateCall(impl->intrinsics->delay_slot_begin, temp_args), mem_ptr_ref); } - /* append debug_insn function call */ - if (UINT64_MAX != debug_insn_addr) { - llvm::IRBuilder<> __debug_ir(block); - auto _debug_insn_fn = module->getFunction(debug_insn_name); - if (!_debug_insn_fn) { - printf("[ERROR] debug_insn is undeclared.\n"); - abort(); - } - __debug_ir.CreateCall(_debug_insn_fn); - } - #if defined(LIFT_INSN_DEBUG) do { std::vector target_addrs = {}; @@ -215,6 +206,16 @@ LiftStatus InstructionLifter::LiftIntoBlock(Instruction &arch_inst, llvm::BasicB ir.CreateStore(ir.CreateCall(impl->intrinsics->delay_slot_end, temp_args), mem_ptr_ref); } + /* append debug_insn function call */ + if (UINT64_MAX != debug_insn_addr) { + llvm::IRBuilder<> __debug_ir(block); + auto _debug_insn_fn = module->getFunction(debug_insn_name); + auto _debug_memory_fn = module->getFunction(debug_memory_name); + CHECK(_debug_insn_fn && _debug_memory_fn); + __debug_ir.CreateCall(_debug_insn_fn); + __debug_ir.CreateCall(_debug_memory_fn); + } + return status; } diff --git a/lifter/MainLifter.cpp b/lifter/MainLifter.cpp index 9a7649a..fd65e9c 100644 --- a/lifter/MainLifter.cpp +++ b/lifter/MainLifter.cpp @@ -282,6 +282,9 @@ llvm::Function *MainLifter::WrapImpl::DeclareDebugFunction() { /* void debug_call_stack() */ llvm::Function::Create(llvm::FunctionType::get(llvm::Type::getVoidTy(context), {}, false), llvm::Function::ExternalLinkage, debug_call_stack_name, *module); + // void debug_memory() + llvm::Function::Create(llvm::FunctionType::get(llvm::Type::getVoidTy(context), {}, false), + llvm::Function::ExternalLinkage, debug_memory_name, *module); /* void debug_insn() */ return llvm::Function::Create(llvm::FunctionType::get(llvm::Type::getVoidTy(context), {}, false), llvm::Function::ExternalLinkage, debug_insn_name, *module); diff --git a/utils/elfconv.cpp b/utils/elfconv.cpp index 562e0b7..80656b6 100644 --- a/utils/elfconv.cpp +++ b/utils/elfconv.cpp @@ -9,6 +9,7 @@ std::cout << std::hex << "x" << #index << ": 0x" << g_state.gpr.x##index.qword << std::endl; extern State g_state; +extern void *_ecv_translate_ptr(addr_t vma_addr); /* debug func */ extern "C" void debug_state_machine() { @@ -69,13 +70,31 @@ extern "C" void debug_state_machine_vectors() { } } +extern "C" void debug_memory() { + static uint64_t target_vma = 0xfffff00000f7f50; // should set target vma + if (0 == target_vma) + return; + static uint64_t old_value = 0; + auto target_pma = (uint64_t *) _ecv_translate_ptr(target_vma); + auto new_value = *target_pma; + if (old_value != new_value) { + std::cout << std::hex << "target_vma: 0x" << target_vma << " target_pma: 0x" << target_pma + << std::endl + << "\told value: 0x" << old_value << std::endl + << "\tnew value: 0x" << new_value << std::endl; + old_value = new_value; + } +} + extern "C" void debug_insn() { auto gpr = g_state.gpr; std::cout << "[DEBUG INSN]" << std::endl; std::cout << std::hex << "PC: 0x" << gpr.pc.qword << ", SP: 0x" << gpr.sp.qword << ", x19: 0x" << gpr.x19.qword << ", x20: 0x" << gpr.x20.qword << ", x21: 0x" << gpr.x21.qword << ", x22: 0x" << gpr.x22.qword << ", x29: 0x" << gpr.x29.qword << ", x30: 0x" - << gpr.x30.qword << std::endl; + << gpr.x30.qword << std::dec << ", x8: " << gpr.x8.dword << std::hex << ", x0: 0x" + << gpr.x0.qword << ", x1: 0x" << gpr.x1.qword << ", x2: 0x" << gpr.x2.qword + << std::endl; } #if defined(LIFT_DEBUG) diff --git a/utils/elfconv.h b/utils/elfconv.h index 4d3e2dd..2b2c528 100644 --- a/utils/elfconv.h +++ b/utils/elfconv.h @@ -9,6 +9,7 @@ /* debug function */ extern "C" void debug_state_machine(); extern "C" void debug_state_machine_vectors(); +extern "C" void debug_memory(); extern "C" void debug_insn(); #if defined(LIFT_DEBUG) extern "C" void segv_debug_state_machine(int sig, siginfo_t *info, void *ctx); From 798bea8aaf797bf65231fe8f3c717abd4a456cdd Mon Sep 17 00:00:00 2001 From: yomaytk Date: Mon, 4 Mar 2024 16:24:46 +0900 Subject: [PATCH 05/21] Add some aarch64 instructions. FCVTAS , , FRINTA
, , STR , [, (|){, {}}] --- backend/remill/lib/Arch/AArch64/Arch.cpp | 16 ++++++-- backend/remill/lib/Arch/AArch64/Decode.cpp | 38 ------------------- .../lib/Arch/AArch64/Semantics/CONVERT.cpp | 23 +++++++++++ .../lib/Arch/AArch64/Semantics/DATAXFER.cpp | 7 ++++ 4 files changed, 42 insertions(+), 42 deletions(-) diff --git a/backend/remill/lib/Arch/AArch64/Arch.cpp b/backend/remill/lib/Arch/AArch64/Arch.cpp index 8ff9b10..fcb70c8 100644 --- a/backend/remill/lib/Arch/AArch64/Arch.cpp +++ b/backend/remill/lib/Arch/AArch64/Arch.cpp @@ -821,7 +821,7 @@ bool AArch64Arch::ArchDecodeInstruction(uint64_t address, std::string_view inst_ /* set operands of insn */ if (!aarch64::TryDecode(dinst, inst)) { inst.category = Instruction::kCategoryInvalid; - printf("[WARNING] Unsupported instruction at address: 0x%08lx (TryDecode), instFrom: %s\n", + printf("[WARNING] Unsupported instruction at address: 0x%08lx (TryDecode), instForm: %s\n", address, inst.function.c_str()); return false; } @@ -2940,9 +2940,9 @@ bool TryDecodeUCVTF_ASISDMISC_R(const InstData &data, Instruction &inst) { // FRINTA
, bool TryDecodeFRINTA_D_FLOATDP1(const InstData &data, Instruction &inst) { - // AddRegOperand(inst, kActionWrite, kRegD, kUseAsValue, data.Rd); - // AddRegOperand(inst, kActionRead, kRegD, kUseAsValue, data.Rn); - return false; + AddRegOperand(inst, kActionWrite, kRegD, kUseAsValue, data.Rd); + AddRegOperand(inst, kActionRead, kRegD, kUseAsValue, data.Rn); + return true; } bool IsUnallocatedFloatEncoding(const InstData &data) { @@ -3021,6 +3021,13 @@ bool TryDecodeFCVTZS_64D_FLOAT2INT(const InstData &data, Instruction &inst) { return true; } +// FCVTAS , +bool TryDecodeFCVTAS_64D_FLOAT2INT(const InstData &data, Instruction &inst) { + AddRegOperand(inst, kActionWrite, kRegX, kUseAsValue, data.Rd); + AddRegOperand(inst, kActionRead, kRegD, kUseAsValue, data.Rn); + return true; +} + // FCVTZU , bool TryDecodeFCVTZU_32S_FLOAT2INT(const InstData &data, Instruction &inst) { if (IsUnallocatedFloatEncoding(data)) { @@ -3555,6 +3562,7 @@ static bool TryDecodeSTR_Vn_LDST_IMMPRE(const InstData &data, Instruction &inst, AddPreIndexMemOp(inst, kActionWrite, num_bits, data.Rn, offset); return true; } + // STR , [, #]! bool TryDecodeSTR_Q_LDST_IMMPRE(const InstData &data, Instruction &inst) { return TryDecodeSTR_Vn_LDST_IMMPRE(data, inst, kRegQ); diff --git a/backend/remill/lib/Arch/AArch64/Decode.cpp b/backend/remill/lib/Arch/AArch64/Decode.cpp index ab942df..47bb291 100644 --- a/backend/remill/lib/Arch/AArch64/Decode.cpp +++ b/backend/remill/lib/Arch/AArch64/Decode.cpp @@ -29224,44 +29224,6 @@ bool TryDecodeFCVTAS_32D_FLOAT2INT(const InstData &, Instruction &) { return false; } -// FCVTAS FCVTAS_64D_float2int: -// 0 x Rd 0 -// 1 x Rd 1 -// 2 x Rd 2 -// 3 x Rd 3 -// 4 x Rd 4 -// 5 x Rn 0 -// 6 x Rn 1 -// 7 x Rn 2 -// 8 x Rn 3 -// 9 x Rn 4 -// 10 0 -// 11 0 -// 12 0 -// 13 0 -// 14 0 -// 15 0 -// 16 0 opcode 0 -// 17 0 opcode 1 -// 18 1 opcode 2 -// 19 0 rmode 0 -// 20 0 rmode 1 -// 21 1 -// 22 1 type 0 -// 23 0 type 1 -// 24 0 -// 25 1 -// 26 1 -// 27 1 -// 28 1 -// 29 0 S 0 -// 30 0 -// 31 1 sf 0 -// FCVTAS , -bool TryDecodeFCVTAS_64D_FLOAT2INT(const InstData &, Instruction &) { - return false; -} - // SSHLL SXTL_SSHLL_asimdshf_L: // 0 x Rd 0 // 1 x Rd 1 diff --git a/backend/remill/lib/Arch/AArch64/Semantics/CONVERT.cpp b/backend/remill/lib/Arch/AArch64/Semantics/CONVERT.cpp index a70a0e9..f7fe928 100644 --- a/backend/remill/lib/Arch/AArch64/Semantics/CONVERT.cpp +++ b/backend/remill/lib/Arch/AArch64/Semantics/CONVERT.cpp @@ -115,6 +115,15 @@ DEF_SEM(FCVTZS_Float64ToSInt64, R64W dst, V64 src) { return memory; } +// FCVTAS , +// (FIXME) not using rounding to nearest with ties to Away +DEF_SEM(FCVTAS_Float64ToSInt64, R64W dst, V64 src) { + auto float_val = FExtractV64(FReadV64(src), 0); + auto res = CheckedCast(state, float_val); + WriteZExt(dst, res); + return memory; +} + DEF_SEM(FCVT_Float32ToFloat64, V128W dst, V32 src) { auto float_val = FExtractV32(FReadV32(src), 0); auto res = CheckedCast(state, float_val); @@ -128,6 +137,16 @@ DEF_SEM(FCVT_Float64ToFloat32, V128W dst, V64 src) { FWriteV32(dst, res); return memory; } + +// FRINTA
, +// (FIXME) not using rounding to nearest with ties to Away +DEF_SEM(FRINTA_Float64ToSInt64, R64W dst, V64 src) { + auto float_val = FExtractV64(FReadV64(src), 0); + auto res = CheckedCast(state, float_val); + WriteZExt(dst, res); + return memory; +} + } // namespace // TODO(pag): UCVTF_H32_FLOAT2INT. @@ -153,9 +172,13 @@ DEF_ISEL(FCVTZS_32S_FLOAT2INT) = FCVTZS_Float32ToSInt32; DEF_ISEL(FCVTZS_32D_FLOAT2INT) = FCVTZS_Float64ToSInt32; DEF_ISEL(FCVTZS_64D_FLOAT2INT) = FCVTZS_Float64ToSInt64; +DEF_ISEL(FCVTAS_64D_FLOAT2INT) = FCVTAS_Float64ToSInt64; + DEF_ISEL(FCVT_DS_FLOATDP1) = FCVT_Float32ToFloat64; DEF_ISEL(FCVT_SD_FLOATDP1) = FCVT_Float64ToFloat32; +DEF_ISEL(FRINTA_D_FLOATDP1) = FRINTA_Float64ToSInt64; + namespace { DEF_SEM(SCVTF_Int32ToFloat32, V128W dst, R32 src) { diff --git a/backend/remill/lib/Arch/AArch64/Semantics/DATAXFER.cpp b/backend/remill/lib/Arch/AArch64/Semantics/DATAXFER.cpp index 5d16946..6d83efe 100644 --- a/backend/remill/lib/Arch/AArch64/Semantics/DATAXFER.cpp +++ b/backend/remill/lib/Arch/AArch64/Semantics/DATAXFER.cpp @@ -138,6 +138,11 @@ DEF_SEM(StoreToOffset, S src, D base, ADDR offset) { return memory; } +DEF_SEM(StoreWordToOffset, V32 src, MV32W base, ADDR offset) { + FWriteV32(DisplaceAddress(base, Read(offset)), FReadV32(src)); + return memory; +} + DEF_SEM(StoreDoubleToOffset, V64 src, MV64W base, ADDR offset) { FWriteV64(DisplaceAddress(base, Read(offset)), FReadV64(src)); return memory; @@ -212,6 +217,7 @@ DEF_ISEL(STRH_32_LDST_POS) = Store; DEF_ISEL(STR_32_LDST_REGOFF) = StoreToOffset; DEF_ISEL(STR_64_LDST_REGOFF) = StoreToOffset; +DEF_ISEL(STR_S_LDST_REGOFF) = StoreWordToOffset; DEF_ISEL(STR_D_LDST_REGOFF) = StoreDoubleToOffset; DEF_ISEL(SWP_32_MEMOP) = SWP_MEMOP; @@ -567,6 +573,7 @@ DEF_ISEL(FMOV_D64_FLOAT2INT) = FMOV_I64ToF64; DEF_ISEL(FMOV_S_FLOATDP1) = FMOV_S; DEF_ISEL(FMOV_D_FLOATDP1) = FMOV_D; + namespace { DEF_SEM(ADRP, R64W dst, PC label) { From e5416a5748bb683409f9fbc5e26c3a34afe02cac Mon Sep 17 00:00:00 2001 From: yomaytk Date: Mon, 4 Mar 2024 16:26:17 +0900 Subject: [PATCH 06/21] Change default heap area size. --- runtime/Entry.cpp | 2 +- runtime/Memory.cpp | 9 +++++---- runtime/Memory.h | 2 +- tests/aarch64/Test.cpp | 2 +- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/runtime/Entry.cpp b/runtime/Entry.cpp index f6c329c..eca0a8a 100644 --- a/runtime/Entry.cpp +++ b/runtime/Entry.cpp @@ -55,7 +55,7 @@ int main(int argc, char *argv[]) { g_state.sr.dczid_el0 = {.qword = 0x4}; /* set global RuntimeManager */ g_run_mgr = new RuntimeManager(mapped_memorys, stack_memory, heap_memory); - g_run_mgr->heaps_end_addr = HEAPS_START_VMA + HEAP_SIZE; + g_run_mgr->heaps_end_addr = HEAPS_START_VMA + HEAP_UNIT_SIZE; /* set lifted function pointer table */ for (int i = 0; __g_fn_vmas[i] && __g_fn_ptr_table[i]; i++) { g_run_mgr->addr_fn_map[__g_fn_vmas[i]] = __g_fn_ptr_table[i]; diff --git a/runtime/Memory.cpp b/runtime/Memory.cpp index 6efec9e..3182ab6 100644 --- a/runtime/Memory.cpp +++ b/runtime/Memory.cpp @@ -69,10 +69,11 @@ MappedMemory *MappedMemory::VMAStackEntryInit(int argc, char *argv[], } MappedMemory *MappedMemory::VMAHeapEntryInit() { - auto bytes = reinterpret_cast(malloc(HEAP_SIZE)); - auto upper_bytes = bytes + HEAP_SIZE; - auto heap = new MappedMemory(MemoryAreaType::HEAP, "Heap", HEAPS_START_VMA, - HEAPS_START_VMA + HEAP_SIZE, HEAP_SIZE, bytes, upper_bytes, true); + auto bytes = reinterpret_cast(malloc(HEAP_UNIT_SIZE)); + auto upper_bytes = bytes + HEAP_UNIT_SIZE; + auto heap = + new MappedMemory(MemoryAreaType::HEAP, "Heap", HEAPS_START_VMA, + HEAPS_START_VMA + HEAP_UNIT_SIZE, HEAP_UNIT_SIZE, bytes, upper_bytes, true); heap->heap_cur = HEAPS_START_VMA; return heap; } diff --git a/runtime/Memory.h b/runtime/Memory.h index 593d33c..ef0441b 100644 --- a/runtime/Memory.h +++ b/runtime/Memory.h @@ -12,7 +12,7 @@ const addr_t STACK_START_VMA = 0x0fff'ff00'0000'0000; /* 65535 TiB FIXME! */ const size_t STACK_SIZE = 1 * 1024 * 1024; /* 4 MiB */ const addr_t HEAPS_START_VMA = 0x4000'0000'0000; /* 64 TiB FIXME! */ -const uint64_t HEAP_SIZE = 1 * 1024 * 1024; /* 1 MiB */ +const uint64_t HEAP_UNIT_SIZE = 1 * 1024 * 1024 * 1024; /* 1 MiB */ typedef uint32_t _ecv_reg_t; typedef uint64_t _ecv_reg64_t; diff --git a/tests/aarch64/Test.cpp b/tests/aarch64/Test.cpp index 596d564..1890295 100644 --- a/tests/aarch64/Test.cpp +++ b/tests/aarch64/Test.cpp @@ -30,7 +30,7 @@ int main(int argc, char *argv[]) { g_state.sr.dczid_el0 = {.qword = 0x4}; /* set global RuntimeManager */ g_run_mgr = new RuntimeManager(mapped_memorys); - g_run_mgr->heaps_end_addr = HEAPS_START_VMA + HEAP_SIZE; + g_run_mgr->heaps_end_addr = HEAPS_START_VMA + HEAP_UNIT_SIZE; /* set lifted function pointer table */ for (int i = 0; __g_fn_vmas[i] && __g_fn_ptr_table[i]; i++) { g_run_mgr->addr_fn_map[__g_fn_vmas[i]] = __g_fn_ptr_table[i]; From 0f85295b4d05be8ad94f926ab9ce2a51a6037ce5 Mon Sep 17 00:00:00 2001 From: yomaytk Date: Mon, 4 Mar 2024 16:27:23 +0900 Subject: [PATCH 07/21] Add close(2). --- runtime/Syscall.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/runtime/Syscall.cpp b/runtime/Syscall.cpp index 5d36db0..3b99ee6 100644 --- a/runtime/Syscall.cpp +++ b/runtime/Syscall.cpp @@ -34,6 +34,7 @@ #define AARCH64_SYS_IOCTL 29 #define AARCH64_SYS_FACCESSAT 48 #define AARCH64_SYS_OPENAT 56 +#define AARCH64_SYS_CLOSE 57 #define AARCH64_SYS_READ 63 #define AARCH64_SYS_WRITE 64 #define AARCH64_SYS_WRITEV 66 @@ -173,8 +174,11 @@ void __svc_call(void) { state_gpr.x0.dword = openat( state_gpr.x0.dword, (char *) _ecv_translate_ptr(state_gpr.x1.qword), state_gpr.x2.dword); break; + case AARCH64_SYS_CLOSE: /* int close (unsigned int fd) */ + state_gpr.x0.dword = close(state_gpr.x0.dword); + break; case AARCH64_SYS_READ: /* read (unsigned int fd, char *buf, size_t count) */ - state_gpr.x0.qword = read(3, (char *) _ecv_translate_ptr(state_gpr.x1.qword), + state_gpr.x0.qword = read(state_gpr.x0.dword, (char *) _ecv_translate_ptr(state_gpr.x1.qword), static_cast(state_gpr.x2.qword)); break; case AARCH64_SYS_WRITE: /* write (unsigned int fd, const char *buf, size_t count) */ From 8cb5928ddeb66d99ce3703be30ac397aca7c90a2 Mon Sep 17 00:00:00 2001 From: yomaytk Date: Mon, 4 Mar 2024 16:41:31 +0900 Subject: [PATCH 08/21] Minor fix. --- runtime/Entry.cpp | 6 +++--- runtime/Memory.cpp | 33 +++++++++++++++++++++++++++------ runtime/Memory.h | 8 ++++---- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/runtime/Entry.cpp b/runtime/Entry.cpp index eca0a8a..ef8224d 100644 --- a/runtime/Entry.cpp +++ b/runtime/Entry.cpp @@ -31,9 +31,9 @@ int main(int argc, char *argv[]) { #endif /* allocate Stack */ - auto stack_memory = MappedMemory::VMAStackEntryInit(argc, argv, &g_state); + auto mapped_stack = MappedMemory::VMAStackEntryInit(argc, argv, &g_state); /* allocate Heap */ - auto heap_memory = MappedMemory::VMAHeapEntryInit(); + auto mapped_heap = MappedMemory::VMAHeapEntryInit(); /* allocate every sections */ for (int i = 0; i < __g_data_sec_num; i++) { // remove covered section (FIXME) @@ -54,7 +54,7 @@ int main(int argc, char *argv[]) { g_state.sr.ctr_el0 = {.qword = 0x80038003}; g_state.sr.dczid_el0 = {.qword = 0x4}; /* set global RuntimeManager */ - g_run_mgr = new RuntimeManager(mapped_memorys, stack_memory, heap_memory); + g_run_mgr = new RuntimeManager(mapped_memorys, mapped_stack, mapped_heap); g_run_mgr->heaps_end_addr = HEAPS_START_VMA + HEAP_UNIT_SIZE; /* set lifted function pointer table */ for (int i = 0; __g_fn_vmas[i] && __g_fn_ptr_table[i]; i++) { diff --git a/runtime/Memory.cpp b/runtime/Memory.cpp index 3182ab6..c1d42ca 100644 --- a/runtime/Memory.cpp +++ b/runtime/Memory.cpp @@ -40,12 +40,33 @@ MappedMemory *MappedMemory::VMAStackEntryInit(int argc, char *argv[], _ecv_reg64_t _ecv_a_val; } _ecv_a_un; } _ecv_auxv64[] = { - {3 /* AT_PHDR */, phdr}, {4 /* AT_PHENT */, __g_e_phent}, - {5 /* AT_PHNUM */, __g_e_phnum}, {6 /* AT_PAGESZ */, 4096}, - {9 /* AT_ENTRY */, __g_entry_pc}, {11 /* AT_UID */, 42}, - {12 /* AT_EUID */, 42}, {13 /* AT_GID */, 42}, - {14 /* AT_EGID */, 42}, {23 /* AT_SECURE */, 0}, - {25 /* AT_RANDOM */, randomp}, {0 /* AT_NULL */, 0}, +#if defined(__linux__) + {3 /* AT_PHDR */, phdr}, + {4 /* AT_PHENT */, __g_e_phent}, + {5 /* AT_PHNUM */, __g_e_phnum}, + {6 /* AT_PAGESZ */, 4096}, + {9 /* AT_ENTRY */, __g_entry_pc}, + {11 /* AT_UID */, getuid()}, + {12 /* AT_EUID */, geteuid()}, + {13 /* AT_GID */, getgid()}, + {14 /* AT_EGID */, getegid()}, + {23 /* AT_SECURE */, 0}, + {25 /* AT_RANDOM */, randomp}, + {0 /* AT_NULL */, 0}, +#elif defined(__wasm__) + {3 /* AT_PHDR */, phdr}, + {4 /* AT_PHENT */, __g_e_phent}, + {5 /* AT_PHNUM */, __g_e_phnum}, + {6 /* AT_PAGESZ */, 4096}, + {9 /* AT_ENTRY */, __g_entry_pc}, + {11 /* AT_UID */, 42}, + {12 /* AT_EUID */, 42}, + {13 /* AT_GID */, 42}, + {14 /* AT_EGID */, 42}, + {23 /* AT_SECURE */, 0}, + {25 /* AT_RANDOM */, randomp}, + {0 /* AT_NULL */, 0}, +#endif }; sp -= sizeof(_ecv_auxv64); memcpy(bytes + (sp - vma), _ecv_auxv64, sizeof(_ecv_auxv64)); diff --git a/runtime/Memory.h b/runtime/Memory.h index ef0441b..1e31fcd 100644 --- a/runtime/Memory.h +++ b/runtime/Memory.h @@ -111,11 +111,11 @@ class MappedMemory { class RuntimeManager { public: - RuntimeManager(std::vector __mapped_memorys, MappedMemory *__stack_memory, - MappedMemory *__heap_memory) + RuntimeManager(std::vector __mapped_memorys, MappedMemory *__mapped_stack, + MappedMemory *__mapped_heap) : mapped_memorys(__mapped_memorys), - stack_memory(__stack_memory), - heap_memory(__heap_memory), + stack_memory(__mapped_stack), + heap_memory(__mapped_heap), addr_fn_map({}) {} RuntimeManager() {} ~RuntimeManager() { From ed008147a07891f4e5ad3a21084dd1390885feb2 Mon Sep 17 00:00:00 2001 From: yomaytk Date: Mon, 4 Mar 2024 23:40:28 +0900 Subject: [PATCH 09/21] Fix FRINTA instruction. --- backend/remill/lib/Arch/AArch64/Semantics/CONVERT.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/backend/remill/lib/Arch/AArch64/Semantics/CONVERT.cpp b/backend/remill/lib/Arch/AArch64/Semantics/CONVERT.cpp index f7fe928..5cc39bb 100644 --- a/backend/remill/lib/Arch/AArch64/Semantics/CONVERT.cpp +++ b/backend/remill/lib/Arch/AArch64/Semantics/CONVERT.cpp @@ -140,10 +140,11 @@ DEF_SEM(FCVT_Float64ToFloat32, V128W dst, V64 src) { // FRINTA
, // (FIXME) not using rounding to nearest with ties to Away -DEF_SEM(FRINTA_Float64ToSInt64, R64W dst, V64 src) { +DEF_SEM(FRINTA_Float64ToSInt64, V64W dst, V64 src) { auto float_val = FExtractV64(FReadV64(src), 0); - auto res = CheckedCast(state, float_val); - WriteZExt(dst, res); + auto res = (double) (long) float_val; + // auto res = CheckedCast(state, float_val); + FWriteV64(dst, res); return memory; } From aa18cdcdf8156fa453ae2aa695d98121e29a2ef8 Mon Sep 17 00:00:00 2001 From: yomaytk Date: Mon, 4 Mar 2024 23:41:42 +0900 Subject: [PATCH 10/21] Remove LLVM IR based aarch64 test. --- tests/aarch64/CMakeLists.txt | 27 ------ tests/aarch64/Test.cpp | 45 ---------- tests/aarch64/Test.h | 10 --- tests/aarch64/TestHelper.cpp | 37 -------- tests/aarch64/TestInstructions.cpp | 14 --- tests/aarch64/TestLift.cpp | 117 ------------------------- tests/aarch64/TestLift.h | 21 ----- tests/aarch64/TestMainLifter.cpp | 133 ----------------------------- tests/aarch64/TestMainLifter.h | 48 ----------- tests/aarch64/TestState.h | 21 ----- 10 files changed, 473 deletions(-) delete mode 100644 tests/aarch64/CMakeLists.txt delete mode 100644 tests/aarch64/Test.cpp delete mode 100644 tests/aarch64/Test.h delete mode 100644 tests/aarch64/TestHelper.cpp delete mode 100644 tests/aarch64/TestInstructions.cpp delete mode 100644 tests/aarch64/TestLift.cpp delete mode 100644 tests/aarch64/TestLift.h delete mode 100644 tests/aarch64/TestMainLifter.cpp delete mode 100644 tests/aarch64/TestMainLifter.h delete mode 100644 tests/aarch64/TestState.h diff --git a/tests/aarch64/CMakeLists.txt b/tests/aarch64/CMakeLists.txt deleted file mode 100644 index a4c55d9..0000000 --- a/tests/aarch64/CMakeLists.txt +++ /dev/null @@ -1,27 +0,0 @@ -include_directories("/usr/lib/llvm-16/include") - -add_executable(aarch64_test_lift - "${CMAKE_SOURCE_DIR}/lifter/MainLifter.cpp" - "${CMAKE_SOURCE_DIR}/lifter/TraceManager.cpp" - "${CMAKE_SOURCE_DIR}/utils/Util.cpp" - "${CMAKE_SOURCE_DIR}/lifter/Binary/Loader.cpp" - TestInstructions.cpp - TestLift.cpp - TestMainLifter.cpp -) - -set_target_properties(aarch64_test_lift PROPERTIES - POSITION_INDEPENDENT_CODE ON - COMPILE_FLAGS "-O0 -fPIC" -) - -target_link_libraries( - aarch64_test_lift - PRIVATE - bfd - elf -) - -target_link_libraries(aarch64_test_lift PUBLIC remill ${PROJECT_LIBRARIES} ) -target_include_directories(aarch64_test_lift PUBLIC ${PROJECT_INCLUDEDIRECTORIES}) -target_include_directories(aarch64_test_lift PRIVATE ${CMAKE_SOURCE_DIR}) diff --git a/tests/aarch64/Test.cpp b/tests/aarch64/Test.cpp deleted file mode 100644 index 1890295..0000000 --- a/tests/aarch64/Test.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#include "Test.h" - -#include "remill/Arch/AArch64/Runtime/State.h" -#include "remill/Arch/Runtime/Intrinsics.h" -#include "remill/BC/HelperMacro.h" -#include "runtime/Memory.h" - -#include -#include -#include -#include - -State g_state = State(); -RuntimeManager *g_run_mgr; - -int main(int argc, char *argv[]) { - - std::vector mapped_memorys; - - /* allocate Stack */ - mapped_memorys.push_back(MappedMemory::VMAStackEntryInit(argc, argv, &g_state)); - /* allocate Heap */ - mapped_memorys.push_back(MappedMemory::VMAHeapEntryInit()); - /* set program counter */ - g_state.gpr.pc = {.qword = __g_entry_pc}; - /* set system register (FIXME) */ - g_state.sr.tpidr_el0 = {.qword = 0}; - g_state.sr.midr_el1 = {.qword = 0xf0510}; - g_state.sr.ctr_el0 = {.qword = 0x80038003}; - g_state.sr.dczid_el0 = {.qword = 0x4}; - /* set global RuntimeManager */ - g_run_mgr = new RuntimeManager(mapped_memorys); - g_run_mgr->heaps_end_addr = HEAPS_START_VMA + HEAP_UNIT_SIZE; - /* set lifted function pointer table */ - for (int i = 0; __g_fn_vmas[i] && __g_fn_ptr_table[i]; i++) { - g_run_mgr->addr_fn_map[__g_fn_vmas[i]] = __g_fn_ptr_table[i]; - } - /* go to the aarch64_insn_test_main_func */ - __g_entry_func(&g_state, __g_entry_pc, reinterpret_cast(g_run_mgr)); - - std::cout << "Test Success!" << std::endl; - delete (g_run_mgr); - - return 0; -} diff --git a/tests/aarch64/Test.h b/tests/aarch64/Test.h deleted file mode 100644 index 52cefff..0000000 --- a/tests/aarch64/Test.h +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once -#include "runtime/Memory.h" - -const _ecv_reg_t __G__E_PHENT = 56; -const _ecv_reg_t __G_E_PHNUM = 7; - -const addr_t __g_entry_pc = 0x00400000; -_ecv_reg_t __g_e_phent = __G__E_PHENT; -_ecv_reg_t __g_e_phnum = __G_E_PHNUM; -uint8_t __g_e_ph[__G__E_PHENT * __G_E_PHNUM] = {0}; diff --git a/tests/aarch64/TestHelper.cpp b/tests/aarch64/TestHelper.cpp deleted file mode 100644 index edd36ba..0000000 --- a/tests/aarch64/TestHelper.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include "TestState.h" -#include "remill/Arch/AArch64/Runtime/State.h" -#include "utils/Util.h" -#include "utils/elfconv.h" - -#include - -extern State g_state; -extern std::map g_disasm_funcs; - -extern "C" void get_failed_lifting_detail() { - auto failed_inst_vma = g_state.gpr.pc.qword; - if (1 == g_disasm_funcs.count(failed_inst_vma)) { - auto test_inst_state = g_disasm_funcs[failed_inst_vma]; - printf("[TEST FAIELD] inst_vma: 0x%llx\n", failed_inst_vma); - printf("Expected: "); - for (auto &[mname, required] : test_inst_state.required_state) - printf("%s: %lld", mname.c_str(), required); - printf("\n"); - printf("Actual:\n"); - debug_state_machine(); - debug_state_machine_vectors(); - exit(EXIT_FAILURE); - } else { - elfconv_runtime_error("%lld is not included in g_disasm_funcs at %s.\n", failed_inst_vma, - __func__); - } -} - -extern "C" void show_test_target_insn() { - auto test_inst_vma = g_state.gpr.pc.qword; - if (1 == g_disasm_funcs.count(test_inst_vma)) - printf("\"%s\" Test Start.\n", g_disasm_funcs[test_inst_vma].mnemonic.c_str()); - else - elfconv_runtime_error("%lld is not included in g_disasm_funcs at %s.\n", test_inst_vma, - __func__); -} diff --git a/tests/aarch64/TestInstructions.cpp b/tests/aarch64/TestInstructions.cpp deleted file mode 100644 index f3bd682..0000000 --- a/tests/aarch64/TestInstructions.cpp +++ /dev/null @@ -1,14 +0,0 @@ -#include "TestState.h" - -#include - -uint64_t g_inst_vma = g_test_disasm_func_vma; - -#define NEXT_VMA() (g_inst_vma += 4, g_inst_vma) - -std::map g_disasm_funcs = { - {g_inst_vma, TestInstructionState("mov x0, #42", {}, {{"X0", 42}})}, - {NEXT_VMA(), TestInstructionState("mov x1, #52", {}, {{"X1", 52}})}, - {NEXT_VMA(), TestInstructionState("mov x2, #62", {}, {{"X2", 62}})}, - {NEXT_VMA(), TestInstructionState("add x1, x2, x3", {{"X2", 12}, {"X3", 21}}, {{"X1", 33}})}, - /* must be inserted at end */ {NEXT_VMA(), TestInstructionState("ret", {}, {})}}; diff --git a/tests/aarch64/TestLift.cpp b/tests/aarch64/TestLift.cpp deleted file mode 100644 index 54bbaf9..0000000 --- a/tests/aarch64/TestLift.cpp +++ /dev/null @@ -1,117 +0,0 @@ -#include "TestLift.h" - -#include "TestMainLifter.h" -#include "remill/BC/Util.h" -#include "utils/Util.h" - -DEFINE_string(bc_out, "", "Name of the file in which to place the generated bitcode."); - -DEFINE_string(os, REMILL_OS, - "Operating system name of the code being " - "translated. Valid OSes: linux, macos, windows, solaris."); -DEFINE_string(arch, REMILL_ARCH, - "Architecture of the code being translated. " - "Valid architectures: aarch64"); - -extern std::map g_disasm_funcs; - -/* DisassembleCmd class */ -/* - e.g. - 'mov x0, #42' -> 0x400580d2 - rasm2_exe_buf[0] = '4', [1] = '0', ... [8] = '2' - --> insn_bytes[0] = d2, [1] = 80, [2] = 05, [3] = 40 -*/ -void DisassembleCmd::ExecRasm2(const std::string &mnemonic, uint8_t insn_bytes[4]) { - uint8_t rasm2_exe_buf[128]; - memset(rasm2_exe_buf, 0, sizeof(rasm2_exe_buf)); - std::string cmd = "rasm2 -a arm -b 64 '" + mnemonic + "'"; - FILE *pipe = popen(cmd.c_str(), "r"); - if (!pipe) - elfconv_runtime_error("[TEST_ERROR] rasm2 disassemble pipe is invalid. mnemonic: %s\n", - mnemonic.c_str()); - fgets(reinterpret_cast(rasm2_exe_buf), sizeof(rasm2_exe_buf), pipe); - // check - { - char dum_buf[128]; - CHECK(NULL == fgets(dum_buf, sizeof(dum_buf), pipe)); - } - /* decode rasm2_exe_buf */ - auto char2hex = [&rasm2_exe_buf](int id) -> int { - if ('0' <= rasm2_exe_buf[id] && rasm2_exe_buf[id] <= '9') - return rasm2_exe_buf[id] - '0'; - else if ('a' <= rasm2_exe_buf[id] && rasm2_exe_buf[id] <= 'f') - return rasm2_exe_buf[id] - 'a' + 10; - else - elfconv_runtime_error("ExecRasm2 Error: rasm2_exe_buf has invalid num.\n"); - return 0; - }; - insn_bytes[0] = char2hex(0) * 16 + char2hex(1); - insn_bytes[1] = char2hex(2) * 16 + char2hex(3); - insn_bytes[2] = char2hex(4) * 16 + char2hex(5); - insn_bytes[3] = char2hex(6) * 16 + char2hex(7); -} - -int main(int argc, char *argv[]) { - google::ParseCommandLineFlags(&argc, &argv, true); - google::InitGoogleLogging(argv[0]); - - uintptr_t test_disasm_func_vma = g_test_disasm_func_vma; - uint64_t test_disasm_func_size = AARCH64_OP_SIZE * g_disasm_funcs.size(); - - TestAArch64TraceManager manager("DummyELF"); - - /* set insn data to manager.memory */ - for (auto &[_vma, _test_aarch64_insn] : g_disasm_funcs) { - manager.test_inst_state_map[_vma] = &_test_aarch64_insn; - uint8_t insn_data[4]; - DisassembleCmd::ExecRasm2(_test_aarch64_insn.mnemonic, insn_data); - manager.memory[_vma] = insn_data[0]; - manager.memory[_vma + 1] = insn_data[1]; - manager.memory[_vma + 2] = insn_data[2]; - manager.memory[_vma + 3] = insn_data[3]; - } - - /* set test_main_function using g_disasm_funcs */ - manager.disasm_funcs = { - {test_disasm_func_vma, - DisasmFunc("aarch64_insn_test_main_func", test_disasm_func_vma, test_disasm_func_size)}}; - manager.entry_func_lifted_name = "aarch64_insn_test_main_func"; - - llvm::LLVMContext context; - auto os_name = remill::GetOSName(REMILL_OS); - auto arch_name = remill::GetArchName(FLAGS_arch); - auto arch = remill::Arch::Build(&context, os_name, arch_name); - auto module = remill::LoadArchSemantics(arch.get()); - - remill::IntrinsicTable intrinsics(module.get()); - TestLifter test_lifter(arch.get(), &manager); - - std::unordered_map addr_fn_map; - - /* declare helper function for lifted LLVM bitcode */ - test_lifter.DeclareHelperFunction(); - test_lifter.DeclareDebugFunction(); - /* lift every disassembled function */ - for (const auto &[addr, dasm_func] : manager.disasm_funcs) { - if (!test_lifter.Lift(dasm_func.vma, dasm_func.func_name.c_str())) { - elfconv_runtime_error("[ERROR] Failed to Lift \"%s\"\n", dasm_func.func_name.c_str()); - } - addr_fn_map[addr] = dasm_func.func_name.c_str(); - /* set function name */ - auto lifted_fn = manager.GetLiftedTraceDefinition(dasm_func.vma); - lifted_fn->setName(dasm_func.func_name.c_str()); - } - /* set lifted entry function */ - test_lifter.SetEntryPoint(manager.entry_func_lifted_name); - /* set lifted function pointer table (necessary for indirect call) */ - test_lifter.SetLiftedFunPtrTable(addr_fn_map); - - /* generate LLVM bitcode file */ - auto host_arch = remill::Arch::Build(&context, os_name, remill::GetArchName(REMILL_ARCH)); - host_arch->PrepareModule(module.get()); - remill::StoreModuleToFile(module.get(), FLAGS_bc_out); - - printf("[INFO] Lift Done.\n"); - return 0; -} diff --git a/tests/aarch64/TestLift.h b/tests/aarch64/TestLift.h deleted file mode 100644 index c2e2b94..0000000 --- a/tests/aarch64/TestLift.h +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once -#include "TestState.h" -#include "lifter/Lift.h" -#include "lifter/MainLifter.h" -#include "lifter/TraceManager.h" -#include "runtime/Memory.h" - -#include -#include - -class DisassembleCmd { - public: - static void ExecRasm2(const std::string &nemonic, uint8_t insn_bytes[4]); -}; - -class TestAArch64TraceManager final : public AArch64TraceManager { - public: - TestAArch64TraceManager(std::string __target_elf_file_name) - : AArch64TraceManager(__target_elf_file_name){}; - std::unordered_map test_inst_state_map; -}; diff --git a/tests/aarch64/TestMainLifter.cpp b/tests/aarch64/TestMainLifter.cpp deleted file mode 100644 index 20b1c43..0000000 --- a/tests/aarch64/TestMainLifter.cpp +++ /dev/null @@ -1,133 +0,0 @@ -#include "TestMainLifter.h" - -#include "TestLift.h" -#include "utils/Util.h" - -/* TestLifter class */ -void TestLifter::DeclareHelperFunction() { - static_cast(impl.get())->DeclareHelperFunction(); -} - -llvm::BasicBlock *TestLifter::TestWrapImpl::PreVirtualMachineForInsnTest( - uint64_t inst_addr, TraceManager &trace_manager, llvm::BranchInst *pre_check_branch_inst) { - llvm::BasicBlock *pre_test_vm_bb; - - auto test_manager = static_cast(&trace_manager); - if (1 == test_manager->test_inst_state_map.count(inst_addr)) { - auto insn_state = test_manager->test_inst_state_map[inst_addr]; - pre_test_vm_bb = llvm::BasicBlock::Create(context, GetUniquePreVMBBName().c_str(), func); - llvm::IRBuilder<> ir(pre_test_vm_bb); - auto state_ptr = NthArgument(func, kStatePointerArgNum); - /* Set next pc */ - auto [pc_ref, pc_ref_type] = - arch->DefaultLifter(*intrinsics)->LoadRegAddress(block, state_ptr, kPCVariableName); - auto [next_pc_ref, next_pc_ref_type] = - arch->DefaultLifter(*intrinsics)->LoadRegAddress(block, state_ptr, kNextPCVariableName); - ir.CreateStore(ir.CreateLoad(word_type, next_pc_ref), pc_ref); - /* show target inst */ - auto show_test_func = module->getFunction(show_test_target_inst_name); - if (!show_test_func) - elfconv_runtime_error("[ERROR] %s doesn't exist in LLVM module.\n", - show_test_target_inst_name.c_str()); - ir.CreateCall(show_test_func); - /* set every initial state of virtual machine */ - for (auto &[_reg_name, ini_num] : insn_state->ini_state) { - auto [reg_ptr, _reg_ty] = - arch->DefaultLifter(*intrinsics)->LoadRegAddress(pre_test_vm_bb, state_ptr, _reg_name); - ir.CreateStore(llvm::ConstantInt::get(_reg_ty, ini_num), reg_ptr); - } - } else { - elfconv_runtime_error( - "[ERROR] %lld is invalid address at Preparation of instruction test state.\n", inst_addr); - } - - /* change the succesor of pre_check_branch_inst to `L_pre_vmX`*/ - if (nullptr != pre_check_branch_inst) { - if (pre_check_branch_inst->getSuccessor(0) != block) - elfconv_runtime_error( - "pre_check_branch_inst->getSuccessor(0) must be equaul to current block.\n"); - pre_check_branch_inst->setSuccessor(0, pre_test_vm_bb); - } - - return pre_test_vm_bb; -} - -llvm::BranchInst * -TestLifter::TestWrapImpl::CheckVirtualMahcineForInsnTest(uint64_t inst_addr, - TraceManager &trace_manager) { - llvm::BasicBlock *check_test_vm_bb; - llvm::BranchInst *block_branch_inst; - llvm::BasicBlock *next_insn_block; - llvm::BranchInst *check_branch_inst; - - for (llvm::Instruction &ir_instr : *block) - if (block_branch_inst = llvm::dyn_cast(&ir_instr); - nullptr != block_branch_inst) { - next_insn_block = block_branch_inst->getSuccessor(0); - /* why? */ // CHECK(nullptr == block_branch_inst->getSuccessor(1)); - break; - } - if (nullptr == block_branch_inst || nullptr == next_insn_block) - elfconv_runtime_error( - "[TESTERROR] cannot find the llvm::BranchInst* from the already lifted basic block.\n"); - - auto test_manager = static_cast(&trace_manager); - if (1 == test_manager->test_inst_state_map.count(inst_addr)) { - auto insn_state = test_manager->test_inst_state_map[inst_addr]; - check_test_vm_bb = llvm::BasicBlock::Create(context, GetUniqueCheckVMBBName().c_str(), func); - /* change the branch block to `L_check` */ - block_branch_inst->setSuccessor(0, check_test_vm_bb); - llvm::IRBuilder<> ir_1(check_test_vm_bb); - CHECK(inst.IsValid()); - auto state_ptr = NthArgument(func, kStatePointerArgNum); - /* check every state of virtual machine */ - llvm::Value *cond_val = llvm::ConstantInt::get(llvm::Type::getInt1Ty(context), 1); - for (auto &[_reg_name, required_num] : insn_state->required_state) { - auto reg_val = inst.GetLifter()->LoadRegValue(check_test_vm_bb, state_ptr, _reg_name); - auto is_eq = - ir_1.CreateICmpEQ(reg_val, llvm::ConstantInt::get(reg_val->getType(), required_num)); - cond_val = ir_1.CreateAnd(cond_val, is_eq); /* cond_val = cond_1 && cond_2 && ... cond_n */ - } - CHECK(test_failed_block); - check_branch_inst = ir_1.CreateCondBr(cond_val, next_insn_block, test_failed_block); - } else { - elfconv_runtime_error("[ERROR] %lld is invalid address at Check of instruction test state.\n", - inst_addr); - } - - return check_branch_inst; -} - -void TestLifter::TestWrapImpl::AddTestFailedBlock() { - if (test_failed_block) - return; - CHECK(!test_failed_block); - test_failed_block = llvm::BasicBlock::Create(context, test_failed_bb_name.c_str(), func); - llvm::IRBuilder<> ir(test_failed_block); - auto failed_fun = module->getFunction(test_failed_result_fn_name.c_str()); - if (!failed_fun) - elfconv_runtime_error("[ERROR] %s is not defined in LLVM module.\n", - test_failed_result_fn_name.c_str()); - ir.CreateCall(failed_fun); - /* actually unreachable */ - auto mem_ptr_val = inst.GetLifter()->LoadRegValue( - test_failed_block, NthArgument(func, kStatePointerArgNum), kMemoryVariableName); - ir.CreateRet(mem_ptr_val); -} - -void TestLifter::TestWrapImpl::DeclareHelperFunction() { - /* void get_failed_lifting_detail() */ - llvm::Function::Create(llvm::FunctionType::get(llvm::Type::getVoidTy(context), {}, false), - llvm::Function::ExternalLinkage, test_failed_result_fn_name, *module); - /* void show_test_target_inst() */ - llvm::Function::Create(llvm::FunctionType::get(llvm::Type::getVoidTy(context), {}, false), - llvm::Function::ExternalLinkage, show_test_target_inst_name, *module); -} - -std::string TestLifter::TestWrapImpl::GetUniquePreVMBBName() { - return pre_vm_bb_name + to_string(unique_num_of_bb++); -} - -std::string TestLifter::TestWrapImpl::GetUniqueCheckVMBBName() { - return check_vm_bb_name + to_string(unique_num_of_bb++); -} diff --git a/tests/aarch64/TestMainLifter.h b/tests/aarch64/TestMainLifter.h deleted file mode 100644 index 546f473..0000000 --- a/tests/aarch64/TestMainLifter.h +++ /dev/null @@ -1,48 +0,0 @@ -#pragma once - -#include "lifter/MainLifter.h" - -class TestLifter final : public MainLifter { - class TestWrapImpl final : public MainLifter::WrapImpl { - public: - TestWrapImpl(const Arch *__arch, TraceManager *__manager) - : MainLifter::WrapImpl(__arch, __manager), - pre_vm_bb_name("L_pre_vm"), - check_vm_bb_name("L_check_vm"), - test_failed_bb_name("L_test_failed"), - test_failed_result_fn_name("get_failed_lifting_detail"), - show_test_target_inst_name("show_test_target_insn"), - unique_num_of_bb(0), - test_failed_block(nullptr) {} - - ~TestWrapImpl() final {} - - /* Prepare the virtual machine for instruction test (need override) */ - llvm::BasicBlock *PreVirtualMachineForInsnTest(uint64_t inst_addr, TraceManager &trace_manager, - llvm::BranchInst *pre_check_branch_inst) final; - /* Check the virtual machine for instruction test (need override) */ - llvm::BranchInst *CheckVirtualMahcineForInsnTest(uint64_t inst_addr, - TraceManager &trace_manager) final; - void AddTestFailedBlock() final; - void DeclareHelperFunction() final; - - inline std::string GetUniquePreVMBBName(); - inline std::string GetUniqueCheckVMBBName(); - - public: - std::string pre_vm_bb_name; - std::string check_vm_bb_name; - std::string test_failed_bb_name; - std::string test_failed_result_fn_name; - std::string show_test_target_inst_name; - uint32_t unique_num_of_bb; - - llvm::BasicBlock *test_failed_block; - }; - - public: - TestLifter(const Arch *__arch, TraceManager *__manager) - : MainLifter(static_cast(new TestWrapImpl(__arch, __manager))) {} - - void DeclareHelperFunction() final; -}; diff --git a/tests/aarch64/TestState.h b/tests/aarch64/TestState.h deleted file mode 100644 index 5422f90..0000000 --- a/tests/aarch64/TestState.h +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once - -#include -#include - -static const uintptr_t g_test_disasm_func_vma = 0x00400000; - -class TestInstructionState { - public: - std::string mnemonic; - std::unordered_map ini_state; - std::unordered_map required_state; - - TestInstructionState(std::string __mnemonic, - std::unordered_map __ini_state, - std::unordered_map __required_state) - : mnemonic(__mnemonic), - ini_state(__ini_state), - required_state(__required_state) {} - TestInstructionState() {} -}; From 67625db396c175e4703dc7291f2f016a2b6342e2 Mon Sep 17 00:00:00 2001 From: yomaytk Date: Mon, 4 Mar 2024 23:42:20 +0900 Subject: [PATCH 11/21] Add new aarch64 tests. --- tests/aarch64/Makefile | 14 ++++++++++++++ tests/aarch64/TargetInstructions.c | 12 ++++++++++++ tests/aarch64/TestExec.c | 14 ++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 tests/aarch64/Makefile create mode 100644 tests/aarch64/TargetInstructions.c create mode 100644 tests/aarch64/TestExec.c diff --git a/tests/aarch64/Makefile b/tests/aarch64/Makefile new file mode 100644 index 0000000..b55c837 --- /dev/null +++ b/tests/aarch64/Makefile @@ -0,0 +1,14 @@ +CC=clang-16 + +aarch64_custom_test: TestExec.c + @ARCH=$$(uname -m); \ + if [ "$$ARCH" = "x86_64" ]; then \ + $(CC) -static --target=aarch64-linux-gnu --gcc-toolchain=/usr --sysroot=/usr/aarch64-linux-gnu TestExec.c-fuse-ld=lld -pthread; \ + elif [ "$$ARCH" = "aarch64" ]; then \ + $(CC) -static TestExec.c; \ + else \ + echo "Unknown architecture"; exit 1; \ + fi + +clean: + rm a.out diff --git a/tests/aarch64/TargetInstructions.c b/tests/aarch64/TargetInstructions.c new file mode 100644 index 0000000..d461e02 --- /dev/null +++ b/tests/aarch64/TargetInstructions.c @@ -0,0 +1,12 @@ + +long fcvtas_Xd_Dn(double val) { + long res; + asm("fcvtas %0, d0" : "=r"(res) : "w"(val) :); + return res; +} + +double frinta_Dd_Dn(double val) { + double res; + asm("frinta %d0, %d1" : "=w"(res) : "w"(val) :); + return res; +} diff --git a/tests/aarch64/TestExec.c b/tests/aarch64/TestExec.c new file mode 100644 index 0000000..3b227aa --- /dev/null +++ b/tests/aarch64/TestExec.c @@ -0,0 +1,14 @@ +#include "TargetInstructions.c" + +#include +#include + +int main() { + // FCVTAS , + assert(fcvtas_Xd_Dn(43.3f) == 43); + // FRINTA
, + assert(frinta_Dd_Dn(43.3f) == 43); + + printf("Test Success!\n"); + return 0; +} From c400290c4ca1648f9f2c44a4bac86714f6401e36 Mon Sep 17 00:00:00 2001 From: yomaytk Date: Mon, 4 Mar 2024 23:43:57 +0900 Subject: [PATCH 12/21] Remove aarch64 test target from CMakeLists.txt --- CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d78c191..a158fe0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,5 +22,4 @@ add_subdirectory(backend/remill) add_subdirectory(lifter) # tests -add_subdirectory(tests/aarch64) add_subdirectory(tests/elfconv) \ No newline at end of file From 755a8bcc0551592ab0427cf1202a1f5273e7fe72 Mon Sep 17 00:00:00 2001 From: yomaytk Date: Tue, 5 Mar 2024 21:16:59 +0900 Subject: [PATCH 13/21] Add runtime when using wasi sdk. --- runtime/Entry.cpp | 4 +-- runtime/Memory.cpp | 20 +++++++-------- runtime/Syscall.cpp | 59 +++++++++++++++++++++++++++++++++++---------- scripts/dev.sh | 43 +++++++++++++++++++-------------- utils/elfconv.cpp | 9 ++----- utils/elfconv.h | 4 +-- 6 files changed, 87 insertions(+), 52 deletions(-) diff --git a/runtime/Entry.cpp b/runtime/Entry.cpp index ef8224d..1418568 100644 --- a/runtime/Entry.cpp +++ b/runtime/Entry.cpp @@ -3,7 +3,7 @@ #include #include #include -#if defined(LIFT_DEBUG) +#if defined(LIFT_DEBUG) && defined(__linux__) # include # include # include @@ -22,7 +22,7 @@ int main(int argc, char *argv[]) { std::vector mapped_memorys; -#if defined(LIFT_DEBUG) +#if defined(LIFT_DEBUG) && defined(__linux__) struct sigaction segv_action = {0}; segv_action.sa_flags = SA_SIGINFO; segv_action.sa_sigaction = segv_debug_state_machine; diff --git a/runtime/Memory.cpp b/runtime/Memory.cpp index c1d42ca..1802473 100644 --- a/runtime/Memory.cpp +++ b/runtime/Memory.cpp @@ -40,29 +40,29 @@ MappedMemory *MappedMemory::VMAStackEntryInit(int argc, char *argv[], _ecv_reg64_t _ecv_a_val; } _ecv_a_un; } _ecv_auxv64[] = { -#if defined(__linux__) +#if defined(WASI_ENV) {3 /* AT_PHDR */, phdr}, {4 /* AT_PHENT */, __g_e_phent}, {5 /* AT_PHNUM */, __g_e_phnum}, {6 /* AT_PAGESZ */, 4096}, {9 /* AT_ENTRY */, __g_entry_pc}, - {11 /* AT_UID */, getuid()}, - {12 /* AT_EUID */, geteuid()}, - {13 /* AT_GID */, getgid()}, - {14 /* AT_EGID */, getegid()}, + {11 /* AT_UID */, 42}, + {12 /* AT_EUID */, 42}, + {13 /* AT_GID */, 42}, + {14 /* AT_EGID */, 42}, {23 /* AT_SECURE */, 0}, {25 /* AT_RANDOM */, randomp}, {0 /* AT_NULL */, 0}, -#elif defined(__wasm__) +#else {3 /* AT_PHDR */, phdr}, {4 /* AT_PHENT */, __g_e_phent}, {5 /* AT_PHNUM */, __g_e_phnum}, {6 /* AT_PAGESZ */, 4096}, {9 /* AT_ENTRY */, __g_entry_pc}, - {11 /* AT_UID */, 42}, - {12 /* AT_EUID */, 42}, - {13 /* AT_GID */, 42}, - {14 /* AT_EGID */, 42}, + {11 /* AT_UID */, getuid()}, + {12 /* AT_EUID */, geteuid()}, + {13 /* AT_GID */, getgid()}, + {14 /* AT_EGID */, getegid()}, {23 /* AT_SECURE */, 0}, {25 /* AT_RANDOM */, randomp}, {0 /* AT_NULL */, 0}, diff --git a/runtime/Syscall.cpp b/runtime/Syscall.cpp index 3b99ee6..9d63d7c 100644 --- a/runtime/Syscall.cpp +++ b/runtime/Syscall.cpp @@ -6,7 +6,6 @@ #include #include #include -#include #include #include #include @@ -14,7 +13,10 @@ #include #include #include -#include +#if defined(__linux__) +# include +# include +#endif #include #include #include @@ -138,6 +140,12 @@ void __svc_call(void) { #endif switch (state_gpr.x8.qword) { case AARCH64_SYS_IOCTL: /* ioctl (unsigned int fd, unsigned int cmd, unsigned long arg) */ +#if defined(ELFC_RUNTIME_HOST_ENV) + EMPTY_SYSCALL(AARCH64_SYS_IOCTL) + state_gpr.x0.qword = -1; + errno = _ECV_EACCESS; + break; +#else { unsigned int fd = state_gpr.x0.dword; unsigned int cmd = state_gpr.x1.dword; @@ -164,6 +172,7 @@ void __svc_call(void) { default: break; } } +#endif case AARCH64_SYS_FACCESSAT: /* faccessat (int dfd, const char *filename, int mode) */ /* TODO */ state_gpr.x0.qword = -1; @@ -226,7 +235,11 @@ void __svc_call(void) { break; case AARCH64_SYS_SET_TID_ADDRESS: /* set_tid_address(int *tidptr) */ { +#if defined(WASI_ENV) + pid_t tid = 42; +#else pid_t tid = gettid(); +#endif *reinterpret_cast(_ecv_translate_ptr(state_gpr.x0.qword)) = tid; state_gpr.x0.qword = tid; } break; @@ -246,6 +259,12 @@ void __svc_call(void) { errno = _ECV_EACCESS; break; case AARCH64_SYS_CLOCK_GETTIME: /* clock_gettime (clockid_t which_clock, struct __kernel_timespace *tp) */ +#if defined(WASI_ENV) + EMPTY_SYSCALL(AARCH64_SYS_CLOCK_GETTIME); + state_gpr.x0.qword = -1; + errno = _ECV_EACCESS; + break; +#else { clockid_t which_clock = state_gpr.x0.dword; struct timespec emu_tp; @@ -253,13 +272,16 @@ void __svc_call(void) { memcpy(_ecv_translate_ptr(state_gpr.x1.qword), &emu_tp, sizeof(timespec)); state_gpr.x0.qword = (_ecv_reg64_t) clock_time; } break; +#endif case AARCH64_SYS_TGKILL: /* tgkill (pid_t tgid, pid_t pid, int sig) */ -#if defined(__linux__) - state_gpr.x0.qword = tgkill(state_gpr.x0.dword, state_gpr.x1.dword, state_gpr.x2.dword); +#if defined(ELFC_RUNTIME_HOST_ENV) + EMPTY_SYSCALL(AARCH64_SYS_TGKILL); + state_gpr.x0.qword = -1; + errno = _ECV_EACCESS; #elif defined(__wasm__) state_gpr.x0.qword = kill(state_gpr.x0.dword, state_gpr.x1.dword); -#else - elfconv_runtime_error("Unknown Environment\n"); +#elif defined(__linux__) + state_gpr.x0.qword = tgkill(state_gpr.x0.dword, state_gpr.x1.dword, state_gpr.x2.dword); #endif break; case AARCH64_SYS_RT_SIGPROCMASK: /* rt_sigprocmask (int how, sigset_t *set, sigset_t *oset, size_t sigsetsize) */ @@ -268,10 +290,17 @@ void __svc_call(void) { EMPTY_SYSCALL(AARCH64_SYS_RT_SIGPROCMASK); break; case AARCH64_SYS_RT_SIGACTION: /* rt_sigaction (int signum, const struct sigaction *act, struct sigaction *oldact) */ +#if defined(ELFC_RUNTIME_HOST_ENV) + state_gpr.x0.qword = -1; + errno = _ECV_EACCESS; + EMPTY_SYSCALL(AARCH64_SYS_RT_SIGACTION) + break; +#else state_gpr.x0.dword = sigaction( state_gpr.x0.dword, (const struct sigaction *) _ecv_translate_ptr(state_gpr.x1.qword), (struct sigaction *) _ecv_translate_ptr(state_gpr.x2.qword)); break; +#endif case AARCH64_SYS_UNAME: /* uname (struct old_utsname* buf) */ #if defined(__linux__) { @@ -280,7 +309,7 @@ void __svc_call(void) { memcpy(_ecv_translate_ptr(state_gpr.x0.qword), &_utsname, sizeof(utsname)); state_gpr.x0.dword = ret; } -#elif defined(__wasm__) +#else { struct __my_utsname { char sysname[65]; @@ -296,19 +325,23 @@ void __svc_call(void) { } #endif break; +#if defined(ELFC_RUNTIME_HOST_ENV) + case AARCH64_SYS_GETPID: /* getpid () */ state_gpr.x0.dword = 42; break; + case AARCH64_SYS_GETPPID: /* getppid () */ state_gpr.x0.dword = 42; break; + case AARCH64_SYS_GETTUID: /* getuid () */ state_gpr.x0.dword = 42; break; + case AARCH64_SYS_GETEUID: /* geteuid () */ state_gpr.x0.dword = 42; break; + case AARCH64_SYS_GETGID: /* getgid () */ state_gpr.x0.dword = 42; break; + case AARCH64_SYS_GETEGID: /* getegid () */ state_gpr.x0.dword = 42; break; + case AARCH64_SYS_GETTID: /* getttid () */ state_gpr.x0.dword = 42; break; +#else case AARCH64_SYS_GETPID: /* getpid () */ state_gpr.x0.dword = getpid(); break; case AARCH64_SYS_GETPPID: /* getppid () */ state_gpr.x0.dword = getppid(); break; case AARCH64_SYS_GETTUID: /* getuid () */ state_gpr.x0.dword = getuid(); break; case AARCH64_SYS_GETEUID: /* geteuid () */ state_gpr.x0.dword = geteuid(); break; case AARCH64_SYS_GETGID: /* getgid () */ state_gpr.x0.dword = getgid(); break; case AARCH64_SYS_GETEGID: /* getegid () */ state_gpr.x0.dword = getegid(); break; - case AARCH64_SYS_GETTID: /* getttid () */ -#if defined(__linux__) - state_gpr.x0.dword = gettid(); -#else - state_gpr.x0.qword = 0; + case AARCH64_SYS_GETTID: /* getttid () */ state_gpr.x0.dword = gettid(); break; #endif - break; case AARCH64_SYS_BRK: /* brk (unsigned long brk) */ { auto heap_memory = g_run_mgr->heap_memory; diff --git a/scripts/dev.sh b/scripts/dev.sh index 8b40e1a..38fcf3b 100755 --- a/scripts/dev.sh +++ b/scripts/dev.sh @@ -16,7 +16,7 @@ setting() { BUILD_LIFTER_DIR=${BUILD_DIR}/lifter BUILD_TESTS_AARCH64_DIR=${BUILD_DIR}/tests/aarch64 CXX=clang++-16 - OPTFLAGS="-O0" + OPTFLAGS="-O3" CLANGFLAGS="${OPTFLAGS} -static -I${ROOT_DIR}/backend/remill/include -I${ROOT_DIR}" CXXX64=x86_64-linux-gnu-g++-11 CROSS_COMPILE_FLAGS_X64="-static --target=x86-64-linux-gnu nostdin_linpack.c -fuse-ld=lld -pthread;" @@ -27,6 +27,8 @@ setting() { WASISDKFLAGS="${OPTFLAGS} --sysroot=${WASI_SDK_PATH}/share/wasi-sysroot -I${ROOT_DIR}/backend/remill/include -I${ROOT_DIR}" ELFCONV_MACROS="-DELFCONV_BROWSER_ENV=1" ELFCONV_DEBUG_MACROS= + WASMCC=$EMCC + WASMCCFLAGS=$EMCCFLAGS if [ -n "$DEBUG" ]; then ELFCONV_DEBUG_MACROS="-DELFC_RUNTIME_SYSCALL_DEBUG=1 -DELFC_RUNTIME_MULSECTIONS_WARNING=1 " @@ -47,7 +49,7 @@ aarch64_test() { echo "[INFO] Generate aarch64_lift.ll" # generate execute file (lift_test.aarch64) - ${CXX} ${CLANGFLAGS} ${ELFCONV_MACROS} ${ELFCONV_DEBUG_MACROS} -o lift_test.aarch64 aarch64_test.ll ${AARCH64_TEST_DIR}/Test.cpp ${AARCH64_TEST_DIR}/TestHelper.cpp \ + ${CXX} ${CLANGFLAGS} $ELFCONV_MACROS $ELFCONV_DEBUG_MACROS -o lift_test.aarch64 aarch64_test.ll ${AARCH64_TEST_DIR}/Test.cpp ${AARCH64_TEST_DIR}/TestHelper.cpp \ ${AARCH64_TEST_DIR}/TestInstructions.cpp ${RUNTIME_DIR}/Memory.cpp ${RUNTIME_DIR}/Syscall.cpp ${RUNTIME_DIR}/VmIntrinsics.cpp ${UTILS_DIR}/Util.cpp ${UTILS_DIR}/elfconv.cpp echo "[INFO] Generate lift_test.aarch64" @@ -95,14 +97,14 @@ main() { ;; wasm-browser) cd "${BUILD_LIFTER_DIR}" && \ - ${EMCC} ${EMCCFLAGS} ${ELFCONV_MACROS} ${ELFCONV_DEBUG_MACROS} -o Entry.wasm.o -c ${RUNTIME_DIR}/Entry.cpp && \ - ${EMCC} ${EMCCFLAGS} ${ELFCONV_MACROS} ${ELFCONV_DEBUG_MACROS} -o Memory.wasm.o -c ${RUNTIME_DIR}/Memory.cpp && \ - ${EMCC} ${EMCCFLAGS} ${ELFCONV_MACROS} ${ELFCONV_DEBUG_MACROS} -o Syscall.wasm.o -c ${RUNTIME_DIR}/Syscall.cpp && \ - ${EMCC} ${EMCCFLAGS} ${ELFCONV_MACROS} ${ELFCONV_DEBUG_MACROS} -o VmIntrinsics.wasm.o -c ${RUNTIME_DIR}/VmIntrinsics.cpp && \ - ${EMCC} ${EMCCFLAGS} ${ELFCONV_MACROS} ${ELFCONV_DEBUG_MACROS} -o Util.wasm.o -c ${UTILS_DIR}/Util.cpp && \ - ${EMCC} ${EMCCFLAGS} ${ELFCONV_MACROS} ${ELFCONV_DEBUG_MACROS} -o elfconv.wasm.o -c ${UTILS_DIR}/elfconv.cpp && \ - ${EMCC} ${EMCCFLAGS} -c lift.ll -o lift.wasm.o - ${EMCC} ${EMCCFLAGS} -o exe.wasm.html -sWASM -sALLOW_MEMORY_GROWTH lift.wasm.o Entry.wasm.o Memory.wasm.o Syscall.wasm.o \ + $WASMCC $WASMCCFLAGS $ELFCONV_MACROS $ELFCONV_DEBUG_MACROS -o Entry.wasm.o -c ${RUNTIME_DIR}/Entry.cpp && \ + $WASMCC $WASMCCFLAGS $ELFCONV_MACROS $ELFCONV_DEBUG_MACROS -o Memory.wasm.o -c ${RUNTIME_DIR}/Memory.cpp && \ + $WASMCC $WASMCCFLAGS $ELFCONV_MACROS $ELFCONV_DEBUG_MACROS -o Syscall.wasm.o -c ${RUNTIME_DIR}/Syscall.cpp && \ + $WASMCC $WASMCCFLAGS $ELFCONV_MACROS $ELFCONV_DEBUG_MACROS -o VmIntrinsics.wasm.o -c ${RUNTIME_DIR}/VmIntrinsics.cpp && \ + $WASMCC $WASMCCFLAGS $ELFCONV_MACROS $ELFCONV_DEBUG_MACROS -o Util.wasm.o -c ${UTILS_DIR}/Util.cpp && \ + $WASMCC $WASMCCFLAGS $ELFCONV_MACROS $ELFCONV_DEBUG_MACROS -o elfconv.wasm.o -c ${UTILS_DIR}/elfconv.cpp && \ + $WASMCC $WASMCCFLAGS -c lift.ll -o lift.wasm.o + $WASMCC $WASMCCFLAGS -o exe.wasm.html -sWASM -sALLOW_MEMORY_GROWTH lift.wasm.o Entry.wasm.o Memory.wasm.o Syscall.wasm.o \ VmIntrinsics.wasm.o Util.wasm.o elfconv.wasm.o echo "[INFO] Generate WASM binary." # delete obj @@ -111,15 +113,20 @@ main() { ;; wasm-host) ELFCONV_MACROS="-DELFC_RUNTIME_HOST_ENV=1" + if [ -n "$WASISDK" ]; then + WASMCC=$WASISDK_CXX + WASMCCFLAGS=$WASISDKFLAGS + ELFCONV_MACROS="${ELFCONV_MACROS} -DWASI_ENV=1 -fno-exceptions" + fi cd "${BUILD_LIFTER_DIR}" && \ - ${EMCC} ${EMCCFLAGS} ${ELFCONV_MACROS} ${ELFCONV_DEBUG_MACROS} -o Entry.wasm.o -c ${RUNTIME_DIR}/Entry.cpp && \ - ${EMCC} ${EMCCFLAGS} ${ELFCONV_MACROS} ${ELFCONV_DEBUG_MACROS} -o Memory.wasm.o -c ${RUNTIME_DIR}/Memory.cpp && \ - ${EMCC} ${EMCCFLAGS} ${ELFCONV_MACROS} ${ELFCONV_DEBUG_MACROS} -o Syscall.wasm.o -c ${RUNTIME_DIR}/Syscall.cpp && \ - ${EMCC} ${EMCCFLAGS} ${ELFCONV_MACROS} ${ELFCONV_DEBUG_MACROS} -o VmIntrinsics.wasm.o -c ${RUNTIME_DIR}/VmIntrinsics.cpp && \ - ${EMCC} ${EMCCFLAGS} ${ELFCONV_MACROS} ${ELFCONV_DEBUG_MACROS} -o Util.wasm.o -c ${UTILS_DIR}/Util.cpp && \ - ${EMCC} ${EMCCFLAGS} ${ELFCONV_MACROS} ${ELFCONV_DEBUG_MACROS} -o elfconv.wasm.o -c ${UTILS_DIR}/elfconv.cpp && \ - ${EMCC} -c lift.ll -o lift.wasm.o - ${EMCC} -o exe.wasm lift.wasm.o Entry.wasm.o Memory.wasm.o Syscall.wasm.o VmIntrinsics.wasm.o Util.wasm.o elfconv.wasm.o + $WASMCC $WASMCCFLAGS $ELFCONV_MACROS $ELFCONV_DEBUG_MACROS -o Entry.wasm.o -c ${RUNTIME_DIR}/Entry.cpp && \ + $WASMCC $WASMCCFLAGS $ELFCONV_MACROS $ELFCONV_DEBUG_MACROS -o Memory.wasm.o -c ${RUNTIME_DIR}/Memory.cpp && \ + $WASMCC $WASMCCFLAGS $ELFCONV_MACROS $ELFCONV_DEBUG_MACROS -o Syscall.wasm.o -c ${RUNTIME_DIR}/Syscall.cpp && \ + $WASMCC $WASMCCFLAGS $ELFCONV_MACROS $ELFCONV_DEBUG_MACROS -o VmIntrinsics.wasm.o -c ${RUNTIME_DIR}/VmIntrinsics.cpp && \ + $WASMCC $WASMCCFLAGS $ELFCONV_MACROS $ELFCONV_DEBUG_MACROS -o Util.wasm.o -c ${UTILS_DIR}/Util.cpp && \ + $WASMCC $WASMCCFLAGS $ELFCONV_MACROS $ELFCONV_DEBUG_MACROS -o elfconv.wasm.o -c ${UTILS_DIR}/elfconv.cpp && \ + $WASMCC -c lift.ll -o lift.wasm.o + $WASMCC -o exe.wasm lift.wasm.o Entry.wasm.o Memory.wasm.o Syscall.wasm.o VmIntrinsics.wasm.o Util.wasm.o elfconv.wasm.o echo "[INFO] Generate WASM binary." # delete obj cd "${BUILD_LIFTER_DIR}" && rm *.o diff --git a/utils/elfconv.cpp b/utils/elfconv.cpp index 80656b6..c514c22 100644 --- a/utils/elfconv.cpp +++ b/utils/elfconv.cpp @@ -89,15 +89,10 @@ extern "C" void debug_memory() { extern "C" void debug_insn() { auto gpr = g_state.gpr; std::cout << "[DEBUG INSN]" << std::endl; - std::cout << std::hex << "PC: 0x" << gpr.pc.qword << ", SP: 0x" << gpr.sp.qword << ", x19: 0x" - << gpr.x19.qword << ", x20: 0x" << gpr.x20.qword << ", x21: 0x" << gpr.x21.qword - << ", x22: 0x" << gpr.x22.qword << ", x29: 0x" << gpr.x29.qword << ", x30: 0x" - << gpr.x30.qword << std::dec << ", x8: " << gpr.x8.dword << std::hex << ", x0: 0x" - << gpr.x0.qword << ", x1: 0x" << gpr.x1.qword << ", x2: 0x" << gpr.x2.qword - << std::endl; + std::cout << std::hex << "PC: 0x" << gpr.pc.qword << std::endl; } -#if defined(LIFT_DEBUG) +#if defined(LIFT_DEBUG) && defined(__linux__) extern "C" void segv_debug_state_machine(int sig, siginfo_t *info, void *ctx) { std::cout << "[ERROR] Segmantation Fault." << std::endl; std::cout << "signo: " << info->si_signo << " code: " << info->si_code << std::endl; diff --git a/utils/elfconv.h b/utils/elfconv.h index 2b2c528..39aebb2 100644 --- a/utils/elfconv.h +++ b/utils/elfconv.h @@ -2,7 +2,7 @@ #include #include -#if defined(LIFT_DEBUG) +#if defined(LIFT_DEBUG) && defined(__linux__) # include #endif @@ -11,6 +11,6 @@ extern "C" void debug_state_machine(); extern "C" void debug_state_machine_vectors(); extern "C" void debug_memory(); extern "C" void debug_insn(); -#if defined(LIFT_DEBUG) +#if defined(LIFT_DEBUG) && defined(__linux__) extern "C" void segv_debug_state_machine(int sig, siginfo_t *info, void *ctx); #endif \ No newline at end of file From 05c4f9f2a2d48c6762ddcf7cf140e6033f534273 Mon Sep 17 00:00:00 2001 From: yomaytk Date: Wed, 6 Mar 2024 23:47:53 +0900 Subject: [PATCH 14/21] Fix openat and readlinkat for wasi-sdk. --- runtime/Memory.h | 2 +- runtime/Syscall.cpp | 15 ++++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/runtime/Memory.h b/runtime/Memory.h index 1e31fcd..508c7a3 100644 --- a/runtime/Memory.h +++ b/runtime/Memory.h @@ -12,7 +12,7 @@ const addr_t STACK_START_VMA = 0x0fff'ff00'0000'0000; /* 65535 TiB FIXME! */ const size_t STACK_SIZE = 1 * 1024 * 1024; /* 4 MiB */ const addr_t HEAPS_START_VMA = 0x4000'0000'0000; /* 64 TiB FIXME! */ -const uint64_t HEAP_UNIT_SIZE = 1 * 1024 * 1024 * 1024; /* 1 MiB */ +const uint64_t HEAP_UNIT_SIZE = 1 * 1024 * 1024 * 1024; /* 1 GiB */ typedef uint32_t _ecv_reg_t; typedef uint64_t _ecv_reg64_t; diff --git a/runtime/Syscall.cpp b/runtime/Syscall.cpp index 9d63d7c..a6d5655 100644 --- a/runtime/Syscall.cpp +++ b/runtime/Syscall.cpp @@ -180,8 +180,15 @@ void __svc_call(void) { errno = _ECV_EACCESS; break; case AARCH64_SYS_OPENAT: /* openat (int dfd, const char* filename, int flags, umode_t mode) */ +#if defined(WASI_ENV) + if (-100 == state_gpr.x0.dword) + state_gpr.x0.qword = AT_FDCWD; // AT_FDCWD on WASI: -2 (-100 on Linux) + state_gpr.x2.dword = O_RDWR; +#endif state_gpr.x0.dword = openat( state_gpr.x0.dword, (char *) _ecv_translate_ptr(state_gpr.x1.qword), state_gpr.x2.dword); + if (-1 == state_gpr.x0.dword) + perror("openat error!"); break; case AARCH64_SYS_CLOSE: /* int close (unsigned int fd) */ state_gpr.x0.dword = close(state_gpr.x0.dword); @@ -209,15 +216,9 @@ void __svc_call(void) { free(cache_vec); } break; case AARCH64_SYS_READLINKAT: /* readlinkat (int dfd, const char *path, char *buf, int bufsiz) */ -#if defined(ELFC_RUNTIME_HOST_ENV) - /* FIXME! */ - memcpy((char *) _ecv_translate_ptr(state_gpr.x2.qword), - (const char *) _ecv_translate_ptr(state_gpr.x1.qword), state_gpr.x3.dword); -#else state_gpr.x0.qword = readlinkat(state_gpr.x0.dword, (const char *) _ecv_translate_ptr(state_gpr.x1.qword), (char *) _ecv_translate_ptr(state_gpr.x2.qword), state_gpr.x3.dword); -#endif break; case AARCH64_SYS_NEWFSTATAT: /* newfstatat (int dfd, const char *filename, struct stat *statbuf, int flag) */ /* TODO */ @@ -362,7 +363,7 @@ void __svc_call(void) { EMPTY_SYSCALL(AARCH64_SYS_MUNMAP); break; case AARCH64_SYS_MMAP: /* mmap (void *start, size_t lengt, int prot, int flags, int fd, off_t offset) */ - /* TODO */ + /* FIXME */ { auto heap_memory = g_run_mgr->heap_memory; if (state_gpr.x4.dword != -1) From 58fccdc7975e6037fa65c9f267fd9b5e03308aca Mon Sep 17 00:00:00 2001 From: yomaytk Date: Thu, 7 Mar 2024 00:02:35 +0900 Subject: [PATCH 15/21] Undef LIFT_DEBUG. --- backend/remill/include/remill/BC/HelperMacro.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/remill/include/remill/BC/HelperMacro.h b/backend/remill/include/remill/BC/HelperMacro.h index b593021..8073171 100644 --- a/backend/remill/include/remill/BC/HelperMacro.h +++ b/backend/remill/include/remill/BC/HelperMacro.h @@ -1,6 +1,6 @@ #pragma once -#define LIFT_DEBUG 1 +// #define LIFT_DEBUG 1 // #define LIFT_CALLSTACK_DEBUG 1 // #define LIFT_INSN_DEBUG 1 // #define ELFCONV_SYSCALL_DEBUG 1 From 511ac844474fd421986bfbfd62d955d4ef97992b Mon Sep 17 00:00:00 2001 From: yomaytk Date: Thu, 7 Mar 2024 11:25:01 +0900 Subject: [PATCH 16/21] Add wasi-sdk use for container. --- Dockerfile | 4 ++++ bin/elfconv.sh | 41 ++++++++++++++++++++++++++--------------- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/Dockerfile b/Dockerfile index 0034743..0d74344 100644 --- a/Dockerfile +++ b/Dockerfile @@ -46,6 +46,10 @@ RUN apt update && \ RUN cd /root && git clone https://github.com/emscripten-core/emsdk.git && cd emsdk && \ git pull && ./emsdk install latest && ./emsdk activate latest && . ./emsdk_env.sh && echo 'source "/root/emsdk/emsdk_env.sh"' >> /root/.bash_profile +# wasi-sdk install +RUN cd /root && export WASI_VERSION=21 && export WASI_VERSION_FULL=${WASI_VERSION}.0 && echo -e 'export WASI_VERSION=21\nexport WASI_VERSION_FULL=${WASI_VERSION}.0\nexport WASI_SDK_PATH=`pwd`/wasi-sdk-${WASI_VERSION_FULL}' >> /root/.bash_profile && \ +wget https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-${WASI_VERSION}/wasi-sdk-${WASI_VERSION_FULL}-linux.tar.gz && tar xvf wasi-sdk-${WASI_VERSION_FULL}-linux.tar.gz + # WASI Runtimes install RUN curl -sSf https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/utils/install.sh | bash RUN curl https://wasmtime.dev/install.sh -sSf | bash && echo 'export PATH=$PATH:/root/.wasmtime/bin' >> /root/.bash_profile diff --git a/bin/elfconv.sh b/bin/elfconv.sh index 081a44e..8d40c62 100755 --- a/bin/elfconv.sh +++ b/bin/elfconv.sh @@ -14,6 +14,12 @@ setting() { ELFCONV_MACROS="-DELFCONV_BROWSER_ENV=1" ELFCONV_DEBUG_MACROS= ELFPATH=$( realpath "$1" ) + WASMCC=$EMCC + WASMCCFLAGS=$EMCCFLAGS + WASMAR=$EMAR + WASISDKCXX=${WASI_SDK_PATH}/bin/clang++ + WASISDKAR=${WASI_SDK_PATH}/bin/ar + WASISDKFLAGS="${OPTFLAGS} --sysroot=${WASI_SDK_PATH}/share/wasi-sysroot -I${ROOT_DIR}/backend/remill/include -I${ROOT_DIR}" if [ "$TARGET" = "wasm-host" ]; then ELFCONV_MACROS="-DELFC_RUNTIME_HOST_ENV=1" @@ -33,14 +39,19 @@ main() { # build runtime echo "[INFO] Building elfconv-Runtime ..." - cd "${RUNTIME_DIR}" - $EMCC $EMCCFLAGS $ELFCONV_MACROS $ELFCONV_DEBUG_MACROS -o Entry.o -c Entry.cpp && \ - $EMCC $EMCCFLAGS $ELFCONV_MACROS $ELFCONV_DEBUG_MACROS -o Memory.o -c Memory.cpp && \ - $EMCC $EMCCFLAGS $ELFCONV_MACROS $ELFCONV_DEBUG_MACROS -o Syscall.o -c Syscall.cpp && \ - $EMCC $EMCCFLAGS $ELFCONV_MACROS $ELFCONV_DEBUG_MACROS -o VmIntrinsics.o -c VmIntrinsics.cpp && \ - $EMCC $EMCCFLAGS $ELFCONV_MACROS $ELFCONV_DEBUG_MACROS -o Util.o -c ${UTILS_DIR}/Util.cpp && \ - $EMCC $EMCCFLAGS $ELFCONV_MACROS $ELFCONV_DEBUG_MACROS -o elfconv.o -c ${UTILS_DIR}/elfconv.cpp && \ - $EMAR rcs libelfconv.a Entry.o Memory.o Syscall.o VmIntrinsics.o Util.o elfconv.o + if [ "$TARGET" = "wasm-host" ]; then + WASMCC=$WASISDKCXX + WASMCCFLAGS=$WASISDKFLAGS + WASMAR=$WASISDKAR + fi + cd "${RUNTIME_DIR}" || { echo "cd Failure"; exit 1; } + $WASMCC "$WASMCCFLAGS" $ELFCONV_MACROS "$ELFCONV_DEBUG_MACROS" -o Entry.o -c Entry.cpp && \ + $WASMCC "$WASMCCFLAGS" $ELFCONV_MACROS "$ELFCONV_DEBUG_MACROS" -o Memory.o -c Memory.cpp && \ + $WASMCC "$WASMCCFLAGS" $ELFCONV_MACROS "$ELFCONV_DEBUG_MACROS" -o Syscall.o -c Syscall.cpp && \ + $WASMCC "$WASMCCFLAGS" $ELFCONV_MACROS "$ELFCONV_DEBUG_MACROS" -o VmIntrinsics.o -c VmIntrinsics.cpp && \ + $WASMCC "$WASMCCFLAGS" $ELFCONV_MACROS "$ELFCONV_DEBUG_MACROS" -o Util.o -c "${UTILS_DIR}"/Util.cpp && \ + $WASMCC "$WASMCCFLAGS" $ELFCONV_MACROS "$ELFCONV_DEBUG_MACROS" -o elfconv.o -c "${UTILS_DIR}"/elfconv.cpp && \ + $WASMAR rcs libelfconv.a Entry.o Memory.o Syscall.o VmIntrinsics.o Util.o elfconv.o mv libelfconv.a "${BIN_DIR}/" rm *.o echo "[INFO] Generate libelfconv.a." @@ -48,7 +59,7 @@ main() { # ELF -> LLVM bc cp -p "${BUILD_LIFTER_DIR}/elflift" "${BIN_DIR}/" echo "[INFO] Converting ELF to LLVM bitcode ..." - cd "${BIN_DIR}" + cd "${BIN_DIR}" || { echo "cd Failure"; exit 1; } ./elflift \ --arch aarch64 \ --bc_out lift.bc \ @@ -60,17 +71,17 @@ main() { case "$TARGET" in wasm-browser) echo "[INFO] Converting LLVM bitcode to WASM binary (for browser) ..." - cd "${BIN_DIR}" - $EMCC -c lift.bc -o lift.o && \ - $EMCC -o exe.wasm.html -L"./" -sWASM -sALLOW_MEMORY_GROWTH lift.o -lelfconv + cd "${BIN_DIR}" || { echo "cd Failure"; exit 1; } + $WASMCC -c lift.bc -o lift.o && \ + $WASMCC -o exe.wasm.html -L"./" -sWASM -sALLOW_MEMORY_GROWTH lift.o -lelfconv echo "[INFO] Generate WASM binary." return 0 ;; wasm-host) echo "[INFO] Converting LLVM bitcode to WASM binary (for server) ..." - cd "${BIN_DIR}" - $EMCC -c lift.bc -o lift.o && \ - $EMCC -o exe.wasm -L"./" lift.o -lelfconv + cd "${BIN_DIR}" || { echo "cd Failure"; exit 1; } + $WASMCC -c lift.bc -o lift.o && \ + $WASMCC -o exe.wasm -L"./" lift.o -lelfconv echo "[INFO] Generate WASM binary." return 0 ;; From fec88d17aff1a13858aae62da15a0a19f703d8ad Mon Sep 17 00:00:00 2001 From: yomaytk Date: Thu, 7 Mar 2024 11:32:21 +0900 Subject: [PATCH 17/21] Fix .gitignore. --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 296540e..5c92aa8 100644 --- a/.gitignore +++ b/.gitignore @@ -83,6 +83,7 @@ remill_disass.egg-info/* *.app *.wasm elflift +test *.pyc From 1adc0ff1d1ee877182679a4839787df86af3c538 Mon Sep 17 00:00:00 2001 From: yomaytk Date: Thu, 7 Mar 2024 11:41:29 +0900 Subject: [PATCH 18/21] Fix error of scripts/build.sh --- scripts/build.sh | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/scripts/build.sh b/scripts/build.sh index 0b78503..c6205af 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -22,27 +22,20 @@ REMILL_DIR=$( cd "$( realpath "${ROOT_DIR}/backend/remill" )" && pwd ) DOWNLOAD_DIR="$( cd "$( dirname "${REMILL_DIR}" )" && pwd )/lifting-bits-downloads" BUILD_DIR="${ROOT_DIR}/build" BUILD_LIFTER_DIR="${BUILD_DIR}/lifter" -ELFCOV_INSTALL_DIR="${HOME}/.elfconv/bin" -INSTALL_LIB_DIR="${HOME}/.elfconv/lib" +ELFCONV_INSTALL_DIR="${HOME}/.elfconv/bin" LLVM_VERSION=llvm-16 OS_VERSION= ARCH_VERSION= BUILD_FLAGS= LIFT_DEBUG_MACROS= CXX_COMMON_VERSION="0.5.0" -EMCC=emcc -EMAR=emar -EMCCFLAGS="-I"$( realpath "${REMILL_DIR}" )"/include -I${ROOT_DIR} -O3" -RUNTIME_DIR=""$( realpath "${ROOT_DIR}" )"/runtime" -UTILS_DIR=""$( realpath "${ROOT_DIR}" )"/utils" -ELFCONV_MACROS="-DELFCONV_BROWSER_ENV=1" -ELFCONV_DEBUG_MACROS= # There are pre-build versions of various libraries for specific # Ubuntu releases. function GetUbuntuOSVersion { # Version name of OS (e.g. xenial, trusty). + # shellcheck disable=SC1091 source /etc/lsb-release case "${DISTRIB_CODENAME}" in @@ -127,6 +120,7 @@ function DownloadVcpkgLibraries # Attempt to detect the OS distribution name. function GetOSVersion { + # shellcheck disable=SC1091 source /etc/os-release case "${ID,,}" in @@ -243,8 +237,8 @@ function Configure -DREMILL_BUILD_SPARC32_RUNTIME=OFF \ -DCMAKE_C_COMPILER=clang \ -DCMAKE_CXX_COMPILER=clang++ \ - ${BUILD_FLAGS} \ - ${LIFT_DEBUG_MACROS} \ + "${BUILD_FLAGS}" \ + "${LIFT_DEBUG_MACROS}" \ -GNinja \ "${ROOT_DIR}" ) || exit $? @@ -288,7 +282,6 @@ function GetLLVMVersion return 1 ;; esac - return 1 } function Help @@ -308,7 +301,7 @@ function Help function main { - if [ -d $BUILD_DIR ]; then + if [ -d "$BUILD_DIR" ]; then echo "Already build done! (at scripts/build.sh)" exit 0 fi @@ -360,7 +353,6 @@ function main # Disable packages --disable-package) - CREATE_PACKAGES=false echo "[+] Disabled building packages" shift # past argument ;; @@ -376,7 +368,7 @@ function main GetOSVersion if [[ $OS_VERSION != ubuntu* ]] ; then echo "[+] Dyninst frontend is supported only on Ubuntu, try at your own peril" - read -p "Continue? (Y/N): " confirm + read -r -p "Continue? (Y/N): " confirm case $confirm in y|Y ) echo "Confirmed";; n|N ) exit 1;; From ad0b980ee5996ffdbe2d04780b3a03239714d446 Mon Sep 17 00:00:00 2001 From: yomaytk Date: Thu, 7 Mar 2024 14:41:11 +0900 Subject: [PATCH 19/21] Change some apt to apt-get not to fail build image in ubuntu 22.04 on MacOS. --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 0d74344..62ad914 100644 --- a/Dockerfile +++ b/Dockerfile @@ -27,8 +27,8 @@ echo "deb http://apt.llvm.org/${DISTRO_NAME}/ llvm-toolchain-${DISTRO_NAME}-${LL echo "deb-src http://apt.llvm.org/${DISTRO_NAME}/ llvm-toolchain-${DISTRO_NAME}-${LLVM_VERSION} main" >> /etc/apt/sources.list # several install -RUN apt update -RUN apt install -qqy --no-install-recommends file libtinfo-dev libzstd-dev python3-pip python3-setuptools python-setuptools python3 build-essential \ +RUN apt-get update +RUN apt-get install -qqy --no-install-recommends file libtinfo-dev libzstd-dev python3-pip python3-setuptools python-setuptools python3 build-essential \ clang-${LLVM_VERSION} lld-${LLVM_VERSION} llvm-${LLVM_VERSION} ninja-build pixz xz-utils make rpm curl unzip tar git zip pkg-config vim \ libc6-dev liblzma-dev zlib1g-dev libselinux1-dev libbsd-dev ccache binutils-dev libelf-dev && \ apt upgrade --yes && apt clean --yes && \ From 9a04910a2d9900e1f0adafa2f84ec0f2323718c7 Mon Sep 17 00:00:00 2001 From: yomaytk Date: Thu, 7 Mar 2024 16:32:21 +0900 Subject: [PATCH 20/21] Fix to pass the elfconv test using wasi-sdk. --- Dockerfile | 11 +++++++++-- bin/elfconv.sh | 2 +- runtime/Memory.cpp | 2 +- runtime/Syscall.cpp | 18 +++++++++--------- scripts/dev.sh | 11 ++++------- tests/elfconv/Test.cpp | 22 +++++++++++----------- 6 files changed, 35 insertions(+), 31 deletions(-) diff --git a/Dockerfile b/Dockerfile index 62ad914..b393f5e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -47,8 +47,15 @@ RUN cd /root && git clone https://github.com/emscripten-core/emsdk.git && cd ems git pull && ./emsdk install latest && ./emsdk activate latest && . ./emsdk_env.sh && echo 'source "/root/emsdk/emsdk_env.sh"' >> /root/.bash_profile # wasi-sdk install -RUN cd /root && export WASI_VERSION=21 && export WASI_VERSION_FULL=${WASI_VERSION}.0 && echo -e 'export WASI_VERSION=21\nexport WASI_VERSION_FULL=${WASI_VERSION}.0\nexport WASI_SDK_PATH=`pwd`/wasi-sdk-${WASI_VERSION_FULL}' >> /root/.bash_profile && \ -wget https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-${WASI_VERSION}/wasi-sdk-${WASI_VERSION_FULL}-linux.tar.gz && tar xvf wasi-sdk-${WASI_VERSION_FULL}-linux.tar.gz +# takes long times to build wasi-sdk in arm64 because wasi-sdk doesn't release arm64 packages. +RUN \ + if [ "$( uname -m )" = "x86_64" ]; then \ + cd /root && echo -e "export WASI_VERSION=21\nexport WASI_VERSION_FULL=${WASI_VERSION}.0\nexport WASI_SDK_PATH=/root/wasi-sdk-${WASI_VERSION_FULL}" >> /root/.bash_profile && \ + wget https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-${WASI_VERSION}/wasi-sdk-${WASI_VERSION_FULL}-linux.tar.gz && tar xvf wasi-sdk-${WASI_VERSION_FULL}-linux.tar.gz && rm wasi-sdk-${WASI_VERSION_FULL}-linux.tar.gz; \ + elif [ "$( uname -m )" = "aarch64" ]; then \ + cd /root && git clone --recursive https://github.com/WebAssembly/wasi-sdk.git; \ + cd wasi-sdk && NINJA_FLAGS=-v make package; \ + fi # WASI Runtimes install RUN curl -sSf https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/utils/install.sh | bash diff --git a/bin/elfconv.sh b/bin/elfconv.sh index 8d40c62..55cc2ff 100755 --- a/bin/elfconv.sh +++ b/bin/elfconv.sh @@ -22,7 +22,7 @@ setting() { WASISDKFLAGS="${OPTFLAGS} --sysroot=${WASI_SDK_PATH}/share/wasi-sysroot -I${ROOT_DIR}/backend/remill/include -I${ROOT_DIR}" if [ "$TARGET" = "wasm-host" ]; then - ELFCONV_MACROS="-DELFC_RUNTIME_HOST_ENV=1" + ELFCONV_MACROS="-DELFC_WASI_ENV=1" fi } diff --git a/runtime/Memory.cpp b/runtime/Memory.cpp index 1802473..3d6a0a4 100644 --- a/runtime/Memory.cpp +++ b/runtime/Memory.cpp @@ -40,7 +40,7 @@ MappedMemory *MappedMemory::VMAStackEntryInit(int argc, char *argv[], _ecv_reg64_t _ecv_a_val; } _ecv_a_un; } _ecv_auxv64[] = { -#if defined(WASI_ENV) +#if defined(ELFC_WASI_ENV) {3 /* AT_PHDR */, phdr}, {4 /* AT_PHENT */, __g_e_phent}, {5 /* AT_PHNUM */, __g_e_phnum}, diff --git a/runtime/Syscall.cpp b/runtime/Syscall.cpp index a6d5655..84e52c9 100644 --- a/runtime/Syscall.cpp +++ b/runtime/Syscall.cpp @@ -140,7 +140,7 @@ void __svc_call(void) { #endif switch (state_gpr.x8.qword) { case AARCH64_SYS_IOCTL: /* ioctl (unsigned int fd, unsigned int cmd, unsigned long arg) */ -#if defined(ELFC_RUNTIME_HOST_ENV) +#if defined(ELFC_WASI_ENV) EMPTY_SYSCALL(AARCH64_SYS_IOCTL) state_gpr.x0.qword = -1; errno = _ECV_EACCESS; @@ -180,7 +180,7 @@ void __svc_call(void) { errno = _ECV_EACCESS; break; case AARCH64_SYS_OPENAT: /* openat (int dfd, const char* filename, int flags, umode_t mode) */ -#if defined(WASI_ENV) +#if defined(ELFC_WASI_ENV) if (-100 == state_gpr.x0.dword) state_gpr.x0.qword = AT_FDCWD; // AT_FDCWD on WASI: -2 (-100 on Linux) state_gpr.x2.dword = O_RDWR; @@ -236,7 +236,7 @@ void __svc_call(void) { break; case AARCH64_SYS_SET_TID_ADDRESS: /* set_tid_address(int *tidptr) */ { -#if defined(WASI_ENV) +#if defined(ELFC_WASI_ENV) pid_t tid = 42; #else pid_t tid = gettid(); @@ -260,7 +260,7 @@ void __svc_call(void) { errno = _ECV_EACCESS; break; case AARCH64_SYS_CLOCK_GETTIME: /* clock_gettime (clockid_t which_clock, struct __kernel_timespace *tp) */ -#if defined(WASI_ENV) +#if defined(ELFC_WASI_ENV) EMPTY_SYSCALL(AARCH64_SYS_CLOCK_GETTIME); state_gpr.x0.qword = -1; errno = _ECV_EACCESS; @@ -275,7 +275,7 @@ void __svc_call(void) { } break; #endif case AARCH64_SYS_TGKILL: /* tgkill (pid_t tgid, pid_t pid, int sig) */ -#if defined(ELFC_RUNTIME_HOST_ENV) +#if defined(ELFC_WASI_ENV) EMPTY_SYSCALL(AARCH64_SYS_TGKILL); state_gpr.x0.qword = -1; errno = _ECV_EACCESS; @@ -291,7 +291,7 @@ void __svc_call(void) { EMPTY_SYSCALL(AARCH64_SYS_RT_SIGPROCMASK); break; case AARCH64_SYS_RT_SIGACTION: /* rt_sigaction (int signum, const struct sigaction *act, struct sigaction *oldact) */ -#if defined(ELFC_RUNTIME_HOST_ENV) +#if defined(ELFC_WASI_ENV) state_gpr.x0.qword = -1; errno = _ECV_EACCESS; EMPTY_SYSCALL(AARCH64_SYS_RT_SIGACTION) @@ -326,7 +326,7 @@ void __svc_call(void) { } #endif break; -#if defined(ELFC_RUNTIME_HOST_ENV) +#if defined(ELFC_WASI_ENV) case AARCH64_SYS_GETPID: /* getpid () */ state_gpr.x0.dword = 42; break; case AARCH64_SYS_GETPPID: /* getppid () */ state_gpr.x0.dword = 42; break; case AARCH64_SYS_GETTUID: /* getuid () */ state_gpr.x0.dword = 42; break; @@ -389,7 +389,7 @@ void __svc_call(void) { break; case AARCH64_SYS_GETRANDOM: /* getrandom (char *buf, size_t count, unsigned int flags) */ { -#if defined(ELFC_RUNTIME_HOST_ENV) +#if defined(ELFC_WASI_ENV) memset(_ecv_translate_ptr(state_gpr.x0.qword), 1, static_cast(state_gpr.x1.qword)); state_gpr.x0.qword = state_gpr.x1.qword; #else @@ -406,7 +406,7 @@ void __svc_call(void) { elfconv_runtime_error("[ERROR] Unsupported statx(flags=0x%08u)\n", flags); struct stat _stat; // execute fstat -#if defined(ELFC_RUNTIME_HOST_ENV) +#if defined(ELFC_WASI_ENV) errno = _ECV_EACCESS; EMPTY_SYSCALL(AARCH64_SYS_STATX); #else diff --git a/scripts/dev.sh b/scripts/dev.sh index 38fcf3b..6052d29 100755 --- a/scripts/dev.sh +++ b/scripts/dev.sh @@ -18,9 +18,6 @@ setting() { CXX=clang++-16 OPTFLAGS="-O3" CLANGFLAGS="${OPTFLAGS} -static -I${ROOT_DIR}/backend/remill/include -I${ROOT_DIR}" - CXXX64=x86_64-linux-gnu-g++-11 - CROSS_COMPILE_FLAGS_X64="-static --target=x86-64-linux-gnu nostdin_linpack.c -fuse-ld=lld -pthread;" - X64CLANGFLAGS="${OPTFLAGS} -static -I${ROOT_DIR}/backend/remill/include" EMCC=emcc EMCCFLAGS="${OPTFLAGS} -I${ROOT_DIR}/backend/remill/include -I${ROOT_DIR}" WASISDK_CXX=${HOME}/wasi-sdk/build/install/opt/wasi-sdk/bin/clang++ @@ -40,7 +37,7 @@ aarch64_test() { # generate LLVM bc echo "[INFO] AArch64 Test Lifting Start." - cd ${BUILD_TESTS_AARCH64_DIR} && \ + cd "${BUILD_TESTS_AARCH64_DIR}" && \ ./aarch64_test_lift \ --arch aarch64 \ --bc_out ./aarch64_test.bc @@ -49,8 +46,8 @@ aarch64_test() { echo "[INFO] Generate aarch64_lift.ll" # generate execute file (lift_test.aarch64) - ${CXX} ${CLANGFLAGS} $ELFCONV_MACROS $ELFCONV_DEBUG_MACROS -o lift_test.aarch64 aarch64_test.ll ${AARCH64_TEST_DIR}/Test.cpp ${AARCH64_TEST_DIR}/TestHelper.cpp \ - ${AARCH64_TEST_DIR}/TestInstructions.cpp ${RUNTIME_DIR}/Memory.cpp ${RUNTIME_DIR}/Syscall.cpp ${RUNTIME_DIR}/VmIntrinsics.cpp ${UTILS_DIR}/Util.cpp ${UTILS_DIR}/elfconv.cpp + ${CXX} "${CLANGFLAGS}" $ELFCONV_MACROS "$ELFCONV_DEBUG_MACROS" -o lift_test.aarch64 aarch64_test.ll "${AARCH64_TEST_DIR}"/Test.cpp "${AARCH64_TEST_DIR}"/TestHelper.cpp \ + "${AARCH64_TEST_DIR}"/TestInstructions.cpp "${RUNTIME_DIR}"/Memory.cpp "${RUNTIME_DIR}"/Syscall.cpp "${RUNTIME_DIR}"/VmIntrinsics.cpp "${UTILS_DIR}"/Util.cpp "${UTILS_DIR}"/elfconv.cpp echo "[INFO] Generate lift_test.aarch64" } @@ -112,7 +109,7 @@ main() { return 0 ;; wasm-host) - ELFCONV_MACROS="-DELFC_RUNTIME_HOST_ENV=1" + ELFCONV_MACROS="-DELFC_WASI_ENV=1" if [ -n "$WASISDK" ]; then WASMCC=$WASISDK_CXX WASMCCFLAGS=$WASISDKFLAGS diff --git a/tests/elfconv/Test.cpp b/tests/elfconv/Test.cpp index c9b73b3..e775e0e 100644 --- a/tests/elfconv/Test.cpp +++ b/tests/elfconv/Test.cpp @@ -10,16 +10,16 @@ using ::testing::TestInfo; using ::testing::UnitTest; #define ECV_PATH(path) "../../../" #path -#define EMCC_HOST_CMD(ident) \ - "emcc -O0 -DELFC_RUNTIME_HOST_ENV=1 -I../../../backend/remill/include -I../../../ -o " #ident \ +#define WASMCC_OPTION_HOST_CMD(ident) \ + "${WASI_SDK_PATH}/bin/clang++ --sysroot=${WASI_SDK_PATH}/share/wasi-sysroot -DELFC_WASI_ENV=1 -fno-exceptions -I../../../backend/remill/include -I../../../ -o " #ident \ ".test.wasm.o " \ "-c ../../../runtime/" #ident ".cpp" -#define EMCC_UTILS_CMD(ident) \ - "emcc -O0 -DELFC_RUNTIME_HOST_ENV=1 -I../../../backend/remill/include -I../../../ -o " #ident \ +#define WASMCC_OPTION_UTILS_CMD(ident) \ + "${WASI_SDK_PATH}/bin/clang++ --sysroot=${WASI_SDK_PATH}/share/wasi-sysroot -DELFC_WASI_ENV=1 -fno-exceptions -I../../../backend/remill/include -I../../../ -o " #ident \ ".test.wasm.o " \ "-c ../../../utils/" #ident ".cpp" -#define EMCC_WASM_O(bc_ident) \ - "emcc -c " bc_ident ".bc" \ +#define WASMCC_OPTION_WASM_O(bc_ident) \ + "${WASI_SDK_PATH}/bin/clang++ --sysroot=${WASI_SDK_PATH}/share/wasi-sysroot -c " bc_ident ".bc" \ " -o " bc_ident ".wasm.o" #define RUNTIME_OBJS \ "Entry.test.wasm.o Memory.test.wasm.o Syscall.test.wasm.o VmIntrinsics.test.wasm.o Util.test.wasm.o elfconv.test.wasm.o" @@ -42,9 +42,9 @@ class TestEnvironment : public ::testing::Environment { // compile `elfconv/runtime` void compile_runtime_emscripten() { - std::string cmds[] = {EMCC_HOST_CMD(Entry), EMCC_HOST_CMD(Memory), - EMCC_HOST_CMD(Syscall), EMCC_HOST_CMD(VmIntrinsics), - EMCC_UTILS_CMD(Util), EMCC_UTILS_CMD(elfconv)}; + std::string cmds[] = {WASMCC_OPTION_HOST_CMD(Entry), WASMCC_OPTION_HOST_CMD(Memory), + WASMCC_OPTION_HOST_CMD(Syscall), WASMCC_OPTION_HOST_CMD(VmIntrinsics), + WASMCC_OPTION_UTILS_CMD(Util), WASMCC_OPTION_UTILS_CMD(elfconv)}; for (auto &cmd : cmds) { FILE *pipe = popen(cmd.c_str(), "r"); if (!pipe) @@ -90,9 +90,9 @@ std::string binary_lifting(const char *elf_path) { void gen_wasm_for_wasi_runtimes() { std::string cmds[] = {// generate lift.wasm.o - "emcc -c lift.bc -o lift.wasm.o", + "${WASI_SDK_PATH}/bin/clang++ -c lift.bc -o lift.wasm.o", // generate wasm - "emcc -o exe.wasm lift.wasm.o " RUNTIME_OBJS}; + "${WASI_SDK_PATH}/bin/clang++ -o exe.wasm lift.wasm.o " RUNTIME_OBJS}; for (auto &cmd : cmds) { FILE *pipe = popen(cmd.c_str(), "r"); if (!pipe) From 85f84842d363929b8ab935377e8cf8de770e3110 Mon Sep 17 00:00:00 2001 From: yomaytk Date: Thu, 7 Mar 2024 16:42:09 +0900 Subject: [PATCH 21/21] Minor fix. --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index b393f5e..0903c38 100644 --- a/Dockerfile +++ b/Dockerfile @@ -50,7 +50,7 @@ git pull && ./emsdk install latest && ./emsdk activate latest && . ./emsdk_env.s # takes long times to build wasi-sdk in arm64 because wasi-sdk doesn't release arm64 packages. RUN \ if [ "$( uname -m )" = "x86_64" ]; then \ - cd /root && echo -e "export WASI_VERSION=21\nexport WASI_VERSION_FULL=${WASI_VERSION}.0\nexport WASI_SDK_PATH=/root/wasi-sdk-${WASI_VERSION_FULL}" >> /root/.bash_profile && \ + cd /root && export WASI_VERSION=21 && export WASI_VERSION_FULL=${WASI_VERSION}.0 && echo -e "export WASI_VERSION=21\nexport WASI_VERSION_FULL=${WASI_VERSION}.0\nexport WASI_SDK_PATH=/root/wasi-sdk-${WASI_VERSION_FULL}" >> /root/.bash_profile && \ wget https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-${WASI_VERSION}/wasi-sdk-${WASI_VERSION_FULL}-linux.tar.gz && tar xvf wasi-sdk-${WASI_VERSION_FULL}-linux.tar.gz && rm wasi-sdk-${WASI_VERSION_FULL}-linux.tar.gz; \ elif [ "$( uname -m )" = "aarch64" ]; then \ cd /root && git clone --recursive https://github.com/WebAssembly/wasi-sdk.git; \