Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do seg faults happen with virtual memory?

I'm confused how it's possible for a process to seg fault when using virtual memory. As I understand, 'virtual' memory allows a process to access all available memory, which is then mapped to 'actual' hardware memory. With this translation how is it possible for a process to try and access a part of memory it is not allowed to?

like image 398
dylan12345 Avatar asked Jul 15 '15 18:07

dylan12345


1 Answers

It sounds like you may be confused by confusing Unix usage. On Unix, you can get a SIGSEG (Invalid memory reference) signal. This signal can be sent to a process even when there are no "segments" in the underlying hardware. SIGBUS is another memory error that you can get. Over the years, I have not found a lot of consistency on various unix implementations over what condition causes which signal.

These are the programming errors that you can get in virtual memory accesses:

  1. There is no page table entry for the address (processes rarely have page table entries for every possible page in the address space--despite what the useless OS book many people ask questions about here).

  2. There is a page table entry for the address but no memory mapped (e.g., the first page is usually not mapped).

  3. There is memory mapped to the address but the page is not in memory (page fault).

  4. The memory is mapped but access is protected by processor mode. (e.g., attempting to access a kernel only page from user mode).

  5. The memory is mapped but the type of access is not allowed to the page. E.g.

    • Trying to execute a non-executable page.
    • Trying to write to a read-only page

(Others will point out if I have missed any.)

These events (other than #3——that gets handled by the OS) will usually trigger a SIGBUS or SIGSEG signal. However, as I said, there is little consistency as to which event above will trigger which of those signals.

like image 127
user3344003 Avatar answered Sep 23 '22 13:09

user3344003