Skip to content

Commit

Permalink
libbpf-tools/memleak: Fixes off-by-one error for -Z and -z option
Browse files Browse the repository at this point in the history
These are the description of each options written in memleak.c file
-z option: capture only allocations larger than this size
-Z option: capture only allocations smaller than this size

But actually they work like this
-z option: capture only allocations larger or equal to this size
-Z option: capture only allocations smaller or equal to this size

Test program

  #include <stdlib.h>
  #include <unistd.h>
  int* baz() { return malloc(sizeof(int)); }
  int* bar() { return baz(); }
  int* foo() { return bar(); }
  int main() {
    while (1) {
      sleep(5);
      int* p = foo();
    }
  }

off-by-one error for -z option

  $ sudo ./memleak -z 4 -c a.out
  using default object: libc.so.6
  using page size: 4096
  tracing kernel: false
  child created with pid: 136672
  Tracing outstanding memory allocs...  Hit Ctrl-C to end
  received go event. executing child command
  [11:31:15] Top 1 stacks with outstanding allocations:
  4 bytes in 1 allocations from stack
        0 [<00005718ca5ba17b>] baz+0x12 [/home/bojun/bcc-seo/libbpf-tools/a.out]
        1 [<00005718ca5ba18f>] bar+0x12 [/home/bojun/bcc-seo/libbpf-tools/a.out]
        2 [<00005718ca5ba1a3>] foo+0x12 [/home/bojun/bcc-seo/libbpf-tools/a.out]
        3 [<00005718ca5ba1c5>] main+0x20 [/home/bojun/bcc-seo/libbpf-tools/a.out]
        4 [<000074374542a1ca>] [/usr/lib/x86_64-linux-gnu/libc.so.6]
        5 [<000074374542a28b>] __libc_start_main+0x8b [/usr/lib/x86_64-linux-gnu/libc.so.6]
        6 [<00005718ca5ba0a5>] _start+0x25 [/home/bojun/bcc-seo/libbpf-tools/a.out]
  ^C[11:31:17] Top 1 stacks with outstanding allocations:
  4 bytes in 1 allocations from stack
        0 [<00005718ca5ba17b>] baz+0x12 [/home/bojun/bcc-seo/libbpf-tools/a.out]
        1 [<00005718ca5ba18f>] bar+0x12 [/home/bojun/bcc-seo/libbpf-tools/a.out]
        2 [<00005718ca5ba1a3>] foo+0x12 [/home/bojun/bcc-seo/libbpf-tools/a.out]
        3 [<00005718ca5ba1c5>] main+0x20 [/home/bojun/bcc-seo/libbpf-tools/a.out]
        4 [<000074374542a1ca>] [/usr/lib/x86_64-linux-gnu/libc.so.6]
        5 [<000074374542a28b>] __libc_start_main+0x8b [/usr/lib/x86_64-linux-gnu/libc.so.6]
        6 [<00005718ca5ba0a5>] _start+0x25 [/home/bojun/bcc-seo/libbpf-tools/a.out]
  reaped child process
  done

off-by-one error for -Z option

  $ sudo ./memleak -Z 4 -c a.out
  using default object: libc.so.6
  using page size: 4096
  tracing kernel: false
  child created with pid: 136685
  Tracing outstanding memory allocs...  Hit Ctrl-C to end
  received go event. executing child command
  [11:31:53] Top 1 stacks with outstanding allocations:
  4 bytes in 1 allocations from stack
        0 [<00005c4d1ffa217b>] baz+0x12 [/home/bojun/bcc-seo/libbpf-tools/a.out]
        1 [<00005c4d1ffa218f>] bar+0x12 [/home/bojun/bcc-seo/libbpf-tools/a.out]
        2 [<00005c4d1ffa21a3>] foo+0x12 [/home/bojun/bcc-seo/libbpf-tools/a.out]
        3 [<00005c4d1ffa21c5>] main+0x20 [/home/bojun/bcc-seo/libbpf-tools/a.out]
        4 [<0000793ca842a1ca>] [/usr/lib/x86_64-linux-gnu/libc.so.6]
        5 [<0000793ca842a28b>] __libc_start_main+0x8b [/usr/lib/x86_64-linux-gnu/libc.so.6]
        6 [<00005c4d1ffa20a5>] _start+0x25 [/home/bojun/bcc-seo/libbpf-tools/a.out]
  ^C[11:31:55] Top 1 stacks with outstanding allocations:
  4 bytes in 1 allocations from stack
        0 [<00005c4d1ffa217b>] baz+0x12 [/home/bojun/bcc-seo/libbpf-tools/a.out]
        1 [<00005c4d1ffa218f>] bar+0x12 [/home/bojun/bcc-seo/libbpf-tools/a.out]
        2 [<00005c4d1ffa21a3>] foo+0x12 [/home/bojun/bcc-seo/libbpf-tools/a.out]
        3 [<00005c4d1ffa21c5>] main+0x20 [/home/bojun/bcc-seo/libbpf-tools/a.out]
        4 [<0000793ca842a1ca>] [/usr/lib/x86_64-linux-gnu/libc.so.6]
        5 [<0000793ca842a28b>] __libc_start_main+0x8b [/usr/lib/x86_64-linux-gnu/libc.so.6]
        6 [<00005c4d1ffa20a5>] _start+0x25 [/home/bojun/bcc-seo/libbpf-tools/a.out]
  reaped child process
  done

off-by-one error fixed after this patch applied

  $ sudo ./memleak -Z 4 -c a.out
  using default object: libc.so.6
  using page size: 4096
  tracing kernel: false
  child created with pid: 136953
  Tracing outstanding memory allocs...  Hit Ctrl-C to end
  received go event. executing child command
  [11:32:35] Top 0 stacks with outstanding allocations:
  ^C[11:32:37] Top 0 stacks with outstanding allocations:
  reaped child process
  done
  $ sudo ./memleak -z 4 -c a.out
  using default object: libc.so.6
  using page size: 4096
  tracing kernel: false
  child created with pid: 136957
  Tracing outstanding memory allocs...  Hit Ctrl-C to end
  received go event. executing child command
  [11:32:46] Top 0 stacks with outstanding allocations:
  [11:32:51] Top 0 stacks with outstanding allocations:
  ^C[11:32:51] Top 0 stacks with outstanding allocations:
  reaped child process
  done
  • Loading branch information
Bojun-Seo committed Nov 19, 2024
1 parent b384150 commit 3d751f4
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion libbpf-tools/memleak.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ static void update_statistics_del(u64 stack_id, u64 sz)

static int gen_alloc_enter(size_t size)
{
if (size < min_size || size > max_size)
if (size <= min_size || max_size <= size)
return 0;

if (sample_rate > 1) {
Expand Down

0 comments on commit 3d751f4

Please sign in to comment.