From a4762b6ffe74f5878882ef238d37bfa92d90e418 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 15 Nov 2024 12:04:53 -0300 Subject: [PATCH] 'objsize' returns 'l_mem' Sums of size_t may not fit in a size_t. --- lfunc.c | 10 +++++----- lfunc.h | 2 +- lgc.c | 39 ++++++++++++++++++++++++++------------- lstate.c | 5 +++-- lstate.h | 2 +- ltable.c | 5 +++-- ltable.h | 2 +- 7 files changed, 40 insertions(+), 25 deletions(-) diff --git a/lfunc.c b/lfunc.c index 2b041281..0ea05e00 100644 --- a/lfunc.c +++ b/lfunc.c @@ -264,16 +264,16 @@ Proto *luaF_newproto (lua_State *L) { } -size_t luaF_protosize (Proto *p) { - size_t sz = sizeof(Proto) +lu_mem luaF_protosize (Proto *p) { + lu_mem sz = cast(lu_mem, sizeof(Proto)) + cast_uint(p->sizep) * sizeof(Proto*) + cast_uint(p->sizek) * sizeof(TValue) + cast_uint(p->sizelocvars) * sizeof(LocVar) + cast_uint(p->sizeupvalues) * sizeof(Upvaldesc); if (!(p->flag & PF_FIXED)) { - sz += cast_uint(p->sizecode) * sizeof(Instruction) - + cast_uint(p->sizelineinfo) * sizeof(lu_byte) - + cast_uint(p->sizeabslineinfo) * sizeof(AbsLineInfo); + sz += cast_uint(p->sizecode) * sizeof(Instruction); + sz += cast_uint(p->sizelineinfo) * sizeof(lu_byte); + sz += cast_uint(p->sizeabslineinfo) * sizeof(AbsLineInfo); } return sz; } diff --git a/lfunc.h b/lfunc.h index b9651074..b981edeb 100644 --- a/lfunc.h +++ b/lfunc.h @@ -56,7 +56,7 @@ LUAI_FUNC void luaF_newtbcupval (lua_State *L, StkId level); LUAI_FUNC void luaF_closeupval (lua_State *L, StkId level); LUAI_FUNC StkId luaF_close (lua_State *L, StkId level, int status, int yy); LUAI_FUNC void luaF_unlinkupval (UpVal *uv); -LUAI_FUNC size_t luaF_protosize (Proto *p); +LUAI_FUNC lu_mem luaF_protosize (Proto *p); LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, int pc); diff --git a/lgc.c b/lgc.c index e8b9ad5e..e4130bd5 100644 --- a/lgc.c +++ b/lgc.c @@ -110,43 +110,54 @@ static void entersweep (lua_State *L); #define gnodelast(h) gnode(h, cast_sizet(sizenode(h))) -static size_t objsize (GCObject *o) { +static l_mem objsize (GCObject *o) { + lu_mem res; switch (o->tt) { case LUA_VTABLE: { - return luaH_size(gco2t(o)); + res = luaH_size(gco2t(o)); + break; } case LUA_VLCL: { LClosure *cl = gco2lcl(o); - return sizeLclosure(cl->nupvalues); + res = sizeLclosure(cl->nupvalues); + break; } case LUA_VCCL: { CClosure *cl = gco2ccl(o); - return sizeCclosure(cl->nupvalues); + res = sizeCclosure(cl->nupvalues); + break; break; } case LUA_VUSERDATA: { Udata *u = gco2u(o); - return sizeudata(u->nuvalue, u->len); + res = sizeudata(u->nuvalue, u->len); + break; } case LUA_VPROTO: { - return luaF_protosize(gco2p(o)); + res = luaF_protosize(gco2p(o)); + break; } case LUA_VTHREAD: { - return luaE_threadsize(gco2th(o)); + res = luaE_threadsize(gco2th(o)); + break; } case LUA_VSHRSTR: { TString *ts = gco2ts(o); - return sizestrshr(cast_uint(ts->shrlen)); + res = sizestrshr(cast_uint(ts->shrlen)); + break; } case LUA_VLNGSTR: { TString *ts = gco2ts(o); - return luaS_sizelngstr(ts->u.lnglen, ts->shrlen); + res = luaS_sizelngstr(ts->u.lnglen, ts->shrlen); + break; } case LUA_VUPVAL: { - return sizeof(UpVal); + res = sizeof(UpVal); + break; } - default: lua_assert(0); return 0; + default: res = 0; lua_assert(0); } + return cast(l_mem, res); } @@ -327,7 +338,7 @@ GCObject *luaC_newobj (lua_State *L, lu_byte tt, size_t sz) { ** (only closures can), and a userdata's metatable must be a table. */ static void reallymarkobject (global_State *g, GCObject *o) { - g->GCmarked += cast(l_mem, objsize(o)); + g->GCmarked += objsize(o); switch (o->tt) { case LUA_VSHRSTR: case LUA_VLNGSTR: { @@ -803,6 +814,7 @@ static void freeupval (lua_State *L, UpVal *uv) { static void freeobj (lua_State *L, GCObject *o) { + assert_code(l_mem newmem = gettotalbytes(G(L)) - objsize(o)); switch (o->tt) { case LUA_VPROTO: luaF_freeproto(L, gco2p(o)); @@ -846,6 +858,7 @@ static void freeobj (lua_State *L, GCObject *o) { } default: lua_assert(0); } + lua_assert(gettotalbytes(G(L)) == newmem); } @@ -1167,7 +1180,7 @@ static GCObject **sweepgen (lua_State *L, global_State *g, GCObject **p, lua_assert(age != G_OLD1); /* advanced in 'markold' */ setage(curr, nextage[age]); if (getage(curr) == G_OLD1) { - addedold += cast(l_mem, objsize(curr)); /* bytes becoming old */ + addedold += objsize(curr); /* bytes becoming old */ if (*pfirstold1 == NULL) *pfirstold1 = curr; /* first OLD1 object in the list */ } diff --git a/lstate.c b/lstate.c index a8db9477..0e1cb01e 100644 --- a/lstate.c +++ b/lstate.c @@ -257,8 +257,9 @@ static void preinit_thread (lua_State *L, global_State *g) { } -size_t luaE_threadsize (lua_State *L) { - size_t sz = sizeof(LX) + cast_uint(L->nci) * sizeof(CallInfo); +lu_mem luaE_threadsize (lua_State *L) { + lu_mem sz = cast(lu_mem, sizeof(LX)) + + cast_uint(L->nci) * sizeof(CallInfo); if (L->stack.p != NULL) sz += cast_uint(stacksize(L) + EXTRA_STACK) * sizeof(StackValue); return sz; diff --git a/lstate.h b/lstate.h index a1b463c6..d1bc0542 100644 --- a/lstate.h +++ b/lstate.h @@ -416,7 +416,7 @@ union GCUnion { LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt); LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); -LUAI_FUNC size_t luaE_threadsize (lua_State *L); +LUAI_FUNC lu_mem luaE_threadsize (lua_State *L); LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L); LUAI_FUNC void luaE_shrinkCI (lua_State *L); LUAI_FUNC void luaE_checkcstack (lua_State *L); diff --git a/ltable.c b/ltable.c index 981e9df4..21fafa5e 100644 --- a/ltable.c +++ b/ltable.c @@ -863,8 +863,9 @@ Table *luaH_new (lua_State *L) { } -size_t luaH_size (Table *t) { - size_t sz = sizeof(Table) + luaH_realasize(t) * (sizeof(Value) + 1); +lu_mem luaH_size (Table *t) { + lu_mem sz = cast(lu_mem, sizeof(Table)) + + luaH_realasize(t) * (sizeof(Value) + 1); if (!isdummy(t)) sz += sizehash(t); return sz; diff --git a/ltable.h b/ltable.h index c352da38..17e4fb4a 100644 --- a/ltable.h +++ b/ltable.h @@ -163,7 +163,7 @@ LUAI_FUNC Table *luaH_new (lua_State *L); LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned nasize, unsigned nhsize); LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, unsigned nasize); -LUAI_FUNC size_t luaH_size (Table *t); +LUAI_FUNC lu_mem luaH_size (Table *t); LUAI_FUNC void luaH_free (lua_State *L, Table *t); LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); LUAI_FUNC lua_Unsigned luaH_getn (Table *t);