ltests.c 16 KB

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