bebbo / libnix

libnix (v4): a C link library for AmigaOS/m68k
14 stars 9 forks source link

libnix: swapstack via __stack + linking libnix/lib/swapstack.o does not work correctly #38

Closed githubaf closed 3 years ago

githubaf commented 3 years ago

I tried to enforce a big stack by defining a global variable __stack and linking with libnix/lib/swapstack.o. The program seems to damage the memory structures of the Amiga somehow. I use the following test program:

/*
 * AF, HGW, 09.Jan.2021, selco
 * 
~/opt/m68k-amigaos_01Jan21/bin/m68k-amigaos-gcc stack_test.c ~/opt/m68k-amigaos_01Jan21/m68k-amigaos/libnix/lib/swapstack.o  -Wall --pedantic -noixemul -o stacktest # -Wl,-Map=- | grep swapstack
*/

#include <stdio.h>
#define BUFFER_SIZE 1000000

unsigned long __stack={BUFFER_SIZE+4096};

#include <exec/exec.h> 
#include <dos/dos.h> 
#include <proto/exec.h> 
#include <proto/dos.h> 
ULONG getStackSize(void)
{
    struct Process *myproc = (struct Process *)FindTask(NULL); 
    return  (char*)myproc->pr_Task.tc_SPUpper - (char*)myproc->pr_Task.tc_SPLower; 
}

int main(void)
{
    char Buffer[BUFFER_SIZE];  // huge buffer on Stack
    memset(Buffer,0x42,BUFFER_SIZE);
    printf("Stacksize=%lu\n",getStackSize());
    return 0;
}

The program crashes. If I comment the memset()-line, it displays Stacksize=1004096 as expected. If I set the stack in the shell to the same value the program requires, it displays that value and does not crash, even with the memset-line.

Am I doing something wrong or is swapstack.o broken?

bebbo commented 3 years ago

works here as expected. maybe your libnix gets built without -fomit-frame-pointer ?

githubaf commented 3 years ago

Thanks for looking. What I always do is rebuilding libnix with debug-information. So I have a script that does

time CFLAGS_FOR_TARGET=-g make libnix -j2 PREFIX=$PREFIX

The idea was to just add the -g, or does it overwrite your flags?

bebbo commented 3 years ago

that overrides the flags. for debugging it's maybe a good idea.

with the last change swapstack.o is always built with -fomit-frame-pointer

githubaf commented 3 years ago

Yes, now it works again. You build it with -fomit-frame-pointer even if I specify -g only. Thank you!