Skip to content

Commit

Permalink
[libc] Add proper alloca to OpenWatcom C library
Browse files Browse the repository at this point in the history
  • Loading branch information
ghaerr committed Dec 5, 2024
1 parent 8e6d1e2 commit b5f6cb3
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 12 deletions.
25 changes: 25 additions & 0 deletions libc/include/alloca.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef __ALLOCA_H
#define __ALLOCA_H

// void *alloca(size_t);

int __stackavail(unsigned int size);
#define __ALLOCA_ALIGN(s) (((s)+(sizeof(int)-1))&~(sizeof(int)-1))

#define alloca(s) (__stackavail(__ALLOCA_ALIGN(s))? \
__alloca(__ALLOCA_ALIGN(s)): (void *)0)

#ifdef __WATCOMC__
#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
1 change: 0 additions & 1 deletion libc/include/malloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ void *malloc(size_t);
void free(void *);
void *realloc(void *, size_t);
void *calloc(size_t elm, size_t sz);
void *alloca(size_t);

#ifdef __LIBC__
/*
Expand Down
1 change: 1 addition & 0 deletions libc/include/stdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <features.h>
#include <sys/types.h>
#include <malloc.h>
#include <alloca.h>
#include <arch/divmod.h>

/* Don't overwrite user definitions of NULL */
Expand Down
13 changes: 2 additions & 11 deletions libc/watcom/syscall/crt0.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,8 @@ int __argc;
char **__argv;
char *__program_filename;
char **environ;
unsigned int __stacklow;
unsigned char _HShift = 12; /* huge pointer support required by pia.asm */

static unsigned int _SP(void);
#pragma aux _SP = __value [__sp]

/* called by alloca() to check stack available */
unsigned int stackavail(void)
{
return (_SP() - __stacklow);
}
unsigned int __stacklow; /* lowest valid SP value */
unsigned char _HShift = 12; /* huge pointer support required by pia.asm */

#if defined(__SMALL__) || defined(__MEDIUM__) /* near data models */
/* no argv/environ rewrite */
Expand Down
69 changes: 69 additions & 0 deletions libc/watcom/syscall/stackcheck.c
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
}

0 comments on commit b5f6cb3

Please sign in to comment.