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 segfault for invalid AssignVarStatement visit #2423

Merged
merged 1 commit into from
Nov 15, 2022

Conversation

Rtoax
Copy link
Contributor

@Rtoax Rtoax commented Nov 13, 2022

When CodegenLLVM::visit calls CreateStore() with NULL expr_ as parameter, segfault is generated, for example:

  #include <linux/fs.h>
  kprobe:vfs_open {
    $file = (struct file *)
    printf("Hello\n");
  }

Acctually, we want to '$file = (struct file *)arg1;' here. When writing incorrectly '$file = (struct file *)', printf() make expr_ == nullptr, when call CreateStore(NULL, ...), segfault has occurred, and the stack backtrace is as follows:

  (gdb) bt
  #0  0x00000000007098c6 in llvm::Value::getType (this=0x0) at /usr/include/llvm/IR/Value.h:255
  #1  0x000000000070cc76 in llvm::IRBuilderBase::CreateAlignedStore (this=0x7fffffffbe40, Val=0x0,
      Ptr=0x15e2340, Align=..., isVolatile=false) at /usr/include/llvm/IR/IRBuilder.h:1689
  #2  0x000000000070cab3 in llvm::IRBuilderBase::CreateStore (this=0x7fffffffbe40, Val=0x0, Ptr=0x15e2340,
      isVolatile=false) at /usr/include/llvm/IR/IRBuilder.h:1663
  #3  0x00000000006f6948 in bpftrace::ast::CodegenLLVM::visit (this=0x7fffffffbe10, assignment=...)
      at /home/rongtao/Git/bpftrace/src/ast/passes/codegen_llvm.cpp:2140
  #4  0x0000000000751258 in bpftrace::ast::AssignVarStatement::accept (this=0x7fffe99b9a30, v=...)
      at /home/rongtao/Git/bpftrace/src/ast/ast.cpp:35
  #5  0x00000000006ff6ea in bpftrace::ast::CodegenLLVM::accept (this=0x7fffffffbe10, node=0x7fffe99b9a30)
      at /home/rongtao/Git/bpftrace/src/ast/passes/codegen_llvm.cpp:3360

We should prompt the user for some useful information, like:

  $ sudo ./sample.bt
  ./t3-call.bt:4:9-10: FATAL: Invalid expression for "$file"
    $file = (struct file *)
          ~
  Aborted

We can't solve this problem semantic_analyser because the syntax is correct. Maybe in the future we can solve it by modifying parser.yy, but this modification is not considered because it is more complicated. In any case, the CreateStore() parameter should not be NULL during the codegen phase.

LLVM/Clang Version: 15.0.1

@Rtoax Rtoax force-pushed the patch-34-CreateStore-segfault branch from 44778fd to 51726ca Compare November 13, 2022 04:54
@viktormalik
Copy link
Contributor

I'd say that the actual problem is that typecast gets an invalid expression (one with None type), could we instead check that in the semantic analyser?

@Rtoax
Copy link
Contributor Author

Rtoax commented Nov 14, 2022

@viktormalik You are right, thanks a lot, i just update this PR.

$ cat sample.bt

#include <linux/fs.h>
kprobe:vfs_open {
    $file = (struct file *)
    printf("Hello\n");
}
$ sudo ./sample.bt
./sample.bt:4:11-26: ERROR: Cannot cast from "none" type
$file = (struct file *)
         ~~~~~~~~~~~~~~~

@Rtoax Rtoax force-pushed the patch-34-CreateStore-segfault branch from 51726ca to a49eccb Compare November 14, 2022 10:54
Copy link
Contributor

@viktormalik viktormalik left a comment

Choose a reason for hiding this comment

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

Looks good, can you update the CHANGELOG, too?

Nit: the change now fixes an invalid typecast rather than variable assignment. Perhaps the commit message (and the CHANGELOG entry) could reflect that.

@Rtoax Rtoax force-pushed the patch-34-CreateStore-segfault branch from a49eccb to 6a293be Compare November 14, 2022 13:06
The change now fixes an invalid typecast rather than variable assignment.

When CodegenLLVM::visit calls CreateStore() with NULL expr_ as parameter,
segfault is generated, for example:

  $ cat sample.bt
  #include <linux/fs.h>
  kprobe:vfs_open {
    $file = (struct file *)
    printf("Hello\n");
  }

Acctually, we want to '$file = (struct file *)arg1;' here. When writing
incorrectly '$file = (struct file *)', printf() make expr_ == nullptr,
when call CreateStore(NULL, ...), segfault has occurred, and the stack
backtrace is as follows:

  (gdb) bt
  #0  0x00000000007098c6 in llvm::Value::getType (this=0x0) at /usr/include/llvm/IR/Value.h:255
  bpftrace#1  0x000000000070cc76 in llvm::IRBuilderBase::CreateAlignedStore (this=0x7fffffffbe40, Val=0x0,
      Ptr=0x15e2340, Align=..., isVolatile=false) at /usr/include/llvm/IR/IRBuilder.h:1689
  bpftrace#2  0x000000000070cab3 in llvm::IRBuilderBase::CreateStore (this=0x7fffffffbe40, Val=0x0, Ptr=0x15e2340,
      isVolatile=false) at /usr/include/llvm/IR/IRBuilder.h:1663
  bpftrace#3  0x00000000006f6948 in bpftrace::ast::CodegenLLVM::visit (this=0x7fffffffbe10, assignment=...)
      at /home/rongtao/Git/bpftrace/src/ast/passes/codegen_llvm.cpp:2140
  bpftrace#4  0x0000000000751258 in bpftrace::ast::AssignVarStatement::accept (this=0x7fffe99b9a30, v=...)
      at /home/rongtao/Git/bpftrace/src/ast/ast.cpp:35
  bpftrace#5  0x00000000006ff6ea in bpftrace::ast::CodegenLLVM::accept (this=0x7fffffffbe10, node=0x7fffe99b9a30)
      at /home/rongtao/Git/bpftrace/src/ast/passes/codegen_llvm.cpp:3360

We should prompt the user for some useful information, like:

  $ sudo ./sample.bt
  ./sample.bt:4:11-26: ERROR: Cannot cast from "none" type
    $file = (struct file *)
            ~~~~~~~~~~~~~~~

LLVM/Clang Version: 15.0.1

Signed-off-by: Rong Tao <[email protected]>
@Rtoax Rtoax force-pushed the patch-34-CreateStore-segfault branch from 6a293be to e223ef0 Compare November 14, 2022 13:08
@Rtoax
Copy link
Contributor Author

Rtoax commented Nov 14, 2022

Done to update the CHANGELOG, thanks.

@viktormalik viktormalik merged commit d0d1fc7 into bpftrace:master Nov 15, 2022
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