Skip to content

Commit ca7835f

Browse files
committed
luajit: allow one to mark a function as blocked for debug stuff
1 parent 5fda101 commit ca7835f

4 files changed

Lines changed: 36 additions & 3 deletions

File tree

LuaJIT-2.1/src/lib_base.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ LJLIB_ASM(setmetatable) LJLIB_REC(.)
141141
return FFH_RES(1);
142142
}
143143

144+
void blockDebug(lua_State* L, GCfunc* func);
144145
LJLIB_CF(getfenv) LJLIB_REC(.)
145146
{
146147
GCfunc *fn;
@@ -155,6 +156,7 @@ LJLIB_CF(getfenv) LJLIB_REC(.)
155156
if (LJ_FR2) o--;
156157
}
157158
fn = &gcval(o)->fn;
159+
blockDebug(L, fn);
158160
settabV(L, L->top++, isluafunc(fn) ? tabref(fn->l.env) : tabref(L->env));
159161
return 1;
160162
}
@@ -181,6 +183,7 @@ LJLIB_CF(setfenv)
181183
fn = &gcval(o)->fn;
182184
if (!isluafunc(fn))
183185
lj_err_caller(L, LJ_ERR_SETFENV);
186+
blockDebug(L, fn);
184187
setgcref(fn->l.env, obj2gco(t));
185188
lj_gc_objbarrier(L, obj2gco(fn), t);
186189
setfuncV(L, L->top++, fn);

LuaJIT-2.1/src/lib_debug.c

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ LJLIB_CF(debug_getregistry)
3131

3232
LJLIB_CF(debug_getmetatable) LJLIB_REC(.)
3333
{
34-
lj_lib_checkany(L, 1);
34+
if (tviscdata(lj_lib_checkany(L, 1)));
35+
lj_err_arg(L, 1, LJ_ERR_DENYCDATA);
36+
3537
if (!lua_getmetatable(L, 1)) {
3638
setnilV(L->top-1);
3739
}
@@ -40,6 +42,9 @@ LJLIB_CF(debug_getmetatable) LJLIB_REC(.)
4042

4143
LJLIB_CF(debug_setmetatable)
4244
{
45+
if (tviscdata(lj_lib_checkany(L, 1)));
46+
lj_err_arg(L, 1, LJ_ERR_DENYCDATA);
47+
4348
lj_lib_checktabornil(L, 2);
4449
L->top = L->base+2;
4550
lua_setmetatable(L, 1);
@@ -49,15 +54,31 @@ LJLIB_CF(debug_setmetatable)
4954
return 1;
5055
}
5156

57+
void blockDebug(lua_State* L, GCfunc* func)
58+
{
59+
if (isblockdebug(func->c))
60+
lj_err_arg(L, 1, LJ_ERR_BLOCKDEBUG);
61+
}
62+
63+
void blockDebugCheck(lua_State* L, int idx)
64+
{
65+
TValue *o = L->base + idx-1;
66+
if (!(o < L->top && tvisfunc(o)))
67+
{
68+
blockDebug(L, funcV(o));
69+
}
70+
}
71+
5272
LJLIB_CF(debug_getfenv)
5373
{
54-
lj_lib_checkany(L, 1);
74+
blockDebugCheck(L, 1);
5575
lua_getfenv(L, 1);
5676
return 1;
5777
}
5878

5979
LJLIB_CF(debug_setfenv)
6080
{
81+
blockDebugCheck(L, 1);
6182
lj_lib_checktab(L, 2);
6283
L->top = L->base+2;
6384
if (!lua_setfenv(L, 1))
@@ -202,7 +223,7 @@ static int debug_getupvalue(lua_State *L, int get)
202223
{
203224
int32_t n = lj_lib_checkint(L, 2);
204225
const char *name;
205-
lj_lib_checkfunc(L, 1);
226+
blockDebug(L, lj_lib_checkfunc(L, 1));
206227
name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);
207228
if (name) {
208229
lua_pushstring(L, name);
@@ -228,6 +249,7 @@ LJLIB_CF(debug_setupvalue)
228249
LJLIB_CF(debug_upvalueid)
229250
{
230251
GCfunc *fn = lj_lib_checkfunc(L, 1);
252+
blockDebug(L, fn);
231253
int32_t n = lj_lib_checkint(L, 2) - 1;
232254
if ((uint32_t)n >= fn->l.nupvalues)
233255
lj_err_arg(L, 2, LJ_ERR_IDXRNG);
@@ -244,6 +266,7 @@ LJLIB_CF(debug_upvaluejoin)
244266
for (i = 0; i < 2; i++) {
245267
int32_t n;
246268
fn[i] = lj_lib_checkfunc(L, 2*i+1);
269+
blockDebug(L, fn[i]);
247270
if (!isluafunc(fn[i]))
248271
lj_err_arg(L, 2*i+1, LJ_ERR_NOLFUNC);
249272
n = lj_lib_checkint(L, 2*i+2) - 1;

LuaJIT-2.1/src/lj_errmsg.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ ERRDEF(BADSELF, "calling " LUA_QS " on bad self (%s)")
4242
ERRDEF(BADARG, "bad argument #%d to " LUA_QS " (%s)")
4343
ERRDEF(BADTYPE, "%s expected, got %s")
4444
ERRDEF(BADVAL, "invalid value")
45+
ERRDEF(DENYCDATA, "cdata is not allowed to be used here")
4546
ERRDEF(NOVAL, "value expected")
4647
ERRDEF(NOCORO, "coroutine expected")
4748
ERRDEF(NOTABN, "nil or table expected")
@@ -52,6 +53,7 @@ ERRDEF(NOPROXY, "boolean or proxy expected")
5253
ERRDEF(FORINIT, LUA_QL("for") " initial value must be a number")
5354
ERRDEF(FORLIM, LUA_QL("for") " limit must be a number")
5455
ERRDEF(FORSTEP, LUA_QL("for") " step must be a number")
56+
ERRDEF(BLOCKDEBUG, "function is marked to block any debug access")
5557

5658
/* C API checks. */
5759
ERRDEF(NOENV, "no calling environment")

LuaJIT-2.1/src/lj_gc.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ enum {
2424
#define LJ_GC_FIXED 0x20
2525
#define LJ_GC_SFIXED 0x40
2626
#define LJ_GC_READONLY 0x80
27+
#define LJ_GC_BLOCKDEBUG 0x80 // Same as LJ_GC_READONLY, but we'll use this one for functions.
2728

2829
#define LJ_GC_WHITES (LJ_GC_WHITE0 | LJ_GC_WHITE1)
2930
#define LJ_GC_COLORS (LJ_GC_WHITES | LJ_GC_BLACK)
@@ -50,6 +51,10 @@ enum {
5051
#define markreadonly(x) ((x)->marked |= LJ_GC_READONLY)
5152
#define unmarkreadonly(x) ((x)->marked &= ~LJ_GC_READONLY)
5253

54+
#define isblockdebug(x) ((x).marked & LJ_GC_BLOCKDEBUG)
55+
#define markblockdebug(x) ((x).marked |= LJ_GC_BLOCKDEBUG)
56+
#define unmarkblockdebug(x) ((x).marked &= ~LJ_GC_BLOCKDEBUG)
57+
5358
/* Collector. */
5459
LJ_FUNC size_t lj_gc_separateudata(global_State *g, int all);
5560
LJ_FUNC void lj_gc_finalize_udata(lua_State *L);

0 commit comments

Comments
 (0)