ltests.c 16 KB

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