Commit 3f28e7bc authored by Brenden Blanco's avatar Brenden Blanco

Disable non-static function calls

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: #653
Signed-off-by: default avatarBrenden Blanco <bblanco@gmail.com>
parent cee60569
...@@ -462,6 +462,15 @@ bool BTypeVisitor::VisitCallExpr(CallExpr *Call) { ...@@ -462,6 +462,15 @@ bool BTypeVisitor::VisitCallExpr(CallExpr *Call) {
rewriter_.ReplaceText(expansionRange(Call->getSourceRange()), text); 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; return true;
......
...@@ -522,7 +522,17 @@ int do_next(struct pt_regs *ctx) { ...@@ -522,7 +522,17 @@ int do_next(struct pt_regs *ctx) {
with self.assertRaises(KeyError): with self.assertRaises(KeyError):
b1["dummy"][c_key] 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__": if __name__ == "__main__":
......
...@@ -28,7 +28,7 @@ enum stat_types { ...@@ -28,7 +28,7 @@ enum stat_types {
BPF_ARRAY(stats, u64, S_MAXSTAT + 1); BPF_ARRAY(stats, u64, S_MAXSTAT + 1);
void stats_increment(int key) { static void stats_increment(int key) {
u64 *leaf = stats.lookup(&key); u64 *leaf = stats.lookup(&key);
if (leaf) (*leaf)++; if (leaf) (*leaf)++;
} }
......
...@@ -51,7 +51,7 @@ enum stat_types { ...@@ -51,7 +51,7 @@ enum stat_types {
BPF_ARRAY(stats, u64, S_MAXSTAT + 1); BPF_ARRAY(stats, u64, S_MAXSTAT + 1);
void stats_increment(int key) { static void stats_increment(int key) {
u64 *leaf = stats.lookup(&key); u64 *leaf = stats.lookup(&key);
if (leaf) (*leaf)++; if (leaf) (*leaf)++;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment