Allow testsuite to be build with CC -fsanitize
Some checks are pending
build and test / test-x86_64-linux (push) Waiting to run
build and test / test-x86_64-osx (push) Waiting to run
build and test / test-aarch64-osx (push) Waiting to run
build and test / test-x86_64-win32 (push) Waiting to run
build and test / test-i386-win32 (push) Waiting to run
build and test / test-armv7-linux (push) Waiting to run
build and test / test-aarch64-linux (push) Waiting to run
build and test / test-riscv64-linux (push) Waiting to run

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.
This commit is contained in:
herman ten brugge 2025-08-05 16:47:11 +02:00
parent 5364bc7c82
commit 311218ee5f
6 changed files with 22 additions and 9 deletions

View File

@ -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:"

5
tcc.h
View File

@ -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

View File

@ -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);

View File

@ -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) {

View File

@ -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

View File

@ -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 \