-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2123 from ghaerr/alloca
[libc] Add proper alloca to OpenWatcom C library
- Loading branch information
Showing
5 changed files
with
109 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#ifndef __ALLOCA_H | ||
#define __ALLOCA_H | ||
/* | ||
* Stack-checking alloca for GCC and OWC | ||
*/ | ||
|
||
// void *alloca(size_t); | ||
|
||
#define alloca(s) (__stackavail(__ALLOCA_ALIGN(s))? \ | ||
__alloca(__ALLOCA_ALIGN(s)): (void *)0) | ||
|
||
#define __ALLOCA_ALIGN(s) (((s)+(sizeof(int)-1))&~(sizeof(int)-1)) | ||
|
||
#ifdef __GNUC__ | ||
/* The compiler auto-aligns the stack from the parameter somewhat strangely: | ||
* 0 -> 0, 1 -> 2, 2 -> 4, 3 -> 4, 4 -> 6 etc. | ||
* Thus, __stackavail should check for two more bytes available than asked for. | ||
*/ | ||
#define __alloca(s) __builtin_alloca(s) | ||
#define __stackavail(s) 0 /* temp no stack checking */ | ||
#endif | ||
|
||
#ifdef __WATCOMC__ | ||
int __stackavail(unsigned int size); | ||
#pragma aux __stackavail "*" __modify __nomemory | ||
|
||
extern void __based(__segname("_STACK")) *__alloca(unsigned int __size); | ||
#pragma aux __alloca = \ | ||
"sub sp,ax" \ | ||
__parm __nomemory [__ax] \ | ||
__value [__sp] \ | ||
__modify __exact __nomemory [__sp] | ||
|
||
#endif | ||
|
||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Open Watcom C stack checking helper routines | ||
* | ||
* 4 Dec 2024 Greg Haerr | ||
*/ | ||
|
||
#include <sys/cdefs.h> | ||
#include <alloca.h> | ||
#include <unistd.h> | ||
|
||
extern unsigned int __stacklow; | ||
|
||
#define errmsg(str) write(STDERR_FILENO, str, sizeof(str) - 1) | ||
|
||
static unsigned int __SP(void); | ||
#pragma aux __SP = __value [__sp] | ||
|
||
static void stack_alloca_warning(void) | ||
{ | ||
errmsg("ALLOCA FAIL, INCREASE STACK\n"); | ||
} | ||
|
||
/* | ||
* Return true if stack can be extended by size bytes, | ||
* called by alloca() to check stack available. | ||
*/ | ||
#pragma aux __stackavail "*" | ||
int __stackavail(unsigned int size) | ||
{ | ||
unsigned int remaining = __SP() - __stacklow; | ||
|
||
if ((int)remaining >= 0 && remaining >= size) | ||
return 1; | ||
stack_alloca_warning(); | ||
return 0; | ||
} | ||
|
||
#if LATER | ||
static void stack_overflow(void) | ||
{ | ||
errmsg("STACK OVERFLOW\n"); | ||
} | ||
|
||
static void stack_overlimit(void) | ||
{ | ||
errmsg("STACK OVER LIMIT\n"); | ||
} | ||
#endif | ||
|
||
/* | ||
* Check if size bytes can be allocated from stack, | ||
* called from function prologue when -fstack-check set. | ||
*/ | ||
#pragma aux __STK "*" | ||
void __STK(unsigned int size) | ||
{ | ||
unsigned int remaining = __SP() - __stacklow; | ||
unsigned int curbreak; | ||
|
||
if ((int)remaining >= 0 && remaining >= size) | ||
return; | ||
curbreak = (unsigned int)sbrk(0); /* NOTE syscall here will cause SIGSEGV sent */ | ||
#if LATER | ||
if (__SP() < curbreak) | ||
stack_overflow(); | ||
else | ||
stack_overlimit(); | ||
#endif | ||
} |