Skip to content

Commit

Permalink
Restore older debug info fields to their original positions
Browse files Browse the repository at this point in the history
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.)
  • Loading branch information
lukego committed May 9, 2018
1 parent d60c595 commit c552236
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 32 deletions.
17 changes: 8 additions & 9 deletions src/lj_bcread.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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;
}
Expand Down
12 changes: 7 additions & 5 deletions src/lj_bcwrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) : "";

Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions src/lj_obj.h
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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 ------------------------------------------------------ */

Expand Down
33 changes: 17 additions & 16 deletions src/lj_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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. */
}

Expand Down Expand Up @@ -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. */
Expand All @@ -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);
Expand All @@ -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. */
Expand Down

0 comments on commit c552236

Please sign in to comment.