summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndy Wingo <wingo@igalia.com>2021-01-07 10:58:43 +0100
committerAndy Wingo <wingo@igalia.com>2021-01-07 11:04:17 +0100
commit35cd7fac8bdeb36cf64206b76b1bf0d3c71b499a (patch)
tree7107d23c5d522cee95f4514921c2db05b1064148
parent8b37b783ead3a426ac5538d778e2f83e39bc3077 (diff)
downloadguile-35cd7fac8bdeb36cf64206b76b1bf0d3c71b499a.tar.gz
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.
-rw-r--r--lightening/x86.c4
-rw-r--r--tests/jmp0.c24
2 files changed, 28 insertions, 0 deletions
diff --git a/lightening/x86.c b/lightening/x86.c
index 5d75eb012..f8ac4b0b8 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 000000000..261a399b9
--- /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);
+}