From f290cc017309494ec6c7bc952b63b2de1e8b1935 Mon Sep 17 00:00:00 2001 From: Cacodemon345 Date: Sat, 1 Mar 2025 22:31:10 +0600 Subject: [PATCH 1/3] x64 NDR: Fix crashes/undefined behaviour with 2+ GB of allocated RAM --- src/codegen_new/codegen_backend_x86-64.c | 2 +- src/codegen_new/codegen_backend_x86-64_defs.h | 2 +- src/codegen_new/codegen_backend_x86-64_ops.c | 64 +++++++++---------- 3 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/codegen_new/codegen_backend_x86-64.c b/src/codegen_new/codegen_backend_x86-64.c index 3cbca28f8..f4a5d024a 100644 --- a/src/codegen_new/codegen_backend_x86-64.c +++ b/src/codegen_new/codegen_backend_x86-64.c @@ -360,7 +360,7 @@ codegen_backend_prologue(codeblock_t *block) host_x86_MOV32_BASE_OFFSET_REG(block, REG_RSP, IREG_TOP_diff_stack_offset, REG_EAX); } if (block->flags & CODEBLOCK_NO_IMMEDIATES) - host_x86_MOV64_REG_IMM(block, REG_R12, (uintptr_t) ram); + host_x86_MOV64_REG_IMM(block, REG_R12, ((uintptr_t) ram) + 2147483648ULL); } void diff --git a/src/codegen_new/codegen_backend_x86-64_defs.h b/src/codegen_new/codegen_backend_x86-64_defs.h index 12f05f01c..7c58fec31 100644 --- a/src/codegen_new/codegen_backend_x86-64_defs.h +++ b/src/codegen_new/codegen_backend_x86-64_defs.h @@ -1,5 +1,5 @@ /*RBP = cpu_state + 128 - R12 = ram (if block->flags & CODEBLOCK_NO_IMMEDIATES)*/ + R12 = ram + 2147483648 (if block->flags & CODEBLOCK_NO_IMMEDIATES)*/ #define REG_AX 0 #define REG_CX 1 #define REG_DX 2 diff --git a/src/codegen_new/codegen_backend_x86-64_ops.c b/src/codegen_new/codegen_backend_x86-64_ops.c index 2f65b46e3..e53c58083 100644 --- a/src/codegen_new/codegen_backend_x86-64_ops.c +++ b/src/codegen_new/codegen_backend_x86-64_ops.c @@ -505,7 +505,7 @@ host_x86_MOV8_ABS_IMM(codeblock_t *block, void *p, uint32_t imm_data) { int64_t offset = (uintptr_t) p - (((uintptr_t) &cpu_state) + 128); - if (offset >= -128 && offset < 127) { + if (offset >= -128 && offset <= 127) { codegen_alloc_bytes(block, 4); codegen_addbyte3(block, 0xc6, 0x45, offset); /*MOVB offset[RBP], imm_data*/ codegen_addbyte(block, imm_data); @@ -528,7 +528,7 @@ host_x86_MOV16_ABS_IMM(codeblock_t *block, void *p, uint16_t imm_data) { int64_t offset = (uintptr_t) p - (((uintptr_t) &cpu_state) + 128); - if (offset >= -128 && offset < 127) { + if (offset >= -128 && offset <= 127) { codegen_alloc_bytes(block, 6); codegen_addbyte4(block, 0x66, 0xc7, 0x45, offset); /*MOV offset[RBP], imm_data*/ codegen_addword(block, imm_data); @@ -551,7 +551,7 @@ host_x86_MOV32_ABS_IMM(codeblock_t *block, void *p, uint32_t imm_data) { int64_t offset = (uintptr_t) p - (((uintptr_t) &cpu_state) + 128); - if (offset >= -128 && offset < 127) { + if (offset >= -128 && offset <= 127) { codegen_alloc_bytes(block, 7); codegen_addbyte3(block, 0xc7, 0x45, offset); /*MOV offset[RBP], imm_data*/ codegen_addlong(block, imm_data); @@ -578,7 +578,7 @@ host_x86_MOV8_ABS_REG(codeblock_t *block, void *p, int src_reg) if (src_reg & 8) fatal("host_x86_MOV8_ABS_REG - bad reg\n"); - if (offset >= -128 && offset < 127) { + if (offset >= -128 && offset <= 127) { codegen_alloc_bytes(block, 3); codegen_addbyte3(block, 0x88, 0x45 | ((src_reg & 7) << 3), offset); /*MOVB offset[RBP], src_reg*/ } else if (offset < (1ULL << 32)) { @@ -602,7 +602,7 @@ host_x86_MOV16_ABS_REG(codeblock_t *block, void *p, int src_reg) if (src_reg & 8) fatal("host_x86_MOV16_ABS_REG - bad reg\n"); - if (offset >= -128 && offset < 127) { + if (offset >= -128 && offset <= 127) { codegen_alloc_bytes(block, 4); codegen_addbyte4(block, 0x66, 0x89, 0x45 | ((src_reg & 7) << 3), offset); /*MOV offset[RBP], src_reg*/ } else if (offset < (1ULL << 32)) { @@ -622,7 +622,7 @@ host_x86_MOV32_ABS_REG(codeblock_t *block, void *p, int src_reg) if (src_reg & 8) fatal("host_x86_MOV32_ABS_REG - bad reg\n"); - if (offset >= -128 && offset < 127) { + if (offset >= -128 && offset <= 127) { codegen_alloc_bytes(block, 3); codegen_addbyte3(block, 0x89, 0x45 | ((src_reg & 7) << 3), offset); /*MOV offset[RBP], src_reg*/ } else if (offset < (1ULL << 32)) { @@ -646,7 +646,7 @@ host_x86_MOV64_ABS_REG(codeblock_t *block, void *p, int src_reg) if (src_reg & 8) fatal("host_x86_MOV64_ABS_REG - bad reg\n"); - if (offset >= -128 && offset < 127) { + if (offset >= -128 && offset <= 127) { codegen_alloc_bytes(block, 4); codegen_addbyte4(block, 0x48, 0x89, 0x45 | ((src_reg & 7) << 3), offset); /*MOV offset[RBP], src_reg*/ } else if (offset < (1ULL << 32)) { @@ -706,19 +706,19 @@ void host_x86_MOV8_REG_ABS(codeblock_t *block, int dst_reg, void *p) { int64_t offset = (uintptr_t) p - (((uintptr_t) &cpu_state) + 128); - int64_t ram_offset = (uintptr_t) p - (uintptr_t) ram; + int64_t ram_offset = (uintptr_t) p - (((uintptr_t) ram) + 2147483648ULL); if (dst_reg & 8) fatal("host_x86_MOV8_REG_ABS reg & 8\n"); - if (offset >= -128 && offset < 127) { + if (offset >= -128 && offset <= 127) { codegen_alloc_bytes(block, 3); codegen_addbyte3(block, 0x8a, 0x45 | ((dst_reg & 7) << 3), offset); /*MOV dst_reg, offset[RBP]*/ } else if (offset < (1ULL << 32)) { codegen_alloc_bytes(block, 6); codegen_addbyte2(block, 0x8a, 0x85 | ((dst_reg & 7) << 3)); /*MOV dst_reg, offset[RBP]*/ codegen_addlong(block, offset); - } else if ((ram_offset < (1ULL << 32)) && (block->flags & CODEBLOCK_NO_IMMEDIATES)) { + } else if ((ram_offset >= -2147483648LL) && (ram_offset <= 2147483647LL) && (block->flags & CODEBLOCK_NO_IMMEDIATES)) { codegen_alloc_bytes(block, 8); codegen_addbyte4(block, 0x41, 0x8a, 0x84 | ((dst_reg & 7) << 3), 0x24); /*MOV dst_reg, ram_offset[R12]*/ codegen_addlong(block, ram_offset); @@ -730,19 +730,19 @@ void host_x86_MOV16_REG_ABS(codeblock_t *block, int dst_reg, void *p) { int64_t offset = (uintptr_t) p - (((uintptr_t) &cpu_state) + 128); - int64_t ram_offset = (uintptr_t) p - (uintptr_t) ram; + int64_t ram_offset = (uintptr_t) p - (((uintptr_t) ram) + 2147483648ULL); if (dst_reg & 8) fatal("host_x86_MOV16_REG_ABS reg & 8\n"); - if (offset >= -128 && offset < 127) { + if (offset >= -128 && offset <= 127) { codegen_alloc_bytes(block, 4); codegen_addbyte4(block, 0x66, 0x8b, 0x45 | ((dst_reg & 7) << 3), offset); /*MOV dst_reg, offset[RBP]*/ } else if (offset < (1ULL << 32)) { codegen_alloc_bytes(block, 7); codegen_addbyte3(block, 0x66, 0x8b, 0x85 | ((dst_reg & 7) << 3)); /*MOV dst_reg, offset[RBP]*/ codegen_addlong(block, offset); - } else if ((ram_offset < (1ULL << 32)) && (block->flags & CODEBLOCK_NO_IMMEDIATES)) { + } else if ((ram_offset >= -2147483648LL) && (ram_offset <= 2147483647LL) && (block->flags & CODEBLOCK_NO_IMMEDIATES)) { codegen_alloc_bytes(block, 9); codegen_addbyte4(block, 0x66, 0x41, 0x8b, 0x84 | ((dst_reg & 7) << 3)); /*MOV dst_reg, ram_offset[R12]*/ codegen_addbyte(block, 0x24); @@ -760,19 +760,19 @@ void host_x86_MOV32_REG_ABS(codeblock_t *block, int dst_reg, void *p) { int64_t offset = (uintptr_t) p - (((uintptr_t) &cpu_state) + 128); - int64_t ram_offset = (uintptr_t) p - (uintptr_t) ram; + int64_t ram_offset = (uintptr_t) p - (((uintptr_t) ram) + 2147483648ULL); if (dst_reg & 8) fatal("host_x86_MOV32_REG_ABS reg & 8\n"); - if (offset >= -128 && offset < 127) { + if (offset >= -128 && offset <= 127) { codegen_alloc_bytes(block, 3); codegen_addbyte3(block, 0x8b, 0x45 | ((dst_reg & 7) << 3), offset); /*MOV dst_reg, offset[RBP]*/ } else if (offset < (1ULL << 32)) { codegen_alloc_bytes(block, 6); codegen_addbyte2(block, 0x8b, 0x85 | ((dst_reg & 7) << 3)); /*MOV dst_reg, offset[RBP]*/ codegen_addlong(block, offset); - } else if ((ram_offset < (1ULL << 32)) && (block->flags & CODEBLOCK_NO_IMMEDIATES)) { + } else if ((ram_offset >= -2147483648LL) && (ram_offset <= 2147483647LL) && (block->flags & CODEBLOCK_NO_IMMEDIATES)) { codegen_alloc_bytes(block, 8); codegen_addbyte4(block, 0x41, 0x8b, 0x84 | ((dst_reg & 7) << 3), 0x24); /*MOV dst_reg, ram_offset[R12]*/ codegen_addlong(block, ram_offset); @@ -792,7 +792,7 @@ host_x86_MOV64_REG_ABS(codeblock_t *block, int dst_reg, void *p) if (dst_reg & 8) fatal("host_x86_MOV64_REG_ABS reg & 8\n"); - if (offset >= -128 && offset < 127) { + if (offset >= -128 && offset <= 127) { codegen_alloc_bytes(block, 4); codegen_addbyte4(block, 0x48, 0x8b, 0x45 | ((dst_reg & 7) << 3), offset); /*MOV dst_reg, offset[RBP]*/ } else if (offset < (1ULL << 32)) { @@ -845,7 +845,7 @@ host_x86_MOV16_REG_BASE_OFFSET(codeblock_t *block, int dst_reg, int base_reg, in if ((dst_reg & 8) || (base_reg & 8)) fatal("host_x86_MOV16_REG_BASE_OFFSET reg & 8\n"); - if (offset >= -128 && offset < 127) { + if (offset >= -128 && offset <= 127) { if (base_reg == REG_RSP) { codegen_alloc_bytes(block, 5); codegen_addbyte(block, 0x66); @@ -863,7 +863,7 @@ host_x86_MOV32_REG_BASE_OFFSET(codeblock_t *block, int dst_reg, int base_reg, in if ((dst_reg & 8) || (base_reg & 8)) fatal("host_x86_MOV32_REG_BASE_OFFSET reg & 8\n"); - if (offset >= -128 && offset < 127) { + if (offset >= -128 && offset <= 127) { if (base_reg == REG_RSP) { codegen_alloc_bytes(block, 4); codegen_addbyte4(block, 0x8b, 0x40 | base_reg | (dst_reg << 3), 0x24, offset); @@ -880,7 +880,7 @@ host_x86_MOV64_REG_BASE_OFFSET(codeblock_t *block, int dst_reg, int base_reg, in if ((dst_reg & 8) || (base_reg & 8)) fatal("host_x86_MOV64_REG_BASE_OFFSET reg & 8\n"); - if (offset >= -128 && offset < 127) { + if (offset >= -128 && offset <= 127) { if (base_reg == REG_RSP) { codegen_alloc_bytes(block, 5); codegen_addbyte(block, 0x48); @@ -899,7 +899,7 @@ host_x86_MOV32_BASE_OFFSET_REG(codeblock_t *block, int base_reg, int offset, int if ((src_reg & 8) || (base_reg & 8)) fatal("host_x86_MOV32_BASE_OFFSET_REG reg & 8\n"); - if (offset >= -128 && offset < 127) { + if (offset >= -128 && offset <= 127) { if (base_reg == REG_RSP) { codegen_alloc_bytes(block, 4); codegen_addbyte4(block, 0x89, 0x40 | base_reg | (src_reg << 3), 0x24, offset); @@ -916,7 +916,7 @@ host_x86_MOV64_BASE_OFFSET_REG(codeblock_t *block, int base_reg, int offset, int if ((src_reg & 8) || (base_reg & 8)) fatal("host_x86_MOV64_BASE_OFFSET_REG reg & 8\n"); - if (offset >= -128 && offset < 127) { + if (offset >= -128 && offset <= 127) { if (base_reg == REG_RSP) { codegen_alloc_bytes(block, 5); codegen_addbyte(block, 0x48); @@ -935,7 +935,7 @@ host_x86_MOV32_BASE_OFFSET_IMM(codeblock_t *block, int base_reg, int offset, uin if (base_reg & 8) fatal("host_x86_MOV32_BASE_OFFSET_IMM reg & 8\n"); - if (offset >= -128 && offset < 127) { + if (offset >= -128 && offset <= 127) { if (base_reg == REG_RSP) { codegen_alloc_bytes(block, 8); codegen_addbyte4(block, 0xc7, 0x40 | base_reg, 0x24, offset); @@ -1107,16 +1107,16 @@ void host_x86_MOVZX_REG_ABS_16_8(codeblock_t *block, int dst_reg, void *p) { int64_t offset = (uintptr_t) p - (((uintptr_t) &cpu_state) + 128); - int64_t ram_offset = (uintptr_t) p - (uintptr_t) ram; + int64_t ram_offset = (uintptr_t) p - (((uintptr_t) ram) + 2147483648ULL); if (dst_reg & 8) fatal("host_x86_MOVZX_REG_ABS_16_8 - bad reg\n"); - if (offset >= -128 && offset < 127) { + if (offset >= -128 && offset <= 127) { codegen_alloc_bytes(block, 5); codegen_addbyte(block, 0x66); codegen_addbyte4(block, 0x0f, 0xb6, 0x45 | ((dst_reg & 7) << 3), offset); /*MOVZX dst_reg, offset[RBP]*/ - } else if ((ram_offset < (1ULL << 32)) && (block->flags & CODEBLOCK_NO_IMMEDIATES)) { + } else if ((ram_offset >= -2147483648LL) && (ram_offset <= 2147483647LL) && (block->flags & CODEBLOCK_NO_IMMEDIATES)) { codegen_alloc_bytes(block, 10); codegen_addbyte2(block, 0x66, 0x41); codegen_addbyte4(block, 0x0f, 0xb6, 0x84 | ((dst_reg & 7) << 3), 0x24); /*MOVZX dst_reg, ram_offset[R12]*/ @@ -1134,14 +1134,14 @@ void host_x86_MOVZX_REG_ABS_32_8(codeblock_t *block, int dst_reg, void *p) { int64_t offset = (uintptr_t) p - (((uintptr_t) &cpu_state) + 128); - int64_t ram_offset = (uintptr_t) p - (uintptr_t) ram; + int64_t ram_offset = (uintptr_t) p - (((uintptr_t) ram) + 2147483648ULL); #if 0 if (dst_reg & 8) fatal("host_x86_MOVZX_REG_ABS_32_8 - bad reg\n"); #endif - if (offset >= -128 && offset < 127) { + if (offset >= -128 && offset <= 127) { if (dst_reg & 8) { codegen_alloc_bytes(block, 5); codegen_addbyte(block, 0x44); @@ -1150,7 +1150,7 @@ host_x86_MOVZX_REG_ABS_32_8(codeblock_t *block, int dst_reg, void *p) codegen_alloc_bytes(block, 4); codegen_addbyte4(block, 0x0f, 0xb6, 0x45 | ((dst_reg & 7) << 3), offset); /*MOVZX dst_reg, offset[RBP]*/ } - } else if ((ram_offset < (1ULL << 32)) && (block->flags & CODEBLOCK_NO_IMMEDIATES)) { + } else if ((ram_offset >= -2147483648LL) && (ram_offset <= 2147483647LL) && (block->flags & CODEBLOCK_NO_IMMEDIATES)) { if (dst_reg & 8) fatal("host_x86_MOVZX_REG_ABS_32_8 - bad reg\n"); @@ -1173,15 +1173,15 @@ void host_x86_MOVZX_REG_ABS_32_16(codeblock_t *block, int dst_reg, void *p) { int64_t offset = (uintptr_t) p - (((uintptr_t) &cpu_state) + 128); - int64_t ram_offset = (uintptr_t) p - (uintptr_t) ram; + int64_t ram_offset = (uintptr_t) p - (((uintptr_t) ram) + 2147483648ULL); if (dst_reg & 8) fatal("host_x86_MOVZX_REG_ABS_32_16 - bad reg\n"); - if (offset >= -128 && offset < 127) { + if (offset >= -128 && offset <= 127) { codegen_alloc_bytes(block, 4); codegen_addbyte4(block, 0x0f, 0xb7, 0x45 | ((dst_reg & 7) << 3), offset); /*MOVZX dst_reg, offset[RBP]*/ - } else if ((ram_offset < (1ULL << 32)) && (block->flags & CODEBLOCK_NO_IMMEDIATES)) { + } else if ((ram_offset >= -2147483648LL) && (ram_offset <= 2147483647LL) && (block->flags & CODEBLOCK_NO_IMMEDIATES)) { codegen_alloc_bytes(block, 9); codegen_addbyte(block, 0x41); codegen_addbyte4(block, 0x0f, 0xb7, 0x84 | ((dst_reg & 7) << 3), 0x24); /*MOVZX dst_reg, ram_offset[R12]*/ From 6b55b2383a3fd6096f0e39a4c749272ecddc5650 Mon Sep 17 00:00:00 2001 From: Cacodemon345 Date: Sun, 2 Mar 2025 23:14:10 +0600 Subject: [PATCH 2/3] Misc NDR changes Force NDR on ARMv7/ARM64 Make GCC/Clang not use anything in the red zone Compile with `-fno-omit-frame-pointer` --- CMakeLists.txt | 7 +++++- src/CMakeLists.txt | 3 +++ src/codegen_new/codegen_backend_x86-64.c | 18 +++++++++++++++ src/codegen_new/codegen_backend_x86-64_ops.c | 24 ++++++++++---------- 4 files changed, 39 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index aa687222a..5c33bb3e9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -132,13 +132,18 @@ option(RTMIDI "RtMidi" option(FLUIDSYNTH "FluidSynth" ON) option(MUNT "MUNT" ON) option(VNC "VNC renderer" OFF) -option(NEW_DYNAREC "Use the PCem v15 (\"new\") dynamic recompiler" OFF) option(MINITRACE "Enable Chrome tracing using the modified minitrace library" OFF) option(GDBSTUB "Enable GDB stub server for debugging" OFF) option(DEV_BRANCH "Development branch" OFF) option(DISCORD "Discord Rich Presence support" ON) option(DEBUGREGS486 "Enable debug register opeartion on 486+ CPUs" OFF) +if((ARCH STREQUAL "arm64") OR (ARCH STREQUAL "arm")) + set(NEW_DYNAREC ON) +else() + option(NEW_DYNAREC "Use the PCem v15 (\"new\") dynamic recompiler" OFF) +endif() + if(WIN32) set(QT ON) option(CPPTHREADS "C++11 threads" OFF) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7d6daff9c..ee17a7a21 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -223,6 +223,9 @@ elseif(APPLE AND NOT QT) COMPONENT Runtime) endif() +if(NEW_DYNAREC AND (ARCH STREQUAL "x86_64") AND (NOT WIN32)) + add_compile_options(-mno-red-zone -fno-omit-frame-pointer) +endif() # Install the PDB file on Windows builds if(MSVC) diff --git a/src/codegen_new/codegen_backend_x86-64.c b/src/codegen_new/codegen_backend_x86-64.c index f4a5d024a..6242ea40b 100644 --- a/src/codegen_new/codegen_backend_x86-64.c +++ b/src/codegen_new/codegen_backend_x86-64.c @@ -315,13 +315,19 @@ codegen_backend_init(void) # endif host_x86_CALL(block, (void *) x86gpf); codegen_exit_rout = &codeblock[block_current].data[block_pos]; +#ifdef _WIN64 host_x86_ADD64_REG_IMM(block, REG_RSP, 0x38); +#else + host_x86_ADD64_REG_IMM(block, REG_RSP, 0x48); +#endif host_x86_POP(block, REG_R15); host_x86_POP(block, REG_R14); host_x86_POP(block, REG_R13); host_x86_POP(block, REG_R12); +#ifdef _WIN64 host_x86_POP(block, REG_RDI); host_x86_POP(block, REG_RSI); +#endif host_x86_POP(block, REG_RBP); host_x86_POP(block, REG_RDX); host_x86_RET(block); @@ -346,13 +352,19 @@ codegen_backend_prologue(codeblock_t *block) block_pos = BLOCK_START; /*Entry code*/ host_x86_PUSH(block, REG_RBX); host_x86_PUSH(block, REG_RBP); +#ifdef _WIN64 host_x86_PUSH(block, REG_RSI); host_x86_PUSH(block, REG_RDI); +#endif host_x86_PUSH(block, REG_R12); host_x86_PUSH(block, REG_R13); host_x86_PUSH(block, REG_R14); host_x86_PUSH(block, REG_R15); +#ifdef _WIN64 host_x86_SUB64_REG_IMM(block, REG_RSP, 0x38); +#else + host_x86_SUB64_REG_IMM(block, REG_RSP, 0x48); +#endif host_x86_MOV64_REG_IMM(block, REG_RBP, ((uintptr_t) &cpu_state) + 128); if (block->flags & CODEBLOCK_HAS_FPU) { host_x86_MOV32_REG_ABS(block, REG_EAX, &cpu_state.TOP); @@ -366,13 +378,19 @@ codegen_backend_prologue(codeblock_t *block) void codegen_backend_epilogue(codeblock_t *block) { +#ifdef _WIN64 host_x86_ADD64_REG_IMM(block, REG_RSP, 0x38); +#else + host_x86_ADD64_REG_IMM(block, REG_RSP, 0x48); +#endif host_x86_POP(block, REG_R15); host_x86_POP(block, REG_R14); host_x86_POP(block, REG_R13); host_x86_POP(block, REG_R12); +#ifdef _WIN64 host_x86_POP(block, REG_RDI); host_x86_POP(block, REG_RSI); +#endif host_x86_POP(block, REG_RBP); host_x86_POP(block, REG_RDX); host_x86_RET(block); diff --git a/src/codegen_new/codegen_backend_x86-64_ops.c b/src/codegen_new/codegen_backend_x86-64_ops.c index e53c58083..39173505b 100644 --- a/src/codegen_new/codegen_backend_x86-64_ops.c +++ b/src/codegen_new/codegen_backend_x86-64_ops.c @@ -848,11 +848,11 @@ host_x86_MOV16_REG_BASE_OFFSET(codeblock_t *block, int dst_reg, int base_reg, in if (offset >= -128 && offset <= 127) { if (base_reg == REG_RSP) { codegen_alloc_bytes(block, 5); - codegen_addbyte(block, 0x66); + codegen_addbyte(block, 0x66); /* MOV dst_reg, [RSP + offset] */ codegen_addbyte4(block, 0x8b, 0x40 | base_reg | (dst_reg << 3), 0x24, offset); } else { codegen_alloc_bytes(block, 4); - codegen_addbyte4(block, 0x66, 0x8b, 0x40 | base_reg | (dst_reg << 3), offset); + codegen_addbyte4(block, 0x66, 0x8b, 0x40 | base_reg | (dst_reg << 3), offset); /* MOV dst_reg, [base_reg + offset] */ } } else fatal("MOV16_REG_BASE_OFFSET - offset %i\n", offset); @@ -866,10 +866,10 @@ host_x86_MOV32_REG_BASE_OFFSET(codeblock_t *block, int dst_reg, int base_reg, in if (offset >= -128 && offset <= 127) { if (base_reg == REG_RSP) { codegen_alloc_bytes(block, 4); - codegen_addbyte4(block, 0x8b, 0x40 | base_reg | (dst_reg << 3), 0x24, offset); + codegen_addbyte4(block, 0x8b, 0x40 | base_reg | (dst_reg << 3), 0x24, offset); /* MOV dst_reg, [RSP + offset] */ } else { codegen_alloc_bytes(block, 3); - codegen_addbyte3(block, 0x8b, 0x40 | base_reg | (dst_reg << 3), offset); + codegen_addbyte3(block, 0x8b, 0x40 | base_reg | (dst_reg << 3), offset); /* MOV dst_reg, [base_reg + offset] */ } } else fatal("MOV32_REG_BASE_OFFSET - offset %i\n", offset); @@ -883,11 +883,11 @@ host_x86_MOV64_REG_BASE_OFFSET(codeblock_t *block, int dst_reg, int base_reg, in if (offset >= -128 && offset <= 127) { if (base_reg == REG_RSP) { codegen_alloc_bytes(block, 5); - codegen_addbyte(block, 0x48); + codegen_addbyte(block, 0x48); /* MOV dst_reg, [RSP + offset] */ codegen_addbyte4(block, 0x8b, 0x40 | base_reg | (dst_reg << 3), 0x24, offset); } else { codegen_alloc_bytes(block, 4); - codegen_addbyte4(block, 0x48, 0x8b, 0x40 | base_reg | (dst_reg << 3), offset); + codegen_addbyte4(block, 0x48, 0x8b, 0x40 | base_reg | (dst_reg << 3), offset); /* MOV dst_reg, [base_reg + offset] */ } } else fatal("MOV32_REG_BASE_OFFSET - offset %i\n", offset); @@ -901,11 +901,11 @@ host_x86_MOV32_BASE_OFFSET_REG(codeblock_t *block, int base_reg, int offset, int if (offset >= -128 && offset <= 127) { if (base_reg == REG_RSP) { - codegen_alloc_bytes(block, 4); + codegen_alloc_bytes(block, 4); /* MOV [RSP + offset], src_reg*/ codegen_addbyte4(block, 0x89, 0x40 | base_reg | (src_reg << 3), 0x24, offset); } else { codegen_alloc_bytes(block, 3); - codegen_addbyte3(block, 0x89, 0x40 | base_reg | (src_reg << 3), offset); + codegen_addbyte3(block, 0x89, 0x40 | base_reg | (src_reg << 3), offset); /* MOV [base_reg + offset], src_reg*/ } } else fatal("MOV32_BASE_OFFSET_REG - offset %i\n", offset); @@ -919,11 +919,11 @@ host_x86_MOV64_BASE_OFFSET_REG(codeblock_t *block, int base_reg, int offset, int if (offset >= -128 && offset <= 127) { if (base_reg == REG_RSP) { codegen_alloc_bytes(block, 5); - codegen_addbyte(block, 0x48); + codegen_addbyte(block, 0x48); /* MOV [RSP + offset], src_reg*/ codegen_addbyte4(block, 0x89, 0x40 | base_reg | (src_reg << 3), 0x24, offset); } else { codegen_alloc_bytes(block, 4); - codegen_addbyte4(block, 0x48, 0x89, 0x40 | base_reg | (src_reg << 3), offset); + codegen_addbyte4(block, 0x48, 0x89, 0x40 | base_reg | (src_reg << 3), offset); /* MOV [base_reg + offset], src_reg*/ } } else fatal("MOV64_BASE_OFFSET_REG - offset %i\n", offset); @@ -938,11 +938,11 @@ host_x86_MOV32_BASE_OFFSET_IMM(codeblock_t *block, int base_reg, int offset, uin if (offset >= -128 && offset <= 127) { if (base_reg == REG_RSP) { codegen_alloc_bytes(block, 8); - codegen_addbyte4(block, 0xc7, 0x40 | base_reg, 0x24, offset); + codegen_addbyte4(block, 0xc7, 0x40 | base_reg, 0x24, offset); /* MOV [RSP + offset], imm_data */ codegen_addlong(block, imm_data); } else { codegen_alloc_bytes(block, 7); - codegen_addbyte3(block, 0xc7, 0x40 | base_reg, offset); + codegen_addbyte3(block, 0xc7, 0x40 | base_reg, offset); /* MOV [base_reg + offset], src_reg*/ codegen_addlong(block, imm_data); } } else From 8d6087a016ba1eb034c9c6d6d99533d124cb418e Mon Sep 17 00:00:00 2001 From: Cacodemon345 Date: Mon, 3 Mar 2025 01:06:03 +0600 Subject: [PATCH 3/3] Revert some unnecessary changes for NDR --- src/CMakeLists.txt | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ee17a7a21..fb3bf9f50 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -223,10 +223,6 @@ elseif(APPLE AND NOT QT) COMPONENT Runtime) endif() -if(NEW_DYNAREC AND (ARCH STREQUAL "x86_64") AND (NOT WIN32)) - add_compile_options(-mno-red-zone -fno-omit-frame-pointer) -endif() - # Install the PDB file on Windows builds if(MSVC) # CMake fully supports PDB files on MSVC-compatible compilers