bpftrace: Variables declared inside loop does not respect lexical scope

What reproduces the bug? Provide code if possible.

#!/usr/bin/env bpftrace

BEGIN {
        $i = 0;
        while ($i < 1) {
                $j = 0;
                $i++;
        }

        print($j);
        exit();
}
$ $ sudo ./build/src/bpftrace ~/scratch/loop_var.bt
Attaching 1 probe...
0

I expect to see some error about $j being undefined.

bpftrace --info output

$ sudo ./build/src/bpftrace --info
System
  OS: Linux 6.7.3-arch1-2 #1 SMP PREEMPT_DYNAMIC Fri, 02 Feb 2024 17:03:55 +0000
  Arch: x86_64

Build
  version: v0.20.0-43-g6d9d8
  LLVM: 16.0.6
  unsafe probe: no
  bfd: yes
  libdw (DWARF support): yes

Kernel helpers
  probe_read: yes
  probe_read_str: yes
  probe_read_user: yes
  probe_read_user_str: yes
  probe_read_kernel: yes
  probe_read_kernel_str: yes
  get_current_cgroup_id: yes
  send_signal: yes
  override_return: yes
  get_boot_ns: yes
  dpath: yes
  skboutput: yes
  get_tai_ns: yes
  get_func_ip: yes
  jiffies64: yes

Kernel features
  Instruction limit: 1000000
  Loop support: yes
  btf: yes
  module btf: yes
  map batch: yes
  uprobe refcount (depends on Build:bcc bpf_attach_uprobe refcount): yes

Map types
  hash: yes
  percpu hash: yes
  array: yes
  percpu array: yes
  stack_trace: yes
  perf_event_array: yes
  ringbuf: yes

Probe types
  kprobe: yes
  tracepoint: yes
  perf_event: yes
  kfunc: yes
  kprobe_multi: yes
  uprobe_multi: no
  raw_tp_special: yes
  iter: yes

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 18 (18 by maintainers)

Commits related to this issue

Most upvoted comments

While I think looking at other languages is useful to see what’s been done and how it played out, I think we should be careful to start from first principles here, as we are somewhat in language design. Contrasted to syntax where I think it’s less important to innovate.

What are our goals with this change? Is it to make the language more terse for one-liners? To prevent programming errors? To back out of a dead-end design choice (accidental or otherwise)? Something else? And what are the relative priorities?

I think I initially opened this issue b/c I got bit by a correctness bug in one of my scripts. The original intent was to block scope only, but it turns out there are tools that take advantage of probe scoping with initialization in both branches of if/else. So we considered doing a hoist if a variable is initialized in both branches.

It would be great if we could build some consensus about our language design goals so we can properly evaluate this issue against them and come up with a acceptable long term solution.

Ah ok, easy enough bug to fix. Semantic analyser (and codegen_llvm but that’s less important) simply does not respect lexical scope. We’d want to create some kind of stack data structure like this: https://github.com/danobi/btrd/blob/master/src/lang/variables.rs

I can take this one

Fixing this would technically be backwards incompatible. But surely nobody is relying on this buggy behavior, right?

Right, we shouldn’t maintain backwards compatibility of an obviously buggy behaviour.