Skip to content

Commit 8025a82

Browse files
committed
Check with clang -fsanitize
Tested code with: clang -fsanitize=address,undefined,nullability -pie -fPIE -Iinclude -I. -g tcc.c -o tcc.tcc -lm -ldl -lpthread ./tcc.tcc -Iinclude -I. -b -g tcc.c -o tcc.tcc1 -lm -ldl -lpthread Also checked on i386/x86_64 with -fsanitize=memory (others not supported). arm-link.c: use read32le/write32le/add32le to avoid unaligned access tcc.h i386-asm.c: fix signed left shift lib/bcheck.c: Add _Atomic libtcc.c: Correct MEM_DEBUG_CHECK3 to avoid unaligned access riscv64-link.c: Fix R_RISCV_SET16 tccpp.c: Align tal_header_t to avoid unaligned access tccgen.c x86_64-gen.c: avoid use of uninitialized value There are still warnings reported: tccgen.c:4031:13: runtime error: member access within null pointer of type 'TCCState' (aka 'struct TCCState') tccelf.c:321:22: runtime error: applying zero offset to null pointer tccelf.c:1132:23: runtime error: applying non-zero offset 169184 to null pointer A lot of left shift of negative value warnings. I ignored these for the moment. Also the -run option does no work well with -fsanitize. It gets confused because it does not detect that the generated code in memory is used without -fsanitize option. There are a lot more -fsanitize options. I did not find serious problems with them.
1 parent a2902d3 commit 8025a82

File tree

9 files changed

+35
-32
lines changed

9 files changed

+35
-32
lines changed

