-2

Is there a way to force the execution of an assembly code that reports the Segmentation fault (core dumped) error?

Sexer
  • 1
  • 1
  • 2
    What do you want it to do? You can of course skip the instruction that caused the error but the effects will probably cascade so your program is unlikely to perform sensibly after that (technically it's not even supported). Once you got the fault you can also patch the code to remove that instruction. – Jester Mar 04 '22 at 19:42
  • I'm not sure what you're asking. Are you looking for an assembly instruction that causes a segfault, like `mov [0], eax`? Also, what architecture are you working on? – xiver77 Mar 04 '22 at 19:59
  • Or do you want to jump to the part that is causing a segfault? You can of course write some code to `jmp` to that part, or do you want some debugger to execute that specific part of code? Than you'll have to specify what tool you're using. – xiver77 Mar 04 '22 at 20:14
  • How? Do you want to map a fresh page of zeroed memory at that address, if the segfault was due to a bad address? Other causes of segfaults include (on x86-64 Linux) misaligned `movaps`/`movdqa` (could emulate by running it as `movups`), or trying to execute a privileged instruction like `invd` or `mov cr0, eax` in user-space (unfixable). – Peter Cordes Mar 05 '22 at 02:14
  • Near duplicate: [Why is a segmentation fault not recoverable?](https://stackoverflow.com/a/70270762) – Peter Cordes Mar 05 '22 at 02:16
  • @Jester In good old days DOS asked **Abort, Retry, Fail?** and OP probably wants to simulate the answer **Fail**. This could be achived by hooking the interrupt which caused error, then skip the failed instruction and return behind it. – vitsoft Mar 05 '22 at 07:59
  • 1
    Except this is a segfault. In the DOS case it was (likely) an I/O service call where the program was actually prepared to handle an error. Here, it doesn't even get an error indication if you just skip the instruction. As I said, it is possible to skip but except under very special circumstances that won't lead to anything sensible. – Jester Mar 05 '22 at 11:33
  • 1
    Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Mar 05 '22 at 14:26

1 Answers1

0

You need to understand what "segmentation fault" means to see why what you are asking for is nonsense.

First, for simplicity, imagine a system without a memory management unit. Suppose your PC has 8GB of RAM. When you cause this error you have told it to read or write from an address in the 9th GB of RAM. It just doesn't exist!

What do you want it to do when you write there? Just throw the data away, or store it somewhere else? If you want to store it somewhere else, then use the correct address of where you want to store it to begin with, then you won't get a segmentation fault in the first place.

What about when you tell it to read from there? What data do you want it to give you back? All zeros? All 42? What you really want is for it to give you back the value that you previously threw away, but you don't have enough RAM to store that value.

The situation gets a bit more complicated when you introduce the memory management unit that is present in modern PCs and servers and phones. In that case each process on the system doesn't use addresses from 0 to 8GB, each one might use different addresses, or the same overlapping addresses.

Suppose one process only needs 1GB of RAM. The operating system might tell it to use addresses from 50GB to 51GB. It then sets up the MMU to redirect these address to 2-3GB, but only if that particular process is running. Another process might need 4GB of RAM, and the OS tells it to use addresses 50 to 54GB, and then map these to 3-7GB of the real physical RAM, only when that process is running. Now what if the first of your processes asks for a byte of memory from address 53 GB? What does it do? That address doesn't mean anything for that process. If it tries to use the mapping for another process then that will break the mapping for the existing 1GB that it needs.

Tom V
  • 4,827
  • 2
  • 5
  • 22
  • I'm still confused what the OP is actually asking, but if he wants to force execution of a specific part of code that's causing a segfault, through a debugger or some reverse engineering tool, then I think that is a valid question, although he needs to add some details. – xiver77 Mar 04 '22 at 20:18
  • 1
    But yes it makes no sense to continue executing after a segfault has actually happened. – xiver77 Mar 04 '22 at 20:20
  • Each process has its own full-sized virtual address space, not shared with other processes. (Only with other threads of the same process). A context switch on a given CPU core involves telling the CPU about the new virt->phys mapping this task uses. But yes, only parts of it are mapped at all, either backed by anonymous memory (DRAM / swap space), or logically backed by memory-mapped files. (Emulated by the OS with help from the virtual memory hardware to see which pages are dirty). – Peter Cordes Mar 05 '22 at 02:09
  • x86-64 can use 1GiB hugepages, but normally OSes work in terms of 4k pages or 2M largepages, not having to defrag physical memory to find contiguous 1GiB regions. Your 1G ranges work ok as examples for this simplified explanation, though. (You're also glossing over lazy allocation / swap space; the effects that can lead to hardware page faults that *aren't* invalid (segfault), either soft or hard page faults for copy-on-write or to page in data from disk. But that's also fine; I mention it here only in case future readers are curious.) – Peter Cordes Mar 05 '22 at 02:11