Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom labels 2 #4

Merged
merged 8 commits into from
Aug 7, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions support/ebpf/interpreter_dispatcher.ebpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ void *get_m_ptr(struct GoCustomLabelsOffsets *offs, UnwindState *state) {

#define MAX_BUCKETS 8

// see https://gcc.gnu.org/onlinedocs/cpp/Stringizing.html#Stringizing
#define STR_HELPER(x) #x
#define STR(x) STR_HELPER(x)

struct GoString {
char *str;
s64 len;
Expand Down Expand Up @@ -311,11 +315,36 @@ bool get_custom_labels(struct pt_regs *ctx, UnwindState *state, GoCustomLabelsOf
DEBUG_PRINT("failed to read custom label: value too long");
continue;
}
if (val_len <= CUSTOM_LABEL_MAX_VAL_LEN)
res = bpf_probe_read(lbl->val.val_bytes, val_len, map_value->values[i].str);
else
res = -1;

// The following assembly statement is equivalent to:
// if (val_len > CUSTOM_LABEL_MAX_VAL_LEN)
// res = bpf_probe_read(lbl->val, val_len, map_value->values[i].str);
// else
// res = -1;
//
// We need to write this in assembly because the verifier doesn't understand
// that val_len has already been bounds-checked above, apparently
// because clang has spilled it to the stack rather than
// keeping it in a register.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious if you tried putting the "register" keyword on val_len?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't try, but I looked into it and apparently modern compilers simply ignore the register keyword: https://stackoverflow.com/questions/10675072/is-the-register-keyword-still-used


// clang-format off
asm volatile(
// Note: this branch is never taken, but we
// need it to appease the verifier.
"if %2 > " STR(CUSTOM_LABEL_MAX_VAL_LEN) " goto bad%=\n"
"r1 = %1\n"
"r2 = %2\n"
"r3 = %3\n"
"call 4\n"
"%0 = r0\n"
"goto good%=\n"
"bad%=: %0 = -1\n"
"good%=:\n"
: "=r"(res)
: "r"(lbl->val), "r"(val_len), "r"(map_value->values[i].str)
// all r0-r5 are clobbered since we make a function call.
: "r0", "r1", "r2", "r3", "r4", "r5", "memory"
);
// clang-format on
if (res) {
DEBUG_PRINT("failed to read value for custom label: %ld", res);
continue;
Expand Down