Commit 52c2999e authored by Brendan Gregg's avatar Brendan Gregg

fix unterminated map.ident strings for print, clear, and zero

parent 759c786f
......@@ -459,9 +459,10 @@ void CodegenLLVM::visit(Call &call)
auto &arg = *call.vargs->at(0);
auto &map = static_cast<Map&>(arg);
Constant *const_str = ConstantDataArray::getString(module_->getContext(), map.ident, true);
AllocaInst *str_buf = b_.CreateAllocaBPF(ArrayType::get(b_.getInt8Ty(), map.ident.length()), "str");
AllocaInst *str_buf = b_.CreateAllocaBPF(ArrayType::get(b_.getInt8Ty(), map.ident.length() + 1), "str");
b_.CreateMemSet(str_buf, b_.getInt8(0), map.ident.length() + 1, 1);
b_.CreateStore(b_.CreateGEP(const_str, b_.getInt64(0)), str_buf);
ArrayType *perfdata_type = ArrayType::get(b_.getInt8Ty(), sizeof(uint64_t) + 2 * sizeof(uint64_t) + map.ident.length());
ArrayType *perfdata_type = ArrayType::get(b_.getInt8Ty(), sizeof(uint64_t) + 2 * sizeof(uint64_t) + map.ident.length() + 1);
AllocaInst *perfdata = b_.CreateAllocaBPF(perfdata_type, "perfdata");
// store asyncactionid:
......@@ -492,8 +493,8 @@ void CodegenLLVM::visit(Call &call)
b_.CreateStore(b_.getInt64(0), b_.CreateGEP(perfdata, {b_.getInt64(0), b_.getInt64(sizeof(uint64_t) + sizeof(uint64_t))}));
// store map ident:
b_.CreateMemCpy(b_.CreateGEP(perfdata, {b_.getInt64(0), b_.getInt64(sizeof(uint64_t) + 2 * sizeof(uint64_t))}), str_buf, map.ident.length(), 1);
b_.CreatePerfEventOutput(ctx_, perfdata, sizeof(uint64_t) + 2 * sizeof(uint64_t) + map.ident.length());
b_.CreateMemCpy(b_.CreateGEP(perfdata, {b_.getInt64(0), b_.getInt64(sizeof(uint64_t) + 2 * sizeof(uint64_t))}), str_buf, map.ident.length() + 1, 1);
b_.CreatePerfEventOutput(ctx_, perfdata, sizeof(uint64_t) + 2 * sizeof(uint64_t) + map.ident.length() + 1);
b_.CreateLifetimeEnd(perfdata);
expr_ = nullptr;
}
......@@ -502,16 +503,17 @@ void CodegenLLVM::visit(Call &call)
auto &arg = *call.vargs->at(0);
auto &map = static_cast<Map&>(arg);
Constant *const_str = ConstantDataArray::getString(module_->getContext(), map.ident, true);
AllocaInst *str_buf = b_.CreateAllocaBPF(ArrayType::get(b_.getInt8Ty(), map.ident.length()), "str");
AllocaInst *str_buf = b_.CreateAllocaBPF(ArrayType::get(b_.getInt8Ty(), map.ident.length() + 1), "str");
b_.CreateMemSet(str_buf, b_.getInt8(0), map.ident.length() + 1, 1);
b_.CreateStore(b_.CreateGEP(const_str, b_.getInt64(0)), str_buf);
ArrayType *perfdata_type = ArrayType::get(b_.getInt8Ty(), sizeof(uint64_t) + map.ident.length());
ArrayType *perfdata_type = ArrayType::get(b_.getInt8Ty(), sizeof(uint64_t) + map.ident.length() + 1);
AllocaInst *perfdata = b_.CreateAllocaBPF(perfdata_type, "perfdata");
if (call.func == "clear")
b_.CreateStore(b_.getInt64(asyncactionint(AsyncAction::clear)), perfdata);
else
b_.CreateStore(b_.getInt64(asyncactionint(AsyncAction::zero)), perfdata);
b_.CreateMemCpy(b_.CreateGEP(perfdata, {b_.getInt64(0), b_.getInt64(sizeof(uint64_t))}), str_buf, map.ident.length(), 1);
b_.CreatePerfEventOutput(ctx_, perfdata, sizeof(uint64_t) + map.ident.length());
b_.CreateMemCpy(b_.CreateGEP(perfdata, {b_.getInt64(0), b_.getInt64(sizeof(uint64_t))}), str_buf, map.ident.length() + 1, 1);
b_.CreatePerfEventOutput(ctx_, perfdata, sizeof(uint64_t) + map.ident.length() + 1);
b_.CreateLifetimeEnd(perfdata);
expr_ = nullptr;
}
......
......@@ -151,19 +151,25 @@ void perf_event_printer(void *cb_cookie, void *data, int size)
std::string arg = (const char *)(static_cast<uint8_t*>(data) + sizeof(uint64_t) + 2 * sizeof(uint64_t));
uint64_t top = (uint64_t)*(static_cast<uint64_t*>(data) + sizeof(uint64_t) / sizeof(uint64_t));
uint64_t div = (uint64_t)*(static_cast<uint64_t*>(data) + (sizeof(uint64_t) + sizeof(uint64_t)) / sizeof(uint64_t));
bpftrace->print_map_ident(arg, top, div);
err = bpftrace->print_map_ident(arg, top, div);
if (err)
throw std::runtime_error("Could not print map with ident \"" + arg + "\", err=" + std::to_string(err));
return;
}
else if (printf_id == asyncactionint(AsyncAction::clear))
{
std::string arg = (const char *)(arg_data+sizeof(uint64_t));
bpftrace->clear_map_ident(arg);
err = bpftrace->clear_map_ident(arg);
if (err)
throw std::runtime_error("Could not clear map with ident \"" + arg + "\", err=" + std::to_string(err));
return;
}
else if (printf_id == asyncactionint(AsyncAction::zero))
{
std::string arg = (const char *)(arg_data+sizeof(uint64_t));
bpftrace->zero_map_ident(arg);
err = bpftrace->zero_map_ident(arg);
if (err)
throw std::runtime_error("Could not zero map with ident \"" + arg + "\", err=" + std::to_string(err));
return;
}
else if (printf_id == asyncactionint(AsyncAction::time))
......
......@@ -1714,18 +1714,21 @@ declare void @llvm.lifetime.end.p0i8(i64, i8* nocapture) #1
define i64 @"kprobe:f"(i8*) local_unnamed_addr section "s_kprobe:f" {
entry:
%perfdata = alloca [26 x i8], align 8
%1 = getelementptr inbounds [26 x i8], [26 x i8]* %perfdata, i64 0, i64 0
%perfdata = alloca [27 x i8], align 8
%1 = getelementptr inbounds [27 x i8], [27 x i8]* %perfdata, i64 0, i64 0
call void @llvm.lifetime.start.p0i8(i64 -1, i8* nonnull %1)
store i64 10001, [26 x i8]* %perfdata, align 8
%2 = getelementptr inbounds [26 x i8], [26 x i8]* %perfdata, i64 0, i64 8
%3 = getelementptr inbounds [26 x i8], [26 x i8]* %perfdata, i64 0, i64 24
%4 = bitcast i8* %3 to i16*
store i64 10001, [27 x i8]* %perfdata, align 8
%2 = getelementptr inbounds [27 x i8], [27 x i8]* %perfdata, i64 0, i64 8
%str.sroa.0.0..sroa_idx = getelementptr inbounds [27 x i8], [27 x i8]* %perfdata, i64 0, i64 24
call void @llvm.memset.p0i8.i64(i8* nonnull %2, i8 0, i64 16, i32 8, i1 false)
store i16 30784, i16* %4, align 8
store i8 64, i8* %str.sroa.0.0..sroa_idx, align 8
%str.sroa.4.0..sroa_idx = getelementptr inbounds [27 x i8], [27 x i8]* %perfdata, i64 0, i64 25
store i8 120, i8* %str.sroa.4.0..sroa_idx, align 1
%str.sroa.5.0..sroa_idx = getelementptr inbounds [27 x i8], [27 x i8]* %perfdata, i64 0, i64 26
store i8 0, i8* %str.sroa.5.0..sroa_idx, align 2
%pseudo = tail call i64 @llvm.bpf.pseudo(i64 1, i64 2)
%get_cpu_id = tail call i64 inttoptr (i64 8 to i64 ()*)()
%perf_event_output = call i64 inttoptr (i64 25 to i64 (i8*, i8*, i64, i8*, i64)*)(i8* %0, i64 %pseudo, i64 %get_cpu_id, [26 x i8]* nonnull %perfdata, i64 26)
%perf_event_output = call i64 inttoptr (i64 25 to i64 (i8*, i8*, i64, i8*, i64)*)(i8* %0, i64 %pseudo, i64 %get_cpu_id, [27 x i8]* nonnull %perfdata, i64 27)
call void @llvm.lifetime.end.p0i8(i64 -1, i8* nonnull %1)
ret i64 0
}
......@@ -1770,16 +1773,19 @@ declare void @llvm.lifetime.end.p0i8(i64, i8* nocapture) #1
define i64 @"kprobe:f"(i8*) local_unnamed_addr section "s_kprobe:f" {
entry:
%perfdata = alloca [10 x i8], align 8
%1 = getelementptr inbounds [10 x i8], [10 x i8]* %perfdata, i64 0, i64 0
%perfdata = alloca [11 x i8], align 8
%1 = getelementptr inbounds [11 x i8], [11 x i8]* %perfdata, i64 0, i64 0
call void @llvm.lifetime.start.p0i8(i64 -1, i8* nonnull %1)
store i64 10002, [10 x i8]* %perfdata, align 8
%2 = getelementptr inbounds [10 x i8], [10 x i8]* %perfdata, i64 0, i64 8
%3 = bitcast i8* %2 to i16*
store i16 30784, i16* %3, align 8
store i64 10002, [11 x i8]* %perfdata, align 8
%str.sroa.0.0..sroa_idx = getelementptr inbounds [11 x i8], [11 x i8]* %perfdata, i64 0, i64 8
store i8 64, i8* %str.sroa.0.0..sroa_idx, align 8
%str.sroa.4.0..sroa_idx = getelementptr inbounds [11 x i8], [11 x i8]* %perfdata, i64 0, i64 9
store i8 120, i8* %str.sroa.4.0..sroa_idx, align 1
%str.sroa.5.0..sroa_idx = getelementptr inbounds [11 x i8], [11 x i8]* %perfdata, i64 0, i64 10
store i8 0, i8* %str.sroa.5.0..sroa_idx, align 2
%pseudo = tail call i64 @llvm.bpf.pseudo(i64 1, i64 2)
%get_cpu_id = tail call i64 inttoptr (i64 8 to i64 ()*)()
%perf_event_output = call i64 inttoptr (i64 25 to i64 (i8*, i8*, i64, i8*, i64)*)(i8* %0, i64 %pseudo, i64 %get_cpu_id, [10 x i8]* nonnull %perfdata, i64 10)
%perf_event_output = call i64 inttoptr (i64 25 to i64 (i8*, i8*, i64, i8*, i64)*)(i8* %0, i64 %pseudo, i64 %get_cpu_id, [11 x i8]* nonnull %perfdata, i64 11)
call void @llvm.lifetime.end.p0i8(i64 -1, i8* nonnull %1)
ret i64 0
}
......@@ -1821,16 +1827,19 @@ declare void @llvm.lifetime.end.p0i8(i64, i8* nocapture) #1
define i64 @"kprobe:f"(i8*) local_unnamed_addr section "s_kprobe:f" {
entry:
%perfdata = alloca [10 x i8], align 8
%1 = getelementptr inbounds [10 x i8], [10 x i8]* %perfdata, i64 0, i64 0
%perfdata = alloca [11 x i8], align 8
%1 = getelementptr inbounds [11 x i8], [11 x i8]* %perfdata, i64 0, i64 0
call void @llvm.lifetime.start.p0i8(i64 -1, i8* nonnull %1)
store i64 10003, [10 x i8]* %perfdata, align 8
%2 = getelementptr inbounds [10 x i8], [10 x i8]* %perfdata, i64 0, i64 8
%3 = bitcast i8* %2 to i16*
store i16 30784, i16* %3, align 8
store i64 10003, [11 x i8]* %perfdata, align 8
%str.sroa.0.0..sroa_idx = getelementptr inbounds [11 x i8], [11 x i8]* %perfdata, i64 0, i64 8
store i8 64, i8* %str.sroa.0.0..sroa_idx, align 8
%str.sroa.4.0..sroa_idx = getelementptr inbounds [11 x i8], [11 x i8]* %perfdata, i64 0, i64 9
store i8 120, i8* %str.sroa.4.0..sroa_idx, align 1
%str.sroa.5.0..sroa_idx = getelementptr inbounds [11 x i8], [11 x i8]* %perfdata, i64 0, i64 10
store i8 0, i8* %str.sroa.5.0..sroa_idx, align 2
%pseudo = tail call i64 @llvm.bpf.pseudo(i64 1, i64 2)
%get_cpu_id = tail call i64 inttoptr (i64 8 to i64 ()*)()
%perf_event_output = call i64 inttoptr (i64 25 to i64 (i8*, i8*, i64, i8*, i64)*)(i8* %0, i64 %pseudo, i64 %get_cpu_id, [10 x i8]* nonnull %perfdata, i64 10)
%perf_event_output = call i64 inttoptr (i64 25 to i64 (i8*, i8*, i64, i8*, i64)*)(i8* %0, i64 %pseudo, i64 %get_cpu_id, [11 x i8]* nonnull %perfdata, i64 11)
call void @llvm.lifetime.end.p0i8(i64 -1, i8* nonnull %1)
ret i64 0
}
......
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