ltests.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. /*
  2. ** $Id: ltests.c,v 1.119 2002/05/02 13:06:20 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 = getline(p, 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 iABx:
  119. sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), GETARG_Bx(i));
  120. break;
  121. case iAsBx:
  122. sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), GETARG_sBx(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. size_t l;
  318. const char *s = luaL_check_lstr(L, 1, &l);
  319. int status = lua_loadbuffer(L1, s, l, s);
  320. if (status == 0)
  321. status = lua_pcall(L1, 0, 0, 0);
  322. lua_pushnumber(L, status);
  323. lua_closethread(L, L1);
  324. return 1;
  325. }
  326. static int s2d (lua_State *L) {
  327. lua_pushnumber(L, *cast(const double *, luaL_check_string(L, 1)));
  328. return 1;
  329. }
  330. static int d2s (lua_State *L) {
  331. double d = luaL_check_number(L, 1);
  332. lua_pushlstring(L, cast(char *, &d), sizeof(d));
  333. return 1;
  334. }
  335. static int newstate (lua_State *L) {
  336. lua_State *L1 = lua_open();
  337. if (L1) {
  338. *cast(int **, L1) = &islocked; /* initialize the lock */
  339. lua_pushnumber(L, (unsigned long)L1);
  340. }
  341. else
  342. lua_pushnil(L);
  343. return 1;
  344. }
  345. static int loadlib (lua_State *L) {
  346. lua_State *L1 = cast(lua_State *, cast(unsigned long, luaL_check_number(L, 1)));
  347. lua_register(L1, "mathlibopen", lua_mathlibopen);
  348. lua_register(L1, "strlibopen", lua_strlibopen);
  349. lua_register(L1, "iolibopen", lua_iolibopen);
  350. lua_register(L1, "dblibopen", lua_dblibopen);
  351. lua_register(L1, "baselibopen", lua_baselibopen);
  352. return 0;
  353. }
  354. static int closestate (lua_State *L) {
  355. lua_State *L1 = cast(lua_State *, cast(unsigned long, luaL_check_number(L, 1)));
  356. lua_close(L1);
  357. lua_unlock(L); /* close cannot unlock that */
  358. return 0;
  359. }
  360. static int doremote (lua_State *L) {
  361. lua_State *L1;
  362. const char *code = luaL_check_string(L, 2);
  363. int status;
  364. L1 = cast(lua_State *, cast(unsigned long, luaL_check_number(L, 1)));
  365. status = lua_dostring(L1, code);
  366. if (status != 0) {
  367. lua_pushnil(L);
  368. lua_pushnumber(L, status);
  369. return 2;
  370. }
  371. else {
  372. int i = 0;
  373. while (!lua_isnone(L1, ++i))
  374. lua_pushstring(L, lua_tostring(L1, i));
  375. lua_pop(L1, i-1);
  376. return i-1;
  377. }
  378. }
  379. static int log2_aux (lua_State *L) {
  380. lua_pushnumber(L, luaO_log2(luaL_check_int(L, 1)));
  381. return 1;
  382. }
  383. /*
  384. ** {======================================================
  385. ** function to test the API with C. It interprets a kind of assembler
  386. ** language with calls to the API, so the test can be driven by Lua code
  387. ** =======================================================
  388. */
  389. static const char *const delimits = " \t\n,;";
  390. static void skip (const char **pc) {
  391. while (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
  392. }
  393. static int getnum_aux (lua_State *L, const char **pc) {
  394. int res = 0;
  395. int sig = 1;
  396. skip(pc);
  397. if (**pc == '.') {
  398. res = cast(int, lua_tonumber(L, -1));
  399. lua_pop(L, 1);
  400. (*pc)++;
  401. return res;
  402. }
  403. else if (**pc == '-') {
  404. sig = -1;
  405. (*pc)++;
  406. }
  407. while (isdigit(cast(int, **pc))) res = res*10 + (*(*pc)++) - '0';
  408. return sig*res;
  409. }
  410. static const char *getname_aux (char *buff, const char **pc) {
  411. int i = 0;
  412. skip(pc);
  413. while (**pc != '\0' && !strchr(delimits, **pc))
  414. buff[i++] = *(*pc)++;
  415. buff[i] = '\0';
  416. return buff;
  417. }
  418. #define EQ(s1) (strcmp(s1, inst) == 0)
  419. #define getnum (getnum_aux(L, &pc))
  420. #define getname (getname_aux(buff, &pc))
  421. static int testC (lua_State *L) {
  422. char buff[30];
  423. const char *pc = luaL_check_string(L, 1);
  424. for (;;) {
  425. const char *inst = getname;
  426. if EQ("") return 0;
  427. else if EQ("isnumber") {
  428. lua_pushnumber(L, lua_isnumber(L, getnum));
  429. }
  430. else if EQ("isstring") {
  431. lua_pushnumber(L, lua_isstring(L, getnum));
  432. }
  433. else if EQ("istable") {
  434. lua_pushnumber(L, lua_istable(L, getnum));
  435. }
  436. else if EQ("iscfunction") {
  437. lua_pushnumber(L, lua_iscfunction(L, getnum));
  438. }
  439. else if EQ("isfunction") {
  440. lua_pushnumber(L, lua_isfunction(L, getnum));
  441. }
  442. else if EQ("isuserdata") {
  443. lua_pushnumber(L, lua_isuserdata(L, getnum));
  444. }
  445. else if EQ("isnil") {
  446. lua_pushnumber(L, lua_isnil(L, getnum));
  447. }
  448. else if EQ("isnull") {
  449. lua_pushnumber(L, lua_isnone(L, getnum));
  450. }
  451. else if EQ("tonumber") {
  452. lua_pushnumber(L, lua_tonumber(L, getnum));
  453. }
  454. else if EQ("tostring") {
  455. const char *s = lua_tostring(L, getnum);
  456. lua_pushstring(L, s);
  457. }
  458. else if EQ("tonumber") {
  459. lua_pushnumber(L, lua_tonumber(L, getnum));
  460. }
  461. else if EQ("strlen") {
  462. lua_pushnumber(L, lua_strlen(L, getnum));
  463. }
  464. else if EQ("tocfunction") {
  465. lua_pushcfunction(L, lua_tocfunction(L, getnum));
  466. }
  467. else if EQ("return") {
  468. return getnum;
  469. }
  470. else if EQ("gettop") {
  471. lua_pushnumber(L, lua_gettop(L));
  472. }
  473. else if EQ("settop") {
  474. lua_settop(L, getnum);
  475. }
  476. else if EQ("pop") {
  477. lua_pop(L, getnum);
  478. }
  479. else if EQ("pushnum") {
  480. lua_pushnumber(L, getnum);
  481. }
  482. else if EQ("pushnil") {
  483. lua_pushnil(L);
  484. }
  485. else if EQ("pushbool") {
  486. lua_pushboolean(L, getnum);
  487. }
  488. else if EQ("tobool") {
  489. lua_pushnumber(L, lua_toboolean(L, getnum));
  490. }
  491. else if EQ("pushvalue") {
  492. lua_pushvalue(L, getnum);
  493. }
  494. else if EQ("pushcclosure") {
  495. lua_pushcclosure(L, testC, getnum);
  496. }
  497. else if EQ("pushupvalues") {
  498. lua_pushupvalues(L);
  499. }
  500. else if EQ("remove") {
  501. lua_remove(L, getnum);
  502. }
  503. else if EQ("insert") {
  504. lua_insert(L, getnum);
  505. }
  506. else if EQ("replace") {
  507. lua_replace(L, getnum);
  508. }
  509. else if EQ("gettable") {
  510. lua_gettable(L, getnum);
  511. }
  512. else if EQ("settable") {
  513. lua_settable(L, getnum);
  514. }
  515. else if EQ("next") {
  516. lua_next(L, -2);
  517. }
  518. else if EQ("concat") {
  519. lua_concat(L, getnum);
  520. }
  521. else if EQ("lessthan") {
  522. int a = getnum;
  523. lua_pushboolean(L, lua_lessthan(L, a, getnum));
  524. }
  525. else if EQ("equal") {
  526. int a = getnum;
  527. lua_pushboolean(L, lua_equal(L, a, getnum));
  528. }
  529. else if EQ("rawcall") {
  530. int narg = getnum;
  531. int nres = getnum;
  532. lua_rawcall(L, narg, nres);
  533. }
  534. else if EQ("call") {
  535. int narg = getnum;
  536. int nres = getnum;
  537. lua_call(L, narg, nres);
  538. }
  539. else if EQ("dostring") {
  540. lua_dostring(L, luaL_check_string(L, getnum));
  541. }
  542. else if EQ("setmetatable") {
  543. lua_setmetatable(L, getnum);
  544. }
  545. else if EQ("getmetatable") {
  546. if (lua_getmetatable(L, getnum) == 0)
  547. lua_pushnil(L);
  548. }
  549. else if EQ("type") {
  550. lua_pushstring(L, lua_typename(L, lua_type(L, getnum)));
  551. }
  552. else luaL_verror(L, "unknown instruction %s", buff);
  553. }
  554. return 0;
  555. }
  556. /* }====================================================== */
  557. static const struct luaL_reg tests_funcs[] = {
  558. {"hash", hash_query},
  559. {"limits", get_limits},
  560. {"listcode", listcode},
  561. {"listk", listk},
  562. {"listlocals", listlocals},
  563. {"loadlib", loadlib},
  564. {"stacklevel", stacklevel},
  565. {"querystr", string_query},
  566. {"querytab", table_query},
  567. {"testC", testC},
  568. {"ref", tref},
  569. {"getref", getref},
  570. {"unref", unref},
  571. {"d2s", d2s},
  572. {"s2d", s2d},
  573. {"metatable", metatable},
  574. {"newuserdata", newuserdata},
  575. {"pushuserdata", pushuserdata},
  576. {"udataval", udataval},
  577. {"doonnewstack", doonnewstack},
  578. {"newstate", newstate},
  579. {"closestate", closestate},
  580. {"doremote", doremote},
  581. {"log2", log2_aux},
  582. {"totalmem", mem_query},
  583. {NULL, NULL}
  584. };
  585. static void fim (void) {
  586. if (!islocked)
  587. lua_close(lua_state);
  588. lua_assert(memdebug_numblocks == 0);
  589. lua_assert(memdebug_total == 0);
  590. }
  591. void luaB_opentests (lua_State *L) {
  592. *cast(int **, L) = &islocked; /* init lock */
  593. lua_state = L; /* keep first state to be opened */
  594. luaL_opennamedlib(L, "T", tests_funcs, 0);
  595. atexit(fim);
  596. }
  597. #endif