wingo pushed a commit to branch master in repository guile. commit 35cd7fac8bdeb36cf64206b76b1bf0d3c71b499a Author: Andy Wingo <wi...@igalia.com> AuthorDate: Thu Jan 7 10:58:43 2021 +0100
Fix jmp-shortening on x64 when target within instruction. * lightening/x86.c (jit_try_shorten): If the address is within the last instruction, don't shorten. If the intstruction is a jump, we could elide it entirely in some cases, but we don't know if the user captured the PC before calling jit_patch_here. Better to leave this to the user. Thanks to Helmut Eller for the bug report and test case in https://gitlab.com/wingo/lightening/-/issues/17. --- lightening/x86.c | 4 ++++ tests/jmp0.c | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/lightening/x86.c b/lightening/x86.c index 5d75eb0..f8ac4b0 100644 --- a/lightening/x86.c +++ b/lightening/x86.c @@ -362,11 +362,15 @@ jit_try_shorten(jit_state_t *_jit, jit_reloc_t reloc, jit_pointer_t addr) { uint8_t *loc = _jit->start + reloc.offset; uint8_t *start = loc - reloc.inst_start_offset; + uint8_t *end = _jit->pc.uc; jit_imm_t i0 = (jit_imm_t)addr; if (loc == start) return; + if (start < (uint8_t*)addr && (uint8_t*)addr <= end) + return; + switch (reloc.kind) { case JIT_RELOC_ABSOLUTE: { diff --git a/tests/jmp0.c b/tests/jmp0.c new file mode 100644 index 0000000..261a399 --- /dev/null +++ b/tests/jmp0.c @@ -0,0 +1,24 @@ +#include "test.h" + +static void +run_test(jit_state_t *j, uint8_t *arena_base, size_t arena_size) +{ + jit_begin(j, arena_base, arena_size); + size_t align = jit_enter_jit_abi(j, 0, 0, 0); + jit_load_args_1(j, jit_operand_gpr (JIT_OPERAND_ABI_WORD, JIT_R0)); + + jit_reloc_t r = jit_jmp(j); + jit_patch_here(j, r); + jit_leave_jit_abi(j, 0, 0, align); + jit_retr(j, JIT_R0); + + jit_word_t (*f)(jit_word_t) = jit_end(j, NULL); + ASSERT(f(42) == 42); + ASSERT(f(-1) == -1); +} + +int +main (int argc, char *argv[]) +{ + return main_helper(argc, argv, run_test); +}