From 708c31b6a7d589d47a1e806e839c1ca983ae3586 Mon Sep 17 00:00:00 2001 From: fesily Date: Thu, 27 Jul 2023 08:42:48 +0800 Subject: [PATCH] add dontstarve patch --- src/lib_base.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++- src/lib_init.c | 27 ++++++++++++++ src/lib_io.c | 2 +- src/lib_package.c | 26 ++++++++++++++ src/lj_api.c | 16 +++++++++ src/lj_arch.h | 37 ++++++++++++++++++++ src/lj_cparse.c | 2 ++ src/lj_ctype.h | 2 +- src/lj_def.h | 2 +- src/lj_lex.c | 6 +++- src/lj_load.c | 58 ++++++++++++++++++++++++++++-- src/lj_parse.c | 80 ++++++++++++++++++++++++++++++++++++++++++ src/lj_str.c | 9 +++++ src/lj_strfmt.c | 10 ++++++ src/lj_tab.c | 15 ++++++-- src/lj_tab.h | 4 +++ src/msvcbuild.bat | 12 +++---- 17 files changed, 381 insertions(+), 16 deletions(-) diff --git a/src/lib_base.c b/src/lib_base.c index c59d54a2..999e4c8f 100644 --- a/src/lib_base.c +++ b/src/lib_base.c @@ -222,10 +222,16 @@ LJLIB_CF(rawlen) LJLIB_REC(.) LJLIB_CF(unpack) { +#if LJ_DS_UNPACK_PATCH +#define TAB_LEN lj_tab_arraylen +#else +#define TAB_LEN lj_tab_len +#endif + GCtab *t = lj_lib_checktab(L, 1); int32_t n, i = lj_lib_optint(L, 2, 1); int32_t e = (L->base+3-1 < L->top && !tvisnil(L->base+3-1)) ? - lj_lib_checkint(L, 3) : (int32_t)lj_tab_len(t); + lj_lib_checkint(L, 3) : (int32_t)TAB_LEN(t); uint32_t nu; if (i > e) return 0; nu = (uint32_t)e - (uint32_t)i; @@ -434,8 +440,89 @@ LJLIB_CF(load) return load_aux(L, status, 4); } +#if LJ_DS_BIG_UPVAL_PATCH +void filter(lua_State* L) { + char ch, check = 0; + int level = 0; + int t = lua_type(L, 1); + int happen = 0; + int quote = 0; + int slash = 0; + const char* p = lua_tostring(L, 1); + long size = lua_objlen(L, 1); + char levelMasks[1024]; + memset(levelMasks, 0, sizeof(levelMasks)); + if (t == LUA_TSTRING) { + char* target = (char*)malloc(size * 2); + char* q = target; + while (size-- > 0) { + ch = *p++; + if (ch == '"' && !slash) { + quote = !quote; + } + + if (!quote) { + if (ch == '{') { + + level++; + if (check) { + const char* ts = "(function () return {"; + --q; + memcpy(q, ts, strlen(ts)); + q += strlen(ts); + levelMasks[level - 2] = 1; + check = 0; + happen = 1; + } + + check = 1; + *q++ = (char)ch; + } else { + *q++ = (char)ch; + if (level > 0 && ch == '}') { + level--; + + if (levelMasks[level] != 0) { + const char* ts = "end)()"; + memcpy(q, ts, strlen(ts)); + q += strlen(ts); + levelMasks[level] = 0; + } + } + check = 0; + } + } else { + if (ch == '\\') { + slash = !slash; + } else { + slash = 0; + } + *q++ = (char)ch; + check = 0; + } + } + + if (happen) { + *q = 0; + /* printf("TARGET: %s\n", target); */ +/* + FILE* tg = fopen("modified.lua", "wb"); + fwrite(target, q-target, 1, tg); + fclose(tg);*/ + lua_pushlstring(L, target, q - target); + lua_replace(L, 1); + } + + free(target); + } +} +#endif + LJLIB_CF(loadstring) { +#if LJ_DS_BIG_UPVAL_PATCH + filter(L); +#endif return lj_cf_load(L); } diff --git a/src/lib_init.c b/src/lib_init.c index 35e06fe2..26b621d4 100644 --- a/src/lib_init.c +++ b/src/lib_init.c @@ -51,5 +51,32 @@ LUALIB_API void luaL_openlibs(lua_State *L) lua_setfield(L, -2, lib->name); } lua_pop(L, 1); +#ifdef LJ_DS +#ifdef LJ_DS_STRING_DUMP_FIX + const char* dump_fix = +"local util = require 'jit.util'\n" +"local std_fns = {}\n" +"for name, mod in pairs(package.loaded) do\n" +" if type(mod) == 'table' then\n" +" for fn_name, fn in pairs(mod) do\n" +" if type(fn) == 'function' then\n" +" if pcall(util.funck, fn, 0) then\n" +" std_fns[fn] = true\n" +" end\n" +" end\n" +" end\n" +" end\n" +"end\n" +"local dump = string.dump\n" +"string.dump = function (f, strip)\n" +" if std_fns[f] then\n" +" error('unable to dump given function', 2)\n" +" end\n" +" return dump(f, strip)\n" +"end\n"; + if (luaL_loadstring(L, dump_fix) == 0) + lua_pcall(L, 0, 0, 0); +#endif +#endif } diff --git a/src/lib_io.c b/src/lib_io.c index c22faa24..71a221f7 100644 --- a/src/lib_io.c +++ b/src/lib_io.c @@ -418,7 +418,7 @@ LJLIB_CF(io_open) LJLIB_CF(io_popen) { -#if LJ_TARGET_POSIX || (LJ_TARGET_WINDOWS && !LJ_TARGET_XBOXONE && !LJ_TARGET_UWP) +#if !LJ_DS && (LJ_TARGET_POSIX || (LJ_TARGET_WINDOWS && !LJ_TARGET_XBOXONE && !LJ_TARGET_UWP)) const char *fname = strdata(lj_lib_checkstr(L, 1)); GCstr *s = lj_lib_optstr(L, 2); const char *mode = s ? strdata(s) : "r"; diff --git a/src/lib_package.c b/src/lib_package.c index 63a91211..9b61efb9 100644 --- a/src/lib_package.c +++ b/src/lib_package.c @@ -471,6 +471,32 @@ static int lj_cf_package_require(lua_State *L) lua_setfield(L, 2, name); /* _LOADED[name] = true */ } lj_lib_checkfpu(L); + + if (strcmp(name, "util") == 0) { + luaL_loadstring(L, "function table.reverse(tab) \n\ + local size = #tab \n\ + local newTable = {} \n\ + for i = 1, size - 1 do \n\ + newTable[i] = tab[size - i] \n\ + end \n\ + newTable[size] = tab[size] \n\ + return newTable \n\ + end \n\ + _loadlua = kleiloadlua \n\ + kleiloadlua = function (name, ...) \n\ + if type(name) == 'string' and name:find('fnhider') then \n\ + local f = io.open(name) \n\ + local s = f:read('*all') \n\ + f:close() \n\ + return loadstring(s) \n\ + else \n\ + return _loadlua(name, ...) \n\ + end \n\ + end \n\ + \n\ +"); + lua_call(L, 0, 0); + } return 1; } diff --git a/src/lj_api.c b/src/lj_api.c index e6b67478..d2e42828 100644 --- a/src/lj_api.c +++ b/src/lj_api.c @@ -495,6 +495,22 @@ LUALIB_API lua_Integer luaL_optinteger(lua_State *L, int idx, lua_Integer def) #endif } + +LUA_API int luaL_checkboolean (lua_State *L, int idx) { + cTValue *o = index2adr(L, idx); + if (!tvisbool(o)) + lj_err_argt(L, idx, LUA_TBOOLEAN); + return boolV(o); +} + + +LUA_API int luaL_optboolean (lua_State *L, int idx, int def) { + cTValue *o = index2adr(L, idx); + if (!tvisbool(o)) + return def; + return boolV(o); +} + LUA_API int lua_toboolean(lua_State *L, int idx) { cTValue *o = index2adr(L, idx); diff --git a/src/lj_arch.h b/src/lj_arch.h index bddd757d..3aef2a27 100644 --- a/src/lj_arch.h +++ b/src/lj_arch.h @@ -722,4 +722,41 @@ extern void *LJ_WIN_LOADLIBA(const char *path); #define LJ_SECURITY_MODESTRING \ "\004prng\007strhash\005strid\005mcode" +#ifndef LUAJIT_DISABLE_DS +#define LJ_DS 1 +#else +#define LJ_DS 0 +#endif + +#ifndef LJ_DS_STRING_HASH +#define LJ_DS_STRING_HASH LJ_DS +#endif + +#ifndef LJ_DS_STRING_DUMP_FIX +#define LJ_DS_STRING_DUMP_FIX LJ_DS +#endif + +#ifndef LJ_DS_MOD_GEMCORE_FIX +#define LJ_DS_MOD_GEMCORE_FIX LJ_DS +#endif + +#ifndef LJ_DS_BIG_UPVAL_PATCH +#define LJ_DS_BIG_UPVAL_PATCH LJ_DS +#endif + +#ifndef LJ_DS_UNPACK_PATCH +#define LJ_DS_UNPACK_PATCH LJ_DS +#endif + +#ifndef LJ_NO_SYSTEM +#define LJ_NO_SYSTEM LJ_DS +#endif + +#ifdef LJ_DS + #ifdef LUAJIT_SECURITY_STRHASH + #undef LUAJIT_SECURITY_STRHASH + #endif +#define LUAJIT_SECURITY_STRHASH !LJ_DS +#endif + #endif diff --git a/src/lj_cparse.c b/src/lj_cparse.c index 126032d5..1b620bad 100644 --- a/src/lj_cparse.c +++ b/src/lj_cparse.c @@ -263,6 +263,8 @@ static CPToken cp_string(CPState *cp) c = c*8 + (cp->c - '0'); cp_get(cp); } + } else { + c = '\\'; } cp_save(cp, (c & 0xff)); continue; diff --git a/src/lj_ctype.h b/src/lj_ctype.h index 3dbcdbfb..634c6188 100644 --- a/src/lj_ctype.h +++ b/src/lj_ctype.h @@ -310,7 +310,7 @@ enum { CTTYDEF(CTTYIDDEF) #undef CTTYIDDEF /* Predefined typedefs and keywords follow. */ - CTID_MAX = 65536 + CTID_MAX = 0x7fffff }; /* Target-dependent type IDs. */ diff --git a/src/lj_def.h b/src/lj_def.h index b61297aa..9df85a57 100644 --- a/src/lj_def.h +++ b/src/lj_def.h @@ -66,7 +66,7 @@ typedef unsigned int uintptr_t; #define LJ_MAX_BCINS (1<<26) /* Max. # of bytecode instructions. */ #define LJ_MAX_SLOTS 250 /* Max. # of slots in a Lua func. */ #define LJ_MAX_LOCVAR 200 /* Max. # of local variables. */ -#define LJ_MAX_UPVAL 60 /* Max. # of upvalues. */ +#define LJ_MAX_UPVAL 160 /* Max. # of upvalues. */ #define LJ_MAX_IDXCHAIN 100 /* __index/__newindex chain limit. */ #define LJ_STACK_EXTRA (5+2*LJ_FR2) /* Extra stack space (metamethods). */ diff --git a/src/lj_lex.c b/src/lj_lex.c index 463a87ce..071dcb65 100644 --- a/src/lj_lex.c +++ b/src/lj_lex.c @@ -206,6 +206,7 @@ static void lex_string(LexState *ls, TValue *tv) case 'r': c = '\r'; break; case 't': c = '\t'; break; case 'v': c = '\v'; break; +#if 0 case 'x': /* Hexadecimal escape '\xXX'. */ c = (lex_next(ls) & 15u) << 4; if (!lj_char_isdigit(ls->c)) { @@ -250,12 +251,15 @@ static void lex_string(LexState *ls, TValue *tv) while (lj_char_isspace(ls->c)) if (lex_iseol(ls)) lex_newline(ls); else lex_next(ls); continue; +#endif case '\n': case '\r': lex_save(ls, '\n'); lex_newline(ls); continue; case '\\': case '\"': case '\'': break; case LEX_EOF: continue; default: - if (!lj_char_isdigit(c)) + if (!lj_char_isdigit(c)) { + break; goto err_xesc; + } c -= '0'; /* Decimal escape '\ddd'. */ if (lj_char_isdigit(lex_next(ls))) { c = c*10 + (ls->c - '0'); diff --git a/src/lj_load.c b/src/lj_load.c index 0aab4884..32e163c4 100644 --- a/src/lj_load.c +++ b/src/lj_load.c @@ -134,13 +134,65 @@ static const char *reader_string(lua_State *L, void *ud, size_t *size) return ctx->str; } +char* hack_gemcore(const char* base, size_t size) +{ + char* target; + char* t = base; + char* s = NULL, *p = NULL, *q = NULL; + if (strstr(base, "return _debug_getinfo") == NULL) return NULL; + + target = (char*)malloc(size * 2 + 32); + memset(target, 0, size * 2 + 32); + q = target; + + while ( + ((p = strstr(t, "return _debug_")) != NULL) || + ((p = strstr(t, "return _getfenv")) != NULL) || + ((p = strstr(t, "return _setfenv")) != NULL) + ) { + memcpy(q, t, p - t); + q += p - t; + if (memcmp(p, "return _debug_getupvalue", 24) == 0) { + memcpy(q, p, 24); + t = p + 24; + q += 24; + continue; + } + + s = strstr(p, "end"); + if (s != NULL) { + memcpy(q, p, s - p); q += s - p; + memcpy(q, ", nil end", 9); q += 9; + t = s + 3; + } else { + break; + } + } + + if (s != NULL) + { + // FILE* fp = fopen("hahaha.txt", "wb"); + memcpy(q, t, base + size - t); + // fwrite(target, 1, (q - target) + (base - t) + size, fp); + // fclose(fp); + return target; + } + + free(target); + return NULL; +} + LUALIB_API int luaL_loadbufferx(lua_State *L, const char *buf, size_t size, const char *name, const char *mode) { StringReaderCtx ctx; - ctx.str = buf; - ctx.size = size; - return lua_loadx(L, reader_string, &ctx, name, mode); + int ret; + char* target = hack_gemcore(buf, size); + ctx.str = target == NULL ? buf : target; + ctx.size = target == NULL ? size : strlen(target); + ret = lua_loadx(L, reader_string, &ctx, name, mode); + if (target != NULL) free(target); + return ret; } LUALIB_API int luaL_loadbuffer(lua_State *L, const char *buf, size_t size, diff --git a/src/lj_parse.c b/src/lj_parse.c index 64ae2c16..fd3688df 100644 --- a/src/lj_parse.c +++ b/src/lj_parse.c @@ -2696,10 +2696,90 @@ static int parse_stmt(LexState *ls) } /* A chunk is a list of statements optionally separated by semicolons. */ + +static void add_argstmt(LexState* ls) +{ + ExpDesc e; + + if (ls->fs->flags & PROTO_VARARG) { + var_new_lit(ls, 0, "arg"); +// nexps = expr_list(ls, &e); + { + synlevel_begin(ls); + // expr_unop(ls, &e); + { + // expr_simple(ls, v); + { + // expr_table(ls, v); + { + ExpDesc key, val; + FuncState *fs = ls->fs; + BCLine line = ls->linenumber; + BCInsLine *ilp; + BCIns *ip; + ExpDesc en; + BCReg base; + + GCtab *t = NULL; + int vcall = 0, needarr = 0, fixt = 0; + uint32_t narr = 1; /* First array index. */ + uint32_t nhash = 0; /* Number of hash entries. */ + BCReg freg = fs->freereg; + BCPos pc = bcemit_AD(fs, BC_TNEW, freg, 0); + expr_init(&e, VNONRELOC, freg); + bcreg_reserve(fs, 1); + freg++; + + vcall = 0; + expr_init(&key, VKNUM, 0); + setintV(&key.u.nval, (int)narr); + narr++; + needarr = vcall = 1; + + // expr(ls, &val); + { + checkcond(ls, fs->flags & PROTO_VARARG, LJ_ERR_XDOTS); + bcreg_reserve(fs, 1); + base = fs->freereg-1; + expr_init(&val, VCALL, bcemit_ABC(fs, BC_VARG, base, 2, fs->numparams)); + val.u.s.aux = base; + } + + if (expr_isk(&key)) expr_index(fs, &e, &key); + bcemit_store(fs, &e, &val); + fs->freereg = freg; + + ilp = &fs->bcbase[fs->pc-1]; + expr_init(&en, VKNUM, 0); + en.u.nval.u32.lo = narr-1; + en.u.nval.u32.hi = 0x43300000; /* Biased integer to avoid denormals. */ + if (narr > 256) { fs->pc--; ilp--; } + ilp->ins = BCINS_AD(BC_TSETM, freg, const_num(fs, &en)); + setbc_b(&ilp[-1].ins, 0); + + e.k = VNONRELOC; /* May have been changed by expr_index. */ + + + ip = &fs->bcbase[pc].ins; + if (!needarr) narr = 0; + else if (narr < 3) narr = 3; + else if (narr > 0x7ff) narr = 0x7ff; + setbc_d(ip, narr|(hsize2hbits(nhash)<<11)); + } + } + } + synlevel_end(ls); + } + assign_adjust(ls, 1, 1, &e); + var_add(ls, 1); + } +} + static void parse_chunk(LexState *ls) { int islast = 0; synlevel_begin(ls); + add_argstmt(ls); while (!islast && !parse_isend(ls->tok)) { islast = parse_stmt(ls); lex_opt(ls, ';'); diff --git a/src/lj_str.c b/src/lj_str.c index a5282da6..dbf555d5 100644 --- a/src/lj_str.c +++ b/src/lj_str.c @@ -75,6 +75,7 @@ int lj_str_haspattern(GCstr *s) /* Keyed sparse ARX string hash. Constant time. */ static StrHash hash_sparse(uint64_t seed, const char *str, MSize len) { +#if 0 /* Constants taken from lookup3 hash by Bob Jenkins. */ StrHash a, b, h = len ^ (StrHash)seed; if (len >= 4) { /* Caveat: unaligned access! */ @@ -92,6 +93,14 @@ static StrHash hash_sparse(uint64_t seed, const char *str, MSize len) a ^= h; a -= lj_rol(h, 11); b ^= a; b -= lj_rol(a, 25); h ^= b; h -= lj_rol(b, 16); +#else + const MSize l = len; + StrHash h = (unsigned int)l; /* seed */ + size_t step = (l>>5)+1; /* if string is too long, don't hash all its chars */ + + for (size_t l1=l; l1>=step; l1-=step) /* compute hash */ + h = h ^ ((h<<5)+(h>>2)+(unsigned char)str[l1-1]); +#endif return h; } diff --git a/src/lj_strfmt.c b/src/lj_strfmt.c index 71ee9f62..2c1689b5 100644 --- a/src/lj_strfmt.c +++ b/src/lj_strfmt.c @@ -79,6 +79,16 @@ SFormat LJ_FASTCALL lj_strfmt_parse(FormatState *fs) return (sf | sx | ((c & 0x20) ? 0 : STRFMT_F_UPPER)); } } + +c = 's'-'A'; +{ +uint32_t sx = strfmt_map[c]; + if (sx) { + fs->p = p+1; + return (sf | sx | ((c & 0x20) ? 0 : STRFMT_F_UPPER)); + } +} + /* Return error location. */ if (*p >= 32) p++; fs->len = (MSize)(p - (const uint8_t *)fs->str); diff --git a/src/lj_tab.c b/src/lj_tab.c index c3609b38..1019a745 100644 --- a/src/lj_tab.c +++ b/src/lj_tab.c @@ -279,8 +279,8 @@ void lj_tab_resize(lua_State *L, GCtab *t, uint32_t asize, uint32_t hbits) } if (oldhmask > 0) { /* Reinsert pairs from old hash part. */ global_State *g; - uint32_t i; - for (i = 0; i <= oldhmask; i++) { + int i; + for (i = oldhmask; i >= 0; i--) { Node *n = &oldnode[i]; if (!tvisnil(&n->val)) copyTV(L, lj_tab_set(L, t, &n->key), &n->val); @@ -691,3 +691,14 @@ MSize LJ_FASTCALL lj_tab_len_hint(GCtab *t, size_t hint) } #endif +#if LJ_DS_UNPACK_PATCH +MSize LJ_FASTCALL lj_tab_arraylen(GCtab *t) +{ + MSize j = (MSize)t->asize; + while (j > 1 && tvisnil(arrayslot(t, j - 1))) { + j--; + } + if (j) --j; + return j; +} +#endif diff --git a/src/lj_tab.h b/src/lj_tab.h index 2a3f76bf..95624ac4 100644 --- a/src/lj_tab.h +++ b/src/lj_tab.h @@ -93,4 +93,8 @@ LJ_FUNCA MSize LJ_FASTCALL lj_tab_len(GCtab *t); LJ_FUNC MSize LJ_FASTCALL lj_tab_len_hint(GCtab *t, size_t hint); #endif +#if LJ_DS_UNPACK_PATCH +LJ_FUNCA MSize LJ_FASTCALL lj_tab_arraylen(GCtab *t); +#endif + #endif diff --git a/src/msvcbuild.bat b/src/msvcbuild.bat index d323d8d4..42d06d47 100644 --- a/src/msvcbuild.bat +++ b/src/msvcbuild.bat @@ -14,7 +14,7 @@ @setlocal @rem Add more debug flags here, e.g. DEBUGCFLAGS=/DLUA_USE_APICHECK -@set DEBUGCFLAGS= +@set DEBUGCFLAGS= /DLUA_USE_APICHECK /DLUA_USE_ASSERT @set LJCOMPILE=cl /nologo /c /O2 /W3 /D_CRT_SECURE_NO_DEPRECATE /D_CRT_STDIO_INLINE=__declspec(dllexport)__inline @set LJLINK=link /nologo @set LJMT=mt /nologo @@ -22,8 +22,8 @@ @set DASMDIR=..\dynasm @set DASM=%DASMDIR%\dynasm.lua @set DASC=vm_x64.dasc -@set LJDLLNAME=lua51.dll -@set LJLIBNAME=lua51.lib +@set LJDLLNAME=lua51DS.dll +@set LJLIBNAME=lua51DS.lib @set BUILDTYPE=release @set ALL_LIB=lib_base.c lib_math.c lib_bit.c lib_string.c lib_table.c lib_io.c lib_os.c lib_package.c lib_debug.c lib_jit.c lib_ffi.c lib_buffer.c @@ -76,7 +76,7 @@ buildvm -m folddef -o lj_folddef.h lj_opt_fold.c @if "%1" neq "debug" goto :NODEBUG @shift @set BUILDTYPE=debug -@set LJCOMPILE=%LJCOMPILE% /Zi %DEBUGCFLAGS% +@set LJCOMPILE=%LJCOMPILE% /Od /Zi %DEBUGCFLAGS% @set LJLINK=%LJLINK% /opt:ref /opt:icf /incremental:no :NODEBUG @set LJLINK=%LJLINK% /%BUILDTYPE% @@ -113,14 +113,14 @@ if exist luajit.exe.manifest^ @del host\buildvm_arch.h @del lj_bcdef.h lj_ffdef.h lj_libdef.h lj_recdef.h lj_folddef.h @echo. -@echo === Successfully built LuaJIT for Windows/%LJARCH% === +@echo === Successfully built LuaJIT for Windows/%LJARCH%[%BUILDTYPE%] === @goto :END :BAD @echo. @echo ******************************************************* @echo *** Build FAILED -- Please check the error messages *** -@echo ******************************************************* +@echo ******************************************************* @goto :END :FAIL @echo You must open a "Visual Studio Command Prompt" to run this script