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

Fix calling ntop on tracepoint args #1365

Merged
merged 2 commits into from
Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ and this project adheres to
- [#1341](https://github.com/iovisor/bpftrace/pull/1341)
- Fix `KBUILD_MODNAME`
- [#1352](https://github.com/iovisor/bpftrace/pull/1352)
- Fix `ntop()` not accepting tracepoint arguments
- [#1365](https://github.com/iovisor/bpftrace/pull/1365)

#### Tools

Expand Down
2 changes: 1 addition & 1 deletion src/ast/codegen_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ void CodegenLLVM::visit(Call &call)
b_.CREATE_MEMSET(inet_offset, b_.getInt8(0), 16, 1);

inet->accept(*this);
if (inet->type.IsArrayTy())
if (inet->type.IsArray())
{
b_.CreateProbeRead(ctx_,
static_cast<AllocaInst *>(inet_offset),
Expand Down
15 changes: 4 additions & 11 deletions src/ast/semantic_analyser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ void SemanticAnalyser::visit(Call &call)
check_arg(call, Type::integer, 0);
}

if (!arg->type.IsIntTy() && !arg->type.IsArrayTy())
if (!arg->type.IsIntTy() && !arg->type.IsArray())
ERR(call.func << "() expects an integer or array argument, got "
<< arg->type.type,
call.loc);
Expand All @@ -641,16 +641,9 @@ void SemanticAnalyser::visit(Call &call)
int buffer_size = 24;
auto type = arg->type;

// Ensure u8 array of either 4 or 16 elements
if (type.IsArrayTy())
{
if (!(type.GetElementTy()->IsIntTy() &&
type.GetElementTy()->GetIntBitWidth() == 8 &&
(type.GetNumElements() == 4 || type.GetNumElements() == 16)))
{
error(call.func + "() invalid array", call.loc);
}
}
if (arg->type.IsArray() && type.size != 4 && type.size != 16)
error(call.func + "() argument must be 4 or 16 bytes in size", call.loc);

call.type = CreateInet(buffer_size);
}
else if (call.func == "join") {
Expand Down
10 changes: 10 additions & 0 deletions tests/mocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ void setup_mock_bpftrace(MockBPFtrace &bpftrace)
.bitfield = {},
} } },
};
bpftrace.structs_["struct _tracepoint_tcp_some_tcp_tp"] = Struct{
.size = 16,
.fields = { { "saddr_v6",
Field{
.type = CreateArray(16, CreateUInt(8)),
.offset = 0,
.is_bitfield = false,
.bitfield = {},
} } },
};

auto ptr_type = CreateUInt64();
ptr_type.is_pointer = true;
Expand Down
6 changes: 6 additions & 0 deletions tests/runtime/regression
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,9 @@ NAME modulo_operator
RUN bpftrace -v -e 'BEGIN { @x = 4; printf("%d\n", @x % 2) }'
EXPECT 0
TIMEOUT 5

NAME ntop_tracepoint_args
RUN bpftrace -v -e 'tracepoint:tcp:tcp_destroy_sock { printf("%s\n", ntop(args->daddr)); exit(); }'
EXPECT [0-9]+.[0-9]+.[0-9]+.[0-9]+
AFTER curl www.google.com &> /dev/null
TIMEOUT 5
3 changes: 3 additions & 0 deletions tests/semantic_analyser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,9 @@ TEST(semantic_analyser, call_ntop)
test(structs + "kprobe:f { @x = ntop(((struct inet*)0)->ipv4); }", 0);
test(structs + "kprobe:f { @x = ntop(((struct inet*)0)->ipv6); }", 0);

// Regression test that ntop can use arguments from the prog context
test("tracepoint:tcp:some_tcp_tp { ntop(args->saddr_v6); }", 0);

test("kprobe:f { ntop(); }", 1);
test("kprobe:f { ntop(2, \"hello\"); }", 1);
test("kprobe:f { ntop(\"hello\"); }", 1);
Expand Down