Tuesday, October 4
Shadow

Asan error

#include <stdio.h>
#include <signal.h>
#include <setjmp.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

static jmp_buf jb;
static int exec_num;
static const char* astr;
static const char* bstr;
static const char* cstr;

void A(void);
void B(void);
void C(void);

void sighandler(int sig) {
   char msg[] = "Handling SIGSEGV signal in stack frame S.\n";
   write(STDOUT_FILENO, msg, sizeof(msg));
   char msg2[] = "Calling back into C().\n";
   write(STDOUT_FILENO, msg2, sizeof(msg2));

   write(STDOUT_FILENO, astr, strlen(astr));
   write(STDOUT_FILENO, bstr, strlen(bstr));
   write(STDOUT_FILENO, cstr, strlen(cstr));
}


int main(int argc, char** argv) {
    signal(SIGSEGV, sighandler);
    A();
}

void A(void) {
    char msg[] = "In stack frame A. Calling B().";
    puts(msg);

    char astrs[] = "This is on the stack in A(). Is it still valid?\n";
    astr = astrs;

    B();
}

void B(void) {
    char msg[] = "In stack frame B. Calling setjmp(), then calling C().";
    puts(msg);

    char bstrs[] = "This is on the stack in B(). Is it still valid?\n";
    bstr = bstrs;

    int second_time = setjmp(jb);
    int first_time  = !second_time;
    if (first_time)
        C();
    else
        raise(SIGSEGV);
}

void C(void) {
    char msg[] = "In stack frame C. Now longjmp() back to B().";
    puts(msg);

    char cstrs[] = "This is on the stack in C(). Is it still valid?\n";
    cstr = cstrs;

    longjmp(jb, 1);
}

// Compiled without asan:
/*
apaz[x][-][~]$ clang asan_err.c && ./a.out 
In stack frame A. Calling B().
In stack frame B. Calling setjmp(), then calling C().
In stack frame C. Now longjmp() back to B().
Handling SIGSEGV signal in stack frame S.
Calling back into C().
This is on the stack in A(). Is it still valid?
This is on the stack in B(). Is it still valid?
This is on the stack in C(). Is it still valid?
apaz[x][-][~]$
*/


// Compiled with asan:
/*
apaz[x][-][~]$ clang asan_err.c -fsanitize=address && ./a.out 
In stack frame A. Calling B().
In stack frame B. Calling setjmp(), then calling C().
In stack frame C. Now longjmp() back to B().
Handling SIGSEGV signal in stack frame S.
Calling back into C().
This is on the stack in A(). Is it still valid?
This is on the stack in B(). Is it still valid?
This is on the stack in .(7vapaz[x][-][~]$ 
*/

Leave a Reply