//---------------------------------------------------------------- // signal6.cpp // // This program will attempt to dereference a null pointer, // but beforehand it installs a signal-handler for SIGSEGV. // Now our signal-handler will 'return' instead of 'exit', // but not until it has adjusted the saved image of the EIP // register, located at tos[15] on the stack, in order that // the 2-byte instruction 'mov (%eax), %eax' which attempts // to read an invalid memory-address will now be 'skipped'. // // compile-and-link using: $ g++ signal6.cpp -o signal6 // // programmer: ALLAN CRUSE // written on: 20 MAR 2006 //---------------------------------------------------------------- #include // for printf() #include // for signal() #include // for exit() void upon_signal( int signo ) { printf( "\nCaught signal number %d \n\n", signo ); int *tos = &signo; for (int k = 1; k <= 20; k++) { int i = 20 - k; printf( "%08X: %08X =tos[%d] \n", &tos[i], tos[i], i ); } printf( "\n" ); tos[15] += 2; // exit( signo ); } int main( int argc, char *argv[] ) { signal( SIGSEGV, upon_signal ); int *ptr = NULL; asm(" movl $0xAAAAAAAA, %eax "); asm(" movl $0xBBBBBBBB, %ebx "); asm(" movl $0xCCCCCCCC, %ecx "); asm(" movl $0xDDDDDDDD, %edx "); asm(" movl $0xEEEEEEEE, %esi "); asm(" movl $0xFFFFFFFF, %edi "); asm(" nop "); int x = *ptr; printf( "\nx = %08X \n\n", x ); }