Coming back to life after Segmentation Violation

前端 未结 13 2126
暗喜
暗喜 2020-12-08 11:26

Is it possible to restore the normal execution flow of a C program, after the Segmentation Fault error?

struct A {
    int x;
};
A* a = 0;

a->x = 123; /         


        
相关标签:
13条回答
  • 2020-12-08 12:17
    #include <unistd.h>
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/mman.h>
    #include <signal.h>
    #include <stdlib.h>
    #include <ucontext.h>
    
    void safe_func(void)
    {
        puts("Safe now ?");
        exit(0); //can't return to main, it's where the segfault occured.
    }
    
    void
    handler (int cause, siginfo_t * info, void *uap)
    {
      //For test. Never ever call stdio functions in a signal handler otherwise*/
      printf ("SIGSEGV raised at address %p\n", info->si_addr);
      ucontext_t *context = uap;
      /*On my particular system, compiled with gcc -O2, the offending instruction
      generated for "*f = 16;" is 6 bytes. Lets try to set the instruction
      pointer to the next instruction (general register 14 is EIP, on linux x86) */
      context->uc_mcontext.gregs[14] += 6; 
      //alternativly, try to jump to a "safe place"
      //context->uc_mcontext.gregs[14] = (unsigned int)safe_func;
    }
    
    int
    main (int argc, char *argv[])
    {
      struct sigaction sa;
      sa.sa_sigaction = handler;
      int *f = NULL;
      sigemptyset (&sa.sa_mask);
      sa.sa_flags = SA_SIGINFO;
      if (sigaction (SIGSEGV, &sa, 0)) {
          perror ("sigaction");
          exit(1);
      }
      //cause a segfault
      *f = 16; 
      puts("Still Alive");
      return 0;
    }
    
    $ ./a.out
    SIGSEGV raised at address (nil)
    Still Alive
    

    I would beat someone with a bat if I saw something like this in production code though, it's an ugly, for-fun hack. You'll have no idea if the segfault have corrupted some of your data, you'll have no sane way of recovering and know that everything is Ok now, there's no portable way of doing this. The only mildly sane thing you could do is try to log an error (use write() directly, not any of the stdio functions - they're not signal safe) and perhaps restart the program. For those cases you're much better off writing a superwisor process that monitors a child process exit, logs it and starts a new child process.

    0 讨论(0)
  • 2020-12-08 12:21

    This glib manual gives you a clear picture of how to write signal handlers.

    A signal handler is just a function that you compile together with the rest
    of the program. Instead of directly invoking the function, you use signal 
    or sigaction to tell the operating system to call it when a signal arrives.
    This is known as establishing the handler.
    

    In your case you will have to wait for the SIGSEGV indicating a segmentation fault. The list of other signals can be found here.

    Signal handlers are broadly classified into tow categories

    1. You can have the handler function note that the signal arrived by tweaking some global data structures, and then return normally.
    2. You can have the handler function terminate the program or transfer control to a point where it can recover from the situation that caused the signal.

    SIGSEGV comes under program error signals

    0 讨论(0)
  • 2020-12-08 12:22

    In POSIX, your process will get sent SIGSEGV when you do that. The default handler just crashes your program. You can add your own handler using the signal() call. You can implement whatever behaviour you like by handling the signal yourself.

    0 讨论(0)
  • 2020-12-08 12:24

    "All things are permissible, but not all are beneficial" - typically a segfault is game over for a good reason... A better idea than picking up where it was would be to keep your data persisted (database, or at least a file system) and enable it to pick up where it left off that way. This will give you much better data reliability all around.

    0 讨论(0)
  • 2020-12-08 12:24

    There is no meaningful way to recover from a SIGSEGV unless you know EXACTLY what caused it, and there's no way to do that in standard C. It may be possible (conceivably) in an instrumented environment, like a C-VM (?). The same is true for all program error signals; if you try to block/ignore them, or establish handlers that return normally, your program will probably break horribly when they happen unless perhaps they're generated by raise or kill.

    Just do yourself a favour and take error cases into account.

    0 讨论(0)
  • 2020-12-08 12:25

    You can use the SetUnhandledExceptionFilter() function (in windows), but even to be able to skip the "illegal" instruction you will need to be able to decode some assembler opcodes. And, as glowcoder said, even if it would "comment out" in runtime the instructions that generates segfaults, what will be left from the original program logic (if it may be called so)? Everything is possible, but it doesn't mean that it has to be done.

    0 讨论(0)
提交回复
热议问题