llsoftsec / llsoftsecbook

Low-Level Software Security for Compiler Developers
https://llsoftsec.github.io/llsoftsecbook/
Other
515 stars 50 forks source link

Section 3.5.1 "Transient Execution": show an example of cpu speculation that is not based on branch prediction #177

Open kbeyls opened 1 year ago

kbeyls commented 1 year ago

See TODO in text.

koutheir commented 1 month ago

Microarchitectural branches can also participate in speculation. The following function, for example, divides a floating-point constant by its input, and that input could be zero:

double divide_a_value(double a) {
    return 3.0 / a;
}
; AArch64 disassembly
divide_a_value(double):
        fmov    d1, #3.00000000
        fdiv    d0, d1, d0
        ret
koutheir commented 1 month ago

Computing addresses may take a few instructions to perform (so, multiple cycles). When loading a value from a computed address for the second time, the CPU may speculate that a computed address used to load a value remains the same, allowing it to start loading the value, this second time, from the previous address, before the actual address, that should be used, is computed. Once the address computation is finished, it is compared to the speculated address, and if it is the same, then the speculatively-loaded value can be used directly. Otherwise, a new load is started.