Skip to content

Commit 311218e

Browse files
committed
Allow testsuite to be build with CC -fsanitize
Makefile: Add sani-test* targets + help tcc.h: redefine offsetof for clang -fsanitize tccelf.c: section_ptr_add: allow clang -fsanitize tcc_add_btstub/tcc_load_object_file: revert previous change. tccpp.c: Fix -fsanitize problem by casting to unsigned. tests/Makefile: tcc -v leaks memory (a lot of returns in main() leak memory) tests/tests2/Makefile: testcase 112 aborts and leaks memory. Disable leak test.
1 parent 5364bc7 commit 311218e

File tree

6 files changed

+22
-9
lines changed

6 files changed

+22
-9
lines changed

Makefile

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,12 +467,17 @@ tcov-tes% : tcc_c$(EXESUF)
467467
@$(MAKE) --no-print-directory TCC_LOCAL=$(CURDIR)/$< tes$*
468468
tcc_c$(EXESUF): $($T_FILES)
469469
$S$(TCC) tcc.c -o $@ -ftest-coverage $(DEFINES) $(LIBS)
470+
# run tests with sanitize option
471+
sani-tes% : tcc_s$(EXESUF)
472+
@$(MAKE) --no-print-directory TCC_LOCAL=$(CURDIR)/$< tes$*
473+
tcc_s$(EXESUF): $($T_FILES)
474+
$S$(CC) tcc.c -o $@ -fsanitize=address,undefined $(DEFINES) $(CFLAGS) $(LIBS)
470475
# test the installed tcc instead
471476
test-install: $(TCCDEFS_H)
472477
@$(MAKE) -C tests TESTINSTALL=yes #_all
473478

474479
clean:
475-
@rm -f tcc *-tcc tcc_p tcc_c
480+
@rm -f tcc *-tcc tcc_p tcc_c tcc_s
476481
@rm -f tags ETAGS *.o *.a *.so* *.out *.log lib*.def *.exe *.dll
477482
@rm -f a.out *.dylib *_.h *.pod *.tcov
478483
@$(MAKE) -s -C lib $@
@@ -501,8 +506,10 @@ help:
501506
@echo " run all/single test(s) from tests2, optionally update .expect"
502507
@echo "make testspp.all / make testspp.17"
503508
@echo " run all/single test(s) from tests/pp"
504-
@echo "make tcov-test / tcov-tests2... / tcov-testspp..."
509+
@echo "make tcov-test / tcov-tests2.37 / tcov-testspp.17"
505510
@echo " run tests as above with code coverage. After test(s) see tcc_c$(EXESUF).tcov"
511+
@echo "make sani-test / sani-tests2.37 / sani-testspp.17"
512+
@echo " run tests as above with sanitize option."
506513
@echo "make test-install"
507514
@echo " run tests with the installed tcc"
508515
@echo "Other supported make targets:"

tcc.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ extern long double strtold (const char *__nptr, char **__endptr);
9999
#define offsetof(type, field) ((size_t) &((type *)0)->field)
100100
#endif
101101

102+
#ifdef __clang__ // clang -fsanitize compains about: NULL+value
103+
#undef offsetof
104+
#define offsetof(type, field) __builtin_offsetof(type, field)
105+
#endif
106+
102107
#ifndef countof
103108
#define countof(tab) (sizeof(tab) / sizeof((tab)[0]))
104109
#endif

tccelf.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,8 @@ ST_FUNC size_t section_add(Section *sec, addr_t size, int align)
318318
ST_FUNC void *section_ptr_add(Section *sec, addr_t size)
319319
{
320320
size_t offset = section_add(sec, size, 1);
321-
return sec->data + offset;
321+
// clang -fsanitize compains about: NULL+value
322+
return sec->data ? sec->data + offset : (void *)offset;
322323
}
323324

324325
#ifndef ELF_OBJ_ONLY
@@ -1597,8 +1598,7 @@ ST_FUNC void tcc_add_btstub(TCCState *s1)
15971598

15981599
s = data_section;
15991600
/* Align to PTR_SIZE */
1600-
if (s->data_offset)
1601-
section_ptr_add(s, -s->data_offset & (PTR_SIZE - 1));
1601+
section_ptr_add(s, -s->data_offset & (PTR_SIZE - 1));
16021602
o = s->data_offset;
16031603
/* create a struct rt_context (see tccrun.c) */
16041604
if (s1->dwarf) {
@@ -3260,7 +3260,7 @@ ST_FUNC int tcc_load_object_file(TCCState *s1,
32603260
sm_table[i].s = s;
32613261
/* concatenate sections */
32623262
size = sh->sh_size;
3263-
if (sh->sh_type != SHT_NOBITS && size) {
3263+
if (sh->sh_type != SHT_NOBITS) {
32643264
unsigned char *ptr;
32653265
lseek(fd, file_offset + sh->sh_offset, SEEK_SET);
32663266
ptr = section_ptr_add(s, size);

tccpp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2055,7 +2055,7 @@ static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long
20552055
expect("more hex digits in universal-character-name");
20562056
else
20572057
goto add_hex_or_ucn;
2058-
n = n * 16 + c;
2058+
n = (unsigned) n * 16 + c;
20592059
p++;
20602060
} while (--i);
20612061
if (is_long) {

tests/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ endif
7979

8080
all test :
8181
@echo ------------ version ------------
82-
@$(TCC_LOCAL) -v
82+
@ASAN_OPTIONS=detect_leaks=0 $(TCC_LOCAL) -v
8383
@$(MAKE) --no-print-directory -s clean
8484
@$(MAKE) --no-print-directory -s -r _all
8585

tests/tests2/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ GEN-ALWAYS =
102102
112_backtrace.test 113_btdll.test 126_bound_global.test: FILTER += \
103103
-e 's;[0-9A-Fa-fx]\{5,\};........;g' \
104104
-e 's;0x[0-9A-Fa-f]\{1,\};0x?;g'
105+
112_backtrace.test: LEAK=ASAN_OPTIONS=detect_leaks=0
105106

106107
# this test creates two DLLs and an EXE
107108
113_btdll.test: T1 = \
@@ -136,7 +137,7 @@ all test tests2.all: $(filter-out $(SKIP),$(TESTS))
136137
@echo Test: $*...
137138
@$(call T1,$<) $(T3)
138139

139-
T1 = $(TCC) $(FLAGS) $(T2) $(ARGS)
140+
T1 = $(LEAK) $(TCC) $(FLAGS) $(T2) $(ARGS)
140141
T2 = $(if $(NORUN),$1 -o $(basename $@).exe && ./$(basename $@).exe,-run $1)
141142
T3 = $(FILTER) >$*.output 2>&1 || true \
142143
&& diff -Nbu $(filter %.expect,$^) $*.output \

0 commit comments

Comments
 (0)