ltests.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. /*
  2. ** $Id: ltests.c,v 1.66 2001/02/09 20:22:29 roberto Exp roberto $
  3. ** Internal Module for Debugging of the Lua Implementation
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <ctype.h>
  7. #include <limits.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "lua.h"
  12. #include "lapi.h"
  13. #include "lauxlib.h"
  14. #include "lcode.h"
  15. #include "ldebug.h"
  16. #include "ldo.h"
  17. #include "lfunc.h"
  18. #include "lmem.h"
  19. #include "lopcodes.h"
  20. #include "lstate.h"
  21. #include "lstring.h"
  22. #include "ltable.h"
  23. #include "luadebug.h"
  24. #include "lualib.h"
  25. /*
  26. ** The whole module only makes sense with LUA_DEBUG on
  27. */
  28. #ifdef LUA_DEBUG
  29. lua_State *lua_state = NULL;
  30. int islocked = 0;
  31. static void setnameval (lua_State *L, const char *name, int val) {
  32. lua_pushstring(L, name);
  33. lua_pushnumber(L, val);
  34. lua_settable(L, -3);
  35. }
  36. /*
  37. ** {======================================================================
  38. ** Controlled version for realloc.
  39. ** =======================================================================
  40. */
  41. /* ensures maximum alignment for HEADER */
  42. #define HEADER (sizeof(union L_Umaxalign))
  43. #define MARKSIZE 32
  44. #define MARK 0x55 /* 01010101 (a nice pattern) */
  45. #define blocksize(b) ((size_t *)((char *)(b) - HEADER))
  46. unsigned long memdebug_numblocks = 0;
  47. unsigned long memdebug_total = 0;
  48. unsigned long memdebug_maxmem = 0;
  49. unsigned long memdebug_memlimit = ULONG_MAX;
  50. static void *checkblock (void *block) {
  51. size_t *b = blocksize(block);
  52. size_t size = *b;
  53. int i;
  54. for (i=0;i<MARKSIZE;i++)
  55. lua_assert(*(((char *)b)+HEADER+size+i) == MARK+i); /* corrupted block? */
  56. return b;
  57. }
  58. static void freeblock (void *block) {
  59. if (block) {
  60. size_t size = *blocksize(block);
  61. block = checkblock(block);
  62. memset(block, -1, size+HEADER+MARKSIZE); /* erase block */
  63. free(block); /* free original block */
  64. memdebug_numblocks--;
  65. memdebug_total -= size;
  66. }
  67. }
  68. void *debug_realloc (void *block, size_t oldsize, size_t size) {
  69. lua_assert((oldsize == 0) ? block == NULL : oldsize == *blocksize(block));
  70. if (size == 0) {
  71. freeblock(block);
  72. return NULL;
  73. }
  74. else if (memdebug_total+size > memdebug_memlimit)
  75. return NULL; /* to test memory allocation errors */
  76. else {
  77. char *newblock;
  78. int i;
  79. size_t realsize = HEADER+size+MARKSIZE;
  80. if (realsize < size) return NULL; /* overflow! */
  81. newblock = (char *)malloc(realsize); /* alloc a new block */
  82. if (newblock == NULL) return NULL;
  83. if (oldsize > size) oldsize = size;
  84. if (block) {
  85. memcpy(newblock+HEADER, block, oldsize);
  86. freeblock(block); /* erase (and check) old copy */
  87. }
  88. /* initialize new part of the block with something `weird' */
  89. memset(newblock+HEADER+oldsize, -MARK, size-oldsize);
  90. memdebug_total += size;
  91. if (memdebug_total > memdebug_maxmem)
  92. memdebug_maxmem = memdebug_total;
  93. memdebug_numblocks++;
  94. *(size_t *)newblock = size;
  95. for (i=0;i<MARKSIZE;i++)
  96. *(newblock+HEADER+size+i) = (char)(MARK+i);
  97. return newblock+HEADER;
  98. }
  99. }
  100. /* }====================================================================== */
  101. /*
  102. ** {======================================================
  103. ** Disassembler
  104. ** =======================================================
  105. */
  106. static const char *const instrname[NUM_OPCODES] = {
  107. "RETURN", "CALL", "TAILCALL", "PUSHNIL", "POP", "PUSHINT",
  108. "PUSHSTRING", "PUSHNUM", "PUSHNEGNUM", "PUSHUPVALUE", "GETLOCAL",
  109. "GETGLOBAL", "GETTABLE", "GETDOTTED", "GETINDEXED", "PUSHSELF",
  110. "CREATETABLE", "SETLOCAL", "SETGLOBAL", "SETTABLE", "SETLIST", "SETMAP",
  111. "ADD", "ADDI", "SUB", "MULT", "DIV", "POW", "CONCAT", "MINUS", "NOT",
  112. "JMPNE", "JMPEQ", "JMPLT", "JMPLE", "JMPGT", "JMPGE", "JMPT", "JMPF",
  113. "JMPONT", "JMPONF", "JMP", "PUSHNILJMP", "FORPREP", "FORLOOP", "LFORPREP",
  114. "LFORLOOP", "CLOSURE"
  115. };
  116. static void pushop (lua_State *L, Proto *p, int pc) {
  117. char buff[100];
  118. Instruction i = p->code[pc];
  119. OpCode o = GET_OPCODE(i);
  120. const char *name = instrname[o];
  121. sprintf(buff, "%5d - ", luaG_getline(p->lineinfo, pc, 1, NULL));
  122. switch ((enum Mode)luaK_opproperties[o].mode) {
  123. case iO:
  124. sprintf(buff+8, "%-12s", name);
  125. break;
  126. case iU:
  127. sprintf(buff+8, "%-12s%4u", name, GETARG_U(i));
  128. break;
  129. case iS:
  130. sprintf(buff+8, "%-12s%4d", name, GETARG_S(i));
  131. break;
  132. case iAB:
  133. sprintf(buff+8, "%-12s%4d %4d", name, GETARG_A(i), GETARG_B(i));
  134. break;
  135. }
  136. lua_pushstring(L, buff);
  137. }
  138. static int listcode (lua_State *L) {
  139. int pc;
  140. Proto *p;
  141. luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  142. 1, "Lua function expected");
  143. p = clvalue(luaA_index(L, 1))->f.l;
  144. lua_newtable(L);
  145. setnameval(L, "maxstack", p->maxstacksize);
  146. setnameval(L, "numparams", p->numparams);
  147. for (pc=0; pc<p->sizecode; pc++) {
  148. lua_pushnumber(L, pc+1);
  149. pushop(L, p, pc);
  150. lua_settable(L, -3);
  151. }
  152. return 1;
  153. }
  154. static int liststrings (lua_State *L) {
  155. Proto *p;
  156. int i;
  157. luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  158. 1, "Lua function expected");
  159. p = clvalue(luaA_index(L, 1))->f.l;
  160. lua_newtable(L);
  161. for (i=0; i<p->sizekstr; i++) {
  162. lua_pushnumber(L, i+1);
  163. lua_pushstring(L, getstr(p->kstr[i]));
  164. lua_settable(L, -3);
  165. }
  166. return 1;
  167. }
  168. static int listlocals (lua_State *L) {
  169. Proto *p;
  170. int pc = luaL_check_int(L, 2) - 1;
  171. int i = 0;
  172. const char *name;
  173. luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  174. 1, "Lua function expected");
  175. p = clvalue(luaA_index(L, 1))->f.l;
  176. while ((name = luaF_getlocalname(p, ++i, pc)) != NULL)
  177. lua_pushstring(L, name);
  178. return i-1;
  179. }
  180. /* }====================================================== */
  181. static int get_limits (lua_State *L) {
  182. lua_newtable(L);
  183. setnameval(L, "BITS_INT", BITS_INT);
  184. setnameval(L, "LFPF", LFIELDS_PER_FLUSH);
  185. setnameval(L, "MAXARG_A", MAXARG_A);
  186. setnameval(L, "MAXARG_B", MAXARG_B);
  187. setnameval(L, "MAXARG_S", MAXARG_S);
  188. setnameval(L, "MAXARG_U", MAXARG_U);
  189. setnameval(L, "MAXLOCALS", MAXLOCALS);
  190. setnameval(L, "MAXPARAMS", MAXPARAMS);
  191. setnameval(L, "MAXSTACK", MAXSTACK);
  192. setnameval(L, "MAXUPVALUES", MAXUPVALUES);
  193. setnameval(L, "MAXVARSLH", MAXVARSLH);
  194. setnameval(L, "RFPF", RFIELDS_PER_FLUSH);
  195. setnameval(L, "SIZE_A", SIZE_A);
  196. setnameval(L, "SIZE_B", SIZE_B);
  197. setnameval(L, "SIZE_OP", SIZE_OP);
  198. setnameval(L, "SIZE_U", SIZE_U);
  199. return 1;
  200. }
  201. static int mem_query (lua_State *L) {
  202. if (lua_isnull(L, 1)) {
  203. lua_pushnumber(L, memdebug_total);
  204. lua_pushnumber(L, memdebug_numblocks);
  205. lua_pushnumber(L, memdebug_maxmem);
  206. return 3;
  207. }
  208. else {
  209. memdebug_memlimit = luaL_check_int(L, 1);
  210. return 0;
  211. }
  212. }
  213. static int hash_query (lua_State *L) {
  214. if (lua_isnull(L, 2)) {
  215. luaL_arg_check(L, lua_tag(L, 1) == LUA_TSTRING, 1, "string expected");
  216. lua_pushnumber(L, tsvalue(luaA_index(L, 1))->u.s.hash);
  217. }
  218. else {
  219. Hash *t;
  220. Node n;
  221. TObject *o = luaA_index(L, 1);
  222. luaL_checktype(L, 2, LUA_TTABLE);
  223. t = hvalue(luaA_index(L, 2));
  224. setobj2key(&n, o);
  225. lua_pushnumber(L, luaH_mainposition(t, &n) - t->node);
  226. }
  227. return 1;
  228. }
  229. static int table_query (lua_State *L) {
  230. const Hash *t;
  231. int i = luaL_opt_int(L, 2, -1);
  232. luaL_checktype(L, 1, LUA_TTABLE);
  233. t = hvalue(luaA_index(L, 1));
  234. if (i == -1) {
  235. lua_pushnumber(L, t->size);
  236. lua_pushnumber(L, t->firstfree - t->node);
  237. return 2;
  238. }
  239. else if (i < t->size) {
  240. TObject o;
  241. setkey2obj(&o, &t->node[i]);
  242. luaA_pushobject(L, &o);
  243. luaA_pushobject(L, &t->node[i].val);
  244. if (t->node[i].next) {
  245. lua_pushnumber(L, t->node[i].next - t->node);
  246. return 3;
  247. }
  248. else
  249. return 2;
  250. }
  251. return 0;
  252. }
  253. static int string_query (lua_State *L) {
  254. stringtable *tb = (*luaL_check_string(L, 1) == 's') ? &G(L)->strt :
  255. &G(L)->udt;
  256. int s = luaL_opt_int(L, 2, 0) - 1;
  257. if (s==-1) {
  258. lua_pushnumber(L ,tb->nuse);
  259. lua_pushnumber(L ,tb->size);
  260. return 2;
  261. }
  262. else if (s < tb->size) {
  263. TString *ts;
  264. int n = 0;
  265. for (ts = tb->hash[s]; ts; ts = ts->nexthash) {
  266. setsvalue(L->top, ts);
  267. incr_top;
  268. n++;
  269. }
  270. return n;
  271. }
  272. return 0;
  273. }
  274. static int tref (lua_State *L) {
  275. luaL_checkany(L, 1);
  276. lua_pushvalue(L, 1);
  277. lua_pushnumber(L, lua_ref(L, luaL_opt_int(L, 2, 1)));
  278. return 1;
  279. }
  280. static int getref (lua_State *L) {
  281. if (lua_getref(L, luaL_check_int(L, 1)))
  282. return 1;
  283. else
  284. return 0;
  285. }
  286. static int unref (lua_State *L) {
  287. lua_unref(L, luaL_check_int(L, 1));
  288. return 0;
  289. }
  290. static int newuserdata (lua_State *L) {
  291. if (lua_isnumber(L, 2)) {
  292. int tag = luaL_check_int(L, 2);
  293. int res = lua_pushuserdata(L, (void *)luaL_check_int(L, 1));
  294. if (tag) lua_settag(L, tag);
  295. lua_pushnumber(L, res);
  296. return 2;
  297. }
  298. else {
  299. size_t size = luaL_check_int(L, 1);
  300. char *p = (char *)lua_newuserdata(L, size);
  301. while (size--) *p++ = '\0';
  302. return 1;
  303. }
  304. }
  305. static int udataval (lua_State *L) {
  306. luaL_checktype(L, 1, LUA_TUSERDATA);
  307. lua_pushnumber(L, (int)lua_touserdata(L, 1));
  308. return 1;
  309. }
  310. static int newtag (lua_State *L) {
  311. lua_pushnumber(L, lua_newtype(L, lua_tostring(L, 1), lua_tonumber(L, 2)));
  312. return 1;
  313. }
  314. static int doonnewstack (lua_State *L) {
  315. lua_State *L1 = lua_open(L, luaL_check_int(L, 1));
  316. if (L1 == NULL) return 0;
  317. *((int **)L1) = &islocked; /* initialize the lock */
  318. lua_dostring(L1, luaL_check_string(L, 2));
  319. lua_pushnumber(L, 1);
  320. lua_close(L1);
  321. return 1;
  322. }
  323. static int newstate (lua_State *L) {
  324. lua_State *L1 = lua_open(NULL, luaL_check_int(L, 1));
  325. if (L1) {
  326. *((int **)L1) = &islocked; /* initialize the lock */
  327. lua_pushuserdata(L, L1);
  328. }
  329. else
  330. lua_pushnil(L);
  331. return 1;
  332. }
  333. static int loadlib (lua_State *L) {
  334. lua_State *L1 = (lua_State *)lua_touserdata(L, 1);
  335. switch (*luaL_check_string(L, 2)) {
  336. case 'm': lua_mathlibopen(L1); break;
  337. case 's': lua_strlibopen(L1); break;
  338. case 'i': lua_iolibopen(L1); break;
  339. case 'd': lua_dblibopen(L1); break;
  340. case 'b': lua_baselibopen(L1); break;
  341. default: luaL_argerror(L, 2, "invalid option");
  342. }
  343. return 0;
  344. }
  345. static int closestate (lua_State *L) {
  346. luaL_checktype(L, 1, LUA_TUSERDATA);
  347. lua_close((lua_State *)lua_touserdata(L, 1));
  348. LUA_UNLOCK(L); /* close cannot unlock that */
  349. return 0;
  350. }
  351. static int doremote (lua_State *L) {
  352. lua_State *L1;
  353. const char *code = luaL_check_string(L, 2);
  354. int status;
  355. luaL_checktype(L, 1, LUA_TUSERDATA);
  356. L1 = (lua_State *)lua_touserdata(L, 1);
  357. status = lua_dostring(L1, code);
  358. if (status != 0) {
  359. lua_pushnil(L);
  360. lua_pushnumber(L, status);
  361. return 2;
  362. }
  363. else {
  364. int i = 0;
  365. while (!lua_isnull(L1, ++i))
  366. lua_pushstring(L, lua_tostring(L1, i));
  367. return i-1;
  368. }
  369. }
  370. static int settagmethod (lua_State *L) {
  371. int tag = luaL_check_int(L, 1);
  372. const char *event = luaL_check_string(L, 2);
  373. luaL_checkany(L, 3);
  374. lua_gettagmethod(L, tag, event);
  375. lua_pushvalue(L, 3);
  376. lua_settagmethod(L, tag, event);
  377. return 1;
  378. }
  379. static int pushbool (lua_State *L, int b) {
  380. if (b) lua_pushnumber(L, 1);
  381. else lua_pushnil(L);
  382. return 1;
  383. }
  384. static int equal (lua_State *L) {
  385. return pushbool(L, lua_equal(L, 1, 2));
  386. }
  387. /*
  388. ** {======================================================
  389. ** function to test the API with C. It interprets a kind of "assembler"
  390. ** language with calls to the API, so the test can be driven by Lua code
  391. ** =======================================================
  392. */
  393. static const char *const delimits = " \t\n,;";
  394. static void skip (const char **pc) {
  395. while (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
  396. }
  397. static int getnum (lua_State *L, const char **pc) {
  398. int res = 0;
  399. int sig = 1;
  400. skip(pc);
  401. if (**pc == '.') {
  402. res = (int)lua_tonumber(L, -1);
  403. lua_pop(L, 1);
  404. (*pc)++;
  405. return res;
  406. }
  407. else if (**pc == '-') {
  408. sig = -1;
  409. (*pc)++;
  410. }
  411. while (isdigit(**pc)) res = res*10 + (*(*pc)++) - '0';
  412. return sig*res;
  413. }
  414. static const char *getname (char *buff, const char **pc) {
  415. int i = 0;
  416. skip(pc);
  417. while (**pc != '\0' && !strchr(delimits, **pc))
  418. buff[i++] = *(*pc)++;
  419. buff[i] = '\0';
  420. return buff;
  421. }
  422. #define EQ(s1) (strcmp(s1, inst) == 0)
  423. #define getnum ((getnum)(L, &pc))
  424. #define getname ((getname)(buff, &pc))
  425. static int testC (lua_State *L) {
  426. char buff[30];
  427. const char *pc = luaL_check_string(L, 1);
  428. for (;;) {
  429. const char *inst = getname;
  430. if EQ("") return 0;
  431. else if EQ("isnumber") {
  432. lua_pushnumber(L, lua_isnumber(L, getnum));
  433. }
  434. else if EQ("isstring") {
  435. lua_pushnumber(L, lua_isstring(L, getnum));
  436. }
  437. else if EQ("istable") {
  438. lua_pushnumber(L, lua_istable(L, getnum));
  439. }
  440. else if EQ("iscfunction") {
  441. lua_pushnumber(L, lua_iscfunction(L, getnum));
  442. }
  443. else if EQ("isfunction") {
  444. lua_pushnumber(L, lua_isfunction(L, getnum));
  445. }
  446. else if EQ("isuserdata") {
  447. lua_pushnumber(L, lua_isuserdata(L, getnum));
  448. }
  449. else if EQ("isnil") {
  450. lua_pushnumber(L, lua_isnil(L, getnum));
  451. }
  452. else if EQ("isnull") {
  453. lua_pushnumber(L, lua_isnull(L, getnum));
  454. }
  455. else if EQ("tonumber") {
  456. lua_pushnumber(L, lua_tonumber(L, getnum));
  457. }
  458. else if EQ("tostring") {
  459. const char *s = lua_tostring(L, getnum);
  460. lua_assert((unsigned long)s % 4 == 0); /* check alignment */
  461. lua_pushstring(L, s);
  462. }
  463. else if EQ("tonumber") {
  464. lua_pushnumber(L, lua_tonumber(L, getnum));
  465. }
  466. else if EQ("strlen") {
  467. lua_pushnumber(L, lua_strlen(L, getnum));
  468. }
  469. else if EQ("tocfunction") {
  470. lua_pushcfunction(L, lua_tocfunction(L, getnum));
  471. }
  472. else if EQ("return") {
  473. return getnum;
  474. }
  475. else if EQ("gettop") {
  476. lua_pushnumber(L, lua_gettop(L));
  477. }
  478. else if EQ("settop") {
  479. lua_settop(L, getnum);
  480. }
  481. else if EQ("pop") {
  482. lua_pop(L, getnum);
  483. }
  484. else if EQ("pushnum") {
  485. lua_pushnumber(L, getnum);
  486. }
  487. else if EQ("pushvalue") {
  488. lua_pushvalue(L, getnum);
  489. }
  490. else if EQ("remove") {
  491. lua_remove(L, getnum);
  492. }
  493. else if EQ("insert") {
  494. lua_insert(L, getnum);
  495. }
  496. else if EQ("gettable") {
  497. lua_gettable(L, getnum);
  498. }
  499. else if EQ("settable") {
  500. lua_settable(L, getnum);
  501. }
  502. else if EQ("next") {
  503. lua_next(L, -2);
  504. }
  505. else if EQ("concat") {
  506. lua_concat(L, getnum);
  507. }
  508. else if EQ("lessthan") {
  509. int a = getnum;
  510. if (lua_lessthan(L, a, getnum))
  511. lua_pushnumber(L, 1);
  512. else
  513. lua_pushnil(L);
  514. }
  515. else if EQ("rawcall") {
  516. int narg = getnum;
  517. int nres = getnum;
  518. lua_rawcall(L, narg, nres);
  519. }
  520. else if EQ("call") {
  521. int narg = getnum;
  522. int nres = getnum;
  523. lua_call(L, narg, nres);
  524. }
  525. else if EQ("dostring") {
  526. lua_dostring(L, luaL_check_string(L, getnum));
  527. }
  528. else if EQ("settagmethod") {
  529. int tag = getnum;
  530. const char *event = getname;
  531. lua_settagmethod(L, tag, event);
  532. }
  533. else if EQ("gettagmethod") {
  534. int tag = getnum;
  535. const char *event = getname;
  536. lua_gettagmethod(L, tag, event);
  537. }
  538. else if EQ("type") {
  539. lua_pushstring(L, lua_typename(L, lua_type(L, getnum)));
  540. }
  541. else luaL_verror(L, "unknown instruction %.30s", buff);
  542. }
  543. return 0;
  544. }
  545. /* }====================================================== */
  546. static const struct luaL_reg tests_funcs[] = {
  547. {"hash", hash_query},
  548. {"limits", get_limits},
  549. {"listcode", listcode},
  550. {"liststrings", liststrings},
  551. {"listlocals", listlocals},
  552. {"loadlib", loadlib},
  553. {"querystr", string_query},
  554. {"querytab", table_query},
  555. {"testC", testC},
  556. {"ref", tref},
  557. {"getref", getref},
  558. {"unref", unref},
  559. {"newuserdata", newuserdata},
  560. {"udataval", udataval},
  561. {"newtag", newtag},
  562. {"doonnewstack", doonnewstack},
  563. {"newstate", newstate},
  564. {"closestate", closestate},
  565. {"doremote", doremote},
  566. {"settagmethod", settagmethod},
  567. {"equal", equal},
  568. {"totalmem", mem_query}
  569. };
  570. void luaB_opentests (lua_State *L) {
  571. *((int **)L) = &islocked; /* init lock */
  572. lua_state = L; /* keep first state to be opened */
  573. /* open lib in a new table */
  574. lua_newtable(L);
  575. lua_getglobals(L);
  576. lua_pushvalue(L, -2);
  577. lua_setglobals(L);
  578. luaL_openl(L, tests_funcs); /* open functions inside new table */
  579. lua_setglobals(L); /* restore old table of globals */
  580. lua_setglobal(L, "T"); /* set new table as global T */
  581. }
  582. #endif