0

I created a assembly referencing invalid address.

.global main
main:
 push %eax
 call 0x12341234 # invalid address

I compiled and run it on linux, and the result was segmentation fault of course.

However I want to do something like call my_exception_handler
whenever it referencing invalid address, instead of raising segmentation fault.

Is there any method to do this in assembly level?

Jiwon
  • 1,074
  • 1
  • 11
  • 27
  • That depends on which operating you are programming for. Some operating systems provide methods to recover from such exceptions. – fuz Nov 03 '18 at 10:30
  • @fuz Oh I missed mentioning that. I use Linux. Ubuntu 16.04 x86 in detail. – Jiwon Nov 03 '18 at 10:33
  • It won't be a C++-style exception. What you can do is install a *signal handler* for `SIGSEGV`. [How to write a signal handler to catch SIGSEGV?](https://stackoverflow.com/a/2663592). Doing this from asm may give you more control than from compiled C, because most C library functions aren't safe from signal handlers. But direct system calls mostly are, I think. – Peter Cordes Nov 03 '18 at 10:44
  • 1
    @Jiwon In this case you can install a signal handler for `SIGSEGV` and `SIGBUS` , determine if the cause of the segmentation violation was a stray function call, and then jump to your exception handler. You can also just handle the exception inside your signal handler. – fuz Nov 03 '18 at 10:44
  • I'm not sure this is an exact duplicate. In asm for Linux specifically, you don't have to worry about nearly as much C / POSIX undefined behaviour. The details of how you'd go about writing a handler may differ, since I *think* you can use `sys_write` safely from a SIGSEGV handler, while you can't use C stdio functions like printf. If anyone thinks we should reopen, let me know (or vote to reopen yourself). But there might be a more specific duplicate; I didn't look for long. – Peter Cordes Nov 03 '18 at 12:02

0 Answers0