ltests.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. /*
  2. ** $Id: ltests.c,v 1.115 2002/04/02 20:43:08 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. static lua_State *lua_state = NULL;
  30. int islocked = 0;
  31. #define index(L,k) (L->ci->base+(k) - 1)
  32. static void setnameval (lua_State *L, const char *name, int val) {
  33. lua_pushstring(L, name);
  34. lua_pushnumber(L, val);
  35. lua_settable(L, -3);
  36. }
  37. /*
  38. ** {======================================================================
  39. ** Controlled version for realloc.
  40. ** =======================================================================
  41. */
  42. /* ensures maximum alignment for HEADER */
  43. #define HEADER (sizeof(union L_Umaxalign))
  44. #define MARKSIZE 32
  45. #define MARK 0x55 /* 01010101 (a nice pattern) */
  46. #define blocksize(b) (cast(size_t *, b) - HEADER/sizeof(size_t))
  47. unsigned long memdebug_numblocks = 0;
  48. unsigned long memdebug_total = 0;
  49. unsigned long memdebug_maxmem = 0;
  50. unsigned long memdebug_memlimit = ULONG_MAX;
  51. static void *checkblock (void *block) {
  52. size_t *b = blocksize(block);
  53. size_t size = *b;
  54. int i;
  55. for (i=0;i<MARKSIZE;i++)
  56. lua_assert(*(cast(char *, b)+HEADER+size+i) == MARK+i); /* corrupted block? */
  57. return b;
  58. }
  59. static void freeblock (void *block) {
  60. if (block) {
  61. size_t size = *blocksize(block);
  62. block = checkblock(block);
  63. memset(block, -1, size+HEADER+MARKSIZE); /* erase block */
  64. free(block); /* free original block */
  65. memdebug_numblocks--;
  66. memdebug_total -= size;
  67. }
  68. }
  69. void *debug_realloc (void *block, size_t oldsize, size_t size) {
  70. lua_assert((oldsize == 0) ? block == NULL : oldsize == *blocksize(block));
  71. if (size == 0) {
  72. freeblock(block);
  73. return NULL;
  74. }
  75. else if (memdebug_total+size-oldsize > memdebug_memlimit)
  76. return NULL; /* to test memory allocation errors */
  77. else {
  78. void *newblock;
  79. int i;
  80. size_t realsize = HEADER+size+MARKSIZE;
  81. if (realsize < size) return NULL; /* overflow! */
  82. newblock = malloc(realsize); /* alloc a new block */
  83. if (newblock == NULL) return NULL;
  84. if (oldsize > size) oldsize = size;
  85. if (block) {
  86. memcpy(cast(char *, newblock)+HEADER, block, oldsize);
  87. freeblock(block); /* erase (and check) old copy */
  88. }
  89. /* initialize new part of the block with something `weird' */
  90. memset(cast(char *, newblock)+HEADER+oldsize, -MARK, size-oldsize);
  91. memdebug_total += size;
  92. if (memdebug_total > memdebug_maxmem)
  93. memdebug_maxmem = memdebug_total;
  94. memdebug_numblocks++;
  95. *cast(size_t *, newblock) = size;
  96. for (i=0;i<MARKSIZE;i++)
  97. *(cast(char *, newblock)+HEADER+size+i) = cast(char, MARK+i);
  98. return cast(char *, newblock)+HEADER;
  99. }
  100. }
  101. /* }====================================================================== */
  102. /*
  103. ** {======================================================
  104. ** Disassembler
  105. ** =======================================================
  106. */
  107. static char *buildop (Proto *p, int pc, char *buff) {
  108. Instruction i = p->code[pc];
  109. OpCode o = GET_OPCODE(i);
  110. const char *name = luaP_opnames[o];
  111. int line = p->lineinfo[pc];
  112. sprintf(buff, "(%4d) %4d - ", line, pc);
  113. switch (getOpMode(o)) {
  114. case iABC:
  115. sprintf(buff+strlen(buff), "%-12s%4d %4d %4d", name,
  116. GETARG_A(i), GETARG_B(i), GETARG_C(i));
  117. break;
  118. case iABc:
  119. sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), GETARG_Bc(i));
  120. break;
  121. case iAsBc:
  122. sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), GETARG_sBc(i));
  123. break;
  124. }
  125. return buff;
  126. }
  127. static int listcode (lua_State *L) {
  128. int pc;
  129. Proto *p;
  130. luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  131. 1, "Lua function expected");
  132. p = clvalue(index(L, 1))->l.p;
  133. lua_newtable(L);
  134. setnameval(L, "maxstack", p->maxstacksize);
  135. setnameval(L, "numparams", p->numparams);
  136. for (pc=0; pc<p->sizecode; pc++) {
  137. char buff[100];
  138. lua_pushnumber(L, pc+1);
  139. lua_pushstring(L, buildop(p, pc, buff));
  140. lua_settable(L, -3);
  141. }
  142. return 1;
  143. }
  144. static int listk (lua_State *L) {
  145. Proto *p;
  146. int i;
  147. luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  148. 1, "Lua function expected");
  149. p = clvalue(index(L, 1))->l.p;
  150. lua_newtable(L);
  151. for (i=0; i<p->sizek; i++) {
  152. lua_pushnumber(L, i+1);
  153. luaA_pushobject(L, p->k+i);
  154. lua_settable(L, -3);
  155. }
  156. return 1;
  157. }
  158. static int listlocals (lua_State *L) {
  159. Proto *p;
  160. int pc = luaL_check_int(L, 2) - 1;
  161. int i = 0;
  162. const char *name;
  163. luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  164. 1, "Lua function expected");
  165. p = clvalue(index(L, 1))->l.p;
  166. while ((name = luaF_getlocalname(p, ++i, pc)) != NULL)
  167. lua_pushstring(L, name);
  168. return i-1;
  169. }
  170. /* }====================================================== */
  171. static int get_limits (lua_State *L) {
  172. lua_newtable(L);
  173. setnameval(L, "BITS_INT", BITS_INT);
  174. setnameval(L, "LFPF", LFIELDS_PER_FLUSH);
  175. setnameval(L, "MAXVARS", MAXVARS);
  176. setnameval(L, "MAXPARAMS", MAXPARAMS);
  177. setnameval(L, "MAXSTACK", MAXSTACK);
  178. setnameval(L, "MAXUPVALUES", MAXUPVALUES);
  179. return 1;
  180. }
  181. static int mem_query (lua_State *L) {
  182. if (lua_isnone(L, 1)) {
  183. lua_pushnumber(L, memdebug_total);
  184. lua_pushnumber(L, memdebug_numblocks);
  185. lua_pushnumber(L, memdebug_maxmem);
  186. return 3;
  187. }
  188. else {
  189. memdebug_memlimit = luaL_check_int(L, 1);
  190. return 0;
  191. }
  192. }
  193. static int hash_query (lua_State *L) {
  194. if (lua_isnone(L, 2)) {
  195. luaL_arg_check(L, lua_type(L, 1) == LUA_TSTRING, 1, "string expected");
  196. lua_pushnumber(L, tsvalue(index(L, 1))->tsv.hash);
  197. }
  198. else {
  199. TObject *o = index(L, 1);
  200. Table *t;
  201. luaL_check_type(L, 2, LUA_TTABLE);
  202. t = hvalue(index(L, 2));
  203. lua_pushnumber(L, luaH_mainposition(t, o) - t->node);
  204. }
  205. return 1;
  206. }
  207. static int stacklevel (lua_State *L) {
  208. unsigned long a = 0;
  209. lua_pushnumber(L, (int)(L->top - L->stack));
  210. lua_pushnumber(L, (int)(L->stack_last - L->stack));
  211. lua_pushnumber(L, (int)(L->ci - L->base_ci));
  212. lua_pushnumber(L, (int)(L->end_ci - L->base_ci));
  213. lua_pushnumber(L, (unsigned long)&a);
  214. return 5;
  215. }
  216. static int table_query (lua_State *L) {
  217. const Table *t;
  218. int i = luaL_opt_int(L, 2, -1);
  219. luaL_check_type(L, 1, LUA_TTABLE);
  220. t = hvalue(index(L, 1));
  221. if (i == -1) {
  222. lua_pushnumber(L, t->sizearray);
  223. lua_pushnumber(L, sizenode(t));
  224. lua_pushnumber(L, t->firstfree - t->node);
  225. }
  226. else if (i < t->sizearray) {
  227. lua_pushnumber(L, i);
  228. luaA_pushobject(L, &t->array[i]);
  229. lua_pushnil(L);
  230. }
  231. else if ((i -= t->sizearray) < sizenode(t)) {
  232. if (ttype(val(node(t, i))) != LUA_TNIL ||
  233. ttype(key(node(t, i))) == LUA_TNIL ||
  234. ttype(key(node(t, i))) == LUA_TNUMBER) {
  235. luaA_pushobject(L, key(node(t, i)));
  236. }
  237. else
  238. lua_pushstring(L, "<undef>");
  239. luaA_pushobject(L, val(&t->node[i]));
  240. if (t->node[i].next)
  241. lua_pushnumber(L, t->node[i].next - t->node);
  242. else
  243. lua_pushnil(L);
  244. }
  245. return 3;
  246. }
  247. static int string_query (lua_State *L) {
  248. stringtable *tb = &G(L)->strt;
  249. int s = luaL_opt_int(L, 2, 0) - 1;
  250. if (s==-1) {
  251. lua_pushnumber(L ,tb->nuse);
  252. lua_pushnumber(L ,tb->size);
  253. return 2;
  254. }
  255. else if (s < tb->size) {
  256. TString *ts;
  257. int n = 0;
  258. for (ts = tb->hash[s]; ts; ts = ts->tsv.nexthash) {
  259. setsvalue(L->top, ts);
  260. incr_top(L);
  261. n++;
  262. }
  263. return n;
  264. }
  265. return 0;
  266. }
  267. static int tref (lua_State *L) {
  268. int level = lua_gettop(L);
  269. int lock = luaL_opt_int(L, 2, 1);
  270. luaL_check_any(L, 1);
  271. lua_pushvalue(L, 1);
  272. lua_pushnumber(L, lua_ref(L, lock));
  273. assert(lua_gettop(L) == level+1); /* +1 for result */
  274. return 1;
  275. }
  276. static int getref (lua_State *L) {
  277. int level = lua_gettop(L);
  278. lua_getref(L, luaL_check_int(L, 1));
  279. assert(lua_gettop(L) == level+1);
  280. return 1;
  281. }
  282. static int unref (lua_State *L) {
  283. int level = lua_gettop(L);
  284. lua_unref(L, luaL_check_int(L, 1));
  285. assert(lua_gettop(L) == level);
  286. return 0;
  287. }
  288. static int metatable (lua_State *L) {
  289. luaL_check_any(L, 1);
  290. if (lua_isnone(L, 2)) {
  291. if (lua_getmetatable(L, 1) == 0)
  292. lua_pushnil(L);
  293. }
  294. else {
  295. lua_settop(L, 2);
  296. luaL_check_type(L, 2, LUA_TTABLE);
  297. lua_setmetatable(L, 1);
  298. }
  299. return 1;
  300. }
  301. static int newuserdata (lua_State *L) {
  302. size_t size = luaL_check_int(L, 1);
  303. char *p = cast(char *, lua_newuserdata(L, size));
  304. while (size--) *p++ = '\0';
  305. return 1;
  306. }
  307. static int pushuserdata (lua_State *L) {
  308. lua_pushudataval(L, cast(void *, luaL_check_int(L, 1)));
  309. return 1;
  310. }
  311. static int udataval (lua_State *L) {
  312. lua_pushnumber(L, cast(int, lua_touserdata(L, 1)));
  313. return 1;
  314. }
  315. static int doonnewstack (lua_State *L) {
  316. lua_State *L1 = lua_newthread(L);
  317. int status = lua_dostring(L1, luaL_check_string(L, 1));
  318. lua_pushnumber(L, status);
  319. lua_closethread(L, L1);
  320. return 1;
  321. }
  322. static int s2d (lua_State *L) {
  323. lua_pushnumber(L, *cast(const double *, luaL_check_string(L, 1)));
  324. return 1;
  325. }
  326. static int d2s (lua_State *L) {
  327. double d = luaL_check_number(L, 1);
  328. lua_pushlstring(L, cast(char *, &d), sizeof(d));
  329. return 1;
  330. }
  331. static int newstate (lua_State *L) {
  332. lua_State *L1 = lua_open();
  333. if (L1) {
  334. *cast(int **, L1) = &islocked; /* initialize the lock */
  335. lua_pushnumber(L, (unsigned long)L1);
  336. }
  337. else
  338. lua_pushnil(L);
  339. return 1;
  340. }
  341. static int loadlib (lua_State *L) {
  342. lua_State *L1 = cast(lua_State *, cast(unsigned long, luaL_check_number(L, 1)));
  343. lua_register(L1, "mathlibopen", lua_mathlibopen);
  344. lua_register(L1, "strlibopen", lua_strlibopen);
  345. lua_register(L1, "iolibopen", lua_iolibopen);
  346. lua_register(L1, "dblibopen", lua_dblibopen);
  347. lua_register(L1, "baselibopen", lua_baselibopen);
  348. return 0;
  349. }
  350. static int closestate (lua_State *L) {
  351. lua_State *L1 = cast(lua_State *, cast(unsigned long, luaL_check_number(L, 1)));
  352. lua_close(L1);
  353. lua_unlock(L); /* close cannot unlock that */
  354. return 0;
  355. }
  356. static int doremote (lua_State *L) {
  357. lua_State *L1;
  358. const char *code = luaL_check_string(L, 2);
  359. int status;
  360. L1 = cast(lua_State *, cast(unsigned long, luaL_check_number(L, 1)));
  361. status = lua_dostring(L1, code);
  362. if (status != 0) {
  363. lua_pushnil(L);
  364. lua_pushnumber(L, status);
  365. return 2;
  366. }
  367. else {
  368. int i = 0;
  369. while (!lua_isnone(L1, ++i))
  370. lua_pushstring(L, lua_tostring(L1, i));
  371. lua_pop(L1, i-1);
  372. return i-1;
  373. }
  374. }
  375. static int log2_aux (lua_State *L) {
  376. lua_pushnumber(L, luaO_log2(luaL_check_int(L, 1)));
  377. return 1;
  378. }
  379. /*
  380. ** {======================================================
  381. ** function to test the API with C. It interprets a kind of assembler
  382. ** language with calls to the API, so the test can be driven by Lua code
  383. ** =======================================================
  384. */
  385. static const char *const delimits = " \t\n,;";
  386. static void skip (const char **pc) {
  387. while (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
  388. }
  389. static int getnum_aux (lua_State *L, const char **pc) {
  390. int res = 0;
  391. int sig = 1;
  392. skip(pc);
  393. if (**pc == '.') {
  394. res = cast(int, lua_tonumber(L, -1));
  395. lua_pop(L, 1);
  396. (*pc)++;
  397. return res;
  398. }
  399. else if (**pc == '-') {
  400. sig = -1;
  401. (*pc)++;
  402. }
  403. while (isdigit(cast(int, **pc))) res = res*10 + (*(*pc)++) - '0';
  404. return sig*res;
  405. }
  406. static const char *getname_aux (char *buff, const char **pc) {
  407. int i = 0;
  408. skip(pc);
  409. while (**pc != '\0' && !strchr(delimits, **pc))
  410. buff[i++] = *(*pc)++;
  411. buff[i] = '\0';
  412. return buff;
  413. }
  414. #define EQ(s1) (strcmp(s1, inst) == 0)
  415. #define getnum (getnum_aux(L, &pc))
  416. #define getname (getname_aux(buff, &pc))
  417. static int testC (lua_State *L) {
  418. char buff[30];
  419. const char *pc = luaL_check_string(L, 1);
  420. for (;;) {
  421. const char *inst = getname;
  422. if EQ("") return 0;
  423. else if EQ("isnumber") {
  424. lua_pushnumber(L, lua_isnumber(L, getnum));
  425. }
  426. else if EQ("isstring") {
  427. lua_pushnumber(L, lua_isstring(L, getnum));
  428. }
  429. else if EQ("istable") {
  430. lua_pushnumber(L, lua_istable(L, getnum));
  431. }
  432. else if EQ("iscfunction") {
  433. lua_pushnumber(L, lua_iscfunction(L, getnum));
  434. }
  435. else if EQ("isfunction") {
  436. lua_pushnumber(L, lua_isfunction(L, getnum));
  437. }
  438. else if EQ("isuserdata") {
  439. lua_pushnumber(L, lua_isuserdata(L, getnum));
  440. }
  441. else if EQ("isnil") {
  442. lua_pushnumber(L, lua_isnil(L, getnum));
  443. }
  444. else if EQ("isnull") {
  445. lua_pushnumber(L, lua_isnone(L, getnum));
  446. }
  447. else if EQ("tonumber") {
  448. lua_pushnumber(L, lua_tonumber(L, getnum));
  449. }
  450. else if EQ("tostring") {
  451. const char *s = lua_tostring(L, getnum);
  452. lua_pushstring(L, s);
  453. }
  454. else if EQ("tonumber") {
  455. lua_pushnumber(L, lua_tonumber(L, getnum));
  456. }
  457. else if EQ("strlen") {
  458. lua_pushnumber(L, lua_strlen(L, getnum));
  459. }
  460. else if EQ("tocfunction") {
  461. lua_pushcfunction(L, lua_tocfunction(L, getnum));
  462. }
  463. else if EQ("return") {
  464. return getnum;
  465. }
  466. else if EQ("gettop") {
  467. lua_pushnumber(L, lua_gettop(L));
  468. }
  469. else if EQ("settop") {
  470. lua_settop(L, getnum);
  471. }
  472. else if EQ("pop") {
  473. lua_pop(L, getnum);
  474. }
  475. else if EQ("pushnum") {
  476. lua_pushnumber(L, getnum);
  477. }
  478. else if EQ("pushnil") {
  479. lua_pushnil(L);
  480. }
  481. else if EQ("pushbool") {
  482. lua_pushboolean(L, getnum);
  483. }
  484. else if EQ("tobool") {
  485. lua_pushnumber(L, lua_toboolean(L, getnum));
  486. }
  487. else if EQ("pushvalue") {
  488. lua_pushvalue(L, getnum);
  489. }
  490. else if EQ("pushcclosure") {
  491. lua_pushcclosure(L, testC, getnum);
  492. }
  493. else if EQ("pushupvalues") {
  494. lua_pushupvalues(L);
  495. }
  496. else if EQ("remove") {
  497. lua_remove(L, getnum);
  498. }
  499. else if EQ("insert") {
  500. lua_insert(L, getnum);
  501. }
  502. else if EQ("replace") {
  503. lua_replace(L, getnum);
  504. }
  505. else if EQ("gettable") {
  506. lua_gettable(L, getnum);
  507. }
  508. else if EQ("settable") {
  509. lua_settable(L, getnum);
  510. }
  511. else if EQ("next") {
  512. lua_next(L, -2);
  513. }
  514. else if EQ("concat") {
  515. lua_concat(L, getnum);
  516. }
  517. else if EQ("lessthan") {
  518. int a = getnum;
  519. lua_pushboolean(L, lua_lessthan(L, a, getnum));
  520. }
  521. else if EQ("equal") {
  522. int a = getnum;
  523. lua_pushboolean(L, lua_equal(L, a, getnum));
  524. }
  525. else if EQ("rawcall") {
  526. int narg = getnum;
  527. int nres = getnum;
  528. lua_rawcall(L, narg, nres);
  529. }
  530. else if EQ("call") {
  531. int narg = getnum;
  532. int nres = getnum;
  533. lua_call(L, narg, nres);
  534. }
  535. else if EQ("dostring") {
  536. lua_dostring(L, luaL_check_string(L, getnum));
  537. }
  538. else if EQ("setmetatable") {
  539. lua_setmetatable(L, getnum);
  540. }
  541. else if EQ("getmetatable") {
  542. if (lua_getmetatable(L, getnum) == 0)
  543. lua_pushnil(L);
  544. }
  545. else if EQ("type") {
  546. lua_pushstring(L, lua_typename(L, lua_type(L, getnum)));
  547. }
  548. else luaL_verror(L, "unknown instruction %.30s", buff);
  549. }
  550. return 0;
  551. }
  552. /* }====================================================== */
  553. static const struct luaL_reg tests_funcs[] = {
  554. {"hash", hash_query},
  555. {"limits", get_limits},
  556. {"listcode", listcode},
  557. {"listk", listk},
  558. {"listlocals", listlocals},
  559. {"loadlib", loadlib},
  560. {"stacklevel", stacklevel},
  561. {"querystr", string_query},
  562. {"querytab", table_query},
  563. {"testC", testC},
  564. {"ref", tref},
  565. {"getref", getref},
  566. {"unref", unref},
  567. {"d2s", d2s},
  568. {"s2d", s2d},
  569. {"metatable", metatable},
  570. {"newuserdata", newuserdata},
  571. {"pushuserdata", pushuserdata},
  572. {"udataval", udataval},
  573. {"doonnewstack", doonnewstack},
  574. {"newstate", newstate},
  575. {"closestate", closestate},
  576. {"doremote", doremote},
  577. {"log2", log2_aux},
  578. {"totalmem", mem_query},
  579. {NULL, NULL}
  580. };
  581. static void fim (void) {
  582. if (!islocked)
  583. lua_close(lua_state);
  584. lua_assert(memdebug_numblocks == 0);
  585. lua_assert(memdebug_total == 0);
  586. }
  587. void luaB_opentests (lua_State *L) {
  588. *cast(int **, L) = &islocked; /* init lock */
  589. lua_state = L; /* keep first state to be opened */
  590. luaL_opennamedlib(L, "T", tests_funcs, 0);
  591. atexit(fim);
  592. }
  593. #endif