LSFMM BPF Proposal: BPF Support for Bounded Loops Revisited
Hi, below is a proposal for the LSFMM BPF Track. Thanks.
---
BPF Support for Bounded Loops Revisited:
BPFs lack of support for bounded loops has proven to make it difficult to implement logic that would be common in other environments. Macros have been invented to unroll complex loops, creative uses of maps and tail calls have appeared, but these solutions albeit "interesting" fail to solve the core problem.
An initial prototype to handle bounded loops proceeded in the normal compiler textbook method. This means building a CFG, constructing the DOM tree, checking (ir)reducibility, finding invariants, and walking all the paths through the loop (Floyd-Hoare) to verify termination. This had some draw backs. First its a fair amount of complex code that impacts all users of the verifier regardless if loops are being used or not. But worse its almost unusable because LLVM generates complex loop structures that are difficult to pattern match (find invariants for) without even more code.
I have decided there is no clean/easy way to do this without bringing the compiler onboard. We are building a prototype v2 that solves this roughly as follows,
1. Provide interface between compiler and verifier to "hint" at loop structures. We propose these can be pushed through BTF.
2. Compiler "hint" gives loop start and end to verifier. Verifier can ensure that these "loops" are contiguous blocks of code that does not have JMPs into it from other blocks.
3. Compiler has to build compliant loops. We plan to start simple only allowing the compiler to build increasing/decreasing loops that fit some pre-agreed template. For example, we "know" where the invariants will be and how they will be compared at the end (and only at the end) of the loop block. If this is violated we will simply abort and complain to the compiler. As part of the hints we can tell the verifier the type of loop to expect.
4. Add as many loop types as needed teaching the verifier the loop types as we go.
The advantages are (a) if we never see a loop hint we never have to run the loop verification code, small/negligible perf hit for non-loop users, (b) easy to find loops no need for DOMs or reducibility checks, (c) always know where to find invariants and (d) hints can be added incrementally allowing compiler and verifier to create more complex structures in the future. The downside is we will be restricting the compilers ability to optimize loops. I anticipate this will be a small price to pay and worth it but lets run some tests on prototypes.
We would like to discuss the above design, compare it with our initial design and share the latest prototype and measurements at the LSFMM BPF track.
Thanks,
John