Add some sanity checks for allocator in 64 bit mode.

This commit is contained in:
Mike Pall 2010-01-18 01:32:33 +01:00
parent 32969abe40
commit 4e39597ba6
4 changed files with 8 additions and 1 deletions

View File

@ -313,6 +313,10 @@ LUALIB_API int luaL_loadstring(lua_State *L, const char *s)
#ifdef LUAJIT_USE_SYSMALLOC
#if LJ_64
#error "Must use builtin allocator for 64 bit target"
#endif
static void *mem_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
{
(void)ud;

View File

@ -89,6 +89,7 @@ typedef unsigned __int32 uintptr_t;
#define checku8(x) ((x) == (int32_t)(uint8_t)(x))
#define checki16(x) ((x) == (int32_t)(int16_t)(x))
#define checku16(x) ((x) == (int32_t)(uint16_t)(x))
#define checkptr32(x) ((uintptr_t)(x) == (uint32_t)(uintptr_t)(x))
/* Every half-decent C compiler transforms this into a rotate instruction. */
#define lj_rol(x, n) (((x)<<(n)) | ((x)>>(32-(n))))

View File

@ -764,6 +764,7 @@ void *lj_mem_realloc(lua_State *L, void *p, MSize osz, MSize nsz)
if (p == NULL && nsz > 0)
lj_err_throw(L, LUA_ERRMEM);
lua_assert((nsz == 0) == (p == NULL));
lua_assert(checkptr32(p));
g->gc.total = (g->gc.total - osz) + nsz;
return p;
}
@ -775,6 +776,7 @@ void *lj_mem_newgco(lua_State *L, MSize size)
GCobj *o = (GCobj *)g->allocf(g->allocd, NULL, 0, size);
if (o == NULL)
lj_err_throw(L, LUA_ERRMEM);
lua_assert(checkptr32(o));
g->gc.total += size;
setgcrefr(o->gch.nextgc, g->gc.root);
setgcref(g->gc.root, o);

View File

@ -163,7 +163,7 @@ LUA_API lua_State *lua_newstate(lua_Alloc f, void *ud)
GG_State *GG = cast(GG_State *, f(ud, NULL, 0, sizeof(GG_State)));
lua_State *L = &GG->L;
global_State *g = &GG->g;
if (GG == NULL) return NULL;
if (GG == NULL || !checkptr32(GG)) return NULL;
memset(GG, 0, sizeof(GG_State));
L->gct = ~LJ_TTHREAD;
L->marked = LJ_GC_WHITE0 | LJ_GC_FIXED | LJ_GC_SFIXED; /* Prevent free. */