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 llvm errors of PositonalParameter #1565

Merged
merged 3 commits into from
Oct 21, 2020
Merged

Conversation

mmisono
Copy link
Collaborator

@mmisono mmisono commented Oct 16, 2020

The first commit fixes the error reported by AddressSanitizer. As of the second commit, I didn't encounter a specific problem but I think it is a correct fix. Please see each commit message for the details.

Checklist
  • Language changes are updated in docs/reference_guide.md
  • User-visible and non-trivial changes updated in CHANGELOG.md
  • The new behaviour is covered by tests

The expr_ of a PositionalParameter is a pointer to a buffer, and an
offset can be added to it (e.g., `str($1 + 1)`). Cast expr_ to int so
that llvm does not complain about type mismatches when operating addition.

This fixes the following error reported by AddressSanitizer.

```
% sudo ./src/bpftrace -e 'BEGIN { printf("%s", str($1 + 1)); exit(); }' hello
AddressSanitizer:DEADLYSIGNAL
=================================================================
==3979==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000f8b (pc 0x7f1947a755ba bp 0x60700000def0 sp 0x7ffc07b486b0 T0)
==3979==The signal is caused by a READ memory access.
==3979==Hint: address points to the zero page.
    #0 0x7f1947a755ba in llvm::Constant::isNullValue() const (/usr/lib/x86_64-linux-gnu/libLLVM-10.so.1+0xa735ba)
    bpftrace#1 0x7f1948826177 in llvm::LazyValueInfo::getPredicateAt(unsigned int, llvm::Value*, llvm::Constant*, llvm::Instruction*) (/usr/lib/x86_64-linux-gnu/libLLVM-10.so.1+0x1824177)
    bpftrace#2 0x7f194841e4a8  (/usr/lib/x86_64-linux-gnu/libLLVM-10.so.1+0x141c4a8)
    bpftrace#3 0x7f1948420049  (/usr/lib/x86_64-linux-gnu/libLLVM-10.so.1+0x141e049)
    bpftrace#4 0x7f1947b13ed5 in llvm::FPPassManager::runOnFunction(llvm::Function&) (/usr/lib/x86_64-linux-gnu/libLLVM-10.so.1+0xb11ed5)
    bpftrace#5 0x7f194879ca90  (/usr/lib/x86_64-linux-gnu/libLLVM-10.so.1+0x179aa90)
    bpftrace#6 0x7f1947b145ff in llvm::legacy::PassManagerImpl::run(llvm::Module&) (/usr/lib/x86_64-linux-gnu/libLLVM-10.so.1+0xb125ff)
    bpftrace#7 0x851bbe in bpftrace::ast::CodegenLLVM::optimize() /home/ubuntu/work/bpftrace/bpftrace/src/ast/codegen_llvm.cpp:2720:6
    bpftrace#8 0x791416 in main /home/ubuntu/work/bpftrace/bpftrace/src/main.cpp:745:10
    bpftrace#9 0x7f1945cc8b96 in __libc_start_main /build/glibc-2ORdQG/glibc-2.27/csu/../csu/libc-start.c:310
    bpftrace#10 0x4d1c79 in _start (/home/ubuntu/work/bpftrace/bpftrace/build_dev/src/bpftrace+0x4d1c79)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV (/usr/lib/x86_64-linux-gnu/libLLVM-10.so.1+0xa735ba) in llvm::Constant::isNullValue() const
==3979==ABORTING
```
Extend the lifetime of a buffer of a PositionalParameter in the binop
so that the buffer live until str() is accepted.

Example:

```
% ./src/bpftrace -e 'BEGIN { printf("%s", str($1+1)); exit(); }' hello
```

before
```
  [...]
  %str1 = alloca [6 x i8], align 1
  [...]
  %4 = getelementptr inbounds [6 x i8], [6 x i8]* %str1, i64 0, i64 0
  [...]
  %5 = zext [6 x i8]* %str1 to i64
  %6 = add nuw nsw i64 %5, 1
  call void @llvm.lifetime.end.p0i8(i64 -1, i8* nonnull %4)
  %probe_read_kernel_str = call i64 inttoptr (i64 115 to i64 ([64 x i8]*, i32, i64)*)([64 x i8]* nonnull %str, i32 64, i64 %6)
```

after

```
  %probe_read_kernel_str = call i64 inttoptr (i64 115 to i64 ([64 x i8]*, i32, i64)*)([64 x i8]* nonnull %str, i32 64, i64 %6)
  call void @llvm.lifetime.end.p0i8(i64 -1, i8* nonnull %4)
```
Copy link
Member

@danobi danobi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thanks

@danobi danobi merged commit 2143c9d into bpftrace:master Oct 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants