Section 14.28

Trampoline

Definition

A trampoline is a short piece of code that execution enters only to be passed on: it does a small fixed job and then jumps to the real destination. In instrumentation §14.24 and live patching §14.33, a jump written over the first instructions of a function leads to a trampoline, which runs added code, such as a probe that records the call, then copies of the overwritten instructions, and jumps back to the instruction after them. A copied instruction that computes an address from its own position must be corrected.

The idea

A developer wants extra code to run every time the function send is called in a running program, without rebuilding or restarting it. Every call passes the start of send, so the developer overwrites its first few bytes with a jump to new code. Those bytes, however, held instructions that send needs. The new code must therefore run the extra code, run the overwritten instructions, and continue send at the first instruction that was not overwritten. That new code is the trampoline.

A road detour is a fair picture. A sign at the start of a closed stretch sends traffic along a side road that rejoins the main road exactly where the closed stretch ends, and a driver already inside the stretch when it closed is stranded. The picture stops being accurate in one respect: a piece of road rebuilt elsewhere still leads where it led before, but some instructions, once moved, reach different addresses.

The word has related uses: a linker’s §12.4 long-branch stub, which Arm tools call a veneer, carries a call whose target is too far for one branch instruction; in functional programming, a trampoline is a loop that calls functions one after another, each returning the next to call, so that the stack does not grow. This entry treats the instrumentation sense.

How it works

Building a trampoline for a patch at the start of a function takes six steps.

  1. Decode. A decoder, a routine that knows the instruction set architecture §4.38 (ISA), meaning the processor’s instructions and their binary formats, finds where each instruction at the patch site starts and ends. In 64-bit Arm code every instruction is 4 bytes long; in x86-64 code instructions are 1 to 15 bytes long, and in the Thumb instruction set §4.40 of Arm microcontrollers §6.1 2 or 4 bytes, so there the boundaries must be decoded.

  2. Choose the jump and the displaced instructions. The jump has a size and a reach: a 4-byte Thumb branch reaches up to about 16 MiB away, a 5-byte x86-64 jump up to about 2 GiB. The displaced instructions are the shortest run of whole instructions that covers the jump’s bytes; an instruction is never split.

  3. Check the displaced range. No jump from elsewhere may target an address inside the range other than its first byte, because it would land inside the new jump’s bytes, and no thread may be stopped inside the range.

  4. Write the trampoline in executable memory: save the registers and condition flags §4.42, the bits that record the outcome of the last comparison, that the added code may change; call the added code; restore what was saved; run the displaced instructions, corrected as in step 5; and jump to the continuation address, the address of the first instruction after the displaced range.

  5. Correct position-dependent instructions. Some instructions compute an address from the program counter §2.9 (PC), the register that holds the address of the instruction being executed: a relative branch or call, a load of a constant stored near the code, an instruction that forms an address. At the trampoline’s address the same bytes give a different address, so each is re-encoded with a new offset or, when that offset does not fit the instruction’s format, replaced with a sequence that uses the absolute address.

  6. Publish. Make the trampoline visible to instruction fetch, the processor’s reading of instructions from memory, then write the jump so that no processor can execute a mixture of old and new bytes; the Dynamic patching and hot update entry treats this step.

Each ISA defines exactly which value an instruction reads as the PC.

Two more kinds of state need care. Condition flags may be read by the instructions after the patch site, so the trampoline saves and restores them if the added code can change them. In Thumb code, an If-Then (IT) instruction makes up to four following instructions conditional; displacing part of such an IT block §4.41, or the IT instruction without the instructions it controls, changes which instructions run.

Worked example

Whole instructions. An invented ISA with instructions of different lengths has a 2-byte instruction at address 1000 and a 4-byte instruction at address 1002, which occupies bytes 1002 to 1005. A 4-byte patch jump would occupy bytes 1000 to 1003, ending in the middle of the second instruction, so both instructions are displaced: 2+4=62 + 4 = 6 bytes, addresses 1000 to 1005. The trampoline ends with a jump to the continuation address 1006. Bytes 1004 and 1005 keep the tail of the old instruction and are never executed. The patch is safe only if no other jump targets address 1002, the start of the second instruction, which now lies inside the new jump.

A relative branch. In the same ISA, a branch’s target is its own address plus a signed offset stored in it. The displaced instruction at 1000 has offset 24, so its target is 1000+24=10241000 + 24 = 1024. Its copy sits at address 2000 in the trampoline. Copied unchanged, it would jump to 2000+24=20242000 + 24 = 2024, 1000 bytes too far. The corrected offset is 1024−2000=−9761024 - 2000 = -976. Check: 2000+(−976)=10242000 + (-976) = 1024.

