Consultor Eletrônico



Kbase P73825: What is SegmentationFault on Unix
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   25/03/2004
Status: Unverified

GOAL:

What is SegmentationFault on Unix

GOAL:

What is SegFault on Unix

FIX:

SegmentationFault is the error which arises when a program tries to access memory to which it is not allowed access (if the hardware and OS provide MemoryProtection?, that is). SegFault is a UnixOperatingSystem term; in the Windows world, the terms GeneralProtectionFault and AccessViolation? are used.

The name derives from the fact that unix organises memory into virtual memory pages or segments, each potentially with its own read and write permissions; writing to a page marked read-only, or reading from a page that does not allow reading, will cause a hardware interrupt which is translated by the kernel into a software interrupt: a SegmentationViolation? signal.

The word "segment" dates to the 1970s, when Unix ran on systems (e.g. the pdp11, and later, the 80286 and 80386) that did not have virtual memory paging, but did have a small number of memory protected "segments"; the two terms are sometimes used synonymously, other times they are used in contrast. When contrasted, typically all "pages" on a system have the same size (e.g. 8192 bytes), while "segments" typically are variable length.

Also the term "page" tends to imply hardware supported virtual memory, that allows pages to be loaded on demand, in response to a hardware interrupt on attempted access (in which case no signal is delivered to the user process), whereas the older "segment" implied hardware memory protection but no ability to demand-load pages; instead entire processes were swapped in and out of memory.

Compare with "bus error", the software signal (standard value 7) that means that, although the memory page was potentially accessible according to its read/write permissions, the attempted memory access caused the hardware to complain with an interrupt. This occurs most often on a RISC machine when word-oriented memory is accessed on a non-word-aligned boundary. It can also happen when non-existent or buggy memory is mapped into the address space.

C programmers can spend their whole careers in environments where Bus Error never happens, since it depends on the nature of the machine and details of the kernel implementation. Also some Unix kernels arbitrarily decide to deliver all such hardware complaints as Segmentation Violations, so it's not guaranteed that one can get a Bus Error on any architecture.

Related signals that are translations of hardware conditions:

SIGILL - illegal instruction (e.g. cpu doesn't recognize opcode)

SIGTRAP - trap ("interrupt") from user space to kernel space (some CPUs have special instructions to do this, typically used to implement system calls; this can indicate e.g. a trap instruction not used by convention for a system call)

SIGIOT - IO trap : error during execution of IO instruction (not all CPUs have such instructions)

SIGFPE - interrupt triggered by floating point unit; typically floating point divide by zero.

See /usr/include/*/signal.h for definitions (not descriptions), e.g. on Linux /usr/include/asm/signal.h