undump.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. ** undump.c
  3. ** load bytecodes from files
  4. */
  5. char* rcs_undump="$Id: undump.c,v 1.23 1997/06/16 16:50:22 roberto Exp roberto $";
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include "auxlib.h"
  9. #include "opcode.h"
  10. #include "luamem.h"
  11. #include "table.h"
  12. #include "undump.h"
  13. #include "zio.h"
  14. static int swapword=0;
  15. static int swapfloat=0;
  16. static TFunc* Main=NULL; /* functions in a chunk */
  17. static TFunc* lastF=NULL;
  18. static void FixCode(Byte* code, Byte* end) /* swap words */
  19. {
  20. Byte* p;
  21. for (p=code; p!=end;)
  22. {
  23. int op=*p;
  24. switch (op)
  25. {
  26. case PUSHNIL:
  27. case PUSH0:
  28. case PUSH1:
  29. case PUSH2:
  30. case PUSHLOCAL0:
  31. case PUSHLOCAL1:
  32. case PUSHLOCAL2:
  33. case PUSHLOCAL3:
  34. case PUSHLOCAL4:
  35. case PUSHLOCAL5:
  36. case PUSHLOCAL6:
  37. case PUSHLOCAL7:
  38. case PUSHLOCAL8:
  39. case PUSHLOCAL9:
  40. case PUSHINDEXED:
  41. case STORELOCAL0:
  42. case STORELOCAL1:
  43. case STORELOCAL2:
  44. case STORELOCAL3:
  45. case STORELOCAL4:
  46. case STORELOCAL5:
  47. case STORELOCAL6:
  48. case STORELOCAL7:
  49. case STORELOCAL8:
  50. case STORELOCAL9:
  51. case STOREINDEXED0:
  52. case ADJUST0:
  53. case EQOP:
  54. case LTOP:
  55. case LEOP:
  56. case GTOP:
  57. case GEOP:
  58. case ADDOP:
  59. case SUBOP:
  60. case MULTOP:
  61. case DIVOP:
  62. case POWOP:
  63. case CONCOP:
  64. case MINUSOP:
  65. case NOTOP:
  66. case POP:
  67. case RETCODE0:
  68. p++;
  69. break;
  70. case PUSHBYTE:
  71. case PUSHLOCAL:
  72. case STORELOCAL:
  73. case STOREINDEXED:
  74. case STORELIST0:
  75. case ADJUST:
  76. case RETCODE:
  77. case VARARGS:
  78. case STOREMAP:
  79. p+=2;
  80. break;
  81. case STORELIST:
  82. case CALLFUNC:
  83. p+=3;
  84. break;
  85. case PUSHFUNCTION:
  86. p+=5; /* TODO: use sizeof(TFunc*) or old? */
  87. break;
  88. case PUSHWORD:
  89. case PUSHSELF:
  90. case CREATEARRAY:
  91. case ONTJMP:
  92. case ONFJMP:
  93. case JMP:
  94. case UPJMP:
  95. case IFFJMP:
  96. case IFFUPJMP:
  97. case SETLINE:
  98. case PUSHSTRING:
  99. case PUSHGLOBAL:
  100. case STOREGLOBAL:
  101. {
  102. Byte t;
  103. t=p[1]; p[1]=p[2]; p[2]=t;
  104. p+=3;
  105. break;
  106. }
  107. case PUSHFLOAT: /* assumes sizeof(float)==4 */
  108. {
  109. Byte t;
  110. t=p[1]; p[1]=p[4]; p[4]=t;
  111. t=p[2]; p[2]=p[3]; p[3]=t;
  112. p+=5;
  113. break;
  114. }
  115. case STORERECORD:
  116. {
  117. int n=*++p;
  118. p++;
  119. while (n--)
  120. {
  121. Byte t;
  122. t=p[0]; p[0]=p[1]; p[1]=t;
  123. p+=2;
  124. }
  125. break;
  126. }
  127. default:
  128. luaL_verror("corrupt binary file: bad opcode %d at %d\n",
  129. op,(int)(p-code));
  130. break;
  131. }
  132. }
  133. }
  134. static void Unthread(Byte* code, int i, int v)
  135. {
  136. while (i!=0)
  137. {
  138. Word w;
  139. Byte* p=code+i;
  140. memcpy(&w,p,sizeof(w));
  141. i=w; w=v;
  142. memcpy(p,&w,sizeof(w));
  143. }
  144. }
  145. static int LoadWord(ZIO* Z)
  146. {
  147. Word w;
  148. zread(Z,&w,sizeof(w));
  149. if (swapword)
  150. {
  151. Byte* p=(Byte*)&w;
  152. Byte t;
  153. t=p[0]; p[0]=p[1]; p[1]=t;
  154. }
  155. return w;
  156. }
  157. static int LoadSize(ZIO* Z)
  158. {
  159. Word hi=LoadWord(Z);
  160. Word lo=LoadWord(Z);
  161. int s=(hi<<16)|lo;
  162. if ((Word)s != s) lua_error("code too long");
  163. return s;
  164. }
  165. static void* LoadBlock(int size, ZIO* Z)
  166. {
  167. void* b=luaI_malloc(size);
  168. zread(Z,b,size);
  169. return b;
  170. }
  171. static char* LoadString(ZIO* Z)
  172. {
  173. int size=LoadWord(Z);
  174. char *b=luaI_buffer(size);
  175. zread(Z,b,size);
  176. return b;
  177. }
  178. static char* LoadNewString(ZIO* Z)
  179. {
  180. return LoadBlock(LoadWord(Z),Z);
  181. }
  182. static void LoadFunction(ZIO* Z)
  183. {
  184. TFunc* tf=new(TFunc);
  185. tf->next=NULL;
  186. tf->locvars=NULL;
  187. tf->size=LoadSize(Z);
  188. tf->lineDefined=LoadWord(Z);
  189. if (IsMain(tf)) /* new main */
  190. {
  191. tf->fileName=LoadNewString(Z);
  192. Main=lastF=tf;
  193. }
  194. else /* fix PUSHFUNCTION */
  195. {
  196. tf->marked=LoadWord(Z);
  197. tf->fileName=Main->fileName;
  198. memcpy(Main->code+tf->marked,&tf,sizeof(tf));
  199. lastF=lastF->next=tf;
  200. }
  201. tf->code=LoadBlock(tf->size,Z);
  202. if (swapword || swapfloat) FixCode(tf->code,tf->code+tf->size);
  203. while (1) /* unthread */
  204. {
  205. int c=zgetc(Z);
  206. if (c==ID_VAR) /* global var */
  207. {
  208. int i=LoadWord(Z);
  209. char* s=LoadString(Z);
  210. int v=luaI_findsymbolbyname(s);
  211. Unthread(tf->code,i,v);
  212. }
  213. else if (c==ID_STR) /* constant string */
  214. {
  215. int i=LoadWord(Z);
  216. char* s=LoadString(Z);
  217. int v=luaI_findconstantbyname(s);
  218. Unthread(tf->code,i,v);
  219. }
  220. else
  221. {
  222. zungetc(Z);
  223. break;
  224. }
  225. }
  226. }
  227. static void LoadSignature(ZIO* Z)
  228. {
  229. char* s=SIGNATURE;
  230. while (*s!=0 && zgetc(Z)==*s)
  231. ++s;
  232. if (*s!=0) lua_error("cannot load binary file: bad signature");
  233. }
  234. static void LoadHeader(ZIO* Z)
  235. {
  236. Word w,tw=TEST_WORD;
  237. float f,tf=TEST_FLOAT;
  238. int version;
  239. LoadSignature(Z);
  240. version=zgetc(Z);
  241. if (version>0x23) /* after 2.5 */
  242. {
  243. int oldsizeofW=zgetc(Z);
  244. int oldsizeofF=zgetc(Z);
  245. int oldsizeofP=zgetc(Z);
  246. if (oldsizeofW!=2)
  247. luaL_verror(
  248. "cannot load binary file created on machine with sizeof(Word)=%d; "
  249. "expected 2",oldsizeofW);
  250. if (oldsizeofF!=4)
  251. luaL_verror(
  252. "cannot load binary file created on machine with sizeof(float)=%d; "
  253. "expected 4\nnot an IEEE machine?",oldsizeofF);
  254. if (oldsizeofP!=sizeof(TFunc*)) /* TODO: pack? */
  255. luaL_verror(
  256. "cannot load binary file created on machine with sizeof(TFunc*)=%d; "
  257. "expected %d",oldsizeofP,(int)sizeof(TFunc*));
  258. }
  259. zread(Z,&w,sizeof(w)); /* test word */
  260. if (w!=tw)
  261. {
  262. swapword=1;
  263. }
  264. zread(Z,&f,sizeof(f)); /* test float */
  265. if (f!=tf)
  266. {
  267. Byte* p=(Byte*)&f;
  268. Byte t;
  269. swapfloat=1;
  270. t=p[0]; p[0]=p[3]; p[3]=t;
  271. t=p[1]; p[1]=p[2]; p[2]=t;
  272. if (f!=tf) /* TODO: try another perm? */
  273. lua_error("cannot load binary file: unknown float representation");
  274. }
  275. }
  276. static void LoadChunk(ZIO* Z)
  277. {
  278. LoadHeader(Z);
  279. while (1)
  280. {
  281. int c=zgetc(Z);
  282. if (c==ID_FUN) LoadFunction(Z); else { zungetc(Z); break; }
  283. }
  284. }
  285. /*
  286. ** load one chunk from a file.
  287. ** return list of functions found, headed by main, or NULL at EOF.
  288. */
  289. TFunc* luaI_undump1(ZIO* Z)
  290. {
  291. int c=zgetc(Z);
  292. if (c==ID_CHUNK)
  293. {
  294. LoadChunk(Z);
  295. return Main;
  296. }
  297. else if (c!=EOZ)
  298. lua_error("not a lua binary file");
  299. return NULL;
  300. }
  301. /*
  302. ** load and run all chunks in a file
  303. */
  304. int luaI_undump(ZIO* Z)
  305. {
  306. TFunc* m;
  307. while ((m=luaI_undump1(Z)))
  308. {
  309. int status=luaI_dorun(m);
  310. luaI_freefunc(m);
  311. if (status!=0) return status;
  312. }
  313. return 0;
  314. }