Skip to content

Commit

Permalink
Disable non-static function calls
Browse files Browse the repository at this point in the history
Compiled BPF programs must consist of a single contiguous code block,
meaning trying to call other function entry points (besides the
kernel-defined helpers) is not possible. The bcc frontend didn't
explicitly prohibit this, even though the program would fail to
compile/load. Add an explicit check and error message.

Fixes: iovisor#653
Signed-off-by: Brenden Blanco <[email protected]>
  • Loading branch information
drzaeus77 committed Apr 20, 2017
1 parent cee6056 commit 3f28e7b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/cc/frontends/clang/b_frontend_action.cc
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,15 @@ bool BTypeVisitor::VisitCallExpr(CallExpr *Call) {
rewriter_.ReplaceText(expansionRange(Call->getSourceRange()), text);
}
}
} else if (FunctionDecl *F = dyn_cast<FunctionDecl>(Decl)) {
if (F->isExternallyVisible() && !F->getBuiltinID()) {
auto start_loc = rewriter_.getSourceMgr().getFileLoc(Decl->getLocStart());
if (rewriter_.getSourceMgr().getFileID(start_loc)
== rewriter_.getSourceMgr().getMainFileID()) {
error(Call->getLocStart(), "cannot call non-static helper function");
return false;
}
}
}
}
return true;
Expand Down
12 changes: 11 additions & 1 deletion tests/python/test_clang.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,17 @@ def test_prog_array_delete(self):
with self.assertRaises(KeyError):
b1["dummy"][c_key]


def test_invalid_noninline_call(self):
text = """
int bar(void) {
return 0;
}
int foo(struct pt_regs *ctx) {
return bar();
}
"""
with self.assertRaises(Exception):
b = BPF(text=text)


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion tools/pidpersec.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
BPF_ARRAY(stats, u64, S_MAXSTAT + 1);
void stats_increment(int key) {
static void stats_increment(int key) {
u64 *leaf = stats.lookup(&key);
if (leaf) (*leaf)++;
}
Expand Down
2 changes: 1 addition & 1 deletion tools/vfsstat.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def usage():
BPF_ARRAY(stats, u64, S_MAXSTAT + 1);
void stats_increment(int key) {
static void stats_increment(int key) {
u64 *leaf = stats.lookup(&key);
if (leaf) (*leaf)++;
}
Expand Down

0 comments on commit 3f28e7b

Please sign in to comment.