-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[libc] Add stack-checking alloca for GCC/IA16
- Loading branch information
Showing
7 changed files
with
80 additions
and
7 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
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,51 @@ | ||
/* | ||
* IA16 stack checking helper routines | ||
* | ||
* 5 Dec 2024 Greg Haerr | ||
*/ | ||
|
||
#include <sys/cdefs.h> | ||
#include <alloca.h> | ||
#include <unistd.h> | ||
#include <stdio.h> | ||
|
||
extern unsigned int __stacklow; | ||
|
||
#define __SP() ((unsigned int)__builtin_frame_address(0)) /* NOTE not exact */ | ||
#define errmsg(str) write(STDERR_FILENO, str, sizeof(str) - 1) | ||
|
||
/* | ||
* Return true if stack can be extended by size bytes, | ||
* called by alloca() to check stack available. | ||
*/ | ||
int __stackavail(unsigned int size) | ||
{ | ||
unsigned int remaining = __SP() - __stacklow; | ||
|
||
if ((int)remaining >= 0 && remaining >= size) | ||
return 1; | ||
errmsg("ALLOCA FAIL, INCREASE STACK\n"); | ||
return 0; | ||
} | ||
|
||
#if 0 | ||
/* | ||
* Check if size bytes can be allocated from stack, | ||
* called from function prologue when -fstack-check set. | ||
*/ | ||
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) | ||
errmsg("STACK OVERFLOW\n"); | ||
else | ||
errmsg("STACK OVER LIMIT\n"); | ||
#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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Define and initialize the __stacklow variable, if needed | ||
// Assume dx = stacksize from entry point, from libc/crt0.S | ||
|
||
.arch i8086, nojumps | ||
.code16 | ||
|
||
.section .preinit,"ax",@progbits | ||
|
||
mov %sp,%si // SP will be 4 too high if __argc or __argv used | ||
sub %dx,%si | ||
mov %si,__stacklow | ||
|
||
//------------------------------------------------------------------------------ | ||
|
||
.comm __stacklow,2 |