From d60c59518ff6ce8639112095a63faa3cf08a4a8a Mon Sep 17 00:00:00 2001 From: Luke Gorrie Date: Tue, 8 May 2018 13:18:33 +0000 Subject: [PATCH 1/3] lj_prase.c: Remove old debug noise --- src/lj_parse.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lj_parse.c b/src/lj_parse.c index 6abff004aa..1e948d97ce 100644 --- a/src/lj_parse.c +++ b/src/lj_parse.c @@ -2206,7 +2206,6 @@ static void parse_func(LexState *ls, BCLine line) ExpDesc v, b; int needself = 0; char log[128]; - log[0] = 'c'; memset(log, 0, sizeof(log)); lj_lex_log(ls, log, sizeof(log)-1); lj_lex_next(ls); /* Skip 'function'. */ From c552236ca957899d74dd876a131751397b7468dd Mon Sep 17 00:00:00 2001 From: Luke Gorrie Date: Wed, 9 May 2018 06:38:17 +0000 Subject: [PATCH 2/3] Restore older debug info fields to their original positions The declname field had been inserted at the beginning of the debug information, but now it is appended to the end. This preserves the order of the existing fields. This is a defensive move to avoid bugs due to unknowingly disturbing invariants in the debug info structure. (Lame, I know, but I have other fish to fry right now.) --- src/lj_bcread.c | 17 ++++++++--------- src/lj_bcwrite.c | 12 +++++++----- src/lj_obj.h | 4 ++-- src/lj_parse.c | 33 +++++++++++++++++---------------- 4 files changed, 34 insertions(+), 32 deletions(-) diff --git a/src/lj_bcread.c b/src/lj_bcread.c index 52a22da9ed..2d84c46736 100644 --- a/src/lj_bcread.c +++ b/src/lj_bcread.c @@ -152,7 +152,7 @@ static uint32_t bcread_uleb128_33(LexState *ls) static void bcread_dbg(LexState *ls, GCproto *pt, MSize sizedbg) { uint32_t *lineinfo = (uint32_t*)proto_lineinfo(pt); - bcread_block(ls, (void*)proto_declname(pt), sizedbg); + bcread_block(ls, (void*)lineinfo, sizedbg); /* Swap lineinfo if the endianess differs. */ if (bcread_swap(ls)) { int i; @@ -298,9 +298,9 @@ GCproto *lj_bcread_proto(LexState *ls) { GCproto *pt; MSize framesize, numparams, flags, sizeuv, sizekgc, sizekn, sizebc, sizept; - MSize ofsk, ofsuv, ofsdbg; + MSize ofsk, ofsuv, ofsdbg, ofsdeclname = 0; MSize sizedbg = 0; - BCLine firstline = 0, numline = 0, ndeclname = 0; + BCLine firstline = 0, numline = 0; /* Read prototype header. */ flags = bcread_byte(ls); @@ -313,9 +313,9 @@ GCproto *lj_bcread_proto(LexState *ls) if (!(bcread_flags(ls) & BCDUMP_F_STRIP)) { sizedbg = bcread_uleb128(ls); if (sizedbg) { - ndeclname = bcread_uleb128(ls); firstline = bcread_uleb128(ls); numline = bcread_uleb128(ls); + ofsdeclname = bcread_uleb128(ls); } } @@ -361,17 +361,16 @@ GCproto *lj_bcread_proto(LexState *ls) pt->numline = numline; if (sizedbg) { MSize sizeli = (sizebc-1) * sizeof(BCLine); - setmref(pt->declname, (char *)pt + ofsdbg); - setmref(pt->lineinfo, (char *)pt + ofsdbg + ndeclname); - setmref(pt->uvinfo, (char *)pt + ofsdbg + ndeclname + sizeli); + setmref(pt->lineinfo, (char *)pt + ofsdbg); + setmref(pt->uvinfo, (char *)pt + ofsdbg + sizeli); + setmref(pt->declname, (char *)pt + ofsdbg + ofsdeclname); bcread_dbg(ls, pt, sizedbg); setmref(pt->varinfo, bcread_varinfo(pt)); - lua_assert(strlen(pt->declname)+1 == ndeclname); } else { - setmref(pt->declname, NULL); setmref(pt->lineinfo, NULL); setmref(pt->uvinfo, NULL); setmref(pt->varinfo, NULL); + setmref(pt->declname, NULL); } return pt; } diff --git a/src/lj_bcwrite.c b/src/lj_bcwrite.c index 5167c731d3..353a677cf6 100644 --- a/src/lj_bcwrite.c +++ b/src/lj_bcwrite.c @@ -210,7 +210,7 @@ static char *bcwrite_bytecode(BCWriteCtx *ctx, char *p, GCproto *pt) /* Write prototype. */ static void bcwrite_proto(BCWriteCtx *ctx, GCproto *pt) { - MSize sizedbg = 0; + MSize sizedbg = 0, ofsdeclname = 0; char *p; const char *declname = pt->declname ? proto_declname(pt) : ""; @@ -239,13 +239,15 @@ static void bcwrite_proto(BCWriteCtx *ctx, GCproto *pt) p = lj_strfmt_wuleb128(p, pt->sizekn); p = lj_strfmt_wuleb128(p, pt->sizebc-1); if (!ctx->strip) { - if (proto_lineinfo(pt)) - sizedbg = pt->sizept - (MSize)((char *)proto_declname(pt) - (char *)pt); + if (proto_lineinfo(pt)) { + sizedbg = pt->sizept - (MSize)((char *)proto_lineinfo(pt) - (char *)pt); + ofsdeclname = (MSize)((char*)proto_declname(pt) - (char *)proto_lineinfo(pt)); + } p = lj_strfmt_wuleb128(p, sizedbg); if (sizedbg) { - p = lj_strfmt_wuleb128(p, strlen(declname)+1); p = lj_strfmt_wuleb128(p, pt->firstline); p = lj_strfmt_wuleb128(p, pt->numline); + p = lj_strfmt_wuleb128(p, ofsdeclname); } } @@ -261,7 +263,7 @@ static void bcwrite_proto(BCWriteCtx *ctx, GCproto *pt) /* Write debug info, if not stripped. */ if (sizedbg) { p = lj_buf_more(&ctx->sb, sizedbg); - p = lj_buf_wmem(p, declname, sizedbg); + p = lj_buf_wmem(p, proto_lineinfo(pt), sizedbg); setsbufP(&ctx->sb, p); } diff --git a/src/lj_obj.h b/src/lj_obj.h index 9d9349ef86..fc6022ef83 100644 --- a/src/lj_obj.h +++ b/src/lj_obj.h @@ -311,10 +311,10 @@ typedef struct GCproto { GCRef chunkname; /* Name of the chunk this function was defined in. */ BCLine firstline; /* First line of the function definition. */ BCLine numline; /* Number of lines for the function definition. */ - MRef declname; /* Declared name of function (null-terminated). */ MRef lineinfo; /* Map from bytecode ins. to source line. */ MRef uvinfo; /* Upvalue names. */ MRef varinfo; /* Names and compressed extents of local variables. */ + MRef declname; /* Declared name of function (null-terminated). */ } GCproto; /* Flags for prototype. */ @@ -345,10 +345,10 @@ typedef struct GCproto { #define proto_chunkname(pt) (strref((pt)->chunkname)) #define proto_chunknamestr(pt) (strdata(proto_chunkname((pt)))) -#define proto_declname(pt) (mref((pt)->declname, const char)) #define proto_lineinfo(pt) (mref((pt)->lineinfo, const uint32_t)) #define proto_uvinfo(pt) (mref((pt)->uvinfo, const uint8_t)) #define proto_varinfo(pt) (mref((pt)->varinfo, const uint8_t)) +#define proto_declname(pt) (mref((pt)->declname, const char)) /* -- Upvalue object ------------------------------------------------------ */ diff --git a/src/lj_parse.c b/src/lj_parse.c index 1e948d97ce..e9672f00d8 100644 --- a/src/lj_parse.c +++ b/src/lj_parse.c @@ -1366,20 +1366,13 @@ static void fs_fixup_line(FuncState *fs, GCproto *pt, } /* Prepare variable info for prototype. */ -static size_t fs_prep_var(LexState *ls, FuncState *fs, size_t *ofsvar, const char *declname) +static size_t fs_prep_var(LexState *ls, FuncState *fs, size_t *ofsvar, + size_t *ofsdeclname, const char *declname) { VarInfo *vs =ls->vstack, *ve; MSize i, n; BCPos lastpc; lj_buf_reset(&ls->sb); /* Copy to temp. string buffer. */ - /* Store function declaration name. */ - { - char *p; - int len = strlen(declname) + 1; - p = lj_buf_more(&ls->sb, len); - p = lj_buf_wmem(p, declname, len); - setsbufP(&ls->sb, p); - } /* Store upvalue names. */ for (i = 0, n = fs->nuv; i < n; i++) { GCstr *s = strref(vs[fs->uvmap[i]].name); @@ -1412,16 +1405,24 @@ static size_t fs_prep_var(LexState *ls, FuncState *fs, size_t *ofsvar, const cha } } lj_buf_putb(&ls->sb, '\0'); /* Terminator for varinfo. */ + /* Store function declaration name. */ + *ofsdeclname = sbuflen(&ls->sb); + { + char *p; + int len = strlen(declname) + 1; + p = lj_buf_more(&ls->sb, len); + p = lj_buf_wmem(p, declname, len); + setsbufP(&ls->sb, p); + } return sbuflen(&ls->sb); } /* Fixup variable info for prototype. */ -static void fs_fixup_var(LexState *ls, GCproto *pt, uint8_t *p, size_t ofsvar) +static void fs_fixup_var(LexState *ls, GCproto *pt, uint8_t *p, size_t ofsvar, size_t ofsdeclname) { - int ndeclname = strlen((char*)p)+1; - setmref(pt->declname, p); - setmref(pt->uvinfo, p + ndeclname); + setmref(pt->uvinfo, p); setmref(pt->varinfo, (char *)p + ofsvar); + setmref(pt->declname, (char*)p + ofsdeclname); memcpy(p, sbufB(&ls->sb), sbuflen(&ls->sb)); /* Copy from temp. buffer. */ } @@ -1481,7 +1482,7 @@ static GCproto *fs_finish(LexState *ls, BCLine line, char *declname) lua_State *L = ls->L; FuncState *fs = ls->fs; BCLine numline = line - fs->linedefined; - size_t sizept, ofsk, ofsuv, ofsli, ofsdbg, ofsvar; + size_t sizept, ofsk, ofsuv, ofsli, ofsdbg, ofsvar, ofsdeclname; GCproto *pt; /* Apply final fixups. */ @@ -1493,7 +1494,7 @@ static GCproto *fs_finish(LexState *ls, BCLine line, char *declname) ofsk = sizept; sizept += fs->nkn*sizeof(TValue); ofsuv = sizept; sizept += ((fs->nuv+1)&~1)*2; ofsli = sizept; sizept += fs_prep_line(fs, numline); - ofsdbg = sizept; sizept += fs_prep_var(ls, fs, &ofsvar, declname); + ofsdbg = sizept; sizept += fs_prep_var(ls, fs, &ofsvar, &ofsdeclname, declname); /* Allocate prototype and initialize its fields. */ pt = (GCproto *)lj_mem_newgco(L, (MSize)sizept); @@ -1511,7 +1512,7 @@ static GCproto *fs_finish(LexState *ls, BCLine line, char *declname) fs_fixup_k(fs, pt, (void *)((char *)pt + ofsk)); fs_fixup_uv1(fs, pt, (uint16_t *)((char *)pt + ofsuv)); fs_fixup_line(fs, pt, (void *)((char *)pt + ofsli), numline); - fs_fixup_var(ls, pt, (uint8_t *)((char *)pt + ofsdbg), ofsvar); + fs_fixup_var(ls, pt, (uint8_t *)((char *)pt + ofsdbg), ofsvar, ofsdeclname); L->top--; /* Pop table of constants. */ ls->vtop = fs->vbase; /* Reset variable stack. */ From 96e00a6f86f55d93b2b2ee95f747c50b0bf87338 Mon Sep 17 00:00:00 2001 From: Luke Gorrie Date: Wed, 9 May 2018 06:59:37 +0000 Subject: [PATCH 3/3] lj_bcwrite.c: Remove unused variable --- src/lj_bcwrite.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lj_bcwrite.c b/src/lj_bcwrite.c index 353a677cf6..e46e2b8757 100644 --- a/src/lj_bcwrite.c +++ b/src/lj_bcwrite.c @@ -212,7 +212,6 @@ static void bcwrite_proto(BCWriteCtx *ctx, GCproto *pt) { MSize sizedbg = 0, ofsdeclname = 0; char *p; - const char *declname = pt->declname ? proto_declname(pt) : ""; /* Recursively write children of prototype. */ if ((pt->flags & PROTO_CHILD)) {