1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-06-11 14:21:10 +02:00

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.
This commit is contained in:
Andy Wingo 2021-01-07 10:58:43 +01:00
parent 8b37b783ea
commit 35cd7fac8b
2 changed files with 28 additions and 0 deletions

View file

@ -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: {

24
tests/jmp0.c Normal file
View file

@ -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);
}