Section 17.6

The cost of interpreter versus JIT execution

Question

Why not replace an interpreter §2.13 with a JIT §2.14 on every constrained device?

Short answer

An interpreter should not be replaced everywhere, because a JIT (just-in-time) compiler buys faster runs with costs that many constrained devices cannot pay. A JIT compiler translates each new program into the processor’s own instructions before running it. Translation needs time for every new program, memory that is written and then executed, a translator built for that particular processor, and trust that the translator is correct. The JIT compiler wins only when each translated program runs often enough to repay its translation and the device can meet the other needs.

Terms in the question

Full answer

Step 1: compare the total cost, not the speed of one run. Let TT be the time to translate one program, cic_i the time of one run under the interpreter, cjc_j the time of one run of the translated code, and NN the number of runs of one program between its arrival and its replacement. The interpreter spends NciN c_i on that program. The JIT compiler spends T+NcjT + N c_j. The JIT compiler is cheaper only when T+Ncj<NciT + N c_j < N c_i. If cj<cic_j < c_i, this inequality rearranges to N>Tci−cj.N > \frac{T}{c_i - c_j}. NN counts runs per translation, not runs over the life of the device. A program that is replaced often, or that runs rarely, may never repay its translation. Translation also delays the first run: if the device translates when the first event arrives, that event waits T+cjT + c_j instead of cic_i. Translating when the program is installed, outside the code that must answer within a deadline, moves that cost but does not remove it.

Step 2: the saving per run may be small. Translation removes the work that the interpreter does around each instruction: fetching the instruction, decoding it and jumping to the code that carries it out. Translation does not speed up the time a run spends inside helper functions §2.19, the functions of the host that the program calls, because helper functions are native code already. When most of a run is spent in helper functions, ci−cjc_i - c_j is small, and the number of runs needed to repay TT is large.

Step 3: the native code needs memory. Firmware on a microcontroller normally runs from flash memory. Flash memory is slow to write, can be erased only in large blocks and wears out after a limited number of erase cycles, so generated code usually goes into RAM, which is the scarcest memory on the device. The translator itself occupies flash memory as well. The bytecode is often kept too, so that the program can be checked or translated again. One instruction of the virtual machine can become several native instructions: a 32-bit processor needs at least two instructions to add two 64-bit numbers.

Step 4: the memory must be writable and then executable. Hardened firmware lets no memory region be writable and executable at the same time, a rule often called write xor execute. On a microcontroller, the firmware enforces that rule with the memory protection unit §6.8 (MPU), hardware that enforces access rights on a small number of memory regions. A JIT compiler writes code and then runs it. The firmware must either allow a region that is writable and executable at once, which lets an attacker who can write to that region run code there, or change the region’s rights between writing and running, which adds more code that must be trusted.

Step 5: the processor must be made to see the new code. A processor may already have fetched the instructions that were at those addresses before the write, or may hold old instructions in an instruction cache, a small fast copy of recently fetched instructions. After writing the code, the JIT compiler must therefore make it visible to instruction fetch. On Arm Cortex-M processors, a data synchronization barrier (DSB) instruction waits until the writes have completed, and an instruction synchronization barrier (ISB) instruction makes the processor fetch every later instruction again. A processor with caches must first copy the new code out of its data cache into memory and discard the stale copies in its instruction cache. If a step is missing, the processor can run the old bytes, and the resulting fault appears only sometimes. The entry on instruction-cache coherence §19.5.3 treats these steps in full.

Step 6: one translator per processor family, and every rule kept. An interpreter written in portable C runs on any processor that has a C compiler. A JIT compiler must be written, tested and maintained separately for each instruction set architecture §4.38, the set of instructions that a family of processors understands, such as the Thumb instruction set §4.40 of Arm Cortex-M processors or RISC-V (RISC stands for reduced instruction set computer). The translated code must also follow the guest-host ABI §2.37 (application binary interface), the agreement at the level of bits and bytes about how the program and the host pass values and call each other. And the translated code must reproduce every rule of the virtual machine exactly. For example, the eBPF §2.1 (extended Berkeley Packet Filter) instruction set defines the result of a division by zero as 0, while the RISC-V divide instructions return a value with all bits set, so a translator for RISC-V must add a test before each division whose divisor can be zero.

Step 7: the translator joins the trusted computing base §1.4. The trusted computing base is all the code whose correctness the safety of the device depends on. An interpreter that checks every memory access and counts the instructions it runs protects the device only on the interpreter’s own path. The translated code must contain the same checks, or a verifier must have shown, before translation, that the checks are unnecessary. A bug in the translator is severe, because wrong native code runs with nothing checking it. An attacker can also feed a JIT compiler programs whose constants, once placed in executable memory, decode as useful machine instructions when execution starts in the middle of them. Combined with a separate bug that redirects execution there, this attack, called JIT spraying, runs the attacker’s instructions.

Conclusion. A JIT compiler is the better choice when the same program runs very often, as when a server processes every network packet with it, when RAM can be spared, when the firmware can switch a region from writable to executable safely, and when a well-tested translator exists for the processor. Where one of these conditions fails, an interpreter costs less in total and keeps a translator out of the trusted computing base.

Worked example

The costs below are invented to show the arithmetic; real costs must be measured on the device. Let translation take T=100T = 100 microseconds, one interpreted run ci=10c_i = 10 microseconds and one run of translated code cj=2c_j = 2 microseconds.

  1. 10 runs. The interpreter spends 10×10=10010 \times 10 = 100 microseconds. The JIT compiler spends 100+10×2=120100 + 10 \times 2 = 120 microseconds. The interpreter wins by 20 microseconds.

  2. 20 runs. The interpreter spends 20×10=20020 \times 10 = 200 microseconds. The JIT compiler spends 100+20×2=140100 + 20 \times 2 = 140 microseconds. The JIT compiler wins by 60 microseconds.

  3. Break-even. The JIT compiler wins when 100+2N<10N100 + 2N < 10N, that is when 100<8N100 < 8N, that is when N>12.5N > 12.5. At N=12N = 12 the costs are 12×10=12012 \times 10 = 120 against 100+12×2=124100 + 12 \times 2 = 124 microseconds, so the interpreter still wins. At N=13N = 13 they are 13×10=13013 \times 10 = 130 against 100+13×2=126100 + 13 \times 2 = 126 microseconds, so the JIT compiler wins.

Frequent replacement. Suppose a new program arrives after every 5 runs. Each program costs the interpreter 5×10=505 \times 10 = 50 microseconds and the JIT compiler 100+5×2=110100 + 5 \times 2 = 110 microseconds. Over 1000 runs, which means 1000/5=2001000 / 5 = 200 programs, the interpreter spends 200×50=10,000200 \times 50 = 10{,}000 microseconds and the JIT compiler spends 200×110=22,000200 \times 110 = 22{,}000 microseconds, more than twice as much.

Time spent in helper functions. Suppose 8 of the 10 microseconds of an interpreted run are spent inside helper functions, and translation makes only the remaining 2 microseconds 4 times faster. Then cj=8+2/4=8.5c_j = 8 + 2 / 4 = 8.5 microseconds, and the JIT compiler wins only when N>100/(10−8.5)=100/1.5≈66.7N > 100 / (10 - 8.5) = 100 / 1.5 \approx 66.7, that is from 67 runs on. Check at N=67N = 67: the interpreter spends 67×10=67067 \times 10 = 670 microseconds and the JIT compiler 100+67×8.5=669.5100 + 67 \times 8.5 = 669.5 microseconds.

First event. If translation happens when the first event arrives, that event waits 100+2=102100 + 2 = 102 microseconds instead of 10 microseconds. A device can miss a deadline this way even when the JIT compiler wins on total time.

Follow-up questions and limits

Why not translate before delivery instead? That is ahead-of-time compilation §2.15 (AOT): a powerful machine translates the program, and the device receives native code. It removes the translator and the translation time from the device. In exchange, the device receives native code for one processor family, which is much harder to check than bytecode, so the device must trust the machine that translated it and must verify where the code came from. The native code also still needs memory from which the processor may execute it.

Could a device keep both? Yes. Some systems interpret a program first and translate it only after it has run often, so that a program that runs rarely never pays TT. Both paths then stay in the trusted computing base, and both must follow the rules of the virtual machine identically.

Is an interpreter automatically safe? No. The interpreter is trusted code too, and it keeps the device safe only if its own checks are correct. Neither path is safe because of its name; each must be shown to enforce the same rules.

This answer compares costs and risks in general. It gives no measured figures for any particular device; those come only from measurement on the device itself.

Fullest treatment: Just-in-time compilation (JIT) (§2.14).

Related entries: Interpreter (§2.13), Interpreter semantic fidelity (§2.38), Guest-host ABI (§2.37), Static versus dynamic footprint (§11.38), Ahead-of-time compilation (AOT) (§2.15), Instruction-cache coherence and executable-code publication (§19.5.3), Trusted computing base (TCB) (§1.4). Further reading: [160].