ldump.c 12 KB

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