arm-link.c

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,11 @@ ST_FUNC void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr,
194194
case R_ARM_PLT32:
195195
{
196196
int x, is_thumb, is_call, h, blx_avail, is_bl, th_ko;
197-
x = (*(int *) ptr) & 0xffffff;
197+
x = read32le(ptr) & 0xffffff;
198198
#ifdef DEBUG_RELOC
199199
printf ("reloc %d: x=0x%x val=0x%x ", type, x, val);
200200
#endif
201-
(*(int *)ptr) &= 0xff000000;
201+
write32le(ptr, read32le(ptr) & 0xff000000);
202202
if (x & 0x800000)
203203
x -= 0x1000000;
204204
x <<= 2;
@@ -220,9 +220,9 @@ ST_FUNC void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr,
220220
/* Only reached if blx is avail and it is a call */
221221
if (is_thumb) {
222222
x |= h << 24;
223-
(*(int *)ptr) = 0xfa << 24; /* bl -> blx */
223+
write32le(ptr, 0xfa << 24); /* bl -> blx */
224224
}
225-
(*(int *) ptr) |= x;
225+
write32le(ptr, read32le(ptr) | x);
226226
}
227227
return;
228228
/* Since these relocations only concern Thumb-2 and blx instruction was
@@ -330,23 +330,23 @@ ST_FUNC void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr,
330330
imm4 = (val >> 12) & 0xf;
331331
x = (imm4 << 16) | imm12;
332332
if (type == R_ARM_THM_MOVT_ABS)
333-
*(int *)ptr |= x;
333+
write32le(ptr, read32le(ptr) | x);
334334
else
335-
*(int *)ptr += x;
335+
add32le(ptr, x);
336336
}
337337
return;
338338
case R_ARM_MOVT_PREL:
339339
case R_ARM_MOVW_PREL_NC:
340340
{
341-
int insn = *(int *)ptr;
341+
int insn = read32le(ptr);
342342
int addend = ((insn >> 4) & 0xf000) | (insn & 0xfff);
343343

344344
addend = (addend ^ 0x8000) - 0x8000;
345345
val += addend - addr;
346346
if (type == R_ARM_MOVT_PREL)
347347
val >>= 16;
348-
*(int *)ptr = (insn & 0xfff0f000) |
349-
((val & 0xf000) << 4) | (val & 0xfff);
348+
write32le(ptr, (insn & 0xfff0f000) |
349+
((val & 0xf000) << 4) | (val & 0xfff));
350350
}
351351
return;
352352
case R_ARM_THM_MOVT_ABS:
@@ -361,21 +361,21 @@ ST_FUNC void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr,
361361
imm4 = (val >> 12) & 0xf;
362362
x = (imm3 << 28) | (imm8 << 16) | (i << 10) | imm4;
363363
if (type == R_ARM_THM_MOVT_ABS)
364-
*(int *)ptr |= x;
364+
write32le(ptr, read32le(ptr) | x);
365365
else
366-
*(int *)ptr += x;
366+
add32le(ptr, x);
367367
}
368368
return;
369369
case R_ARM_PREL31:
370370
{
371371
int x;
372-
x = (*(int *)ptr) & 0x7fffffff;
373-
(*(int *)ptr) &= 0x80000000;
372+
x = read32le(ptr) & 0x7fffffff;
373+
write32le(ptr, read32le(ptr) & 0x80000000);
374374
x = (x * 2) / 2;
375375
x += val - addr;
376376
if((x^(x>>1))&0x40000000)
377377
tcc_error_noabort("can't relocate value at %x,%d",addr, type);
378-
(*(int *)ptr) |= x & 0x7fffffff;
378+
write32le(ptr, read32le(ptr) | (x & 0x7fffffff));
379379
}
380380
return;
381381
case R_ARM_ABS32:
@@ -392,33 +392,33 @@ ST_FUNC void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr,
392392
qrel++;
393393
}
394394
}
395-
*(int *)ptr += val;
395+
add32le(ptr, val);
396396
return;
397397
case R_ARM_REL32:
398-
*(int *)ptr += val - addr;
398+
add32le(ptr, val - addr);
399399
return;
400400
case R_ARM_GOTPC:
401-
*(int *)ptr += s1->got->sh_addr - addr;
401+
add32le(ptr, s1->got->sh_addr - addr);
402402
return;
403403
case R_ARM_GOTOFF:
404-
*(int *)ptr += val - s1->got->sh_addr;
404+
add32le(ptr, val - s1->got->sh_addr);
405405
return;
406406
case R_ARM_GOT32:
407407
/* we load the got offset */
408-
*(int *)ptr += get_sym_attr(s1, sym_index, 0)->got_offset;
408+
add32le(ptr, get_sym_attr(s1, sym_index, 0)->got_offset);
409409
return;
410410
case R_ARM_GOT_PREL:
411411
/* we load the pc relative got offset */
412-
*(int *)ptr += s1->got->sh_addr +
413-
get_sym_attr(s1, sym_index, 0)->got_offset -
414-
addr;
412+
add32le(ptr, s1->got->sh_addr +
413+
get_sym_attr(s1, sym_index, 0)->got_offset -
414+
addr);
415415
return;
416416
case R_ARM_COPY:
417417
return;
418418
case R_ARM_V4BX:
419419
/* trade Thumb support for ARMv4 support */
420-
if ((0x0ffffff0 & *(int*)ptr) == 0x012FFF10)
421-
*(int*)ptr ^= 0xE12FFF10 ^ 0xE1A0F000; /* BX Rm -> MOV PC, Rm */
420+
if ((0x0ffffff0 & read32le(ptr)) == 0x012FFF10)
421+
write32le(ptr, read32le(ptr) ^ 0xE12FFF10 ^ 0xE1A0F000); /* BX Rm -> MOV PC, Rm */
422422
return;
423423
case R_ARM_GLOB_DAT:
424424
case R_ARM_JUMP_SLOT:

