mirror of
git://repo.or.cz/tinycc.git
synced 2026-06-17 15:44:18 +08:00
Add bound check support for alloca on all targets
stddef.h, tccdefs.h, tccgen.c, tests/boundtest.c, tests/tcctest.c: - remove ifdef arround alloca lib/Makefile: - move alloca-bt.o to COMMON_O lib/alloca-bt.S: - add alloca bound check code for arm, arm64, riscv64 lib/alloca.S: - check for leading underscore tcc.h, libtcc.c: - include arm64-asm.c instead of arm-asm.c for arm64 tcctok.h, tccasm.c: - add simple reloc support (only for R_AARCH64_CALL26)
This commit is contained in:
parent
fc424c9f7b
commit
6da45946ae
@ -19,9 +19,7 @@ typedef union { long long __ll; long double __ld; } max_align_t;
|
|||||||
#undef offsetof
|
#undef offsetof
|
||||||
#define offsetof(type, field) __builtin_offsetof(type, field)
|
#define offsetof(type, field) __builtin_offsetof(type, field)
|
||||||
|
|
||||||
#if defined __i386__ || defined __x86_64__
|
|
||||||
void *alloca(size_t size);
|
void *alloca(size_t size);
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@ -347,12 +347,8 @@
|
|||||||
__MAYBE_REDIR(void*, calloc, (__SIZE_TYPE__, __SIZE_TYPE__))
|
__MAYBE_REDIR(void*, calloc, (__SIZE_TYPE__, __SIZE_TYPE__))
|
||||||
__MAYBE_REDIR(void*, memalign, (__SIZE_TYPE__, __SIZE_TYPE__))
|
__MAYBE_REDIR(void*, memalign, (__SIZE_TYPE__, __SIZE_TYPE__))
|
||||||
__MAYBE_REDIR(void, free, (void*))
|
__MAYBE_REDIR(void, free, (void*))
|
||||||
#if defined __i386__ || defined __x86_64__
|
|
||||||
__BOTH(void*, alloca, (__SIZE_TYPE__))
|
__BOTH(void*, alloca, (__SIZE_TYPE__))
|
||||||
void *alloca(__SIZE_TYPE__);
|
void *alloca(__SIZE_TYPE__);
|
||||||
#else
|
|
||||||
__BUILTIN(void*, alloca, (__SIZE_TYPE__))
|
|
||||||
#endif
|
|
||||||
__BUILTIN(void, abort, (void))
|
__BUILTIN(void, abort, (void))
|
||||||
__BOUND(void, longjmp, ())
|
__BOUND(void, longjmp, ())
|
||||||
#if !defined _WIN32
|
#if !defined _WIN32
|
||||||
|
|||||||
@ -35,12 +35,12 @@ endif
|
|||||||
|
|
||||||
XFLAGS += -I$(TOP)
|
XFLAGS += -I$(TOP)
|
||||||
|
|
||||||
I386_O = libtcc1.o alloca-bt.o $(COMMON_O)
|
I386_O = libtcc1.o $(COMMON_O)
|
||||||
X86_64_O = libtcc1.o alloca-bt.o $(COMMON_O)
|
X86_64_O = libtcc1.o $(COMMON_O)
|
||||||
ARM_O = libtcc1.o armeabi.o armflush.o $(COMMON_O)
|
ARM_O = libtcc1.o armeabi.o armflush.o $(COMMON_O)
|
||||||
ARM64_O = lib-arm64.o $(COMMON_O)
|
ARM64_O = lib-arm64.o $(COMMON_O)
|
||||||
RISCV64_O = lib-arm64.o $(COMMON_O)
|
RISCV64_O = lib-arm64.o $(COMMON_O)
|
||||||
COMMON_O = stdatomic.o atomic.o builtin.o alloca.o
|
COMMON_O = stdatomic.o atomic.o builtin.o alloca.o alloca-bt.o
|
||||||
WIN_O = crt1.o crt1w.o wincrt1.o wincrt1w.o dllcrt1.o dllmain.o
|
WIN_O = crt1.o crt1w.o wincrt1.o wincrt1w.o dllcrt1.o dllmain.o
|
||||||
LIN_O = dsohandle.o
|
LIN_O = dsohandle.o
|
||||||
OSX_O =
|
OSX_O =
|
||||||
|
|||||||
104
lib/alloca-bt.S
104
lib/alloca-bt.S
@ -92,5 +92,109 @@ p3:
|
|||||||
ret
|
ret
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* ---------------------------------------------- */
|
||||||
|
#elif defined __arm__
|
||||||
|
|
||||||
|
.globl _(__bound_alloca)
|
||||||
|
_(__bound_alloca):
|
||||||
|
mov r1, r0
|
||||||
|
add r0, r0, #1
|
||||||
|
rsb sp, r0, sp
|
||||||
|
bic sp, sp, #7
|
||||||
|
mov r0, sp
|
||||||
|
push { lr }
|
||||||
|
bl _(__bound_new_region)
|
||||||
|
pop { lr }
|
||||||
|
mov r0, sp
|
||||||
|
mov pc, lr
|
||||||
|
|
||||||
|
/* ---------------------------------------------- */
|
||||||
|
#elif defined __aarch64__ || defined __arm64__
|
||||||
|
|
||||||
|
.globl _(__bound_alloca)
|
||||||
|
_(__bound_alloca):
|
||||||
|
#ifdef __TINYC__
|
||||||
|
.int 0xaa0003e1
|
||||||
|
.int 0x91004000
|
||||||
|
.int 0x927cec00
|
||||||
|
#ifdef _WIN32
|
||||||
|
.int 0xb4000160
|
||||||
|
.int 0xd2820002
|
||||||
|
.int 0xeb02001f
|
||||||
|
.int 0x540000c3
|
||||||
|
.int 0xcb2263e3
|
||||||
|
.int 0xf940007f
|
||||||
|
.int 0xcb2263ff
|
||||||
|
.int 0xcb020000
|
||||||
|
.int 0x17fffffa
|
||||||
|
.int 0xb4000040
|
||||||
|
#endif
|
||||||
|
.int 0xcb2063ff
|
||||||
|
.int 0x910003e0
|
||||||
|
.int 0xa9bf7bfd
|
||||||
|
.reloc ., R_AARCH64_CALL26, _(__bound_new_region)
|
||||||
|
.int 0x94000000
|
||||||
|
.int 0xa8c17bfd
|
||||||
|
.int 0x910003e0
|
||||||
|
.int 0xd65f03c0
|
||||||
|
#else
|
||||||
|
mov x1, x0
|
||||||
|
add x0, x0, #16 // Round up to 16-byte boundary
|
||||||
|
and x0, x0, #-16 // Ensure 16-byte alignment
|
||||||
|
#ifdef _WIN32
|
||||||
|
cbz x0, p100 // If size is 0, skip to return
|
||||||
|
// Windows requires page-wise allocation with stack probing
|
||||||
|
mov x2, #4096 // Page size = 4096 bytes
|
||||||
|
|
||||||
|
p101:
|
||||||
|
cmp x0, x2 // Compare remaining size with page size
|
||||||
|
b.lo p102 // If less than page, jump to remainder
|
||||||
|
|
||||||
|
// Probe first, then allocate
|
||||||
|
sub x3, sp, x2 // Calculate guard page address (sp - 4096)
|
||||||
|
ldr xzr, [x3] // Touch guard page FIRST
|
||||||
|
sub sp, sp, x2 // THEN allocate the page
|
||||||
|
|
||||||
|
sub x0, x0, x2 // Decrement remaining size
|
||||||
|
b p101 // Continue loop
|
||||||
|
|
||||||
|
p102:
|
||||||
|
// Allocate remaining bytes (less than one page)
|
||||||
|
cbz x0, p100 // If no remaining bytes, skip
|
||||||
|
sub sp, sp, x0 // Allocate remaining space
|
||||||
|
#else
|
||||||
|
// Non-Windows: simple one-time allocation
|
||||||
|
sub sp, sp, x0 // Allocate space on stack
|
||||||
|
#endif
|
||||||
|
|
||||||
|
p100:
|
||||||
|
mov x0, sp // Return allocated address
|
||||||
|
stp x29, x30, [sp, #-16]!
|
||||||
|
bl _(__bound_new_region)
|
||||||
|
ldp x29, x30, [sp], #16
|
||||||
|
mov x0, sp // Return allocated address
|
||||||
|
ret // Return to caller
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* ---------------------------------------------- */
|
||||||
|
#elif defined __riscv
|
||||||
|
|
||||||
|
.globl _(__bound_alloca)
|
||||||
|
_(__bound_alloca):
|
||||||
|
mv a1, a0
|
||||||
|
sub sp, sp, a0
|
||||||
|
addi sp, sp, -16
|
||||||
|
andi sp, sp, -16
|
||||||
|
add a0, sp, zero
|
||||||
|
addi sp,sp,-16
|
||||||
|
sd s0,0(sp)
|
||||||
|
sd ra,8(sp)
|
||||||
|
jal _(__bound_new_region)
|
||||||
|
ld s0,0(sp)
|
||||||
|
ld ra,8(sp)
|
||||||
|
addi sp,sp,16
|
||||||
|
add a0, sp, zero
|
||||||
|
ret
|
||||||
|
|
||||||
/* ---------------------------------------------- */
|
/* ---------------------------------------------- */
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
41
lib/alloca.S
41
lib/alloca.S
@ -70,25 +70,18 @@ p3:
|
|||||||
/* ---------------------------------------------- */
|
/* ---------------------------------------------- */
|
||||||
#elif defined __arm__
|
#elif defined __arm__
|
||||||
|
|
||||||
.text
|
.globl _(alloca)
|
||||||
.align 2
|
_(alloca):
|
||||||
.global alloca
|
|
||||||
.type alloca, %function
|
|
||||||
alloca:
|
|
||||||
rsb sp, r0, sp
|
rsb sp, r0, sp
|
||||||
bic sp, sp, #7
|
bic sp, sp, #7
|
||||||
mov r0, sp
|
mov r0, sp
|
||||||
mov pc, lr
|
mov pc, lr
|
||||||
.size alloca, .-alloca
|
|
||||||
|
|
||||||
/* ---------------------------------------------- */
|
/* ---------------------------------------------- */
|
||||||
#elif defined __aarch64__ || defined __arm64__
|
#elif defined __aarch64__ || defined __arm64__
|
||||||
|
|
||||||
.text
|
.globl _(alloca)
|
||||||
.align 2
|
_(alloca):
|
||||||
.global alloca
|
|
||||||
.type alloca, %function
|
|
||||||
alloca:
|
|
||||||
#ifdef __TINYC__
|
#ifdef __TINYC__
|
||||||
.int 0x91003c00
|
.int 0x91003c00
|
||||||
.int 0x927cec00
|
.int 0x927cec00
|
||||||
@ -102,16 +95,16 @@ alloca:
|
|||||||
.int 0xcb2163ff
|
.int 0xcb2163ff
|
||||||
.int 0xcb010000
|
.int 0xcb010000
|
||||||
.int 0x17fffffa
|
.int 0x17fffffa
|
||||||
#endif
|
|
||||||
.int 0xb4000040
|
.int 0xb4000040
|
||||||
|
#endif
|
||||||
.int 0xcb2063ff
|
.int 0xcb2063ff
|
||||||
.int 0x910003e0
|
.int 0x910003e0
|
||||||
.int 0xd65f03c0
|
.int 0xd65f03c0
|
||||||
#else
|
#else
|
||||||
add x0, x0, #15 // Round up to 16-byte boundary
|
add x0, x0, #15 // Round up to 16-byte boundary
|
||||||
and x0, x0, #-16 // Ensure 16-byte alignment
|
and x0, x0, #-16 // Ensure 16-byte alignment
|
||||||
cbz x0, p100 // If size is 0, skip to return
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
cbz x0, p100 // If size is 0, skip to return
|
||||||
// Windows requires page-wise allocation with stack probing
|
// Windows requires page-wise allocation with stack probing
|
||||||
mov x1, #4096 // Page size = 4096 bytes
|
mov x1, #4096 // Page size = 4096 bytes
|
||||||
|
|
||||||
@ -140,20 +133,16 @@ p100:
|
|||||||
mov x0, sp // Return allocated address
|
mov x0, sp // Return allocated address
|
||||||
ret // Return to caller
|
ret // Return to caller
|
||||||
#endif
|
#endif
|
||||||
.size alloca, .-alloca
|
|
||||||
/* ---------------------------------------------- */
|
/* ---------------------------------------------- */
|
||||||
#elif defined __riscv
|
#elif defined __riscv
|
||||||
|
|
||||||
.text
|
.globl _(alloca)
|
||||||
.align 2
|
_(alloca):
|
||||||
.global alloca
|
sub sp, sp, a0
|
||||||
.type alloca, %function
|
addi sp, sp, -15
|
||||||
alloca:
|
andi sp, sp, -16
|
||||||
sub sp, sp, a0
|
add a0, sp, zero
|
||||||
addi sp, sp, -15
|
ret
|
||||||
andi sp, sp, -16
|
|
||||||
add a0, sp, zero
|
|
||||||
ret
|
|
||||||
.size alloca, .-alloca
|
|
||||||
/* ---------------------------------------------- */
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
2
libtcc.c
2
libtcc.c
@ -40,7 +40,7 @@
|
|||||||
#elif defined(TCC_TARGET_ARM64)
|
#elif defined(TCC_TARGET_ARM64)
|
||||||
#include "arm64-gen.c"
|
#include "arm64-gen.c"
|
||||||
#include "arm64-link.c"
|
#include "arm64-link.c"
|
||||||
#include "arm-asm.c"
|
#include "arm64-asm.c"
|
||||||
#elif defined(TCC_TARGET_C67)
|
#elif defined(TCC_TARGET_C67)
|
||||||
#include "c67-gen.c"
|
#include "c67-gen.c"
|
||||||
#include "c67-link.c"
|
#include "c67-link.c"
|
||||||
|
|||||||
2
tcc.h
2
tcc.h
@ -385,7 +385,7 @@ extern long double strtold (const char *__nptr, char **__endptr);
|
|||||||
#elif defined TCC_TARGET_ARM64
|
#elif defined TCC_TARGET_ARM64
|
||||||
# include "arm64-gen.c"
|
# include "arm64-gen.c"
|
||||||
# include "arm64-link.c"
|
# include "arm64-link.c"
|
||||||
# include "arm-asm.c"
|
# include "arm64-asm.c"
|
||||||
#elif defined TCC_TARGET_C67
|
#elif defined TCC_TARGET_C67
|
||||||
# define TCC_TARGET_COFF
|
# define TCC_TARGET_COFF
|
||||||
# include "coff.h"
|
# include "coff.h"
|
||||||
|
|||||||
17
tccasm.c
17
tccasm.c
@ -984,6 +984,23 @@ static void asm_parse_directive(TCCState *s1, int global)
|
|||||||
skip('@');
|
skip('@');
|
||||||
next();
|
next();
|
||||||
break;
|
break;
|
||||||
|
case TOK_ASMDIR_reloc:
|
||||||
|
{
|
||||||
|
ExprValue e;
|
||||||
|
|
||||||
|
next();
|
||||||
|
asm_expr(s1, &e);
|
||||||
|
skip(',');
|
||||||
|
#ifdef __aarch64__
|
||||||
|
if (strcmp(get_tok_str(tok, NULL), "R_AARCH64_CALL26"))
|
||||||
|
#endif
|
||||||
|
tcc_error("unimp: reloc '%s' unknown", get_tok_str(tok, NULL));
|
||||||
|
next();
|
||||||
|
skip(',');
|
||||||
|
greloca(cur_text_section, get_asm_sym(tok, NULL), e.v, R_AARCH64_CALL26, 0);
|
||||||
|
next();
|
||||||
|
}
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
tcc_error("unknown assembler directive '.%s'", get_tok_str(tok, NULL));
|
tcc_error("unknown assembler directive '.%s'", get_tok_str(tok, NULL));
|
||||||
break;
|
break;
|
||||||
|
|||||||
2
tccgen.c
2
tccgen.c
@ -1701,10 +1701,8 @@ ST_FUNC void gbound_args(int nb_args)
|
|||||||
gfunc_call(1);
|
gfunc_call(1);
|
||||||
func_bound_add_epilog = 1;
|
func_bound_add_epilog = 1;
|
||||||
}
|
}
|
||||||
#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
|
|
||||||
if (v == TOK_alloca)
|
if (v == TOK_alloca)
|
||||||
func_bound_add_epilog = 1;
|
func_bound_add_epilog = 1;
|
||||||
#endif
|
|
||||||
#if TARGETOS_NetBSD
|
#if TARGETOS_NetBSD
|
||||||
if (v == TOK_longjmp) /* undo rename to __longjmp14 */
|
if (v == TOK_longjmp) /* undo rename to __longjmp14 */
|
||||||
sv->sym->asm_label = TOK___bound_longjmp;
|
sv->sym->asm_label = TOK___bound_longjmp;
|
||||||
|
|||||||
3
tcctok.h
3
tcctok.h
@ -301,9 +301,7 @@
|
|||||||
DEF(TOK___fixxfdi, "__fixxfdi")
|
DEF(TOK___fixxfdi, "__fixxfdi")
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
|
|
||||||
DEF(TOK_alloca, "alloca")
|
DEF(TOK_alloca, "alloca")
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined TCC_TARGET_PE
|
#if defined TCC_TARGET_PE
|
||||||
DEF(TOK___chkstk, "__chkstk")
|
DEF(TOK___chkstk, "__chkstk")
|
||||||
@ -415,6 +413,7 @@
|
|||||||
DEF_ASMDIR(long)
|
DEF_ASMDIR(long)
|
||||||
DEF_ASMDIR(int)
|
DEF_ASMDIR(int)
|
||||||
DEF_ASMDIR(symver)
|
DEF_ASMDIR(symver)
|
||||||
|
DEF_ASMDIR(reloc)
|
||||||
DEF_ASMDIR(section) /* must be last directive */
|
DEF_ASMDIR(section) /* must be last directive */
|
||||||
|
|
||||||
#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
|
#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
|
||||||
|
|||||||
@ -169,14 +169,6 @@ int test13(void)
|
|||||||
return strlen(tab);
|
return strlen(tab);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined __i386__ || defined __x86_64__
|
|
||||||
#define allocf(x)
|
|
||||||
#else
|
|
||||||
#undef alloca
|
|
||||||
#define alloca(x) malloc(x)
|
|
||||||
#define allocf(x) free(x)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
int test14(void)
|
int test14(void)
|
||||||
{
|
{
|
||||||
char *p = alloca(TAB_SIZE);
|
char *p = alloca(TAB_SIZE);
|
||||||
@ -184,7 +176,6 @@ int test14(void)
|
|||||||
memset(p, 'a', TAB_SIZE);
|
memset(p, 'a', TAB_SIZE);
|
||||||
p[TAB_SIZE-1] = 0;
|
p[TAB_SIZE-1] = 0;
|
||||||
ret = strlen(p);
|
ret = strlen(p);
|
||||||
allocf(p);
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -196,7 +187,6 @@ int test15(void)
|
|||||||
memset(p, 'a', TAB_SIZE);
|
memset(p, 'a', TAB_SIZE);
|
||||||
p[TAB_SIZE-1] = 0;
|
p[TAB_SIZE-1] = 0;
|
||||||
ret = strlen(p);
|
ret = strlen(p);
|
||||||
allocf(p);
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -211,8 +201,6 @@ int test16()
|
|||||||
|
|
||||||
/* Test alloca embedded in a larger expression */
|
/* Test alloca embedded in a larger expression */
|
||||||
printf("alloca : %s : %s\n", p, strcpy(q=alloca(strlen(demo)+1),demo) );
|
printf("alloca : %s : %s\n", p, strcpy(q=alloca(strlen(demo)+1),demo) );
|
||||||
allocf(p);
|
|
||||||
allocf(q);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -228,8 +216,6 @@ int test17()
|
|||||||
|
|
||||||
/* Test alloca embedded in a larger expression */
|
/* Test alloca embedded in a larger expression */
|
||||||
printf("alloca : %s : %s\n", p, strcpy(q=alloca(strlen(demo)),demo) );
|
printf("alloca : %s : %s\n", p, strcpy(q=alloca(strlen(demo)),demo) );
|
||||||
allocf(p);
|
|
||||||
allocf(q);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2941,14 +2941,12 @@ void old_style_function_test(void)
|
|||||||
|
|
||||||
void alloca_test()
|
void alloca_test()
|
||||||
{
|
{
|
||||||
#if defined __i386__ || defined __x86_64__ || defined __arm__
|
|
||||||
char *p = alloca(16);
|
char *p = alloca(16);
|
||||||
strcpy(p,"123456789012345");
|
strcpy(p,"123456789012345");
|
||||||
printf("alloca: p is %s\n", p);
|
printf("alloca: p is %s\n", p);
|
||||||
char *demo = "This is only a test.\n";
|
char *demo = "This is only a test.\n";
|
||||||
/* Test alloca embedded in a larger expression */
|
/* Test alloca embedded in a larger expression */
|
||||||
printf("alloca: %s\n", strcpy(alloca(strlen(demo)+1),demo) );
|
printf("alloca: %s\n", strcpy(alloca(strlen(demo)+1),demo) );
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void *bounds_checking_is_enabled()
|
void *bounds_checking_is_enabled()
|
||||||
@ -4208,7 +4206,6 @@ double get100 () { return 100.0; }
|
|||||||
|
|
||||||
void callsave_test(void)
|
void callsave_test(void)
|
||||||
{
|
{
|
||||||
#if defined __i386__ || defined __x86_64__ || defined __arm__
|
|
||||||
int i, s; double *d; double t;
|
int i, s; double *d; double t;
|
||||||
s = sizeof (double);
|
s = sizeof (double);
|
||||||
printf ("callsavetest: %d\n", s);
|
printf ("callsavetest: %d\n", s);
|
||||||
@ -4221,7 +4218,6 @@ void callsave_test(void)
|
|||||||
generates a segfault. */
|
generates a segfault. */
|
||||||
i = d[0] > get100 ();
|
i = d[0] > get100 ();
|
||||||
printf ("%d\n", i);
|
printf ("%d\n", i);
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user