lightning
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Lightning] Broken jump patching on x86_64


From: Pippijn van Steenhoven
Subject: [Lightning] Broken jump patching on x86_64
Date: Mon, 9 Jun 2008 00:54:05 +0200
User-agent: Mutt/1.5.18 (2008-05-17)

Hello Lightning List,

I am experiencing very strange experience with GNU Lightning on x86_64.
Consider the following simple code:

  #include <lightning.h>

  #define CRASH 1

  struct foo
  {
    jit_state current;
  };
  
  int
  main()
  {
    typedef int (*pifii)(int, int);
  
    jit_insn codeBuffer[1024];
    struct jit_state _jit;
    struct foo as;
  
  #if CRASH
  #define _jit as.current
  #endif
  
    pifii myFunction = (pifii) (jit_set_ip (codeBuffer).iptr);
    int   ofs;
  
    // start off like in add.c
    jit_leaf (2);
    ofs = jit_arg_i ();
    jit_getarg_i (JIT_R0, ofs);
    ofs = jit_arg_i ();
    jit_getarg_i (JIT_R1, ofs);
  
    // if R0 == R1, goto patch
    jit_insn *ref = jit_beqr_i (jit_forward (), JIT_R0, JIT_R1);
    jit_addr_i (JIT_RET, JIT_R0, JIT_R1); // if R0 != R1, RET = R0 + R1
    jit_ret ();
  
    jit_patch (ref); // if R0 == R1, RET = R0 * 2
    jit_muli_i (JIT_RET, JIT_R0, 2);
    jit_ret ();
  
    jit_flush_code (codeBuffer, jit_get_ip ().ptr);
  
    printf ("%d + %d = %d\n", 5, 5, myFunction (5, 5));
  
    return 0;
  }

This code generates broken machine code on x86_64. It's pretty much
random what it produces. The resulting code might contain illegal
instructions or just jump wildly, causing segfaults or cause floating
point exceptions.. anyways completely undefined. What's funny is that if
CRASH is defined to 0, it does not crash. It also does not crash if
"struct foo as" is static. Can anyone confirm these issues? I don't
really know what information I can provide that might be helpful. I could
provide a disassembly of the generated code, but it is so
non-deterministic that I suspect it to be rather useless.

Regards,
  
-- 
Pippijn van Steenhoven

Attachment: signature.asc
Description: Digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]