I have written a segmentation fault handler, using sigsetjmp and siglongjmp. Once it goes to the signal handler, i invoke siglongjmp so that the faulty instruction is skipped.
Problem is, i again want to cause SIGSEGV and go to the same handler, but now sigsetjmp will return 1.
How to reset sigsetjmp?
Here is my code:
#include <stdio.h>
#include <memory.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <signal.h>
#include <setjmp.h>
sigjmp_buf env, env1;
void SIGSEGV_handler(int signal)
{
printf("Segmentation fault caught\n");
siglongjmp(env, 1);
}
int main()
{
void * allocation;
size_t size;
static int devZerofd = -1;
struct sigaction sa, sa1;
sa.sa_handler=(void*)SIGSEGV_handler;
sigaction(SIGSEGV, &sa, NULL);
if ( devZerofd == -1 ) {
devZerofd = open("/dev/zero", O_RDWR);
if ( devZerofd < 0 )
perror("open() on /dev/zero failed");
}
allocation = (caddr_t) mmap(0, 5000, PROT_READ|PROT_NONE, MAP_PRIVATE, devZerofd, 0);
if ( allocation == (caddr_t)-1 )
fprintf(stderr, "mmap() failed ");
if ( mprotect((caddr_t)allocation, 5000, PROT_NONE) < 0 )
fprintf(stderr, "mprotect failed");
else
printf("mprotect done: memory allocated at address %u\n",allocation);
if(sigsetjmp(env, 1)==0) {
printf("Causing SIGSEGV: 1\n");
strcpy(allocation,"Hello, how are you");
}
/****** This can't be done again as sigsetjmp won't return 0*****/
/*
if(sigsetjmp(env, 1)==0) {
printf("Causing SIGSEGV: 1\n");
strcpy(allocation,"Hello, how are you");
}
*/
}
You have misunderstood how
[sig]setjmpworks. If you uncomment the code that you think won’t work, compile it, and run it, you will see that it does in fact work.setjmpcannot be made to return zero by callinglongjmp. If you callsetjmpitself a second time, even with the samejmp_buf(as you do here), it will return zero a second time.You have a bug, by the way: you didn’t set up your
sigactionparameter structure correctly. You should have done this:The use of
mmapis a little infelicitous, but not actually buggy. You don’t need/dev/zeroon most current platforms, you can just useMAP_ANON(some platforms spell itMAP_ANONYMOUS) and a -1 fd argument. And you should be usinggetpagesizeand then asking for a whole number of pages.