Home > Enterprise >  How to detect memory addresses that cause Address Boundary Error when writing data on it
How to detect memory addresses that cause Address Boundary Error when writing data on it

Time:01-03

I have this C program:

void main(){
    *(unsigned int*)0x100000 = 0xdeadbeef;
}

When running this C Program, it obviously causes an address boundary error when running. Is there a way to detect potential address boundary errors on certain memory addresses?

CodePudding user response:

Here is a way to recover from a SIGSEGV signal by using a signal handler and performing a non local goto :

#include <stdio.h>
#include <signal.h>
#include <setjmp.h>

static volatile sig_atomic_t jump = 0;
static sigjmp_buf env;

static void handler(int sig)
{
    if (!jump)
        return;

    siglongjmp(env, 1);
}

int main()
{
    /* install the signal handler */
    struct sigaction sa;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = 0;
    sa.sa_handler = handler;

    if (sigaction(SIGSEGV, &sa, NULL) == -1) {

        perror("sigaction");
        return 1;
    }

    if (sigsetjmp(env, 1) == 0) {

        jump = 1;
        /* will trigger SIGSEGV */
        *(unsigned int *)0x100000 = 0xdeadbeef;

    } else {
        /* after siglongjmp */
        puts("SIGSEGV detected");
        return 0;
    }

    return 1;
}

It is POSIX specific but at least it can gives a clue on how it can be done on Linux for example. Hope this helps.

  • Related