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?
5 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:
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).
There is a page table entry for the address but no memory mapped (e.g., the first page is usually not mapped).
There is memory mapped to the address but the page is not in memory (page fault).
The memory is mapped but access is protected by processor mode. (e.g., attempting to access a kernel only page from user mode).
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.

- 20,574
- 3
- 26
- 62
Segmentation fault, is when you try to access a location that is not allocated to your variable. Common case is going beyond array bounds or trying to access non allocated dynamic variable. You have to make the distinction between this scenario and the virtual memory. The system enforce this to prevent your program from destroying its own data or the data of other programs running on the system; and even worse from destroying the data of the system itself. Think about it.
Virtual memory simply means that you can be allocated more memory than the physical RAM. It does not mean that you can access such a space without requesting it in the first place.

- 4,588
- 1
- 29
- 42
-
1I think the user is referring to a [virtual address space](https://msdn.microsoft.com/en-us/library/windows/desktop/aa366912(v=vs.85).aspx), rather than virtual memory. If the address space you are presented by the operating system belongs entirely to your process, and is mapped to the physical memory by the operating system, then how is it possible to stray beyond these bounds? – dark_perfect Jul 15 '15 at 19:10
-
@dark_perfect, it might seem bizarre! But when you think from the point of view of a program writer, things differ. The simplest case is to allocate an array of size 10 that fits in your address space, and somehow try to access element 1000000000 of the same array. Chances are that you get out of your address space. You will certainly ask me how I got 1000000000. The answer is computed index or typo in the program, or etc. have a look at http://stackoverflow.com/questions/2346806/what-is-segmentation-fault – innoSPG Jul 15 '15 at 19:19
-
2I understand that segmentation faults usually happen via a programming fault, but if your process's virtual address space is created and allocated when the operating system creates your process, then the 100000000th element should be in your address space too, shouldn't it? Isn't the point of it so that it appears as a contigious block of memory to your process, but is mapped to physical memory whichever way the operating system fancies? So if your process tries to access an address in it's OWN address space, but ends up accessing one in another process's...hasn't the OS mapped it incorrectly? – dark_perfect Jul 15 '15 at 19:33
-
@dark_perfect, there is no reason to have 1000000000 elements in your address space since you requested only 10 elements. If you requested 1000000000 in the first place, you will have it in your address space. It is the same problem when you use non initialized pointer in languages like C/C++, fortran, etc. that allow you to manipulate pointers by yourself. – innoSPG Jul 15 '15 at 19:41
Segmentation fault usually arises due to the process trying to access memory which is allocated for another process. Every process is allocated "virtual space" is mapped to a physical address, which is translated using TLB/ Page table and so on.
Now if the application level program tries to use the memory that is not allocated to it, due to errors in the program, that is directed all the way to address translation.
Eg: Imagine a stack has been coded. Now, if the stack overflow condition is untested, then this code is executed and the address translation refers to an address beyond the program's address space which leads to Segmentation fault.

- 85
- 8
Well, you have the whole address space, meaning you can have as much memory as you can. But, you have to ask for it first. Maybe you can allocate yourself the whole address space and then try to access all the memory, you won't get segfault. But, when you try to access beyond the memory where you haven't asked for yet, you get the segmentation fault.

- 7,591
- 9
- 40
- 59
On systems using hardware memory segmentation to provide virtual memory, a segmentation fault occurs when the hardware detects an attempt to refer to a non-existent segment, or to refer to a location outside the bounds of a segment, or to refer to a location in a fashion not allowed by the permissions granted for that segment. On systems using only paging, an invalid page fault generally leads to a segmentation fault, and segmentation faults and page faults are both faults raised by the virtual memory management system. Segmentation faults can also occur independently of page faults: illegal access to a valid page is a segmentation fault, but not an invalid page fault, and segmentation faults can occur in the middle of a page (hence no page fault), for example in a buffer overflow that stays within a page but illegally overwrites memory. From https://en.wikipedia.org/wiki/Segmentation_fault

- 589
- 1
- 5
- 19