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

Add user function call support #3068

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

lenticularis39
Copy link
Contributor

Function defined by the user can now be called in the same ways as built-in functions, for example:

fn add($a: int64, $b: int64): int64 {
return $a + $b;
}

BEGIN {
print(add(5, 9));
}

If a function of the same name as an existing built-in is defined, it overrides the built-in and a warning is printed.

Implementation-wise, the functionality depends on existing relocations against subprogs implemented in commit d345d06 ("Implement relocations against .text for subprogs").

Functions are generated as separate LLVM IR functions and translated into BPF subprogs via the LLVM bpf backend. Relocations make sure the resulting program can be loaded.

Checklist
  • Language changes are updated in man/adoc/bpftrace.adoc
  • User-visible and non-trivial changes updated in CHANGELOG.md
  • The new behaviour is covered by tests

@lenticularis39
Copy link
Contributor Author

Second PR following recently merged #2624 to implement user-defined functions (#2516).

Function defined by the user can now be called in the same ways as built-in
functions, for example:

fn add($a: int64, $b: int64): int64 {
  return $a + $b;
}

BEGIN {
  print(add(5, 9));
}

If a function of the same name as an existing built-in is defined, it
overrides the built-in and a warning is printed.

Implementation-wise, the functionality depends on existing relocations
against subprogs implemented in commit d345d06 ("Implement relocations
against .text for subprogs"). Those are extended to also support R_BPF_64_32
relocations used for direct calls in addition to R_BPF_64_64 used for
indirect calls.

Functions are generated as separate LLVM IR functions and translated into
BPF subprogs via the LLVM bpf backend. Inlining of those function is
disabled to prevent aggressive LLVM optimizations turning recursion into
iteration, which could be rejected by the verifier.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant