Uprobe: Bug(?) when probing small binaries

From: Ravi Bangoria
Date: Mon Feb 12 2018 - 08:14:40 EST


Hi Oleg,

I'm observing a bug in the uprobe infrastructure. When target binary
is quite small, uprobe replaces 'trap' instruction at two different
places. Ex,

Simple test.c that loops for 100 seconds:

ÂÂÂ void main()
ÂÂÂ {
ÂÂÂÂÂÂÂ int i = 0;
ÂÂÂ
ÂÂÂÂÂÂÂ while (i++ != 100) {
ÂÂÂÂÂÂÂÂÂÂÂ printf("hi: %d", i);
ÂÂÂÂÂÂÂÂÂÂÂ sleep(1);
ÂÂÂÂÂÂÂ }
ÂÂÂ }

Add a probe on function main() in test.c

 $ perf probe -x ./a.out main
ÂÂÂ Added new event:
ÂÂÂÂÂ probe_a:mainÂÂÂÂÂÂÂÂÂ (on main in /home/ravi/a.out)

Start the target program and pause it.

 $ gdb --args ./a.out
ÂÂÂ (gdb) r
ÂÂÂ main: 1
ÂÂÂ main: 2
ÂÂÂ ^C

 (gdb) disassemble main
ÂÂÂÂÂÂ 0x000000001000069c <+8>:ÂÂÂ mflrÂÂÂ r0
Â
 (gdb) x/w 0x1001069c
ÂÂÂ 0x1001069c:ÂÂÂ 2080899750

Now enable the probe:

 # echo 1 > events/probe_a/main/enable

Check probed instruction:

 (gdb) disassemble main
ÂÂÂÂÂÂ 0x000000001000069c <+8>:ÂÂÂ trap

*Bug*:

 (gdb) x/w 0x1001069c
ÂÂÂ 0x1001069c:Â 2145386504

In short, when it replaces the probe instruction, it does some corruption
in the readonly vma. This seems to be a bug.

How did I get the other address 0x1001069c?I found build_map_info()
returns these two vmas for the single probe:

 10000000-10010000 r-xp 00000000 08:05 67325595 /home/ravi/a.out
 10010000-10020000 r--p 00000000 08:05 67325595 /home/ravi/a.out

and thusregister_for_each_vmas() calls install_breakpoint() on both of
thesevmas with different vaddr.

The example is on powerpc but same issue is observed on x86 as well. As,
the code is common, it should be reproducible on every architecture.

Also, I don't observe this issue for bigger binaries (maybe for those
whose vma spans across multiple pages).

Thanks,
Ravi