A real case: a Thumb constant load. On an Armv7-M processor, the Arm architecture of microcontroller cores such as the Cortex-M3 and Cortex-M4 §6.3, the 2-byte instruction LDR r0, [PC, #8] at address 0x1002 loads register r0 from memory near the code. It reads the PC as 𝟶𝚡𝟷𝟶𝟶𝟸+4=𝟶𝚡𝟷𝟶𝟶𝟼\texttt{0x1002} + 4 = \texttt{0x1006}, rounds that down to a multiple of 4, giving 0x1004, and loads from 𝟶𝚡𝟷𝟶𝟶𝟺+8=𝟶𝚡𝟷𝟶𝟶𝙲\texttt{0x1004} + 8 = \texttt{0x100C}. A copy at 0x2000 reads the PC as 𝟶𝚡𝟸𝟶𝟶𝟶+4=𝟶𝚡𝟸𝟶𝟶𝟺\texttt{0x2000} + 4 = \texttt{0x2004}, already a multiple of 4, and needs the offset 𝟶𝚡𝟷𝟶𝟶𝙲−𝟶𝚡𝟸𝟶𝟶𝟺\texttt{0x100C} - \texttt{0x2004}, in decimal 4108−8196=−40884108 - 8196 = -4088. The 2-byte form allows only offsets from 0 to 1020. The 4-byte form LDR.W allows offsets from −4095-4095 to +4095+4095, so −4088-4088 fits, and the copy is 2 bytes longer than the original.

Had the trampoline been 64 KiB away, no constant load could reach 0x100C. The copy then becomes MOVW r0, #0x100C and MOVT r0, #0, which put the address 0x100C into r0, followed by LDR r0, [r0], which loads through it. The sequence uses only r0, which the original instruction overwrote anyway, so no other register is disturbed.

Stated precisely

Let pp be the address of a displaced instruction, p′p' the address of its copy in the trampoline, and aa the absolute address that the instruction computes: its branch target or the address it loads from. Let base(x)\mathrm{base}(x) be the PC value that the ISA defines for an instruction at address xx; for example, base(x)=x+4\mathrm{base}(x) = x + 4 for a Thumb branch and base(x)=x\mathrm{base}(x) = x for a 64-bit Arm branch. If the instruction computes a=base(p)+da = \mathrm{base}(p) + d, with dd the offset stored in it, the copy needs the offset d′=a−base(p′).d' = a - \mathrm{base}(p'). The copy is correct only if d′d' fits the copy’s format: it lies within the format’s range and is a multiple of its required step, which is 2 for Thumb branches and 4 for 64-bit Arm branches. Otherwise an equivalent sequence that uses the absolute address aa is needed.

A value is live at an address if some instruction executed from there on reads it before anything overwrites it. Let the displaced range run from address ss up to, but not including, address ee. A trampoline for that range is correct when all of the following hold.

  1. The range holds only whole instructions, and the jump fits inside it.

  2. No transfer of control from outside the range targets an address inside it other than ss, and when the jump is written, no stopped thread, saved return address or saved interrupt §6.11 state points inside it.

  3. The trampoline preserves every register, condition flag and memory location that is live at ss, apart from the added code’s own records.

  4. After the displaced copies run, registers, flags and memory hold what the original instructions would have produced, and control reaches ee, or the target of a displaced branch that is taken.

  5. The trampoline is executable and visible to instruction fetch before the jump at ss becomes visible, and it stays in place while any thread can still execute in it or return into it.

Limits and common mistakes

Correct relocation of the displaced instructions does not make the patch safe to apply. Writing the jump while another processor core executes the old bytes, a stale copy of the old bytes in the instruction cache, the processor’s copy of recently fetched code, and threads stopped inside the displaced range are publication problems, treated in the Dynamic patching and hot update entry and the Quiescent point §14.34 entry. The correction itself, a target minus the address the instruction sits at, is the same arithmetic a linker uses to resolve a relocation §12.50.

A displaced call saves a return address inside the trampoline, so the called function returns into the trampoline. The trampoline must stay in memory until no call that started in it is still running, and debuggers and stack traces, which map return addresses to functions, may not recognize that address.

Some instructions need special handling, such as one that copies the PC into a general register or one inside an IT block. A tool must refuse a patch site that holds an instruction it cannot reproduce exactly. Copying the bytes and assuming they mean the same thing elsewhere is the classic mistake.

A trampoline instruments only the entry it patches. A jump past the patched instructions, another entry point of the same function, and a copy of the function that the compiler inlined elsewhere do not pass through it.

Code that runs directly from flash memory §6.6 cannot be changed by an ordinary store instruction; changing arbitrary bytes there means erasing and rewriting a whole flash page. A system that forbids memory to be writable and executable at once needs the trampoline written first and then made executable. Such systems often rely on hook points §14.26 prepared when the program is built instead.

Questions answered

Which displaced instructions and PC-relative operands must be relocated correctly?

Every displaced instruction must be reproduced correctly in the trampoline, and every operand whose meaning depends on the instruction’s own address must be recomputed for its new address. The displaced instructions are all the whole instructions that the patch jump overwrites, including any that the jump covers only in part. In the trampoline they must have the same effects on registers, condition flags, memory and control flow as at their original address, and control must then continue at the first instruction after them.

The PC-relative operands are the offsets that the processor adds to the program counter (PC), the register that holds the address of the current instruction: offsets of relative branches and calls, of loads of constants stored near the code, and of instructions that form an address from the PC, such as Arm’s ADR. Each new offset is the original target minus the PC value at the new address, computed with the instruction set’s own rule, such as the address plus 4 in Thumb code. If the new offset does not fit the instruction’s format, the instruction is replaced by an equivalent sequence that uses the absolute address. An instruction the tool cannot reproduce exactly, such as one inside an If-Then (IT) block that the tool does not handle, must make the tool reject the patch site.

Related entries: Instruction set architecture (ISA) (§4.38), Conditional execution and IT block (§4.41), Dynamic patching and hot update (§14.33), Quiescent point (§14.34), Program counter (PC) (§2.9), Arm Thumb and Thumb-2 (§4.40), Relocation: turning a symbolic reference into exact encoded bits (§12.50), Runtime instrumentation (§14.24). Further reading: [81, 82].