CS 208 s21 — Exceptions
1 Intro
1.1 Normal control flow
- control flow is the successive transfer of control from one instruction to the next
- each control transfer within this flow involves going from executing \(I_k\) at address \(a_k\) to instruction \(I_{k+1}\) at address \(a_{k+1}\)
- \(I_k\) may be adjacent to \(I_{k+1}\) in memory, but instructions like jumps, calls, and returns can redirect to different regions of stored instructions
1.2 Exceptional control flow (ECF)
- jumps, calls, and returns only react to internal program state, ECF allows all levels of the system to react to abrupt external changes
- underlies mechanisms for I/O, processes, virtual memory
- involved in how application interacts with the OS (system calls)
- an important part of writing event-driven programs like Unix shells and web servers
- basic building block for concurrency
2 Exceptions
- partly hardware, partly software
- thus, details vary by system (general ideas the same)
- exception is abrupt change in control flow in response to change in processor state
- change in state called an event
- Java/Python exceptions are not the same, they react to program state and don't involve the hardware
- indirect jump to exception handler procedure through exception table
2.1 Exception Handling
- each possible exception assigned unique exception number
- either by the processor designers or system kernel, depending on the type of exception
- exception table initialized at boot
- address store in special exception table base register
- differences from a normal procedure call
- return address pushed on the stack can be current or next instruction, depending on exception
- other processor state pushed onto stack to facilitate restoring pre-expception context
- may use the kernel's stack instead of user's
- run in kernel mode, given full access to system resources
2.2 Classes of Exceptions
Class | Cause | Async/sync | Return behavior |
---|---|---|---|
interrupt | signal from I/O device | async | always return to next instruction |
trap | intentional exception | sync | always return to next instruction |
fault | potentially recoverable error | sync | might return to current instruction |
abort | nonrecoverable error | sync | never returns |
2.2.1 Interrupts
- asynchronous since exception arises from I/O rather than specific instruction
- hitting ctrl-C, clicking the mouse, data arrives from the network, timer interrupt
2.2.2 Traps and System Calls
- interface between user programs and kernel: the system call
- request for services like reading a file, creating a new process, exiting current process
- from the programmer's perspective appears like any other function call, implementation very different
- returns to next instruction
2.2.3 Faults
- page faults, segment protection faults, divide-by-zero
// page fault example int a[1000]; int main() { a[500] = 13; // this portion of memory is currently only on disk }
mov
instruction interrupted while page fault handler loads corresponding page into memorymov
restarted afterwards
// seg fault example int a[1000]; int main() { a[5000] = 13; // this refers to an invalid address }
mov
instruction interrupted while page fault handler detects invalid address- sends
SIGSEGV
signal to use process, causing it to exit in a seg fault - handler tries to correct, returns to current instruction if it can
- otherwise returns to
abort
routine
2.2.4 Aborts
- fatal errors, application terminated
- hardware failure
2.3 Exceptions on Linux/x86-64 Systems
- divide error: divide by 0 or result too big for destination, Unix opts to abort ("floating exceptions")
- general protection fault: access to undefined area of virtual memory or attempt to write to read-only segment, no recovery
- notorious "segmentation faults"
- page fault: segment of memory must be loaded from disk into memory, instruction restarted
- machine check: fatal hardware error, always aborts
- C standard library provides wrappers for system calls (system level functions)