Commit 7575c64d authored by 4ast's avatar 4ast

Merge pull request #530 from iovisor/bblanco_dev

Add check for number of arguments
parents 86b819a3 bc94d4c1
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
namespace ebpf { namespace ebpf {
constexpr int MAX_CALLING_CONV_REGS = 6;
const char *calling_conv_regs_x86[] = { const char *calling_conv_regs_x86[] = {
"di", "si", "dx", "cx", "r8", "r9" "di", "si", "dx", "cx", "r8", "r9"
}; };
...@@ -255,6 +256,11 @@ bool BTypeVisitor::VisitFunctionDecl(FunctionDecl *D) { ...@@ -255,6 +256,11 @@ bool BTypeVisitor::VisitFunctionDecl(FunctionDecl *D) {
if (D->isExternallyVisible() && D->hasBody()) { if (D->isExternallyVisible() && D->hasBody()) {
string attr = string("__attribute__((section(\"") + BPF_FN_PREFIX + D->getName().str() + "\")))\n"; string attr = string("__attribute__((section(\"") + BPF_FN_PREFIX + D->getName().str() + "\")))\n";
rewriter_.InsertText(D->getLocStart(), attr); rewriter_.InsertText(D->getLocStart(), attr);
if (D->param_size() > MAX_CALLING_CONV_REGS + 1) {
error(D->getParamDecl(MAX_CALLING_CONV_REGS + 1)->getLocStart(),
"too many arguments, bcc only supports in-register parameters");
return false;
}
// remember the arg names of the current function...first one is the ctx // remember the arg names of the current function...first one is the ctx
fn_args_.clear(); fn_args_.clear();
string preamble = "{"; string preamble = "{";
...@@ -265,12 +271,24 @@ bool BTypeVisitor::VisitFunctionDecl(FunctionDecl *D) { ...@@ -265,12 +271,24 @@ bool BTypeVisitor::VisitFunctionDecl(FunctionDecl *D) {
} }
fn_args_.push_back(arg); fn_args_.push_back(arg);
if (fn_args_.size() > 1) { if (fn_args_.size() > 1) {
// Move the args into a preamble section where the same params are
// declared and initialized from pt_regs.
// Todo: this init should be done only when the program requests it.
string text = rewriter_.getRewrittenText(
SourceRange(arg->getLocStart(), arg->getLocEnd()));
arg->addAttr(UnavailableAttr::CreateImplicit(C, "ptregs")); arg->addAttr(UnavailableAttr::CreateImplicit(C, "ptregs"));
size_t d = fn_args_.size() - 2; size_t d = fn_args_.size() - 2;
const char *reg = calling_conv_regs[d]; const char *reg = calling_conv_regs[d];
preamble += arg->getName().str() + " = " + fn_args_[0]->getName().str() + "->" + string(reg) + ";"; preamble += " " + text + " = " + fn_args_[0]->getName().str() + "->" +
string(reg) + ";";
} }
} }
if (D->param_size() > 1) {
rewriter_.ReplaceText(
SourceRange(D->getParamDecl(0)->getLocEnd(),
D->getParamDecl(D->getNumParams() - 1)->getLocEnd()),
fn_args_[0]->getName());
}
// for each trace argument, convert the variable from ptregs to something on stack // for each trace argument, convert the variable from ptregs to something on stack
if (CompoundStmt *S = dyn_cast<CompoundStmt>(D->getBody())) if (CompoundStmt *S = dyn_cast<CompoundStmt>(D->getBody()))
rewriter_.ReplaceText(S->getLBracLoc(), 1, preamble); rewriter_.ReplaceText(S->getLBracLoc(), 1, preamble);
......
...@@ -446,7 +446,7 @@ class BPF(object): ...@@ -446,7 +446,7 @@ class BPF(object):
@staticmethod @staticmethod
def find_library(libname): def find_library(libname):
return lib.bcc_procutils_which_so(libname) return lib.bcc_procutils_which_so(libname.encode("ascii")).decode()
def attach_uprobe(self, name="", sym="", addr=None, def attach_uprobe(self, name="", sym="", addr=None,
fn_name="", pid=-1, cpu=0, group_fd=-1): fn_name="", pid=-1, cpu=0, group_fd=-1):
......
...@@ -328,5 +328,15 @@ BPF_TABLE("hash", struct bpf_tunnel_key, int, t1, 1); ...@@ -328,5 +328,15 @@ BPF_TABLE("hash", struct bpf_tunnel_key, int, t1, 1);
t1 = b["t1"] t1 = b["t1"]
print(t1.Key().remote_ipv4) print(t1.Key().remote_ipv4)
def test_too_many_args(self):
text = """
#include <uapi/linux/ptrace.h>
int many(struct pt_regs *ctx, int a, int b, int c, int d, int e, int f, int g) {
return 0;
}
"""
with self.assertRaises(Exception):
b = BPF(text=text)
if __name__ == "__main__": if __name__ == "__main__":
main() main()
...@@ -47,7 +47,7 @@ int kprobe__htab_map_lookup_elem(struct pt_regs *ctx, struct bpf_map *map, u64 * ...@@ -47,7 +47,7 @@ int kprobe__htab_map_lookup_elem(struct pt_regs *ctx, struct bpf_map *map, u64 *
stackid = stack_entries[k] stackid = stack_entries[k]
self.assertIsNotNone(stackid) self.assertIsNotNone(stackid)
stack = stack_traces[stackid].ip stack = stack_traces[stackid].ip
self.assertEqual(b.ksym(stack[0]), b"htab_map_lookup_elem") self.assertEqual(b.ksym(stack[0]), "htab_map_lookup_elem")
if __name__ == "__main__": if __name__ == "__main__":
......
...@@ -673,8 +673,8 @@ struct __string_t { char s[%d]; }; ...@@ -673,8 +673,8 @@ struct __string_t { char s[%d]; };
except: except:
if self.args.verbose: if self.args.verbose:
traceback.print_exc() traceback.print_exc()
elif sys.exc_type is not SystemExit: elif sys.exc_info()[0] is not SystemExit:
print(sys.exc_value) print(sys.exc_info()[1])
self._close_probes() self._close_probes()
if __name__ == "__main__": if __name__ == "__main__":
......
...@@ -86,6 +86,6 @@ if __name__ == "__main__": ...@@ -86,6 +86,6 @@ if __name__ == "__main__":
else: else:
print_tracepoints() print_tracepoints()
except: except:
if sys.exc_type is not SystemExit: if sys.exc_info()[0] is not SystemExit:
print(sys.exc_value) print(sys.exc_info()[1])
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