Skip to content

Commit 27607b8

Browse files
committed
examples/c: prevent uprobe_add/uprobe_sub inlining
Depending on compiler, uprobe_add/uprobe_sub can still be inlined. Use no-op volatile asm and noinline attribute to prevent compiler from inlining. Just global function is not enough. Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
1 parent 23f3b6e commit 27607b8

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

examples/c/uprobe.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,19 @@ static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va
1212
return vfprintf(stderr, format, args);
1313
}
1414

15-
/* It's a global function to make sure compiler doesn't inline it. */
16-
int uprobed_add(int a, int b)
15+
/* It's a global function to make sure compiler doesn't inline it. To be extra
16+
* sure we also use "asm volatile" and noinline attributes to prevent
17+
* compiler from local inlining.
18+
*/
19+
__attribute__((noinline)) int uprobed_add(int a, int b)
1720
{
21+
asm volatile ("");
1822
return a + b;
1923
}
2024

21-
int uprobed_sub(int a, int b)
25+
__attribute__((noinline)) int uprobed_sub(int a, int b)
2226
{
27+
asm volatile ("");
2328
return a - b;
2429
}
2530

0 commit comments

Comments
 (0)