Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How exactly an access violation exception is triggered

Sometimes bugs can cause memory access violation exception.

How exactly this exception is triggered? What mechanism works behind the scenes?

Does it need support from the CPU (starting at what CPU?) / from the OS (starting at what version?) / from the compiler (starting at what version?)

Edit:

One specific scenario I want to understand better:

The following code may cause an access violation exception.

TCHAR* czXXX= _T("ABCDEFG");
czXXX[0]= 'A';

I guess czXXX points to a read-only memory block, but what exactly happens?

like image 925
Lior Kogan Avatar asked Jan 30 '11 09:01

Lior Kogan


People also ask

What does Exception access violation mean?

What does Exception access violation mean? Exception access violation comes as a sign of malware infection or that some parts of the software you are trying to launch are accessing protected memory addresses.

What is exited with code 0xC0000005 access violation?

It is stated that 0xC0000005 error usually occurs when a particular application is trying to access memory, which is no longer available or can't be accessed for some reason.


1 Answers

Memory access violations are a large topic :)

The Protection of Information in Computer Systems (from 1973 :) lays out of a mechanism of segments, where processes are allocated a base and a bound; any attempt to access memory outside the range base:base+bound meant the program had done something silly and should be killed.

The 80x86 line of processors implement basic segment support, and the GEMSOS security kernel is an A1-certified operating system kernel based on this mechanism.

But segments aren't very dynamic, and almost all modern operating systems are paging systems, that page in memory when it isn't available. This relies on the CPU having an MMU, memory management unit, that checks all memory accesses for correct privileges and presence/absence of the correct memory mapping. When a process tries to access memory that isn't currently mapped into RAM, the MMU signals the CPU that a fault has occurred, and the CPU suspends the process to load the requested memory page from disk. (Or, if the memory should not be mapped for the process, say it tries to access 0x0 or some random memory location that hasn't been mapped with mmap or similar memory allocating primitives, it kills the process.)

Intel's 80386 was the first Intel chip to support paging, which is why Windows 3.1's "386 Enchanced Mode" was so much better than the 286 mode.

Compilers aren't really involved, but the CPU, MMU, and operating system kernel must all work together.

like image 138
sarnold Avatar answered Nov 02 '22 08:11

sarnold