xref: /freebsd/contrib/lua/src/ltests.h (revision a9490b81b032b43cdb3c8c76b4d01bbad9ff82c1)
1*a9490b81SWarner Losh /*
2*a9490b81SWarner Losh ** $Id: ltests.h $
3*a9490b81SWarner Losh ** Internal Header for Debugging of the Lua Implementation
4*a9490b81SWarner Losh ** See Copyright Notice in lua.h
5*a9490b81SWarner Losh */
6*a9490b81SWarner Losh 
7*a9490b81SWarner Losh #ifndef ltests_h
8*a9490b81SWarner Losh #define ltests_h
9*a9490b81SWarner Losh 
10*a9490b81SWarner Losh 
11*a9490b81SWarner Losh #include <stdio.h>
12*a9490b81SWarner Losh #include <stdlib.h>
13*a9490b81SWarner Losh 
14*a9490b81SWarner Losh /* test Lua with compatibility code */
15*a9490b81SWarner Losh #define LUA_COMPAT_MATHLIB
16*a9490b81SWarner Losh #define LUA_COMPAT_LT_LE
17*a9490b81SWarner Losh 
18*a9490b81SWarner Losh 
19*a9490b81SWarner Losh #define LUA_DEBUG
20*a9490b81SWarner Losh 
21*a9490b81SWarner Losh 
22*a9490b81SWarner Losh /* turn on assertions */
23*a9490b81SWarner Losh #define LUAI_ASSERT
24*a9490b81SWarner Losh 
25*a9490b81SWarner Losh 
26*a9490b81SWarner Losh /* to avoid warnings, and to make sure value is really unused */
27*a9490b81SWarner Losh #define UNUSED(x)       (x=0, (void)(x))
28*a9490b81SWarner Losh 
29*a9490b81SWarner Losh 
30*a9490b81SWarner Losh /* test for sizes in 'l_sprintf' (make sure whole buffer is available) */
31*a9490b81SWarner Losh #undef l_sprintf
32*a9490b81SWarner Losh #if !defined(LUA_USE_C89)
33*a9490b81SWarner Losh #define l_sprintf(s,sz,f,i)	(memset(s,0xAB,sz), snprintf(s,sz,f,i))
34*a9490b81SWarner Losh #else
35*a9490b81SWarner Losh #define l_sprintf(s,sz,f,i)	(memset(s,0xAB,sz), sprintf(s,f,i))
36*a9490b81SWarner Losh #endif
37*a9490b81SWarner Losh 
38*a9490b81SWarner Losh 
39*a9490b81SWarner Losh /* get a chance to test code without jump tables */
40*a9490b81SWarner Losh #define LUA_USE_JUMPTABLE	0
41*a9490b81SWarner Losh 
42*a9490b81SWarner Losh 
43*a9490b81SWarner Losh /* use 32-bit integers in random generator */
44*a9490b81SWarner Losh #define LUA_RAND32
45*a9490b81SWarner Losh 
46*a9490b81SWarner Losh 
47*a9490b81SWarner Losh /* memory-allocator control variables */
48*a9490b81SWarner Losh typedef struct Memcontrol {
49*a9490b81SWarner Losh   int failnext;
50*a9490b81SWarner Losh   unsigned long numblocks;
51*a9490b81SWarner Losh   unsigned long total;
52*a9490b81SWarner Losh   unsigned long maxmem;
53*a9490b81SWarner Losh   unsigned long memlimit;
54*a9490b81SWarner Losh   unsigned long countlimit;
55*a9490b81SWarner Losh   unsigned long objcount[LUA_NUMTYPES];
56*a9490b81SWarner Losh } Memcontrol;
57*a9490b81SWarner Losh 
58*a9490b81SWarner Losh LUA_API Memcontrol l_memcontrol;
59*a9490b81SWarner Losh 
60*a9490b81SWarner Losh 
61*a9490b81SWarner Losh /*
62*a9490b81SWarner Losh ** generic variable for debug tricks
63*a9490b81SWarner Losh */
64*a9490b81SWarner Losh extern void *l_Trick;
65*a9490b81SWarner Losh 
66*a9490b81SWarner Losh 
67*a9490b81SWarner Losh 
68*a9490b81SWarner Losh /*
69*a9490b81SWarner Losh ** Function to traverse and check all memory used by Lua
70*a9490b81SWarner Losh */
71*a9490b81SWarner Losh LUAI_FUNC int lua_checkmemory (lua_State *L);
72*a9490b81SWarner Losh 
73*a9490b81SWarner Losh /*
74*a9490b81SWarner Losh ** Function to print an object GC-friendly
75*a9490b81SWarner Losh */
76*a9490b81SWarner Losh struct GCObject;
77*a9490b81SWarner Losh LUAI_FUNC void lua_printobj (lua_State *L, struct GCObject *o);
78*a9490b81SWarner Losh 
79*a9490b81SWarner Losh 
80*a9490b81SWarner Losh /* test for lock/unlock */
81*a9490b81SWarner Losh 
82*a9490b81SWarner Losh struct L_EXTRA { int lock; int *plock; };
83*a9490b81SWarner Losh #undef LUA_EXTRASPACE
84*a9490b81SWarner Losh #define LUA_EXTRASPACE	sizeof(struct L_EXTRA)
85*a9490b81SWarner Losh #define getlock(l)	cast(struct L_EXTRA*, lua_getextraspace(l))
86*a9490b81SWarner Losh #define luai_userstateopen(l)  \
87*a9490b81SWarner Losh 	(getlock(l)->lock = 0, getlock(l)->plock = &(getlock(l)->lock))
88*a9490b81SWarner Losh #define luai_userstateclose(l)  \
89*a9490b81SWarner Losh   lua_assert(getlock(l)->lock == 1 && getlock(l)->plock == &(getlock(l)->lock))
90*a9490b81SWarner Losh #define luai_userstatethread(l,l1) \
91*a9490b81SWarner Losh   lua_assert(getlock(l1)->plock == getlock(l)->plock)
92*a9490b81SWarner Losh #define luai_userstatefree(l,l1) \
93*a9490b81SWarner Losh   lua_assert(getlock(l)->plock == getlock(l1)->plock)
94*a9490b81SWarner Losh #define lua_lock(l)     lua_assert((*getlock(l)->plock)++ == 0)
95*a9490b81SWarner Losh #define lua_unlock(l)   lua_assert(--(*getlock(l)->plock) == 0)
96*a9490b81SWarner Losh 
97*a9490b81SWarner Losh 
98*a9490b81SWarner Losh 
99*a9490b81SWarner Losh LUA_API int luaB_opentests (lua_State *L);
100*a9490b81SWarner Losh 
101*a9490b81SWarner Losh LUA_API void *debug_realloc (void *ud, void *block,
102*a9490b81SWarner Losh                              size_t osize, size_t nsize);
103*a9490b81SWarner Losh 
104*a9490b81SWarner Losh #if defined(lua_c)
105*a9490b81SWarner Losh #define luaL_newstate()		lua_newstate(debug_realloc, &l_memcontrol)
106*a9490b81SWarner Losh #define luaL_openlibs(L)  \
107*a9490b81SWarner Losh   { (luaL_openlibs)(L); \
108*a9490b81SWarner Losh      luaL_requiref(L, "T", luaB_opentests, 1); \
109*a9490b81SWarner Losh      lua_pop(L, 1); }
110*a9490b81SWarner Losh #endif
111*a9490b81SWarner Losh 
112*a9490b81SWarner Losh 
113*a9490b81SWarner Losh 
114*a9490b81SWarner Losh /* change some sizes to give some bugs a chance */
115*a9490b81SWarner Losh 
116*a9490b81SWarner Losh #undef LUAL_BUFFERSIZE
117*a9490b81SWarner Losh #define LUAL_BUFFERSIZE		23
118*a9490b81SWarner Losh #define MINSTRTABSIZE		2
119*a9490b81SWarner Losh #define MAXIWTHABS		3
120*a9490b81SWarner Losh 
121*a9490b81SWarner Losh #define STRCACHE_N	23
122*a9490b81SWarner Losh #define STRCACHE_M	5
123*a9490b81SWarner Losh 
124*a9490b81SWarner Losh #undef LUAI_USER_ALIGNMENT_T
125*a9490b81SWarner Losh #define LUAI_USER_ALIGNMENT_T   union { char b[sizeof(void*) * 8]; }
126*a9490b81SWarner Losh 
127*a9490b81SWarner Losh 
128*a9490b81SWarner Losh /*
129*a9490b81SWarner Losh ** This one is not compatible with tests for opcode optimizations,
130*a9490b81SWarner Losh ** as it blocks some optimizations
131*a9490b81SWarner Losh #define MAXINDEXRK	0
132*a9490b81SWarner Losh */
133*a9490b81SWarner Losh 
134*a9490b81SWarner Losh 
135*a9490b81SWarner Losh /* make stack-overflow tests run faster */
136*a9490b81SWarner Losh #undef LUAI_MAXSTACK
137*a9490b81SWarner Losh #define LUAI_MAXSTACK   50000
138*a9490b81SWarner Losh 
139*a9490b81SWarner Losh 
140*a9490b81SWarner Losh /* test mode uses more stack space */
141*a9490b81SWarner Losh #undef LUAI_MAXCCALLS
142*a9490b81SWarner Losh #define LUAI_MAXCCALLS	180
143*a9490b81SWarner Losh 
144*a9490b81SWarner Losh 
145*a9490b81SWarner Losh /* force Lua to use its own implementations */
146*a9490b81SWarner Losh #undef lua_strx2number
147*a9490b81SWarner Losh #undef lua_number2strx
148*a9490b81SWarner Losh 
149*a9490b81SWarner Losh 
150*a9490b81SWarner Losh #endif
151*a9490b81SWarner Losh 
152