简体   繁体   中英

What prevents me from jumping to any absolute address using ASM?

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:

  1. The memory location does not exist.
  2. The memory location is not correctly aligned for an instruction (some processors)
  3. The memory location does not contain a valid instruction.

If you are on a system with logical memory translation:

  1. The memory location has no page table entry.
  2. The memory location has a page table entry marked invalid.
  3. The page table entry is marked no execute.
  4. The page table entry is marked as having a more privilege access mode.
  5. The memory location identifies an instruction that attempts access memory that causes any of the above.
  6. The memory location is a privileged instruction that cannot be executed in the current access mode.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM