From a62fcc0e08cf6cadb3e9cf526d604cc5c999183c Mon Sep 17 00:00:00 2001 From: Matthew Francis-Landau Date: Sat, 27 Aug 2016 12:42:10 -0400 Subject: [PATCH] need to track the delta changes in the stack height when performing the resumes --- make | 12 +++++++----- src/config.h | 1 - src/cpp_allocator.h | 8 +++++++- src/jit_internal.h | 12 ++++++++++++ src/manager.cc | 1 - src/simple_compiler.cc | 5 +++-- src/tracer.cc | 4 +++- 7 files changed, 32 insertions(+), 11 deletions(-) diff --git a/make b/make index b3b780e..77c635b 100755 --- a/make +++ b/make @@ -26,6 +26,7 @@ C_FLAGS = ( ) CXX_FLAGS = ( '-std=c++14 ' + '-fno-exceptions ' ) CXX_FLAGS_UNIT = ( '-I ./deps/catch/ ' @@ -54,7 +55,7 @@ def release(): C_FLAGS = C_FLAGS.replace('-O0', '-O2') C_FLAGS = C_FLAGS.replace('-ggdb', '') C_FLAGS += ' -DNDEBUG -DRED_RELEASE -fdata-sections -ffunction-sections -flto ' - LD_FLAGS += '-flto ' #-Wl,--gc-sections -Wl,--print-gc-sections ' + LD_FLAGS += '-flto -O2 ' #-Wl,--gc-sections -Wl,--print-gc-sections ' clean() build() Run('mkdir -p release') @@ -172,12 +173,13 @@ def deps(): if not os.path.isdir('build'): Shell('mkdir -p build') if not os.path.isfile('deps/udis86/libudis86/.libs/libudis86.so') or not os.path.isfile('deps/udis86/libudis86/itab.h'): - Shell('cd deps/udis86 && ./autogen.sh && PYTHON=`which python2` ./configure && ' - #"sed -i '/^CFLAGS\ =/ s/$/\ \-mgeneral\-regs\-only/' Makefile &&" - 'make V=1', shell=True) + Shell('cd deps/udis86 && ./autogen.sh && PYTHON=`which python2` ./configure && ' + + #("sed -i '/^CFLAGS\ =/ s/$/\ \-flto/' Makefile &&" if RELEASE else '') + + 'make V=1 CFLAGS=' + ('"-Wall -O2 -flto"' if RELEASE else '"-Wall -ggdb"') + , shell=True) if not os.path.isfile('build/asmjit/libasmjit.so'): Shell('mkdir -p build/asmjit') - asm_flags = '' # -DASMJIT_ALLOC=test123 + asm_flags = '\-fno-exceptions\ ' # -DASMJIT_ALLOC=test123 cm_args = '-DASMJIT_DISABLE_COMPILER=1 -DASMJIT_CFLAGS=\'==REPLACE_ME==\' -DCMAKE_CXX_COMPILER=g++ -DCMAKE_C_COMPILER=gcc' if RELEASE: Shell('cd build/asmjit && cmake ../../deps/asmjit {} -DASMJIT_RELEASE=1'.format(cm_args), shell=True) diff --git a/src/config.h b/src/config.h index d61bb48..374e610 100644 --- a/src/config.h +++ b/src/config.h @@ -7,7 +7,6 @@ #endif #ifdef CONF_RELEASE_BUILD -# define # define CONF_BUILD_TOGGLE(debug, release) release #else // configure the system to perform more traces to attempt to debug diff --git a/src/cpp_allocator.h b/src/cpp_allocator.h index 527f2a9..b954c72 100644 --- a/src/cpp_allocator.h +++ b/src/cpp_allocator.h @@ -2,6 +2,7 @@ #define REDMAGIC_CPP_ALLOCATOR_H_ #include +#include extern "C" void *__real_malloc(size_t size); extern "C" void __real_free(void *ptr); @@ -32,8 +33,13 @@ namespace redmagic { pointer allocate(size_type n, const_pointer hint = 0) { void* p = __real_malloc(n * sizeof(T)); - if (!p) + if (!p) { +#ifdef __EXCEPTIONS throw std::bad_alloc(); +#else + std::abort(); +#endif + } return static_cast(p); } diff --git a/src/jit_internal.h b/src/jit_internal.h index 63c8939..9a6e7ed 100644 --- a/src/jit_internal.h +++ b/src/jit_internal.h @@ -136,6 +136,14 @@ namespace redmagic { // > RealMallocMap branches; + struct merge_location_info { + RealMallocSet rips; +#ifdef CONF_MERGE_BACK_ON_RET + bool is_method_return = false; +#endif + + }; + #ifdef CONF_CHECK_MERGE_RIP // std::unordered_map< // mem_loc_t, @@ -205,6 +213,10 @@ namespace redmagic { struct tracer_merge_block_stack_s { mem_loc_t merge_head = 0; // head of linked list for this merge point +#ifdef CONF_MERGE_BACK_ON_RET + bool method_merge = false; +#endif + tracer_merge_block_stack_s() {} }; diff --git a/src/manager.cc b/src/manager.cc index 035a116..a720bdc 100644 --- a/src/manager.cc +++ b/src/manager.cc @@ -995,7 +995,6 @@ namespace { } bool Manager::should_trace_method(void *id) { - if(no_trace_methods.find(id) != no_trace_methods.end()) return false; diff --git a/src/simple_compiler.cc b/src/simple_compiler.cc index efc53b0..79f1b58 100644 --- a/src/simple_compiler.cc +++ b/src/simple_compiler.cc @@ -361,8 +361,9 @@ void SimpleCompiler::ResumeBlockJump(mem_loc_t resume_pc) { // this will allow for it to easily write in a direct jump, as being designed now, we will have to redirect the jump through this indirection // so first conditional jump followed by direct jump // also, this will not work with concurrent threads - lea(x86::r9, x86::ptr(label)); - mov(x86::r8, imm_u(0xfbfbfbfbfbfbfbfb)); + lea(x86::r9, x86::ptr(label)); // patch address + mov(x86::r8, imm_u(0xfbfbfbfbfbfbfbfb)); // merge point + jmp(imm_ptr(&red_asm_restart_trace)); diff --git a/src/tracer.cc b/src/tracer.cc index a6f6adf..da7e3f9 100644 --- a/src/tracer.cc +++ b/src/tracer.cc @@ -753,8 +753,9 @@ void Tracer::TempEnableTrace(void *resume_pc) { set_pc((mem_loc_t)resume_pc); SimpleCompiler compiler(buffer); // the "normal" return address will be set to ris when this returns from the temp disabled region - compiler.TestRegister((mem_loc_t)&red_asm_jump_rsi, RSI, (register_t)resume_pc, &merge_block_stack.back()); + auto wb = compiler.TestRegister((mem_loc_t)&red_asm_jump_rsi, RSI, (register_t)resume_pc, &merge_block_stack.back()); auto written = compiler.finalize(); + wb.replace_stump(0xfafafafafafafafa, written.getRawBuffer()); write_interrupt_block(); } @@ -1766,6 +1767,7 @@ void Tracer::evaluate_instruction() { #ifdef CONF_MERGE_BACK_ON_RET merge_block_stack.push_back(tracer_merge_block_stack_s()); method_stack.back().corresponding_merge_block = merge_block_stack.size(); + merge_block_stack.back().method_merge = true; #endif }