History

tests: add memory leak test

Also ... tcctest.c: - exclude stuff that gcc doesn't compile on windows. libtcc.c/tccpp.c: - use unsigned for memory sizes to avoid printf format warnings - use "file:line: message" to make IDE error parsers happy. tccgen.c: fix typo

grischka committed 9 years ago

Commit a1c12b91 parent f7fc4f07 files · +54 −37Browse files

Unified · Split

7 files
  1. libtcc.c +13 −10
  2. tcc.h +1 −0
  3. tccgen.c +5 −8
  4. tccpp.c +19 −16
  5. tests/Makefile +8 −1
  6. tests/tcctest.c +7 −1
  7. tests/tests2/Makefile +1 −1
libtcc.c+13−10
@@ -248,20 +248,20 @@ PUB_FUNC void tcc_memstats(int bench) · 247 unmodified lines
248248 #define MEM_DEBUG_FILE_LEN 15
249249
250250 struct mem_debug_header {
251− size_t magic1;
252− size_t size;
251+ unsigned magic1;
252+ unsigned size;
253253 struct mem_debug_header *prev;
254254 struct mem_debug_header *next;
255− size_t line_num;
256− char file_name[MEM_DEBUG_FILE_LEN + 1];
257− size_t magic2;
255+ int line_num;
256+ char file_name[MEM_DEBUG_FILE_LEN + 1];
257+ unsigned magic2;
258258 };
259259
260260 typedef struct mem_debug_header mem_debug_header_t;
261261
262262 static mem_debug_header_t *mem_debug_chain;
263−static size_t mem_cur_size;
264−static size_t mem_max_size;
263+static unsigned mem_cur_size;
264+static unsigned mem_max_size;
265265
266266 PUB_FUNC void *tcc_malloc_debug(unsigned long size, const char *file, int line)
267267 {
@@ -312,13 +312,13 @@ PUB_FUNC void tcc_free_debug(void *ptr) · 44 unmodified lines
312312 header = (mem_debug_header_t *)ptr;
313313 if (header->magic1 != MEM_DEBUG_MAGIC1 ||
314314 header->magic2 != MEM_DEBUG_MAGIC2 ||
315− header->size == (size_t)-1 )
315+ header->size == (unsigned)-1 )
316316 {
317317 tcc_error("tcc_free check failed");
318318 }
319319
320320 mem_cur_size -= header->size;
321− header->size = (size_t)-1;
321+ header->size = (unsigned)-1;
322322
323323 if (header->next)
324324 header->next->prev = header->prev;
@@ -355,7 +355,7 @@ PUB_FUNC void *tcc_realloc_debug(void *ptr, unsigned long size, const char *file · 30 unmodified lines
355355 header = (mem_debug_header_t *)ptr;
356356 if (header->magic1 != MEM_DEBUG_MAGIC1 ||
357357 header->magic2 != MEM_DEBUG_MAGIC2 ||
358− header->size == (size_t)-1 )
358+ header->size == (unsigned)-1 )
359359 {
360360 check_error:
361361 tcc_error("tcc_realloc check failed");
@@ -414,6 +414,9 @@ PUB_FUNC void tcc_memstats(int bench) · 52 unmodified lines
414414 header->file_name, header->line_num, header->size);
415415 header = header->next;
416416 }
417+#if MEM_DEBUG-0 == 2
418+ exit(2);
419+#endif
417420 }
418421 else if (bench)
419422 fprintf(stderr, "mem_max_size= %d bytes\n", mem_max_size);
tcc.h+1−0
@@ -71,6 +71,7 @@ · 70 unmodified lines
7171 # pragma warning (disable : 4996) // The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name
7272 # pragma warning (disable : 4018) // signed/unsigned mismatch
7373 # pragma warning (disable : 4146) // unary minus operator applied to unsigned type, result still unsigned
74+# define ssize_t intptr_t
7475 # endif
7576 # undef CONFIG_TCC_STATIC
7677 #endif
tccgen.c+5−8
@@ -5288,8 +5288,8 @@ static void expr_cond(void) · 5287 unmodified lines
52885288
52895289 /* this is horrible, but we must also convert first
52905290 operand */
5291− vpushv(&sv);
5292− if (c != 2) {
5291+ if (c != 0) {
5292+ *vtop = sv;
52935293 gen_cast(&type);
52945294 if (islv) {
52955295 mk_pointer(&vtop->type);
@@ -5298,17 +5298,14 @@ static void expr_cond(void) · 2 unmodified lines
52985298 gaddrof();
52995299 }
53005300
5301− if (c != 0)
5302− vswap();
5303− vtop--;
53045301 if (c < 0) {
53055302 r1 = gv(rc);
53065303 move_reg(r2, r1, type.t);
53075304 vtop->r = r2;
5305+ gsym(tt);
5306+ if (islv)
5307+ indir();
53085308 }
5309− gsym(tt);
5310− if (islv)
5311− indir();
53125309 }
53135310 }
53145311 }
tccpp.c+19−16
@@ -137,22 +137,22 @@ ST_FUNC void expect(const char *msg) · 136 unmodified lines
137137 #define CSTR_TAL_LIMIT 1024
138138
139139 typedef struct TinyAlloc {
140− size_t limit;
141− size_t size;
140+ unsigned limit;
141+ unsigned size;
142142 uint8_t *buffer;
143143 uint8_t *p;
144− size_t nb_allocs;
144+ unsigned nb_allocs;
145145 struct TinyAlloc *next, *top;
146146 #ifdef TAL_INFO
147− size_t nb_peak;
148− size_t nb_total;
149− size_t nb_missed;
147+ unsigned nb_peak;
148+ unsigned nb_total;
149+ unsigned nb_missed;
150150 uint8_t *peak_p;
151151 #endif
152152 } TinyAlloc;
153153
154154 typedef struct tal_header_t {
155− size_t size;
155+ unsigned size;
156156 #ifdef TAL_DEBUG
157157 int line_num; /* negative line_num used for double free check */
158158 char file_name[TAL_DEBUG_FILE_LEN + 1];
@@ -161,7 +161,7 @@ typedef struct tal_header_t { · 2 unmodified lines
161161
162162 /* ------------------------------------------------------------------------- */
163163
164−ST_FUNC TinyAlloc *tal_new(TinyAlloc **pal, size_t limit, size_t size)
164+static TinyAlloc *tal_new(TinyAlloc **pal, unsigned limit, unsigned size)
165165 {
166166 TinyAlloc *al = tcc_mallocz(sizeof(TinyAlloc));
167167 al->p = al->buffer = tcc_malloc(size);
@@ -171,7 +171,7 @@ ST_FUNC TinyAlloc *tal_new(TinyAlloc **pal, size_t limit, size_t size) · 3 unmodified lines
171171 return al;
172172 }
173173
174−ST_FUNC void tal_delete(TinyAlloc *al)
174+static void tal_delete(TinyAlloc *al)
175175 {
176176 TinyAlloc *next;
177177
@@ -192,11 +192,14 @@ tail_call: · 14 unmodified lines
192192 while (p < al->p) {
193193 tal_header_t *header = (tal_header_t *)p;
194194 if (header->line_num > 0) {
195− fprintf(stderr, " file %s, line %u: %u bytes\n",
195+ fprintf(stderr, "%s:%d: chunk of %d bytes\n",
196196 header->file_name, header->line_num, header->size);
197197 }
198198 p += header->size + sizeof(tal_header_t);
199199 }
200+#if MEM_DEBUG-0 == 2
201+ exit(2);
202+#endif
200203 }
201204 #endif
202205 next = al->next;
@@ -206,7 +209,7 @@ tail_call: · 3 unmodified lines
206209 goto tail_call;
207210 }
208211
209−ST_FUNC void tal_free_impl(TinyAlloc *al, void *p TAL_DEBUG_PARAMS)
212+static void tal_free_impl(TinyAlloc *al, void *p TAL_DEBUG_PARAMS)
210213 {
211214 if (!p)
212215 return;
@@ -215,10 +218,10 @@ tail_call: · 2 unmodified lines
215218 #ifdef TAL_DEBUG
216219 tal_header_t *header = (((tal_header_t *)p) - 1);
217220 if (header->line_num < 0) {
218− fprintf(stderr, "TAL_DEBUG: file %s, line %u double frees chunk from\n",
221+ fprintf(stderr, "%s:%d: TAL_DEBUG: double frees chunk from\n",
219222 file, line);
220− fprintf(stderr, " file %s, line %u: %u bytes\n",
221− header->file_name, -header->line_num, header->size);
223+ fprintf(stderr, "%s:%d: %d bytes\n",
224+ header->file_name, (int)-header->line_num, (int)header->size);
222225 } else
223226 header->line_num = -header->line_num;
224227 #endif
@@ -233,12 +236,12 @@ tail_call: · 8 unmodified lines
233236 tcc_free(p);
234237 }
235238
236−ST_FUNC void *tal_realloc_impl(TinyAlloc **pal, void *p, size_t size TAL_DEBUG_PARAMS)
239+static void *tal_realloc_impl(TinyAlloc **pal, void *p, unsigned size TAL_DEBUG_PARAMS)
237240 {
238241 tal_header_t *header;
239242 void *ret;
240243 int is_own;
241− size_t adj_size = (size + 3) & -4;
244+ unsigned adj_size = (size + 3) & -4;
242245 TinyAlloc *al = *pal;
243246
244247 tail_call:
tests/Makefile+8−1
@@ -5,7 +5,7 @@ · 4 unmodified lines
55 TOP = ..
66 include $(TOP)/Makefile
77 VPATH = $(TOPSRC)/tests $(TOPSRC) $(TOP)
8−CFLAGS = -I$(TOPSRC) -I$(TOP)
8+CFLAGS := $(filter-out -W% -g% -O%,$(CFLAGS)) -I$(TOPSRC)
99
1010 # what tests to run
1111 TESTS = \
@@ -13,6 +13,7 @@ TESTS = \ · 1 unmodified line
1313 hello-run \
1414 libtest \
1515 test3 \
16+ memtest \
1617 dlltest \
1718 abitest \
1819 vla_test-run \
@@ -145,6 +146,12 @@ ifndef CONFIG_WIN32 · 126 unmodified lines
145146 endif
146147 @rm tcc2$(EXESUF) libtcc2$(DLLSUF)
147148
149+memtest:
150+ @echo ------------ $@ ------------
151+ $(CC) $(CFLAGS) $(NATIVE_DEFINES) -DONE_SOURCE -DMEM_DEBUG=2 ../tcc.c $(LIBS) -o memtest-tcc$(EXESUF)
152+ ./memtest-tcc$(EXESUF) $(TCCFLAGS) $(NATIVE_DEFINES) -DONE_SOURCE ../tcc.c $(LIBS)
153+ ./memtest-tcc$(EXESUF) $(TCCFLAGS) $(NATIVE_DEFINES) -DONE_SOURCE -run ../tcc.c $(TCCFLAGS) tcctest.c
154+
148155
149156 # memory and bound check auto test
150157 BOUNDS_OK = 1 4 8 10 14
tests/tcctest.c+7−1
@@ -3122,6 +3122,7 @@ void other_constraints_test(void) · 3121 unmodified lines
31223122 printf ("oc1: %d\n", ret == (unsigned long)&var);
31233123 }
31243124
3125+#ifndef _WIN32
31253126 /* Test global asm blocks playing with aliases. */
31263127 void base_func(void)
31273128 {
@@ -3161,6 +3162,7 @@ char * get_asm_string (void) · 33 unmodified lines
31613162 char * str = ((char*)bug_table) + bug_table[1];
31623163 return str;
31633164 }
3165+#endif
31643166
31653167 unsigned int set;
31663168
@@ -3301,12 +3303,15 @@ void asm_test(void) · 134 unmodified lines
33013303 printf("set=0x%x\n", set);
33023304 val = 0x01020304;
33033305 printf("swab32(0x%08x) = 0x%0x\n", val, swab32(val));
3306+#ifndef _WIN32
33043307 override_func1();
33053308 override_func2();
33063309 /* The base_func ref from the following inline asm should find
33073310 the global one, not the local decl from this function. */
33083311 asm volatile(".weak override_func3\n.set override_func3, base_func");
33093312 override_func3();
3313+ printf("asmstr: %s\n", get_asm_string());
3314+#endif
33103315 /* Check that we can also load structs of appropriate layout
33113316 into registers. */
33123317 asm volatile("" : "=r" (asmret) : "0"(s2));
@@ -3319,7 +3324,6 @@ void asm_test(void) · 6 unmodified lines
33193324 if (!somebool)
33203325 printf("asmbool: failed\n");
33213326 #endif
3322− printf("asmstr: %s\n", get_asm_string());
33233327 val = 43;
33243328 fancy_copy (&val, &val2);
33253329 printf ("fancycpy(%d)=%d\n", val, val2);
@@ -3677,10 +3681,12 @@ typedef struct gate_struct64 gate_desc; · 351 unmodified lines
36773681 gate_desc a_gate_desc;
36783682 void attrib_test(void)
36793683 {
3684+#ifndef _WIN32
36803685 printf("attr: %d %d %d %d\n", sizeof(struct Spacked),
36813686 sizeof(spacked), sizeof(Spacked2), sizeof(spacked2));
36823687 printf("attr: %d %d\n", sizeof(Spacked3), sizeof(spacked3));
36833688 printf("attr: %d %d\n", sizeof(gate_desc), sizeof(a_gate_desc));
3689+#endif
36843690 }
36853691 extern __attribute__((__unused__)) char * __attribute__((__unused__)) *
36863692 strange_attrib_placement (void);
tests/tests2/Makefile+1−1
@@ -63,7 +63,7 @@ all test: $(filter-out $(SKIP),$(TESTS)) · 62 unmodified lines
6363
6464 # automatically generate .expect files with gcc:
6565 %.expect : %.c
66− (gcc -w $*.c -o a.exe && ./a.exe $(ARGS)) >$*.expect 2>&1; rm -f a.exe
66+ (gcc -w $*.c -o a.exe && ./a.exe $(ARGS)) $(FILTER) >$*.expect 2>&1; rm -f a.exe
6767
6868 # tell make not to delete
6969 .PRECIOUS: %.expect

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t