Dump doesn't need to reuse 'source'

All strings are being reused now, including 'source'.
This commit is contained in:
Roberto Ierusalimschy
2022-12-20 11:14:52 -03:00
parent d70a0c91ad
commit 7d6a97e42b
3 changed files with 35 additions and 12 deletions

12
ldump.c
View File

@@ -126,7 +126,7 @@ static void dumpCode (DumpState *D, const Proto *f) {
} }
static void dumpFunction(DumpState *D, const Proto *f, TString *psource); static void dumpFunction(DumpState *D, const Proto *f);
static void dumpConstants (DumpState *D, const Proto *f) { static void dumpConstants (DumpState *D, const Proto *f) {
int i; int i;
@@ -159,7 +159,7 @@ static void dumpProtos (DumpState *D, const Proto *f) {
int n = f->sizep; int n = f->sizep;
dumpInt(D, n); dumpInt(D, n);
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
dumpFunction(D, f->p[i], f->source); dumpFunction(D, f->p[i]);
} }
@@ -199,9 +199,9 @@ static void dumpDebug (DumpState *D, const Proto *f) {
} }
static void dumpFunction (DumpState *D, const Proto *f, TString *psource) { static void dumpFunction (DumpState *D, const Proto *f) {
if (D->strip || f->source == psource) if (D->strip)
dumpString(D, NULL); /* no debug info or same source as its parent */ dumpString(D, NULL); /* no debug info */
else else
dumpString(D, f->source); dumpString(D, f->source);
dumpInt(D, f->linedefined); dumpInt(D, f->linedefined);
@@ -245,7 +245,7 @@ int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data,
D.nstr = 0; D.nstr = 0;
dumpHeader(&D); dumpHeader(&D);
dumpByte(&D, f->sizeupvalues); dumpByte(&D, f->sizeupvalues);
dumpFunction(&D, f, NULL); dumpFunction(&D, f);
return D.status; return D.status;
} }

View File

@@ -162,7 +162,7 @@ static void loadCode (LoadState *S, Proto *f) {
} }
static void loadFunction(LoadState *S, Proto *f, TString *psource); static void loadFunction(LoadState *S, Proto *f);
static void loadConstants (LoadState *S, Proto *f) { static void loadConstants (LoadState *S, Proto *f) {
@@ -211,7 +211,7 @@ static void loadProtos (LoadState *S, Proto *f) {
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
f->p[i] = luaF_newproto(S->L); f->p[i] = luaF_newproto(S->L);
luaC_objbarrier(S->L, f, f->p[i]); luaC_objbarrier(S->L, f, f->p[i]);
loadFunction(S, f->p[i], f->source); loadFunction(S, f->p[i]);
} }
} }
@@ -266,10 +266,8 @@ static void loadDebug (LoadState *S, Proto *f) {
} }
static void loadFunction (LoadState *S, Proto *f, TString *psource) { static void loadFunction (LoadState *S, Proto *f) {
f->source = loadStringN(S, f); f->source = loadStringN(S, f);
if (f->source == NULL) /* no source in dump? */
f->source = psource; /* reuse parent's source */
f->linedefined = loadInt(S); f->linedefined = loadInt(S);
f->lastlinedefined = loadInt(S); f->lastlinedefined = loadInt(S);
f->numparams = loadByte(S); f->numparams = loadByte(S);
@@ -342,7 +340,7 @@ LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) {
luaD_inctop(L); luaD_inctop(L);
cl->p = luaF_newproto(L); cl->p = luaF_newproto(L);
luaC_objbarrier(L, cl, cl->p); luaC_objbarrier(L, cl, cl->p);
loadFunction(&S, cl->p, NULL); loadFunction(&S, cl->p);
lua_assert(cl->nupvalues == cl->p->sizeupvalues); lua_assert(cl->nupvalues == cl->p->sizeupvalues);
luai_verifycode(L, cl->p); luai_verifycode(L, cl->p);
L->top.p--; /* pop table */ L->top.p--; /* pop table */

View File

@@ -487,5 +487,30 @@ do
end end
end end
do -- check reuse of strings in dumps
local str = "|" .. string.rep("X", 50) .. "|"
local foo = load(string.format([[
local str <const> = "%s"
return {
function () return str end,
function () return str end,
function () return str end
}
]], str))
-- count occurrences of 'str' inside the dump
local dump = string.dump(foo)
local _, count = string.gsub(dump, str, {})
-- there should be only two occurrences:
-- one inside the source, other the string itself.
assert(count == 2)
if T then -- check reuse of strings in undump
local funcs = load(dump)()
assert(string.format("%p", T.listk(funcs[1])[1]) ==
string.format("%p", T.listk(funcs[3])[1]))
end
end
print('OK') print('OK')
return deep return deep