ldump.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. ** $Id: ldump.c,v 2.37.1.1 2017/04/19 17:20:42 roberto Exp $
  3. ** save precompiled Lua chunks
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define ldump_c
  7. #define LUA_CORE
  8. #include "lprefix.h"
  9. #include <stddef.h>
  10. #include "lua.h"
  11. #include "lapi.h"
  12. #include "lauxlib.h"
  13. #include "llex.h"
  14. #include "lgc.h"
  15. #include "lmem.h"
  16. #include "lobject.h"
  17. #include "lstate.h"
  18. #include "lstring.h"
  19. #include "ltable.h"
  20. #include "lundump.h"
  21. typedef struct {
  22. lua_State *L;
  23. lua_Writer writer;
  24. void *data;
  25. int strip;
  26. int status;
  27. #ifdef LUA_USE_HOST
  28. int useStrRefs;
  29. Table *stringIndex;
  30. int sTScnt;
  31. int lTScnt;
  32. int nFixed;
  33. #endif
  34. } DumpState;
  35. /*
  36. ** To ensure that dump files are loadable onto the ESP architectures:
  37. ** 1. Integers are in the range -2^31 .. 2^31-1 (sint32_t)
  38. ** 2. Floats are the IEEE 4 or 8 byte format, with a 4 byte default.
  39. **
  40. ** The file formats are also different to standard because of two add
  41. ** additional goals:
  42. ** 3. The file must be serially loadable into a programmable flash
  43. ** memory through a file-write like API call.
  44. ** 4. Compactness of dump files is a key design goal.
  45. */
  46. #define DumpVector(v,n,D) DumpBlock(v,(n)*sizeof((v)[0]),D)
  47. #define DumpLiteral(s,D) DumpBlock(s, sizeof(s) - sizeof(char), D)
  48. static void DumpBlock (const void *b, size_t size, DumpState *D) {
  49. if (D->status == 0 && size > 0) {
  50. lua_unlock(D->L);
  51. D->status = (*D->writer)(D->L, b, size, D->data);
  52. lua_lock(D->L);
  53. }
  54. }
  55. #define DumpVar(x,D) DumpVector(&x,1,D)
  56. static void DumpByte (lu_byte x, DumpState *D) {
  57. DumpVar(x, D);
  58. }
  59. /*
  60. ** Dump (unsigned) int 0..MAXINT using multibyte encoding (MBE). DumpInt
  61. ** is used for context dependent counts and sizes; no type information
  62. ** is embedded.
  63. */
  64. static void DumpInt (lua_Integer x, DumpState *D) {
  65. lu_byte buf[sizeof(lua_Integer) + 2];
  66. lu_byte *b = buf + sizeof(buf) - 1;
  67. lua_assert(x>=0);
  68. *b-- = x & 0x7f; x >>= 7;
  69. while(x) { *b-- = 0x80 + (x & 0x7f); x >>= 7; }
  70. b++;
  71. lua_assert (b >= buf);
  72. DumpVector(b, (buf - b) + sizeof(buf), D);
  73. }
  74. static void DumpNumber (lua_Number x, DumpState *D) {
  75. DumpByte(LUAU_TNUMFLT, D);
  76. DumpVar(x, D);
  77. }
  78. /*
  79. ** DumpIntTT is MBE and embeds a type encoding for string length and integers.
  80. ** It also handles negative integers by forcing the type to LUAU_TNUMNINT.
  81. ** 0TTTNNNN or 1TTTNNNN (1NNNNNNN)* 0NNNNNNN
  82. */
  83. static void DumpIntTT (lu_byte tt, lua_Integer y, DumpState *D) {
  84. lua_Integer x = y < 0 ? -(y + 1) : y;
  85. lu_byte buf[sizeof(lua_Integer) + 3];
  86. lu_byte *b = buf + sizeof(buf) - 1;
  87. *b-- = x & 0x7f; x >>= 7;
  88. while(x) { *b-- = 0x80 + (x & 0x7f); x >>= 7; }
  89. b++;
  90. if (*b & cast(lu_byte, LUAU_TMASK) )/* Need an extra byte for the type bits? */
  91. *--b = 0x80;
  92. *b |= (y >= 0) ? tt: LUAU_TNUMNINT;
  93. lua_assert (b >= buf);
  94. DumpVector(b, (buf - b) + sizeof(buf), D);
  95. }
  96. #define DumpInteger(i, D) DumpIntTT(LUAU_TNUMPINT, i, D);
  97. /*
  98. ** Strings are stored in LFS uniquely, any string references use this index.
  99. ** The table at D->stringIndex is used to lookup this unique index.
  100. */
  101. static void DumpString (const TString *s, DumpState *D) {
  102. if (s == NULL) {
  103. DumpByte(LUAU_TSSTRING + 0, D);
  104. } else {
  105. lu_byte tt = (gettt(s) == LUA_TSHRSTR) ? LUAU_TSSTRING : LUAU_TLSTRING;
  106. size_t l = tsslen(s);
  107. const char *str = getstr(s);
  108. #ifdef LUA_USE_HOST
  109. if (D->useStrRefs) {
  110. const TValue *o = luaH_getstr(D->stringIndex, cast(TString *,s));
  111. DumpIntTT(tt, ivalue(o), D);
  112. return;
  113. }
  114. #endif
  115. DumpIntTT(tt, l + 1, D); /* include trailing '\0' */
  116. DumpVector(str, l, D); /* no need to save '\0' */
  117. }
  118. }
  119. static void DumpCode (const Proto *f, DumpState *D) {
  120. DumpInt(f->sizecode, D);
  121. DumpVector(f->code, f->sizecode, D);
  122. }
  123. static void DumpFunction(const Proto *f, TString *psource, DumpState *D);
  124. static void DumpConstants (const Proto *f, DumpState *D) {
  125. int i;
  126. int n = f->sizek;
  127. DumpInt(n, D);
  128. for (i = 0; i < n; i++) {
  129. const TValue *o = &f->k[i];
  130. switch (ttype(o)) {
  131. case LUA_TNIL:
  132. DumpByte(LUAU_TNIL, D);
  133. break;
  134. case LUA_TBOOLEAN:
  135. DumpByte(LUAU_TBOOLEAN + bvalue(o), D);
  136. break;
  137. case LUA_TNUMFLT :
  138. DumpNumber(fltvalue(o), D);
  139. break;
  140. case LUA_TNUMINT:
  141. DumpInteger(ivalue(o), D);
  142. break;
  143. case LUA_TSHRSTR:
  144. case LUA_TLNGSTR:
  145. DumpString(tsvalue(o), D);
  146. break;
  147. default:
  148. lua_assert(0);
  149. }
  150. }
  151. }
  152. static void DumpProtos (const Proto *f, DumpState *D) {
  153. int i;
  154. int n = f->sizep;
  155. DumpInt(n, D);
  156. for (i = 0; i < n; i++)
  157. DumpFunction(f->p[i], f->source, D);
  158. }
  159. static void DumpUpvalues (const Proto *f, DumpState *D) {
  160. int i, n = f->sizeupvalues, nostrip = (D->strip == 0);
  161. DumpByte(nostrip, D);
  162. DumpInt(n, D);
  163. for (i = 0; i < n; i++) {
  164. if (nostrip)
  165. DumpString(f->upvalues[i].name, D);
  166. DumpByte(f->upvalues[i].instack, D);
  167. DumpByte(f->upvalues[i].idx, D);
  168. }
  169. }
  170. static void DumpDebug (const Proto *f, DumpState *D) {
  171. int i, keepli = (D->strip <= 1), keeplv = (D->strip == 0);
  172. int n = keepli ? f->sizelineinfo : 0;
  173. DumpInt(n, D);
  174. DumpVector(f->lineinfo, n, D);
  175. n = keeplv ? f->sizelocvars : 0;
  176. DumpInt(n, D);
  177. for (i = 0; i < n; i++) {
  178. DumpString(f->locvars[i].varname, D);
  179. DumpInt(f->locvars[i].startpc, D);
  180. DumpInt(f->locvars[i].endpc, D);
  181. }
  182. }
  183. static void DumpFunction (const Proto *f, TString *psource, DumpState *D) {
  184. if (f->source == psource)
  185. DumpString(NULL, D); /* same source as its parent */
  186. else
  187. DumpString(f->source, D);
  188. DumpInt(f->linedefined, D);
  189. DumpInt(f->lastlinedefined, D);
  190. DumpByte(getnumparams(f), D);
  191. DumpByte(getis_vararg(f), D);
  192. DumpByte(getmaxstacksize(f), D);
  193. DumpProtos(f, D);
  194. DumpCode(f, D);
  195. DumpConstants(f, D);
  196. DumpUpvalues(f, D);
  197. DumpDebug(f, D);
  198. }
  199. static void DumpHeader (DumpState *D, int format) {
  200. DumpLiteral(LUA_SIGNATURE, D);
  201. DumpByte(LUAC_VERSION, D);
  202. DumpByte(format, D);
  203. DumpLiteral(LUAC_DATA, D);
  204. DumpByte(sizeof(int), D);
  205. DumpByte(sizeof(Instruction), D);
  206. DumpByte(sizeof(lua_Integer), D);
  207. DumpByte(sizeof(lua_Number), D);
  208. /* Note that we multi-byte encoded integers so need to check size_t or endian */
  209. DumpNumber(LUAC_NUM, D);
  210. }
  211. /*
  212. ** Dump Lua function as precompiled chunk
  213. */
  214. int luaU_dump (lua_State *L, const Proto *f, lua_Writer w, void *data,
  215. int strip) {
  216. DumpState D = {0};
  217. D.L = L;
  218. D.writer = w;
  219. D.data = data;
  220. D.strip = strip;
  221. DumpHeader(&D, LUAC_FORMAT);
  222. DumpByte(f->sizeupvalues, &D);
  223. DumpFunction(f, NULL, &D);
  224. return D.status;
  225. }
  226. static int stripdebug (lua_State *L, Proto *f, int level) {
  227. int i, len = 0;
  228. switch (level) {
  229. case 2:
  230. if (f->lineinfo) {
  231. f->lineinfo = luaM_freearray(L, f->lineinfo, f->sizelineinfo);
  232. len += f->sizelineinfo;
  233. }
  234. case 1:
  235. for (i=0; i<f->sizeupvalues; i++)
  236. f->upvalues[i].name = NULL;
  237. f->locvars = luaM_freearray(L, f->locvars, f->sizelocvars);
  238. len += f->sizelocvars * sizeof(LocVar);
  239. f->sizelocvars = 0;
  240. }
  241. return len;
  242. }
  243. /* This is a recursive function so it's stack size has been kept to a minimum! */
  244. int luaU_stripdebug (lua_State *L, Proto *f, int level, int recv){
  245. int len = 0, i;
  246. if (recv != 0 && f->sizep != 0) {
  247. for(i=0;i<f->sizep;i++) len += luaU_stripdebug(L, f->p[i], level, recv);
  248. }
  249. len += stripdebug (L, f, level);
  250. return len;
  251. }
  252. /*============================================================================**
  253. **
  254. ** NodeMCU extensions for LFS support and dumping. Note that to keep lua_lock
  255. ** pairing for testing, this dump/unload functionality works within a locked
  256. ** window and therefore has to use the core luaH, ..., APIs rather than the
  257. ** public Lua and lauxlib APIs.
  258. **
  259. **============================================================================*/
  260. #ifdef LUA_USE_HOST
  261. /*
  262. ** Add a TS found in the Proto Load to the table at the ToS. Note that this is
  263. ** a unified table of {string = index} for both short and long TStrings.
  264. */
  265. static void addTS (TString *ts, DumpState *D) {
  266. lua_State *L = D->L;
  267. if (!ts)
  268. return;
  269. if (ttisnil(luaH_getstr(D->stringIndex, ts))) {
  270. TValue k, v, *slot;
  271. gettt(ts)<=LUA_TSHRSTR ? D->sTScnt++ : D->lTScnt++;
  272. setsvalue(L, &k, ts);
  273. setivalue(&v, D->sTScnt + D->lTScnt);
  274. slot = luaH_set(L, D->stringIndex, &k);
  275. setobj2t(L, slot, &v);
  276. luaC_barrierback(L, D->stringIndex, &v);
  277. }
  278. }
  279. /*
  280. ** Add the fixed TS that are created by the luaX and LuaT initialisation
  281. ** and fixed so not collectable. This are always loaded into LFS to save
  282. ** RAM and can be implicitly referenced in any Proto.
  283. */
  284. static void addFixedStrings (DumpState *D) {
  285. int i;
  286. const char *p;
  287. for (i = 0; (p = luaX_getstr(i, 0))!=NULL; i++)
  288. addTS(luaS_new(D->L, p), D);
  289. addTS(G(D->L)->memerrmsg, D);
  290. addTS(luaS_new(D->L, LUA_ENV), D);
  291. for (i = 0; (p = luaT_getstr(i))!=NULL; i++)
  292. addTS(luaS_new(D->L, p), D);
  293. lua_assert(D->lTScnt == 0); /* all of these fixed strings should be short */
  294. D->nFixed = D->sTScnt; /* book mark for later skipping */
  295. }
  296. /*
  297. ** Dump all LFS strings. If there are 71 fixed and 17 LFS strings, say, in
  298. ** the stringIndex, then these fixed and LFS strings are numbered 1..71 and
  299. ** 72..88 respectively; this numbering is swapped to 18..88 and 1..17. The
  300. ** fixed strings are fixed and can be omitted from the LFS image.
  301. */
  302. static void DumpLFSstrings(DumpState *D) {
  303. lua_State *L = D->L;
  304. int n = D->sTScnt + D->lTScnt;
  305. int i, maxlen = 0, nStrings = n - D->nFixed;
  306. Table *revT = luaH_new(L);
  307. sethvalue(L, L->top++, revT); /* Put on stack to prevent GC */
  308. luaH_resize(L, revT, n, 0);
  309. luaC_checkGC(L);
  310. /* luaH_next scan of stringIndex table using two top of stack entries */
  311. setnilvalue(L->top++);
  312. api_incr_top(L);
  313. while (luaH_next(L, D->stringIndex, L->top-2)) {
  314. /*
  315. * Update the value to swap fix and LFS order, then insert (v, k) into
  316. * the reverse index table. Note that luaC_barrier checks not required
  317. * for overwrites and non-collectable values.
  318. */
  319. int len = tsslen(tsvalue(L->top-2));
  320. lua_Integer *i = &L->top[-1].value_.i;
  321. *i += *i > D->nFixed ? -D->nFixed : nStrings; /* recalc index and */
  322. luaH_set(L, D->stringIndex, L->top-2)->value_.i = *i; /* update table value */
  323. luaH_setint(L, revT, ivalue(L->top-1), L->top-2); /* Add str to reverse T */
  324. if (len > maxlen) maxlen = len; /* roll up maximum string size */
  325. }
  326. L->top -= 2; /* discard key and value stack slots */
  327. DumpInt(maxlen, D);
  328. DumpInt(D->sTScnt, D);
  329. DumpInt(D->lTScnt, D);
  330. DumpInt(nStrings, D);
  331. for (i = 1; i <= nStrings; i++) { /* dump out non-fixed strings in order */
  332. const TValue *o = luaH_getint(revT, i);
  333. DumpString(tsvalue(o), D);
  334. }
  335. L->top--; /* pop revT stack entry */
  336. luaC_checkGC(L);
  337. }
  338. /*
  339. ** Recursive scan all of the Protos in the Proto hierarchy
  340. ** to collect all referenced strings in 2 Lua Arrays at ToS.
  341. */
  342. #define OVER(n) for (i = 0; i < (n); i++)
  343. static void scanProtoStrings(const Proto *f, DumpState *D) {
  344. int i;
  345. addTS(f->source, D);
  346. OVER(f->sizek) if (ttisstring(f->k + i))
  347. addTS(tsvalue(f->k + i), D);
  348. OVER(f->sizeupvalues) addTS(f->upvalues[i].name, D);
  349. OVER(f->sizelocvars) addTS(f->locvars[i].varname, D);
  350. OVER(f->sizep) scanProtoStrings(f->p[i], D);
  351. }
  352. /*
  353. ** An LFS image comprises a prologue segment of all of the strings used in
  354. ** the image, followed by a set of Proto dumps. Each of these is essentially
  355. ** the same as standard lua_dump format, except that string constants don't
  356. ** contain the string inline, but are instead an index into the prologue.
  357. ** Separating out the strings in this way simplifies loading the image
  358. ** content into an LFS region.
  359. **
  360. ** A dummy container Proto, main, is used to hold all of the Protos to go
  361. ** into the image. The Proto main itself is not callable; it is used as the
  362. ** image Proto index and only contains a Proto vector and a constant vector
  363. ** where each constant in the string names the corresponding Proto.
  364. */
  365. int luaU_DumpAllProtos(lua_State *L, const Proto *m, lua_Writer w,
  366. void *data, int strip) {
  367. DumpState D = {0};
  368. D.L = L;
  369. D.writer = w;
  370. D.data = data;
  371. D.strip = strip;
  372. lua_assert(L->stack_last - L->top > 5); /* This dump uses 5 stack slots */
  373. DumpHeader(&D, LUAC_LFS_IMAGE_FORMAT);
  374. DumpInteger(G(L)->seed, &D);
  375. D.stringIndex = luaH_new(L);
  376. sethvalue(L, L->top++, D.stringIndex); /* Put on stack to prevent GC */
  377. /* Add fixed strings + strings used in the Protos, then swap fixed/added blocks */
  378. addFixedStrings(&D);
  379. scanProtoStrings(m, &D);
  380. /* Dump out all non-fixed strings */
  381. DumpLiteral(LUA_STRING_SIG, &D);
  382. DumpLFSstrings(&D);
  383. /* Switch to string reference mode and add the Protos themselves */
  384. D.useStrRefs = 1;
  385. DumpLiteral(LUA_PROTO_SIG, &D);
  386. DumpProtos(m, &D);
  387. DumpConstants(m, &D); /* Dump Function name vector */
  388. L->top--;
  389. return D.status;
  390. }
  391. #endif