3 ms·
A place that volatile shows up in C today that is stunningly handy is when writing eBPF programs. In eBPF you have to appease a verifier which is trying to pro
by vvern 3y ago
A place that volatile shows up in C today that is stunningly handy is when writing eBPF programs.
In eBPF you have to appease a verifier which is trying to prove safety and liveness properties of the compiled program. To prove safety properties you often need to ensure that some offset into a buffer (bpf map) will be in bounds. Even if you judiciously sprinkle such bound checks into your code, the compiler may eliminate them entirely or perform them on some different register or stack value it knows to be semantically sufficient. Unfortunately the verifier is not as smart as the compiler.
Using volatile to reload some offset just before bounds checking it and using it to index the map is a very reliable approach to getting code to verify.