+
Skip to content

Conversation

jeromemarchand
Copy link
Contributor

The function min() is called among the arguments of bpf_probe_read_user(). Depending on kernel version, the expansion of the macro may emmit a fake function that prevent the compilation of the BPF code.

Replace the min() function by the MIN() macro to solve the issue. This is safe since both members of the comparison are unsigned integer.

Solves the following error:
/virtual/main.c:48:36: error: cannot call non-static helper function
48 | bpf_probe_read_user(&key.name, min(sizeof(key.name), (size_t)length), (void *)classptr);
| ^
include/linux/minmax.h:129:19: note: expanded from macro 'min'
129 | #define min(x, y) __careful_cmp(min, x, y)
| ^
include/linux/minmax.h:105:2: note: expanded from macro '__careful_cmp'
105 | __careful_cmp_once(op, x, y, _UNIQUE_ID(x), _UNIQUE_ID(y))
| ^
include/linux/minmax.h:100:2: note: expanded from macro '__careful_cmp_once'
100 | BUILD_BUG_ON_MSG(!__types_ok(x,y,ux,uy),
| ^
note: (skipping 2 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all)
include/linux/compiler_types.h:418:2: note: expanded from macro '_compiletime_assert'
418 | __compiletime_assert(condition, msg, prefix, suffix)
| ^
include/linux/compiler_types.h:411:4: note: expanded from macro '__compiletime_assert'
411 | prefix ## suffix();
| ^
:91:1: note: expanded from here
91 | __compiletime_assert_317
| ^
1 error generated.
Traceback (most recent call last):
File "/usr/share/bcc/tools/lib/uobjnew", line 178, in
bpf = BPF(text=program, usdt_contexts=[usdt])
File "/usr/lib/python3.9/site-packages/bcc/init.py", line 507, in init
raise Exception("Failed to compile BPF module %s" % (src_file or ""))
Exception: Failed to compile BPF module

The function min() is called among the arguments of
bpf_probe_read_user(). Depending on kernel version, the expansion of
the macro may emmit a fake function that prevent the compilation of
the BPF code.

Replace the min() function by the MIN() macro to solve the issue. This
is safe since both members of the comparison are unsigned integer.

Solves the following error:
/virtual/main.c:48:36: error: cannot call non-static helper function
   48 |     bpf_probe_read_user(&key.name, min(sizeof(key.name), (size_t)length), (void *)classptr);
      |                                    ^
include/linux/minmax.h:129:19: note: expanded from macro 'min'
  129 | #define min(x, y)       __careful_cmp(min, x, y)
      |                         ^
include/linux/minmax.h:105:2: note: expanded from macro '__careful_cmp'
  105 |         __careful_cmp_once(op, x, y, __UNIQUE_ID(x_), __UNIQUE_ID(y_))
      |         ^
include/linux/minmax.h:100:2: note: expanded from macro '__careful_cmp_once'
  100 |         BUILD_BUG_ON_MSG(!__types_ok(x,y,ux,uy),        \
      |         ^
note: (skipping 2 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all)
include/linux/compiler_types.h:418:2: note: expanded from macro '_compiletime_assert'
  418 |         __compiletime_assert(condition, msg, prefix, suffix)
      |         ^
include/linux/compiler_types.h:411:4: note: expanded from macro '__compiletime_assert'
  411 |                         prefix ## suffix();                             \
      |                         ^
<scratch space>:91:1: note: expanded from here
   91 | __compiletime_assert_317
      | ^
1 error generated.
Traceback (most recent call last):
  File "/usr/share/bcc/tools/lib/uobjnew", line 178, in <module>
    bpf = BPF(text=program, usdt_contexts=[usdt])
  File "/usr/lib/python3.9/site-packages/bcc/__init__.py", line 507, in __init__
    raise Exception("Failed to compile BPF module %s" % (src_file or "<text>"))
Exception: Failed to compile BPF module <text>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
bpf_usdt_readarg(3, ctx, &length);
bpf_usdt_readarg(4, ctx, &size);
bpf_probe_read_user(&key.name, min(sizeof(key.name), (size_t)length), (void *)classptr);
bpf_probe_read_user(&key.name, MIN(sizeof(key.name), (size_t)length), (void *)classptr);
Copy link
Collaborator

Choose a reason for hiding this comment

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

If the content stored in key.name is confirmed to be a NUL-terminated string, would it be possible to replace bpf_probe_read_user with bpf_probe_read_user_str (without the MIN macro)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know about this one: searching for the object__alloc usdt probe returns surprisingly few results.

Using bpf_probe_read_user_str() seems to work.

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

点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载