lundump.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. /*---
  2. ** $Id: lundump.c,v 2.44.1.1 2017/04/19 17:20:42 roberto Exp $
  3. ** load precompiled Lua chunks
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define lundump_c
  7. #define LUA_CORE
  8. #include "lprefix.h"
  9. #include <string.h>
  10. #include "lua.h"
  11. #include "ldebug.h"
  12. #include "ldo.h"
  13. #include "lfunc.h"
  14. #include "llex.h"
  15. #include "lmem.h"
  16. #include "lnodemcu.h"
  17. #include "lobject.h"
  18. #include "lstring.h"
  19. #include "lundump.h"
  20. #include "lzio.h"
  21. /*
  22. ** Unlike the standard Lua version of lundump.c, this NodeMCU version must be
  23. ** able to store the dumped Protos into one of two targets:
  24. **
  25. ** (A) RAM-based heap. This in the same way as standard Lua, where the
  26. ** Proto data structures can be created by direct in memory addressing,
  27. ** with any references complying with Lua GC assumptions, so that all
  28. ** storage can be collected in the case of a thrown error.
  29. **
  30. ** (B) Flash programmable ROM memory. This can only be written to serially,
  31. ** using a write API, it can be subsequently but accessed and directly
  32. ** addressable through a memory-mapped address window after cache flush.
  33. **
  34. ** Mode (B) also know as LFS (Lua FLash Store) enables running Lua apps
  35. ** on small-memory IoT devices which support programmable flash storage such
  36. ** as the ESP8266 SoC. In the case of this chip, the usable RAM heap is
  37. ** roughly 45Kb, so the ability to store an extra 128Kb, say, of program into
  38. ** LFS can materially increase the size of application that can be executed
  39. ** and leave most of the heap for true R/W application data.
  40. **
  41. ** The changes to this source file enable the addition of LFS mode. In mode B,
  42. ** the resources aren't allocated in RAM but are written to Flash using the
  43. ** write API which returns the corresponding Flash read address is returned;
  44. ** also data can't be immediately read back using these addresses because of
  45. ** cache staleness.
  46. **
  47. ** Handling the Proto record has been reordered to avoid interleaved resource
  48. ** writes in mode (B), with the f->k being cached in RAM and the Proto
  49. ** hierarchies walked bottom-up in a way that still maintains GC compliance
  50. ** conformance for mode (A). This no-interleave constraint also complicates
  51. ** the writing of TString resources into flash, so the flashing process
  52. ** circumvents this issue for LFS loads by header by taking two passes to dump
  53. ** the hierarchy. The first dumps all strings that needed to load the Protos,
  54. ** with the subsequent Proto loads use an index to any TString references.
  55. ** This enables all strings to be loaded into an LFS-based ROstrt before
  56. ** starting to load the Protos.
  57. **
  58. ** Note that this module and ldump.c are compiled into both the ESP firmware
  59. ** and a host-based luac cross compiler. LFS dump is currently only supported
  60. ** in the compiler, but both the LFS and standard loads are supported in both
  61. ** the target (lua.c) and the host (luac.cross -e)environments. Both
  62. ** environments are built with the same integer and float formats (e.g. 32 bit,
  63. ** 32-bit IEEE).
  64. **
  65. ** Dumps can either be loaded into RAM or LFS depending on the load format. An
  66. ** extra complication is that luac.cross supports two LFS modes, with the
  67. ** first loading into an host process address space and using host 32 or 64
  68. ** bit address references. The second uses shadow ESP 32 bit addresses to
  69. ** create an absolute binary image for direct provisioning of ESP images.
  70. */
  71. #define MODE_RAM 0 /* Loading into RAM */
  72. #define MODE_LFS 1 /* Loading into a locally executable LFS */
  73. #define MODE_LFSA 2 /* (Host only) Loading into a shadow ESP image */
  74. typedef struct {
  75. lua_State *L; /* cache L to drop parameter list */
  76. ZIO *Z; /* ZIO context */
  77. const char *name; /* Filename of the LFS image being loaded */
  78. LFSHeader *fh; /* LFS flash header block */
  79. void *startLFS; /* Start address of LFS region */
  80. TString **TS; /* List of TStrings being used in the image */
  81. lu_int32 TSlen; /* Length of the same */
  82. lu_int32 TSndx; /* Index into the same */
  83. lu_int32 TSnFixed; /* Number of "fixed" TS */
  84. char *buff; /* Working buffer for assembling a TString */
  85. lu_int32 buffLen; /* Maximum length of TS used in the image */
  86. TString **list; /* TS list used to index the ROstrt */
  87. lu_int32 listLen; /* Length of the same */
  88. Proto **pv; /* List of Protos in LFS */
  89. lu_int32 pvLen; /* Length of the same */
  90. GCObject *protogc; /* LFS proto linked list */
  91. lu_byte useStrRefs; /* Flag if set then TStings are a index into TS */
  92. lu_byte mode; /* Either LFS or RAM */
  93. } LoadState;
  94. static l_noret error(LoadState *S, const char *why) {
  95. luaO_pushfstring(S->L, "%s: %s precompiled chunk", S->name, why);
  96. luaD_throw(S->L, LUA_ERRSYNTAX);
  97. }
  98. #define wordptr(p) cast(lu_int32 *, p)
  99. #define byteptr(p) cast(lu_byte *, p)
  100. #define wordoffset(p,q) (wordptr(p) - wordptr(q))
  101. #define FHaddr(S,t,f) cast(t, wordptr(S->startLFS) + (f))
  102. #define FHoffset(S,o) wordoffset((o), S->startLFS)
  103. #define NewVector(S, n, t) cast(t *,NewVector_(S, n, sizeof(t)))
  104. #define StoreGetPos(S) luaN_writeFlash((S)->Z->data, NULL, 0)
  105. static void *NewVector_(LoadState *S, int n, size_t s) {
  106. void *v;
  107. if (S->mode == MODE_RAM) {
  108. v = luaM_reallocv(S->L, NULL, 0, n, s);
  109. memset (v, 0, n*s);
  110. } else {
  111. v = StoreGetPos(S);
  112. }
  113. return v;
  114. }
  115. static void *Store_(LoadState *S, void *a, int ndx, const void *e, size_t s
  116. #ifdef LUA_USE_HOST
  117. , const char *format
  118. #endif
  119. ) {
  120. if (S->mode == MODE_RAM) {
  121. lu_byte *p = byteptr(a) + ndx*s;
  122. if (p != byteptr(e))
  123. memcpy(p, e, s);
  124. return p;
  125. }
  126. #ifdef LUA_USE_HOST
  127. else if (S->mode == MODE_LFSA && format) { /* do a repack move */
  128. void *p = StoreGetPos(S);
  129. const char *f = format;
  130. int o;
  131. for (o = 0; *f; o++, f++ ) {
  132. luaN_writeFlash(S->Z->data, wordptr(e)+o, sizeof(lu_int32));
  133. if (*f == 'A' || *f == 'W') /* Addr or word followed by alignment fill */
  134. o++;
  135. }
  136. lua_assert(o*sizeof(lu_int32) == s);
  137. return p;
  138. }
  139. #endif
  140. /* mode == LFS or 32bit build */
  141. return luaN_writeFlash(S->Z->data, e, s);
  142. }
  143. #ifdef LUA_USE_HOST
  144. #include <stdio.h>
  145. /* These compression maps must match the definitions in lobject.h etc. */
  146. # define OFFSET_TSTRING (2*(sizeof(lu_int32)-sizeof(size_t)))
  147. # define FMT_TSTRING "AwwA"
  148. # define FMT_TVALUE "WA"
  149. # define FMT_PROTO "AwwwwwwwwwwAAAAAAAA"
  150. # define FMT_UPVALUE "AW"
  151. # define FMT_LOCVAR "Aww"
  152. # define FMT_ROTENTRY "AWA"
  153. # define FMT_ROTABLE "AWAA"
  154. # define StoreR(S,a, i, v, f) Store_(S, (a), i, &(v), sizeof(v), f)
  155. # define Store(S, a, i, v) StoreR(S, (a), i, v, NULL)
  156. # define StoreN(S, v, n) Store_(S, NULL, 0, (v), (n)*sizeof(*(v)), NULL)
  157. static void *StoreAV (LoadState *S, void *a, int n) {
  158. void **av = cast(void**, a);
  159. if (S->mode == MODE_LFSA) {
  160. void *p = StoreGetPos(S);
  161. int i; for (i = 0; i < n; i ++)
  162. luaN_writeFlash(S->Z->data, wordptr(av++), sizeof(lu_int32));
  163. return p;
  164. } else {
  165. return Store_(S, NULL, 0, av, n*sizeof(*av), NULL);
  166. }
  167. }
  168. #else // LUA_USE_ESP
  169. # define OFFSET_TSTRING (0)
  170. # define Store(S, a, i, v) Store_(S, (a), i, &(v), sizeof(v))
  171. # define StoreN(S, v, n) Store_(S, NULL, 0, (v), (n)*sizeof(*(v)))
  172. # define StoreR(S, a, i, v, f) Store(S, a, i, v)
  173. # define StoreAV(S, p, n) StoreN(S, p, n)
  174. # define OPT_FMT
  175. #endif
  176. #define StoreFlush(S) luaN_flushFlash((S)->Z->data);
  177. #define LoadVector(S,b,n) LoadBlock(S,b,(n)*sizeof((b)[0]))
  178. static void LoadBlock (LoadState *S, void *b, size_t size) {
  179. lu_int32 left = luaZ_read(S->Z, b, size);
  180. if ( left != 0)
  181. error(S, "truncated");
  182. }
  183. #define LoadVar(S,x) LoadVector(S,&x,1)
  184. static lu_byte LoadByte (LoadState *S) {
  185. lu_byte x;
  186. LoadVar(S, x);
  187. return x;
  188. }
  189. static lua_Integer LoadInt (LoadState *S) {
  190. lu_byte b;
  191. lua_Integer x = 0;
  192. do { b = LoadByte(S); x = (x<<7) + (b & 0x7f); } while (b & 0x80);
  193. return x;
  194. }
  195. static lua_Number LoadNumber (LoadState *S) {
  196. lua_Number x;
  197. LoadVar(S, x);
  198. return x;
  199. }
  200. static lua_Integer LoadInteger (LoadState *S, lu_byte tt_data) {
  201. lu_byte b;
  202. lua_Integer x = tt_data & LUAU_DMASK;
  203. if (tt_data & 0x80) {
  204. do { b = LoadByte(S); x = (x<<7) + (b & 0x7f); } while (b & 0x80);
  205. }
  206. return (tt_data & LUAU_TMASK) == LUAU_TNUMNINT ? -x-1 : x;
  207. }
  208. static TString *LoadString_ (LoadState *S, int prelen) {
  209. TString *ts;
  210. char buff[LUAI_MAXSHORTLEN];
  211. int n = LoadInteger(S, (prelen < 0 ? LoadByte(S) : prelen)) - 1;
  212. if (n < 0)
  213. return NULL;
  214. if (S->useStrRefs)
  215. ts = S->TS[n];
  216. else if (n <= LUAI_MAXSHORTLEN) { /* short string? */
  217. LoadVector(S, buff, n);
  218. ts = luaS_newlstr(S->L, buff, n);
  219. } else { /* long string */
  220. ts = luaS_createlngstrobj(S->L, n);
  221. LoadVector(S, getstr(ts), n); /* load directly in final place */
  222. }
  223. return ts;
  224. }
  225. #define LoadString(S) LoadString_(S,-1)
  226. #define LoadString2(S,pl) LoadString_(S,(pl))
  227. static void LoadCode (LoadState *S, Proto *f) {
  228. Instruction *p;
  229. f->sizecode = LoadInt(S);
  230. f->code = luaM_newvector(S->L, f->sizecode, Instruction);
  231. LoadVector(S, f->code, f->sizecode);
  232. if (S->mode != MODE_RAM) {
  233. p = StoreN(S, f->code, f->sizecode);
  234. luaM_freearray(S->L, f->code, f->sizecode);
  235. f->code = p;
  236. }
  237. }
  238. static void *LoadFunction(LoadState *S, Proto *f, TString *psource);
  239. static void LoadConstants (LoadState *S, Proto *f) {
  240. int i;
  241. f->sizek = LoadInt(S);
  242. f->k = NewVector(S, f->sizek, TValue);
  243. for (i = 0; i < f->sizek; i++) {
  244. TValue o;
  245. /*
  246. * tt is formatted 0bFTTTDDDD where TTT is the type; the F and the DDDD
  247. * fields are used by the integer decoder as this often saves a byte in
  248. * the endcoding.
  249. */
  250. lu_byte tt = LoadByte(S);
  251. switch (tt & LUAU_TMASK) {
  252. case LUAU_TNIL:
  253. setnilvalue(&o);
  254. break;
  255. case LUAU_TBOOLEAN:
  256. setbvalue(&o, !(tt == LUAU_TBOOLEAN));
  257. break;
  258. case LUAU_TNUMFLT:
  259. setfltvalue(&o, LoadNumber(S));
  260. break;
  261. case LUAU_TNUMPINT:
  262. case LUAU_TNUMNINT:
  263. setivalue(&o, LoadInteger(S, tt));
  264. break;
  265. case LUAU_TSSTRING:
  266. o.value_.gc = cast(GCObject *, LoadString2(S, tt));
  267. o.tt_ = ctb(LUA_TSHRSTR);
  268. break;
  269. case LUAU_TLSTRING:
  270. o.value_.gc = cast(GCObject *, LoadString2(S, tt));
  271. o.tt_ = ctb(LUA_TLNGSTR);
  272. break;
  273. default:
  274. lua_assert(0);
  275. }
  276. StoreR(S, f->k, i, o, FMT_TVALUE);
  277. }
  278. }
  279. /*
  280. ** The handling of Protos has support both modes, and in the case of flash
  281. ** mode, this requires some care as any writes to a Proto f must be deferred
  282. ** until after all of the writes to its sub Protos have been completed; so
  283. ** the Proto record and its p vector must be retained in RAM until stored to
  284. ** flash.
  285. **
  286. ** Recovery of dead resources on error handled by the Lua GC as standard in
  287. ** the case of RAM loading. In the case of loading an LFS image into flash,
  288. ** the error recovery could be done through the S->protogc list, but given
  289. ** that the immediate action is to restart the CPU, there is little point
  290. ** in adding the extra functionality to recover these dangling resources.
  291. */
  292. static void LoadProtos (LoadState *S, Proto *f) {
  293. int i, n = LoadInt(S);
  294. Proto **p = luaM_newvector(S->L, n, Proto *);
  295. f->p = p;
  296. f->sizep = n;
  297. memset (p, 0, n * sizeof(*p));
  298. for (i = 0; i < n; i++)
  299. p[i] = LoadFunction(S, luaF_newproto(S->L), f->source);
  300. if (S->mode != MODE_RAM) {
  301. f->p = StoreAV(S, cast(void **, p), n);
  302. luaM_freearray(S->L, p, n);
  303. }
  304. }
  305. static void LoadUpvalues (LoadState *S, Proto *f) {
  306. int i, nostripnames = LoadByte(S);
  307. f->sizeupvalues = LoadInt(S);
  308. if (f->sizeupvalues) {
  309. f->upvalues = NewVector(S, f->sizeupvalues, Upvaldesc);
  310. for (i = 0; i < f->sizeupvalues ; i++) {
  311. TString *name = nostripnames ? LoadString(S) : NULL;
  312. Upvaldesc uv = {name, LoadByte(S), LoadByte(S)};
  313. StoreR(S, f->upvalues, i, uv, FMT_UPVALUE);
  314. }
  315. }
  316. }
  317. static void LoadDebug (LoadState *S, Proto *f) {
  318. int i;
  319. f->sizelineinfo = LoadInt(S);
  320. if (f->sizelineinfo) {
  321. lu_byte *li = luaM_newvector(S->L, f->sizelineinfo, lu_byte);
  322. LoadVector(S, li, f->sizelineinfo);
  323. if (S->mode == MODE_RAM) {
  324. f->lineinfo = li;
  325. } else {
  326. f->lineinfo = StoreN(S, li, f->sizelineinfo);
  327. luaM_freearray(S->L, li, f->sizelineinfo);
  328. }
  329. }
  330. f->sizelocvars = LoadInt(S);
  331. f->locvars = NewVector(S, f->sizelocvars, LocVar);
  332. for (i = 0; i < f->sizelocvars; i++) {
  333. LocVar lv = {LoadString(S), LoadInt(S), LoadInt(S)};
  334. StoreR(S, f->locvars, i, lv, FMT_LOCVAR);
  335. }
  336. }
  337. static void *LoadFunction (LoadState *S, Proto *f, TString *psource) {
  338. /*
  339. * Main protos have f->source naming the file used to create the hierarchy;
  340. * subordinate protos set f->source != NULL to inherit this name from the
  341. * parent. In LFS mode, the Protos are moved from the GC to a local list
  342. * in S, but no error GC is attempted as discussed in LoadProtos.
  343. */
  344. Proto *p;
  345. global_State *g = G(S->L);
  346. if (S->mode != MODE_RAM) {
  347. lua_assert(g->allgc == obj2gco(f));
  348. g->allgc = f->next; /* remove object from 'allgc' list */
  349. f->next = S->protogc; /* push f into the head of the protogc list */
  350. S->protogc = obj2gco(f);
  351. }
  352. f->source = LoadString(S);
  353. if (f->source == NULL) /* no source in dump? */
  354. f->source = psource; /* reuse parent's source */
  355. f->linedefined = LoadInt(S);
  356. f->lastlinedefined = LoadInt(S);
  357. f->numparams = LoadByte(S);
  358. f->is_vararg = LoadByte(S);
  359. f->maxstacksize = LoadByte(S);
  360. LoadProtos(S, f);
  361. LoadCode(S, f);
  362. LoadConstants(S, f);
  363. LoadUpvalues(S, f);
  364. LoadDebug(S, f);
  365. if (S->mode != MODE_RAM) {
  366. GCObject *save = f->next;
  367. if (f->source != NULL) {
  368. setLFSbit(f);
  369. /* cache the RAM next and set up the next for the LFS proto chain */
  370. f->next = FHaddr(S, GCObject *, S->fh->protoHead);
  371. p = StoreR(S, NULL, 0, *f, FMT_PROTO);
  372. S->fh->protoHead = FHoffset(S, p);
  373. } else {
  374. p = StoreR(S, NULL, 0, *f, FMT_PROTO);
  375. }
  376. S->protogc = save; /* pop f from the head of the protogc list */
  377. luaM_free(S->L, f); /* and collect the dead resource */
  378. f = p;
  379. }
  380. return f;
  381. }
  382. static void checkliteral (LoadState *S, const char *s, const char *msg) {
  383. char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than both */
  384. size_t len = strlen(s);
  385. LoadVector(S, buff, len);
  386. if (memcmp(s, buff, len) != 0)
  387. error(S, msg);
  388. }
  389. static void fchecksize (LoadState *S, size_t size, const char *tname) {
  390. if (LoadByte(S) != size)
  391. error(S, luaO_pushfstring(S->L, "%s size mismatch in", tname));
  392. }
  393. #define checksize(S,t) fchecksize(S,sizeof(t),#t)
  394. static void checkHeader (LoadState *S, int format) {
  395. checkliteral(S, LUA_SIGNATURE + 1, "not a"); /* 1st char already checked */
  396. if (LoadByte(S) != LUAC_VERSION)
  397. error(S, "version mismatch in");
  398. if (LoadByte(S) != format)
  399. error(S, "format mismatch in");
  400. checkliteral(S, LUAC_DATA, "corrupted");
  401. checksize(S, int);
  402. /*
  403. * The standard Lua VM does a check on the sizeof size_t and endian check on
  404. * integer; both are dropped as the former prevents dump files being shared
  405. * across 32 and 64 bit machines, and we use multi-byte coding of ints.
  406. */
  407. checksize(S, Instruction);
  408. checksize(S, lua_Integer);
  409. checksize(S, lua_Number);
  410. LoadByte(S); /* skip number tt field */
  411. if (LoadNumber(S) != LUAC_NUM)
  412. error(S, "float format mismatch in");
  413. }
  414. /*
  415. ** Load precompiled chunk to support standard LUA_API load functions. The
  416. ** extra LFS functionality is effectively NO-OPed out on this MODE_RAM path.
  417. */
  418. LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) {
  419. LoadState S = {0};
  420. LClosure *cl;
  421. if (*name == '@' || *name == '=')
  422. S.name = name + 1;
  423. else if (*name == LUA_SIGNATURE[0])
  424. S.name = "binary string";
  425. else
  426. S.name = name;
  427. S.L = L;
  428. S.Z = Z;
  429. S.mode = MODE_RAM;
  430. S.fh = NULL;
  431. S.useStrRefs = 0;
  432. checkHeader(&S, LUAC_FORMAT);
  433. cl = luaF_newLclosure(L, LoadByte(&S));
  434. setclLvalue(L, L->top, cl);
  435. luaD_inctop(L);
  436. cl->p = luaF_newproto(L);
  437. LoadFunction(&S, cl->p, NULL);
  438. lua_assert(cl->nupvalues == cl->p->sizeupvalues);
  439. return cl;
  440. }
  441. /*============================================================================**
  442. ** NodeMCU extensions for LFS support and Loading. Note that this funtionality
  443. ** is called from a hook in the lua startup within a lua_lock() (as with
  444. ** LuaU_undump), so luaU_undumpLFS() cannot use the external Lua API. It does
  445. ** uses the Lua stack, but staying within LUA_MINSTACK limits.
  446. **
  447. ** The in-RAM Protos used to assemble proto content prior to writing to LFS
  448. ** need special treatment since these hold LFS references rather than RAM ones
  449. ** and will cause the Lua GC to error if swept. Rather than adding complexity
  450. ** to lgc.c for this one-off process, these Protos are removed from the allgc
  451. ** list and fixed in a local one, and collected inline.
  452. **============================================================================*/
  453. /*
  454. ** Write a TString to the LFS. This parallels the lstring.c algo but writes
  455. ** directly to the LFS buffer and also append the LFS address in S->TS. Seeding
  456. ** is based on the seed defined in the LFS image, rather than g->seed.
  457. */
  458. static void addTS(LoadState *S, int l, int extra) {
  459. LFSHeader *fh = S->fh;
  460. TString *ts = cast(TString *, S->buff);
  461. char *s = getstr(ts);
  462. lua_assert (sizelstring(l) <= S->buffLen);
  463. s[l] = '\0';
  464. /* The collectable and LFS bits must be set; all others inc the whitebits clear */
  465. ts->marked = bitmask(LFSBIT) | BIT_ISCOLLECTABLE;
  466. ts->extra = extra;
  467. if (l <= LUAI_MAXSHORTLEN) { /* short string */
  468. TString **p;
  469. ts->tt = LUA_TSHRSTR;
  470. ts->shrlen = cast_byte(l);
  471. ts->hash = luaS_hash(s, l, fh->seed);
  472. p = S->list + lmod(ts->hash, S->listLen);
  473. ts->u.hnext = *p;
  474. ts->next = FHaddr(S, GCObject *, fh->shortTShead);
  475. S->TS[S->TSndx] = *p = StoreR(S, NULL, 0, *ts, FMT_TSTRING);
  476. fh->shortTShead = FHoffset(S, *p);
  477. } else { /* long string */
  478. TString *p;
  479. ts->tt = LUA_TLNGSTR;
  480. ts->shrlen = 0;
  481. ts->u.lnglen = l;
  482. ts->hash = fh->seed;
  483. luaS_hashlongstr(ts); /* sets hash and extra fields */
  484. ts->next = FHaddr(S, GCObject *, fh->longTShead);
  485. S->TS[S->TSndx] = p = StoreR(S, NULL, 0, *ts, FMT_TSTRING);
  486. fh->longTShead = FHoffset(S, p);
  487. }
  488. // printf("%04u(%u): %s\n", S->TSndx, l, S->buff + sizeof(union UTString));
  489. StoreN(S,S->buff + sizeof(union UTString), l+1);
  490. S->TSndx++;
  491. }
  492. /*
  493. ** The runtime (in ltm.c and llex.c) declares ~100 fixed strings and so these
  494. ** are moved into LFS to free up an extra ~2Kb RAM. Extra get token access
  495. ** functions have been added to these modules. These tokens aren't unique as
  496. ** ("nil" and "function" are both tokens and typenames), hardwiring this
  497. ** duplication debounce as a wrapper around addTS() is the simplest way of
  498. ** voiding the need for extra lookup resources.
  499. */
  500. static void addTSnodup(LoadState *S, const char *s, int extra) {
  501. int i, l = strlen(s);
  502. static struct {const char *k; int found; } t[] = {{"nil", 0},{"function", 0}};
  503. for (i = 0; i < sizeof(t)/sizeof(*t); i++) {
  504. if (!strcmp(t[i].k, s)) {
  505. if (t[i].found) return; /* ignore the duplicate copy */
  506. t[i].found = 1; /* flag that this constant is already loaded */
  507. break;
  508. }
  509. }
  510. memcpy(getstr(cast(TString *, S->buff)), s, l);
  511. addTS(S, l, extra);
  512. }
  513. /*
  514. ** Load TStrings in dump format. ALl TStrings used in an LFS image excepting
  515. ** any fixed strings are dumped as a unique collated set. Any strings in the
  516. ** following Proto streams use an index reference into this list rather than an
  517. ** inline copy. This function loads and stores them into LFS, constructing the
  518. ** ROstrt for the shorter interned strings.
  519. */
  520. static void LoadAllStrings (LoadState *S) {
  521. lua_State *L = S->L;
  522. global_State *g = G(L);
  523. int nb = sizelstring(LoadInt(S));
  524. int ns = LoadInt(S);
  525. int nl = LoadInt(S);
  526. int nstrings = LoadInt(S);
  527. int n = ns + nl;
  528. int nlist = 1<<luaO_ceillog2(ns);
  529. int i, extra;
  530. const char *p;
  531. /* allocate dynamic resources and save in S for error path collection */
  532. S->TS = luaM_newvector(L, n+1, TString *);
  533. S->TSlen = n+1;
  534. S->buff = luaM_newvector(L, nb, char);
  535. S->buffLen = nb;
  536. S->list = luaM_newvector(L, nlist, TString *);
  537. S->listLen = nlist;
  538. memset (S->list, 0, nlist*sizeof(TString *));
  539. /* add the strings in the image file to LFS */
  540. for (i = 1; i <= nstrings; i++) {
  541. int tt = LoadByte(S);
  542. lua_assert((tt&LUAU_TMASK)==LUAU_TSSTRING || (tt&LUAU_TMASK)==LUAU_TLSTRING);
  543. int l = LoadInteger(S, tt) - 1; /* No NULL entry in list of TSs */
  544. LoadVector(S, getstr(cast(TString *, S->buff)), l);
  545. addTS(S, l, 0);
  546. }
  547. /* add the fixed strings to LFS */
  548. for (i = 0; (p = luaX_getstr(i, &extra))!=NULL; i++) {
  549. addTSnodup(S, p, extra);
  550. }
  551. addTSnodup(S, getstr(g->memerrmsg), 0);
  552. addTSnodup(S, LUA_ENV, 0);
  553. for (i = 0; (p = luaT_getstr(i))!=NULL; i++) {
  554. addTSnodup(S, p, 0);
  555. }
  556. /* check that the actual size is the same as the predicted */
  557. lua_assert(n == S->TSndx-1);
  558. S->fh->oROhash = FHoffset(S, StoreAV(S, S->list, nlist));
  559. S->fh->nROuse = ns;
  560. S->fh->nROsize = nlist;
  561. StoreFlush(S);
  562. S->buff = luaM_freearray(L, S->buff, nb);
  563. S->buffLen = 0;
  564. S->list = luaM_freearray(L, S->list, nlist);
  565. S->listLen = 0;
  566. }
  567. static void LoadAllProtos (LoadState *S) {
  568. lua_State *L = S->L;
  569. ROTable_entry eol = {NULL, LRO_NILVAL};
  570. int i, n = LoadInt(S);
  571. S->pv = luaM_newvector(L, n, Proto *);
  572. S->pvLen = n;
  573. /* Load Protos and store addresses in the Proto vector */
  574. for (i = 0; i < n; i++) {
  575. S->pv[i] = LoadFunction(S, luaF_newproto(L), NULL);
  576. }
  577. /* generate the ROTable entries from first N constants; the last is a timestamp */
  578. lua_assert(n+1 == LoadInt(S));
  579. ROTable_entry *entry_list = cast(ROTable_entry *, StoreGetPos(S));
  580. for (i = 0; i < n; i++) {
  581. lu_byte tt_data = LoadByte(S);
  582. TString *Tname = LoadString2(S, tt_data);
  583. const char *name = getstr(Tname) + OFFSET_TSTRING;
  584. lua_assert((tt_data & LUAU_TMASK) == LUAU_TSSTRING);
  585. ROTable_entry me = {name, LRO_LUDATA(S->pv[i])};
  586. StoreR(S, NULL, 0, me, FMT_ROTENTRY);
  587. }
  588. StoreR(S, NULL, 0, eol, FMT_ROTENTRY);
  589. /* terminate the ROTable entry list and store the ROTable header */
  590. ROTable ev = { (GCObject *)1, LUA_TTBLROF, LROT_MARKED,
  591. (lu_byte) ~0, n, NULL, entry_list};
  592. S->fh->protoROTable = FHoffset(S, StoreR(S, NULL, 0, ev, FMT_ROTABLE));
  593. /* last const is timestamp */
  594. S->fh->timestamp = LoadInteger(S, LoadByte(S));
  595. }
  596. static void undumpLFS(lua_State *L, void *ud) {
  597. LoadState *S = cast(LoadState *, ud);
  598. void *F = S->Z->data;
  599. S->startLFS = StoreGetPos(S);
  600. luaN_setFlash(F, sizeof(LFSHeader));
  601. S->fh->flash_sig = FLASH_SIG;
  602. if (LoadByte(S) != LUA_SIGNATURE[0])
  603. error(S, "invalid header in");
  604. checkHeader(S, LUAC_LFS_IMAGE_FORMAT);
  605. S->fh->seed = LoadInteger(S, LoadByte(S));
  606. checkliteral(S, LUA_STRING_SIG,"no string vector");
  607. LoadAllStrings (S);
  608. checkliteral(S, LUA_PROTO_SIG,"no Proto vector");
  609. LoadAllProtos(S);
  610. S->fh->flash_size = byteptr(StoreGetPos(S)) - byteptr(S->startLFS);
  611. luaN_setFlash(F, 0);
  612. StoreN(S, S->fh, 1);
  613. luaN_setFlash(F, 0);
  614. S->TS = luaM_freearray(L, S->TS, S->TSlen);
  615. }
  616. /*
  617. ** Load precompiled LFS image. This is called from a hook in the firmware
  618. ** startup if LFS reload is required.
  619. */
  620. LUAI_FUNC int luaU_undumpLFS(lua_State *L, ZIO *Z, int isabs) {
  621. LFSHeader fh = {0};
  622. LoadState S = {0};
  623. int status;
  624. S.L = L;
  625. S.Z = Z;
  626. S.mode = isabs && sizeof(size_t) != sizeof(lu_int32) ? MODE_LFSA : MODE_LFS;
  627. S.useStrRefs = 1;
  628. S.fh = &fh;
  629. L->nny++; /* do not yield during undump LFS */
  630. status = luaD_pcall(L, undumpLFS, &S, savestack(L, L->top), L->errfunc);
  631. luaM_freearray(L, S.TS, S.TSlen);
  632. luaM_freearray(L, S.buff, S.buffLen);
  633. luaM_freearray(L, S.list, S.listLen);
  634. luaM_freearray(L, S.pv, S.pvLen);
  635. L->nny--;
  636. return status;
  637. }