Browse Source

Constrain value range of lj_ir_kptr() to unsigned 32 bit pointers.

Thanks to Peter Cawley.
Mike Pall 9 years ago
parent
commit
ac42037db0
4 changed files with 9 additions and 16 deletions
  1. 1 7
      src/lj_ffrecord.c
  2. 1 1
      src/lj_ir.c
  3. 6 2
      src/lj_obj.h
  4. 1 6
      src/lj_record.c

+ 1 - 7
src/lj_ffrecord.c

@@ -104,7 +104,6 @@ static void recff_stitch(jit_State *J)
   TValue *base = L->base;
   TValue *base = L->base;
   const BCIns *pc = frame_pc(base-1);
   const BCIns *pc = frame_pc(base-1);
   TValue *pframe = frame_prevl(base-1);
   TValue *pframe = frame_prevl(base-1);
-  TRef trcont;
 
 
   lua_assert(!LJ_FR2);  /* TODO_FR2: handle frame shift. */
   lua_assert(!LJ_FR2);  /* TODO_FR2: handle frame shift. */
   /* Move func + args up in Lua stack and insert continuation. */
   /* Move func + args up in Lua stack and insert continuation. */
@@ -118,12 +117,7 @@ static void recff_stitch(jit_State *J)
 
 
   /* Ditto for the IR. */
   /* Ditto for the IR. */
   memmove(&J->base[1], &J->base[-1], sizeof(TRef)*(J->maxslot+1));
   memmove(&J->base[1], &J->base[-1], sizeof(TRef)*(J->maxslot+1));
-#if LJ_64
-  trcont = lj_ir_kptr(J, (void *)((int64_t)cont-(int64_t)lj_vm_asm_begin));
-#else
-  trcont = lj_ir_kptr(J, (void *)cont);
-#endif
-  J->base[0] = trcont | TREF_CONT;
+  J->base[0] = lj_ir_kptr(J, contptr(cont)) | TREF_CONT;
   J->ktracep = lj_ir_k64_reserve(J);
   J->ktracep = lj_ir_k64_reserve(J);
   lua_assert(irt_toitype_(IRT_P64) == LJ_TTRACE);
   lua_assert(irt_toitype_(IRT_P64) == LJ_TTRACE);
   J->base[-1] = emitir(IRT(IR_XLOAD, IRT_P64), lj_ir_kptr(J, &J->ktracep->gcr), 0);
   J->base[-1] = emitir(IRT(IR_XLOAD, IRT_P64), lj_ir_kptr(J, &J->ktracep->gcr), 0);

+ 1 - 1
src/lj_ir.c

@@ -345,7 +345,7 @@ TRef lj_ir_kptr_(jit_State *J, IROp op, void *ptr)
 {
 {
   IRIns *ir, *cir = J->cur.ir;
   IRIns *ir, *cir = J->cur.ir;
   IRRef ref;
   IRRef ref;
-  lua_assert((void *)(intptr_t)i32ptr(ptr) == ptr);
+  lua_assert((void *)(uintptr_t)u32ptr(ptr) == ptr);
   for (ref = J->chain[op]; ref; ref = cir[ref].prev)
   for (ref = J->chain[op]; ref; ref = cir[ref].prev)
     if (mref(cir[ref].ptr, void) == ptr)
     if (mref(cir[ref].ptr, void) == ptr)
       goto found;
       goto found;

+ 6 - 2
src/lj_obj.h

@@ -843,12 +843,16 @@ static LJ_AINLINE void setlightudV(TValue *o, void *p)
 #endif
 #endif
 
 
 #if LJ_FR2
 #if LJ_FR2
-#define setcont(o, f)		((o)->u64 = (uint64_t)(uintptr_t)(void *)(f))
+#define contptr(f)		((void *)(f))
+#define setcont(o, f)		((o)->u64 = (uint64_t)(uintptr_t)contptr(f))
 #elif LJ_64
 #elif LJ_64
+#define contptr(f) \
+  ((void *)(uintptr_t)(uint32_t)((intptr_t)(f) - (intptr_t)lj_vm_asm_begin))
 #define setcont(o, f) \
 #define setcont(o, f) \
   ((o)->u64 = (uint64_t)(void *)(f) - (uint64_t)lj_vm_asm_begin)
   ((o)->u64 = (uint64_t)(void *)(f) - (uint64_t)lj_vm_asm_begin)
 #else
 #else
-#define setcont(o, f)		setlightudV((o), (void *)(f))
+#define contptr(f)		((void *)(f))
+#define setcont(o, f)		setlightudV((o), contptr(f))
 #endif
 #endif
 
 
 #define tvchecklive(L, o) \
 #define tvchecklive(L, o) \

+ 1 - 6
src/lj_record.c

@@ -882,12 +882,7 @@ void lj_record_ret(jit_State *J, BCReg rbase, ptrdiff_t gotresults)
 static BCReg rec_mm_prep(jit_State *J, ASMFunction cont)
 static BCReg rec_mm_prep(jit_State *J, ASMFunction cont)
 {
 {
   BCReg s, top = cont == lj_cont_cat ? J->maxslot : curr_proto(J->L)->framesize;
   BCReg s, top = cont == lj_cont_cat ? J->maxslot : curr_proto(J->L)->framesize;
-#if LJ_64
-  TRef trcont = lj_ir_kptr(J, (void *)((int64_t)cont-(int64_t)lj_vm_asm_begin));
-#else
-  TRef trcont = lj_ir_kptr(J, (void *)cont);
-#endif
-  J->base[top] = trcont | TREF_CONT;
+  J->base[top] = lj_ir_kptr(J, contptr(cont)) | TREF_CONT;
   J->framedepth++;
   J->framedepth++;
   for (s = J->maxslot; s < top; s++)
   for (s = J->maxslot; s < top; s++)
     J->base[s] = 0;  /* Clear frame gap to avoid resurrecting previous refs. */
     J->base[s] = 0;  /* Clear frame gap to avoid resurrecting previous refs. */