From eb9d1982fa0a9bfc2298ab0f6ec2242378700e97 Mon Sep 17 00:00:00 2001 From: GenericHeroGuy <86008856+GenericHeroGuy@users.noreply.github.com> Date: Fri, 3 Jan 2025 20:12:26 +0100 Subject: [PATCH 1/5] Update to BLUAJIT 449b76f --- src/bluajit/jit/p.lua | 4 +-- src/bluajit/lib_bit.c | 2 +- src/bluajit/lj_asm.c | 4 +-- src/bluajit/lj_bcread.c | 2 +- src/bluajit/lj_parse.c | 9 ++++--- src/bluajit/lj_record.c | 51 ++++++++++++++++++++++++-------------- src/bluajit/lj_tab.c | 2 +- src/bluajit/lj_vmmath.c | 2 +- src/bluajit/luaconf.h | 1 - src/bluajit/vm_mips64.dasc | 5 ++-- 10 files changed, 48 insertions(+), 34 deletions(-) diff --git a/src/bluajit/jit/p.lua b/src/bluajit/jit/p.lua index 36f836c51..ef2ee82f5 100644 --- a/src/bluajit/jit/p.lua +++ b/src/bluajit/jit/p.lua @@ -227,9 +227,7 @@ local function prof_finish() local samples = prof_samples if samples == 0 then if prof_raw ~= true then out:write("[No samples collected]\n") end - return - end - if prof_ann then + elseif prof_ann then prof_annotate(prof_count1, samples) else prof_top(prof_count1, prof_count2, samples, "") diff --git a/src/bluajit/lib_bit.c b/src/bluajit/lib_bit.c index ce40e2983..ada9614d0 100644 --- a/src/bluajit/lib_bit.c +++ b/src/bluajit/lib_bit.c @@ -98,7 +98,7 @@ LJLIB_ASM(bit_lshift) LJLIB_REC(bit_shift IR_BSHL) x = lj_carith_shift64(x, sh, curr_func(L)->c.ffid - (int)FF_bit_lshift); return bit_result64(L, id, x); } - if (id2) setintV(L->base+1, sh); + setintV(L->base+1, sh); return FFH_RETRY; #else lj_lib_checknumber(L, 1); diff --git a/src/bluajit/lj_asm.c b/src/bluajit/lj_asm.c index 844910ad9..c31dd1dec 100644 --- a/src/bluajit/lj_asm.c +++ b/src/bluajit/lj_asm.c @@ -949,11 +949,11 @@ static int asm_sunk_store(ASMState *as, IRIns *ira, IRIns *irs) static void asm_snap_alloc1(ASMState *as, IRRef ref) { IRIns *ir = IR(ref); - if (!irref_isk(ref) && ir->r != RID_SUNK) { + if (!irref_isk(ref)) { bloomset(as->snapfilt1, ref); bloomset(as->snapfilt2, hashrot(ref, ref + HASH_BIAS)); if (ra_used(ir)) return; - if (ir->r == RID_SINK) { + if (ir->r == RID_SINK || ir->r == RID_SUNK) { ir->r = RID_SUNK; #if LJ_HASFFI if (ir->o == IR_CNEWI) { /* Allocate CNEWI value. */ diff --git a/src/bluajit/lj_bcread.c b/src/bluajit/lj_bcread.c index 637ef0679..54f483c71 100644 --- a/src/bluajit/lj_bcread.c +++ b/src/bluajit/lj_bcread.c @@ -408,7 +408,7 @@ static int bcread_header(LexState *ls) #endif } if ((flags & BCDUMP_F_STRIP)) { - ls->chunkname = lj_str_newz(ls->L, ls->chunkarg); + ls->chunkname = lj_str_newz(ls->L, *ls->chunkarg == BCDUMP_HEAD1 ? "=?" : ls->chunkarg); } else { MSize len = bcread_uleb128(ls); bcread_need(ls, len); diff --git a/src/bluajit/lj_parse.c b/src/bluajit/lj_parse.c index 0e0b382cf..d5eec92b4 100644 --- a/src/bluajit/lj_parse.c +++ b/src/bluajit/lj_parse.c @@ -2970,10 +2970,11 @@ static int parse_stmt(LexState *ls) case TK_break: lj_lex_next(ls); #if BLUAJIT_MULTILEVEL_BREAK - int levels = numberVint(&ls->tokval); - if (lex_opt(ls, TK_number)) - parse_break(ls, levels); - else + if (ls->tok == TK_number) { + int levels = numberVint(&ls->tokval); + lj_lex_next(ls); + parse_break(ls, levels); + } else #endif parse_break(ls, 1); return !LJ_52; /* Must be last in Lua 5.1. */ diff --git a/src/bluajit/lj_record.c b/src/bluajit/lj_record.c index 28c725d10..81c4f2635 100644 --- a/src/bluajit/lj_record.c +++ b/src/bluajit/lj_record.c @@ -2084,25 +2084,19 @@ static TRef rec_tnew(jit_State *J, uint32_t ah) typedef struct RecCatDataCP { jit_State *J; - RecordIndex *ix; + BCReg baseslot, topslot; + TRef tr; } RecCatDataCP; static TValue *rec_mm_concat_cp(lua_State *L, lua_CFunction dummy, void *ud) { RecCatDataCP *rcd = (RecCatDataCP *)ud; - UNUSED(L); UNUSED(dummy); - rec_mm_arith(rcd->J, rcd->ix, MM_concat); /* Call __concat metamethod. */ - return NULL; -} - -static TRef rec_cat(jit_State *J, BCReg baseslot, BCReg topslot) -{ + jit_State *J = rcd->J; + BCReg baseslot = rcd->baseslot, topslot = rcd->topslot; TRef *top = &J->base[topslot]; - TValue savetv[5+LJ_FR2]; BCReg s; RecordIndex ix; - RecCatDataCP rcd; - int errcode; + UNUSED(L); UNUSED(dummy); lj_assertJ(baseslot < topslot, "bad CAT arg"); for (s = baseslot; s <= topslot; s++) (void)getslot(J, s); /* Ensure all arguments have a reference. */ @@ -2124,7 +2118,10 @@ static TRef rec_cat(jit_State *J, BCReg baseslot, BCReg topslot) } while (trp <= top); tr = emitir(IRTG(IR_BUFSTR, IRT_STR), tr, hdr); J->maxslot = (BCReg)(xbase - J->base); - if (xbase == base) return tr; /* Return simple concatenation result. */ + if (xbase == base) { + rcd->tr = tr; /* Return simple concatenation result. */ + return NULL; + } /* Pass partial result. */ topslot = J->maxslot--; *xbase = tr; @@ -2137,13 +2134,31 @@ static TRef rec_cat(jit_State *J, BCReg baseslot, BCReg topslot) copyTV(J->L, &ix.tabv, &J->L->base[topslot-1]); ix.tab = top[-1]; ix.key = top[0]; - memcpy(savetv, &J->L->base[topslot-1], sizeof(savetv)); /* Save slots. */ + rec_mm_arith(J, &ix, MM_concat); /* Call __concat metamethod. */ + rcd->tr = 0; /* No result yet. */ + return NULL; +} + +static TRef rec_cat(jit_State *J, BCReg baseslot, BCReg topslot) +{ + lua_State *L = J->L; + ptrdiff_t delta = L->top - L->base; + TValue savetv[5+LJ_FR2], errobj; + RecCatDataCP rcd; + int errcode; rcd.J = J; - rcd.ix = &ix; - errcode = lj_vm_cpcall(J->L, NULL, &rcd, rec_mm_concat_cp); - memcpy(&J->L->base[topslot-1], savetv, sizeof(savetv)); /* Restore slots. */ - if (errcode) return (TRef)(-errcode); - return 0; /* No result yet. */ + rcd.baseslot = baseslot; + rcd.topslot = topslot; + memcpy(savetv, &L->base[topslot-1], sizeof(savetv)); /* Save slots. */ + errcode = lj_vm_cpcall(L, NULL, &rcd, rec_mm_concat_cp); + if (errcode) copyTV(L, &errobj, L->top-1); + memcpy(&L->base[topslot-1], savetv, sizeof(savetv)); /* Restore slots. */ + if (errcode) { + L->top = L->base + delta; + copyTV(L, L->top++, &errobj); + return (TRef)(-errcode); + } + return rcd.tr; } /* -- Record bytecode ops ------------------------------------------------- */ diff --git a/src/bluajit/lj_tab.c b/src/bluajit/lj_tab.c index 535a69f5d..869c5f03c 100644 --- a/src/bluajit/lj_tab.c +++ b/src/bluajit/lj_tab.c @@ -635,7 +635,7 @@ LJ_NOINLINE static MSize tab_len_slow(GCtab *t, size_t hi) while ((tv = lj_tab_getint(t, (int32_t)hi)) && !tvisnil(tv)) { lo = hi; hi += hi; - if (hi > (size_t)(INT_MAX-2)) { /* Punt and do a linear search. */ + if (hi > (size_t)(0x7fffffff - 2)) { /* Punt and do a linear search. */ lo = 1; while ((tv = lj_tab_getint(t, (int32_t)lo)) && !tvisnil(tv)) lo++; return (MSize)(lo - 1); diff --git a/src/bluajit/lj_vmmath.c b/src/bluajit/lj_vmmath.c index 6a2ea64bb..9e8f1e0e6 100644 --- a/src/bluajit/lj_vmmath.c +++ b/src/bluajit/lj_vmmath.c @@ -78,7 +78,7 @@ int32_t lj_vm_foldarith_int(int32_t x, int32_t y, int op) case IR_MOD - IR_ADD: return x%y; break; case IR_POW - IR_ADD: return pow(x, y); break; case IR_NEG - IR_ADD: return -x; break; - case IR_ABS - IR_ADD: return fabs(x); break; + case IR_ABS - IR_ADD: return abs(x); break; #if LJ_HASJIT case IR_LDEXP - IR_ADD: return ldexp(x, (int)y); break; case IR_MIN - IR_ADD: return x < y ? x : y; break; diff --git a/src/bluajit/luaconf.h b/src/bluajit/luaconf.h index 669ca8cb1..f59cc1b5f 100644 --- a/src/bluajit/luaconf.h +++ b/src/bluajit/luaconf.h @@ -9,7 +9,6 @@ #ifndef WINVER #define WINVER 0x0501 #endif -#include #include /* Default path for loading Lua and C modules with require(). */ diff --git a/src/bluajit/vm_mips64.dasc b/src/bluajit/vm_mips64.dasc index a8d204137..1f0118ecf 100644 --- a/src/bluajit/vm_mips64.dasc +++ b/src/bluajit/vm_mips64.dasc @@ -1420,9 +1420,10 @@ static void build_subroutines(BuildCtx *ctx) | sltu AT, TMP1, TMP2 | bnez AT, ->fff_fallback |. lbu TMP3, DISPATCH_GL(hookmask)(DISPATCH) - | daddiu NARGS8:RC, NARGS8:RC, -8 - | bltz NARGS8:RC, ->fff_fallback + | daddiu NARGS8:TMP0, NARGS8:RC, -8 + | bltz NARGS8:TMP0, ->fff_fallback |. move TMP2, BASE + | move NARGS8:RC, NARGS8:TMP0 | daddiu BASE, BASE, 16 | // Remember active hook before pcall. | srl TMP3, TMP3, HOOK_ACTIVE_SHIFT From 0fbc44801339355b98ad90e9d035745391ce66b1 Mon Sep 17 00:00:00 2001 From: GenericHeroGuy <86008856+GenericHeroGuy@users.noreply.github.com> Date: Fri, 3 Jan 2025 22:16:11 +0100 Subject: [PATCH 2/5] Use references instead of upvalues for original I/O functions No more unrestricted I/O through debug.getupvalue --- src/lua_baselib.c | 25 ++++++++++++++----------- src/lua_consolelib.c | 8 ++++++-- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/lua_baselib.c b/src/lua_baselib.c index d28b2d10f..0f1068f51 100644 --- a/src/lua_baselib.c +++ b/src/lua_baselib.c @@ -40,6 +40,8 @@ boolean luaL_checkboolean(lua_State *L, int narg) { return lua_toboolean(L, narg); } +static int ref_open, ref_iowrite, ref_fwrite; // original versions of io functions + #define FILELIMIT 1024*1024 // Size limit for reading/writing files static const char *whitelist[] = { // Allow scripters to write files of these types to SRB2's folder @@ -102,7 +104,7 @@ static int lib_open(lua_State *L) lua_pushstring(L, destFilename); lua_replace(L, 1); // now call the real io.open - lua_pushvalue(L, lua_upvalueindex(1)); + lua_rawgeti(L, LUA_REGISTRYINDEX, ref_open); lua_insert(L, 1); lua_call(L, lua_gettop(L)-1, LUA_MULTRET); return lua_gettop(L); @@ -114,8 +116,10 @@ static int lib_write(lua_State *L, int arg) int top = lua_gettop(L); boolean error = true; size_t len, size = 0; + int ogref = ref_iowrite; if (arg != 1) { + ogref = ref_fwrite; FILE *fp = *(FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE); if (fp == NULL) goto call; // real file:write will throw size = ftell(fp); @@ -134,7 +138,7 @@ static int lib_write(lua_State *L, int arg) error = false; call: - lua_pushvalue(L, lua_upvalueindex(1)); + lua_rawgeti(L, LUA_REGISTRYINDEX, ogref); lua_insert(L, 1); lua_call(L, lua_gettop(L)-1, LUA_MULTRET); @@ -3449,25 +3453,24 @@ int LUA_BaseLib(lua_State *L) lua_pop(L, 2); // pop metatable and dummy string // replaces an existing function with another, - // saving the original as an upvalue -#define REPLACE(name, func) \ + // saving the original as a reference +#define REPLACE(name, func, ref) \ lua_pushliteral(L, name); \ lua_rawget(L, -2); \ - lua_pushcclosure(L, &func, 1); \ + ref = luaL_ref(L, LUA_REGISTRYINDEX); \ lua_pushliteral(L, name); \ - lua_pushvalue(L, -2); \ - lua_rawset(L, -4); \ - lua_pop(L, 1); + lua_pushcclosure(L, &func, 0); \ + lua_rawset(L, -3); // replace io.open lua_getglobal(L, "io"); - REPLACE("open", lib_open) - REPLACE("write", lib_write_io) + REPLACE("open", lib_open, ref_open) + REPLACE("write", lib_write_io, ref_iowrite) lua_pop(L, 1); // replace file:write luaL_getmetatable(L, LUA_FILEHANDLE); - REPLACE("write", lib_write_f) + REPLACE("write", lib_write_f, ref_fwrite) lua_pop(L, 1); #undef REPLACE diff --git a/src/lua_consolelib.c b/src/lua_consolelib.c index ea5d0c810..b201881e8 100644 --- a/src/lua_consolelib.c +++ b/src/lua_consolelib.c @@ -329,6 +329,7 @@ static int lib_cvRegisterVar(lua_State *L) const char* category = NULL; const char* menu_name = NULL; boolean hasfunc = false; + int funcref = 0; luaL_checktype(L, 1, LUA_TTABLE); lua_settop(L, 1); // Clear out all other possible arguments, leaving only the first one. @@ -429,7 +430,9 @@ static int lib_cvRegisterVar(lua_State *L) } else if (i == 5 || (k && fasticmp(k, "func"))) { if (!lua_isfunction(L, 4)) TYPEERROR("func", LUA_TFUNCTION) - lua_setfield(L, LUA_REGISTRYINDEX, "CVFUNC"); + if (hasfunc) + luaL_unref(L, LUA_REGISTRYINDEX, funcref); // real clever, huh? + funcref = luaL_ref(L, LUA_REGISTRYINDEX); hasfunc = true; continue; } @@ -450,10 +453,11 @@ static int lib_cvRegisterVar(lua_State *L) if (hasfunc) { lua_getfield(L, LUA_REGISTRYINDEX, "CV_OnChange"); I_Assert(lua_istable(L, 3)); - lua_getfield(L, LUA_REGISTRYINDEX, "CVFUNC"); + lua_rawgeti(L, LUA_REGISTRYINDEX, funcref); lua_setfield(L, 3, cvar->name); lua_pop(L, 1); cvar->func = Lua_OnChange; + luaL_unref(L, LUA_REGISTRYINDEX, funcref); } lua_getfield(L, LUA_REGISTRYINDEX, "CV_Vars"); From 314ab9e9f02758f2acebc70643eef12d15efa8e3 Mon Sep 17 00:00:00 2001 From: GenericHeroGuy <86008856+GenericHeroGuy@users.noreply.github.com> Date: Sun, 5 Jan 2025 23:41:54 +0100 Subject: [PATCH 3/5] Fix some functions leaking doubles in INTONLY --- src/bluajit/lib_base.c | 4 ++++ src/bluajit/lib_table.c | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/src/bluajit/lib_base.c b/src/bluajit/lib_base.c index e7912c729..7ffdc3dd9 100644 --- a/src/bluajit/lib_base.c +++ b/src/bluajit/lib_base.c @@ -470,7 +470,11 @@ LJLIB_CF(collectgarbage) "\4stop\7restart\7collect\5count\1\377\4step\10setpause\12setstepmul\1\377\11isrunning"); int32_t data = lj_lib_optint(L, 2, 0); if (opt == LUA_GCCOUNT) { +#if LJ_INTONLY + setintV(L->top, G(L)->gc.total/1024); +#else setnumV(L->top, (lua_Number)G(L)->gc.total/1024.0); +#endif } else { int res = lua_gc(L, opt, data); if (opt == LUA_GCSTEP || opt == LUA_GCISRUNNING) diff --git a/src/bluajit/lib_table.c b/src/bluajit/lib_table.c index 97ba2847e..f4266b2f0 100644 --- a/src/bluajit/lib_table.c +++ b/src/bluajit/lib_table.c @@ -72,7 +72,11 @@ LJLIB_CF(table_maxn) lua_Number n = numberVnum(&node[i].key); if (n > m) m = n; } +#if LJ_INTONLY + setintV(L->top-1, lj_num2int(m)); +#else setnumV(L->top-1, m); +#endif return 1; } From b400655f3cfb926481d2dfc90bf4bf1bfdcd95cc Mon Sep 17 00:00:00 2001 From: GenericHeroGuy <86008856+GenericHeroGuy@users.noreply.github.com> Date: Mon, 6 Jan 2025 00:02:11 +0100 Subject: [PATCH 4/5] Add cleanjit target Recompiling the whole game after changing a single flag is pretty tiring --- src/Makefile | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Makefile b/src/Makefile index d343e2442..145cab616 100644 --- a/src/Makefile +++ b/src/Makefile @@ -632,6 +632,10 @@ clean: $(REMOVE) $(OBJDIR)/*.o ifndef NOBLUAJIT $(REMOVE) $(OBJDIR)/buildvm* $(OBJDIR)/minilua* $(OBJDIR)/lj_vm* $(ALL_HDRGEN) + +cleanjit: + $(REMOVE) $(LJVMCORE_O) $(ALL_GEN) + $(REMOVE) $(OBJDIR)/buildvm* $(OBJDIR)/minilua* $(OBJDIR)/lj_vm* $(ALL_HDRGEN) endif ifdef MINGW From 744dc87c165c7fa36445df8e7cfae8f1b8c5916a Mon Sep 17 00:00:00 2001 From: GenericHeroGuy <86008856+GenericHeroGuy@users.noreply.github.com> Date: Wed, 29 Jan 2025 22:16:36 +0100 Subject: [PATCH 5/5] In rehashtab, do counthash before adding asize Thanks alug! --- src/bluajit/lj_tab.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/bluajit/lj_tab.c b/src/bluajit/lj_tab.c index 869c5f03c..27b3021dd 100644 --- a/src/bluajit/lj_tab.c +++ b/src/bluajit/lj_tab.c @@ -359,8 +359,14 @@ static void rehashtab(lua_State *L, GCtab *t, cTValue *ek) uint32_t total, asize, na, i; for (i = 0; i < LJ_MAX_ABITS; i++) bins[i] = 0; asize = countarray(t, bins); +#if 0 total = 1 + asize; total += counthash(t, bins, &asize); +#else + /* more closely matches vanilla lua when abusing undefined behavior (frictionfix.....) */ + total = counthash(t, bins, &asize); + total += 1 + asize; +#endif asize += countint(ek, bins); na = bestasize(bins, &asize); total -= na;