i386-asm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ enum {
136136
# define OP_EA32 0
137137
#endif
138138

139-
#define OP_EA 0x40000000
139+
#define OP_EA 0x40000000u
140140
#define OP_REG (OP_REG8 | OP_REG16 | OP_REG32 | OP_REG64)
141141

142142
#ifdef TCC_TARGET_X86_64

lib/bcheck.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ static unsigned char print_heap;
351351
static unsigned char print_statistic;
352352
static unsigned char no_strdup;
353353
static unsigned char use_sem;
354-
static int never_fatal;
354+
static _Atomic int never_fatal;
355355
#if HAVE_TLS_FUNC
356356
#if defined(_WIN32)
357357
static int no_checking = 0;
@@ -393,7 +393,7 @@ static __thread int no_checking = 0;
393393
#define NO_CHECKING_GET() no_checking
394394
#define NO_CHECKING_SET(v) no_checking = v
395395
#else
396-
static int no_checking = 0;
396+
static _Atomic int no_checking = 0;
397397
#define NO_CHECKING_GET() no_checking
398398
#define NO_CHECKING_SET(v) no_checking = v
399399
#endif

libtcc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ PUB_FUNC char *tcc_strdup(const char *str)
314314
#define MEM_DEBUG_MAGIC3 0xFEEDDEB3
315315
#define MEM_DEBUG_FILE_LEN 40
316316
#define MEM_DEBUG_CHECK3(header) \
317-
((mem_debug_header_t*)((char*)header + header->size))->magic3
317+
(((unsigned char *) header->magic3) + header->size)
318318
#define MEM_USER_PTR(header) \
319319
((char *)header + offsetof(mem_debug_header_t, magic3))
320320
#define MEM_HEADER_PTR(ptr) \

riscv64-link.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ ST_FUNC void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr,
347347
*ptr = (*ptr & ~0xff) | (val & 0xff);
348348
return;
349349
case R_RISCV_SET16:
350-
*ptr = (*ptr & ~0xffff) | (val & 0xffff);
350+
write16le(ptr, (read16le(ptr) & ~0xffff) | (val & 0xffff));
351351
return;
352352
case R_RISCV_SUB6:
353353
*ptr = (*ptr & ~0x3f) | ((*ptr - val) & 0x3f);

tcc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1689,7 +1689,7 @@ dwarf_read_sleb128(unsigned char **ln, unsigned char *end)
16891689
retval |= (byte & 0x7f) << (i * 7);
16901690
if ((byte & 0x80) == 0) {
16911691
if ((byte & 0x40) && (i + 1) * 7 < 64)
1692-
retval |= -1LL << ((i + 1) * 7);
1692+
retval |= (uint64_t)-1LL << ((i + 1) * 7);
16931693
break;
16941694
}
16951695
}

tccgen.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,6 +1374,7 @@ ST_FUNC void save_reg_upstack(int r, int n)
13741374
l = get_temp_local_var(size, align, &r2);
13751375
sv.r = VT_LOCAL | VT_LVAL;
13761376
sv.c.i = l;
1377+
sv.sym = NULL;
13771378
store(p->r & VT_VALMASK, &sv);
13781379
#if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
13791380
/* x86 specific: need to pop fp register ST0 if saved */
@@ -3768,6 +3769,7 @@ ST_FUNC void vstore(void)
37683769
sv.type.t = VT_PTRDIFF_T;
37693770
sv.r = VT_LOCAL | VT_LVAL;
37703771
sv.c.i = vtop[-1].c.i;
3772+
sv.sym = NULL;
37713773
load(r, &sv);
37723774
vtop[-1].r = r | VT_LVAL;
37733775
}

tccpp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ typedef struct TinyAlloc {
157157
} TinyAlloc;
158158

159159
typedef struct tal_header_t {
160-
unsigned size;
160+
ALIGNED(PTR_SIZE) unsigned size;
161161
#ifdef TAL_DEBUG
162162
int line_num; /* negative line_num used for double free check */
163163
char file_name[TAL_DEBUG_FILE_LEN + 1];
@@ -246,7 +246,7 @@ static void *tal_realloc_impl(TinyAlloc **pal, void *p, unsigned size TAL_DEBUG_
246246
tal_header_t *header;
247247
void *ret;
248248
int is_own;
249-
unsigned adj_size = (size + 3) & -4;
249+
unsigned adj_size = (size + PTR_SIZE - 1) & -PTR_SIZE;
250250
TinyAlloc *al = *pal;
251251

252252
tail_call:

x86_64-gen.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ void load(int r, SValue *sv)
396396
v1.type.t = VT_PTR;
397397
v1.r = VT_LOCAL | VT_LVAL;
398398
v1.c.i = fc;
399+
v1.sym = NULL;
399400
fr = r;
400401
if (!(reg_classes[fr] & (RC_INT|RC_R11)))
401402
fr = get_reg(RC_INT);

0 commit comments

Comments
 (0)