1

I have a bound check statement that is optimized by clang and as a result verifier doesn't accept the program. I would like to write it using embedded assembly. I am not sure how to do it. I know that the __asm__ block given bellow will compile to if r3 > r1 goto pc+5 . What I don't know is how to reference labels in the embedded assembly.

Writing eBPF assembly is very different than x86 (actually feels more intuitive). I wonder if there is a manual or a document or source file in Clang that can help me understand the syntax.

__asm__("if %0 > %1 goto 5  \n\t"
:
: "r"(tmp_ptr), "r"(data_end)
:
);

1 Answer 1

2

You can find an example of how to do that in the kernel BPF selftests:

$ git grep -hC5 '\bif\b.*goto %' tools/testing/selftests/bpf/

#define __bpf_cmp(LHS, OP, PRED, RHS, DEFAULT)                              \
    ({                                                                      \
        __label__ l_true;                                                   \
        bool ret = DEFAULT;                                                 \
        asm volatile goto("if %[lhs] " OP " %[rhs] goto %l[l_true]"         \
                  :: [lhs] "r"((short)LHS), [rhs] PRED (RHS) :: l_true);    \
        ret = !DEFAULT;                                                     \
l_true:                                                                     \
        ret;                                                                \
    })

So you can just refer to the C label from the inline asm, with %l[label].

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.