What prevents me from executing just jmp @9274592
even if it's outside of my program's limits? Don't I write directly into the CPU? What exactly, in the operating system I presume, prevents the command from being executed?
That depends on your operating system, and on your hardware. On modern systems, the operating system controls hardware-provided memory protection .
In typical microprocessors of the 1980s, nothing prevents access (including execution) anywhere in the memory space - and erroneous code will likely crash the machine. This is also true of early single-user machines with cooperative multitasking, such as DOS and the original Mac.
In modern workstation-class machines, the processor includes a Memory Management Unit (MMU) , which controls access to memory. The most widely-used mechanism is virtual memory , with per-page permission bits. Each process has its own virtual address space.
Operating systems such as Unix or Linux write to the MMU to specify which pages can be read/written/executed by the current process (writing to MMU is a privileged operation that can't be done by the process itself).
A jump to a location in a page without execute permission will cause a processor trap; this switches the processor to privileged mode and enters the kernel, which can then decide what to do. Usually, this means sending a SIGSEGV
signal to the process (and this is why you might see an error message like Segmentation fault
when your program crashes).
When several user processes are running simultaneously in a time-sliced environment , it is the kernel's job to switch the MMU contents at every context switch. The cost of invalidating the TLB on an address-space switch (and the resulting TLB misses) can constitute the major overhead in time-shared systems.
A long list of things could go wrong if you jump to an arbitrary location:
Assuming for the moment you are not on a system logical memory translation, things that might happen:
If you are on a system with logical memory translation:
Any of the above will trigger either a fault or trap.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.