diff --git a/Makefile b/Makefile index 70cec7ee..7162633c 100644 --- a/Makefile +++ b/Makefile @@ -467,12 +467,17 @@ tcov-tes% : tcc_c$(EXESUF) @$(MAKE) --no-print-directory TCC_LOCAL=$(CURDIR)/$< tes$* tcc_c$(EXESUF): $($T_FILES) $S$(TCC) tcc.c -o $@ -ftest-coverage $(DEFINES) $(LIBS) +# run tests with sanitize option +sani-tes% : tcc_s$(EXESUF) + @$(MAKE) --no-print-directory TCC_LOCAL=$(CURDIR)/$< tes$* +tcc_s$(EXESUF): $($T_FILES) + $S$(CC) tcc.c -o $@ -fsanitize=address,undefined $(DEFINES) $(CFLAGS) $(LIBS) # test the installed tcc instead test-install: $(TCCDEFS_H) @$(MAKE) -C tests TESTINSTALL=yes #_all clean: - @rm -f tcc *-tcc tcc_p tcc_c + @rm -f tcc *-tcc tcc_p tcc_c tcc_s @rm -f tags ETAGS *.o *.a *.so* *.out *.log lib*.def *.exe *.dll @rm -f a.out *.dylib *_.h *.pod *.tcov @$(MAKE) -s -C lib $@ @@ -501,8 +506,10 @@ help: @echo " run all/single test(s) from tests2, optionally update .expect" @echo "make testspp.all / make testspp.17" @echo " run all/single test(s) from tests/pp" - @echo "make tcov-test / tcov-tests2... / tcov-testspp..." + @echo "make tcov-test / tcov-tests2.37 / tcov-testspp.17" @echo " run tests as above with code coverage. After test(s) see tcc_c$(EXESUF).tcov" + @echo "make sani-test / sani-tests2.37 / sani-testspp.17" + @echo " run tests as above with sanitize option." @echo "make test-install" @echo " run tests with the installed tcc" @echo "Other supported make targets:" diff --git a/tcc.h b/tcc.h index f9b5a740..f2b0dada 100644 --- a/tcc.h +++ b/tcc.h @@ -99,6 +99,11 @@ extern long double strtold (const char *__nptr, char **__endptr); #define offsetof(type, field) ((size_t) &((type *)0)->field) #endif +#ifdef __clang__ // clang -fsanitize compains about: NULL+value +#undef offsetof +#define offsetof(type, field) __builtin_offsetof(type, field) +#endif + #ifndef countof #define countof(tab) (sizeof(tab) / sizeof((tab)[0])) #endif diff --git a/tccelf.c b/tccelf.c index a8232c8a..edf4c4bb 100644 --- a/tccelf.c +++ b/tccelf.c @@ -318,7 +318,8 @@ ST_FUNC size_t section_add(Section *sec, addr_t size, int align) ST_FUNC void *section_ptr_add(Section *sec, addr_t size) { size_t offset = section_add(sec, size, 1); - return sec->data + offset; + // clang -fsanitize compains about: NULL+value + return sec->data ? sec->data + offset : (void *)offset; } #ifndef ELF_OBJ_ONLY @@ -1597,8 +1598,7 @@ ST_FUNC void tcc_add_btstub(TCCState *s1) s = data_section; /* Align to PTR_SIZE */ - if (s->data_offset) - section_ptr_add(s, -s->data_offset & (PTR_SIZE - 1)); + section_ptr_add(s, -s->data_offset & (PTR_SIZE - 1)); o = s->data_offset; /* create a struct rt_context (see tccrun.c) */ if (s1->dwarf) { @@ -3260,7 +3260,7 @@ invalid: sm_table[i].s = s; /* concatenate sections */ size = sh->sh_size; - if (sh->sh_type != SHT_NOBITS && size) { + if (sh->sh_type != SHT_NOBITS) { unsigned char *ptr; lseek(fd, file_offset + sh->sh_offset, SEEK_SET); ptr = section_ptr_add(s, size); diff --git a/tccpp.c b/tccpp.c index 8a496667..092a4452 100644 --- a/tccpp.c +++ b/tccpp.c @@ -2055,7 +2055,7 @@ static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long expect("more hex digits in universal-character-name"); else goto add_hex_or_ucn; - n = n * 16 + c; + n = (unsigned) n * 16 + c; p++; } while (--i); if (is_long) { diff --git a/tests/Makefile b/tests/Makefile index 6e0f3bd6..2cacf039 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -79,7 +79,7 @@ endif all test : @echo ------------ version ------------ - @$(TCC_LOCAL) -v + @ASAN_OPTIONS=detect_leaks=0 $(TCC_LOCAL) -v @$(MAKE) --no-print-directory -s clean @$(MAKE) --no-print-directory -s -r _all diff --git a/tests/tests2/Makefile b/tests/tests2/Makefile index 0726e99d..fb3327ee 100644 --- a/tests/tests2/Makefile +++ b/tests/tests2/Makefile @@ -102,6 +102,7 @@ GEN-ALWAYS = 112_backtrace.test 113_btdll.test 126_bound_global.test: FILTER += \ -e 's;[0-9A-Fa-fx]\{5,\};........;g' \ -e 's;0x[0-9A-Fa-f]\{1,\};0x?;g' +112_backtrace.test: LEAK=ASAN_OPTIONS=detect_leaks=0 # this test creates two DLLs and an EXE 113_btdll.test: T1 = \ @@ -136,7 +137,7 @@ all test tests2.all: $(filter-out $(SKIP),$(TESTS)) @echo Test: $*... @$(call T1,$<) $(T3) -T1 = $(TCC) $(FLAGS) $(T2) $(ARGS) +T1 = $(LEAK) $(TCC) $(FLAGS) $(T2) $(ARGS) T2 = $(if $(NORUN),$1 -o $(basename $@).exe && ./$(basename $@).exe,-run $1) T3 = $(FILTER) >$*.output 2>&1 || true \ && diff -Nbu $(filter %.expect,$^) $*.output \