Skip to contentSkip to conversation

traceevent support for kernels with CONFIG_GCC_PLUGIN_STRUCTLEAK=y

PATCH In review John Koepi 2 participants 6 messages Dec 12, 2019 – Jan 15, 2020

lore
Share & tools
lore

John Koepi

author
#0 lore raw

Hi!

In kernels built with CONFIG_GCC_PLUGIN_STRUCTLEAK=y definition of macros __user differs from the usual one:

If __CHECKER__ is false:

Quoted 5 lines
# ifdef STRUCTLEAK_PLUGIN
#  define __user __attribute__((user))
# else
#  define __user
# endif

at linux-5.4.2-arch1/include/linux/compiler_types.h:28.

It seems that this fact leads the syscalls definitions macros linux-5.4.2-arch1/include/linux/syscalls.h

Quoted 3 lines

214:#define SYSCALL_DEFINE1(name, ...) SYSCALL_DEFINEx(1, _##name, __VA_ARGS__) 198:#define SYSCALL_METADATA(sname, nb, ...)
135:#define SYSCALL_TRACE_ENTER_EVENT(sname)

to include GGC attributes extensions definitions into the syscalls format metadata that is then delivered to the tracefs/syscalls/format.

This gets syscalls formats to have __attribute__ inside:

Quoted 8 lines
$ cat /sys/kernel/debug/tracing/events/syscalls/sys_enter_io_submit/format
name: sys_enter_io_submit
ID: 897
format:
field:unsigned short common_type; offset:0; size:2; signed:0;
...
field:struct iocb __attribute__((user)) * __attribute__((user)) * iocbpp; offset:32; size:8; signed:0;
...

Having __attribute__ leaked into syscalls format, it makes its impossible for the traceevent lib from the kernel/tools/lib to parse such fields, like in the example above.

This in its turn makes it impossible to use tracing for those syscalls:

Quoted 3 lines
$ sudo perf record -e syscalls:sys_enter_io_submit -aR
libtraceevent: No such file or directory
 Error: expected type 4 but read 5

Thus, tracing does not work for some syscalls in Arch Linux kernels. And I suppose for all kernels that built with structleak plugin support.

Reproduce: https://github.com/sitano/traceevent_attribute

My question:

Is it a bug that the traceevent lib does not support parsing __attribute__ in syscalls formats?

or it is a bug of the SYSCALL_DEFINEx macroses / build system that they do allow C attributes to leak?

maybe this is already fixed in the latest kernel? or maybe I am missing something?

Thx,
Ivan

#1 lore raw
Quoted 26 lines · On Thu, 12 Dec 2019 19:07:46 +0200 John Koepi <[email protected]> wrote:
Quoted 8 lines
$ cat /sys/kernel/debug/tracing/events/syscalls/sys_enter_io_submit/format
name: sys_enter_io_submit
ID: 897
format:
field:unsigned short common_type; offset:0; size:2; signed:0;
...
field:struct iocb __attribute__((user)) * __attribute__((user)) * iocbpp; offset:32; size:8; signed:0;
...  

Having __attribute__ leaked into syscalls format, it makes its impossible for the traceevent lib from the kernel/tools/lib to parse such fields, like in the example above.

This in its turn makes it impossible to use tracing for those syscalls:

Quoted 3 lines
$ sudo perf record -e syscalls:sys_enter_io_submit -aR
libtraceevent: No such file or directory
 Error: expected type 4 but read 5  

Thus, tracing does not work for some syscalls in Arch Linux kernels. And I suppose for all kernels that built with structleak plugin support.

Reproduce: https://github.com/sitano/traceevent_attribute

My question:

Quoted 8 lines

Is it a bug that the traceevent lib does not support parsing __attribute__ in syscalls formats?

or it is a bug of the SYSCALL_DEFINEx macroses / build system that they do allow C attributes to leak?

maybe this is already fixed in the latest kernel? or maybe I am missing something?

Thanks for the report. This looks like something we can have libtraceveent handle, no need to change the kernel.

Could you file a bug report?

https://bugzilla.kernel.org/buglist.cgi?component=Trace-cmd%2FKernelshark&product=Tools&resolution=---

-- Steve

author
#2 lore raw

Filed at https://bugzilla.kernel.org/show_bug.cgi?id=205857

-- Ivan

Quoted 48 lines · Steven Rostedt
Quoted 26 lines · On Thu, 12 Dec 2019 19:07:46 +0200 John Koepi <[email protected]> wrote:
Quoted 8 lines
$ cat /sys/kernel/debug/tracing/events/syscalls/sys_enter_io_submit/format
name: sys_enter_io_submit
ID: 897
format:
field:unsigned short common_type; offset:0; size:2; signed:0;
...
field:struct iocb __attribute__((user)) * __attribute__((user)) * iocbpp; offset:32; size:8; signed:0;
...

Having __attribute__ leaked into syscalls format, it makes its impossible for the traceevent lib from the kernel/tools/lib to parse such fields, like in the example above.

This in its turn makes it impossible to use tracing for those syscalls:

Quoted 3 lines
$ sudo perf record -e syscalls:sys_enter_io_submit -aR
libtraceevent: No such file or directory
 Error: expected type 4 but read 5

Thus, tracing does not work for some syscalls in Arch Linux kernels. And I suppose for all kernels that built with structleak plugin support.

Reproduce: https://github.com/sitano/traceevent_attribute

My question:

Quoted 8 lines

Is it a bug that the traceevent lib does not support parsing __attribute__ in syscalls formats?

or it is a bug of the SYSCALL_DEFINEx macroses / build system that they do allow C attributes to leak?

maybe this is already fixed in the latest kernel? or maybe I am missing something?

Thanks for the report. This looks like something we can have libtraceveent handle, no need to change the kernel.

Could you file a bug report?

https://bugzilla.kernel.org/buglist.cgi?component=Trace-cmd%2FKernelshark&product=Tools&resolution=---

-- Steve

#3 lore raw

PATCH 1/1 traceevent: ignore __attribute__ in fields format

To support kernel (e.g. Arch linux) tracing that have events fields with C attributes (__attribute__((xxx))), traceevent must ignore __attribute__ parts when parsing fields types.

An example from Arch linux kernel 4.2:

  $ cat /sys/kernel/.../sys_enter_io_submit/format
  ...
  field:struct iocb __attribute__((user)) * ...
                    ^^^

This fix adds support for fields types C attributes parsing to event_read_fields function. When it sees __attribute__ ((attribute-list)) expression it skips it.

Fixes: https://bugzilla.kernel.org/show_bug.cgi?id=205857
Base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf.git/

---
 tools/lib/traceevent/event-parse.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
tools/lib/traceevent/event-parse.c +22 -0
index beaa8b8c08ff..fbc1ea536742 100644
@@ -1486,6 +1486,28 @@ static int event_read_fields(struct tep_event *event, struct tep_format_field **
 			    (event->flags & TEP_EVENT_FL_ISFTRACE &&
 			     type == TEP_EVENT_OP && strcmp(token, ".") == 0)) {
 
+				/* ignore C attributes: __attribute__((expr)) */
+				if (strcmp(token, "__attribute__") == 0) {
+					free(token);
+					for (int i = 0; i < 2; i++) {
+						if (read_expected_item(TEP_EVENT_DELIM, "(") < 0) {
+							goto fail_expect;
+						}
+					}
+					for (int brackets = 2; brackets > 0;) {
+						if (read_token(&token) == TEP_EVENT_NONE) {
+							do_warning_event(event, "%s: __attribute__ not full", __func__);
+							goto fail_expect;
+						}
+						if (strcmp(token, "(") == 0)
+							brackets++;
+						else if (strcmp(token, ")") == 0)
+							brackets--;
+						free(token);
+					}
+					continue;
+				}
+
 				if (strcmp(token, "*") == 0)
 					field->flags |= TEP_FIELD_IS_POINTER;
 
Signature · 1 line
2.24.1
author
#4 lore raw

Re: [PATCH bpf] traceevent: ignore __attribute__ in fields format

I am not sure this is the right mailing list (I was not sure about @netdev). If it is not - I am sorry, I can resend it. The test for this change is at https://github.com/sitano/traceevent_attribute.

Quoted 61 lines · Ivan Prisyazhnyy

To support kernel (e.g. Arch linux) tracing that have events fields with C attributes (__attribute__((xxx))), traceevent must ignore __attribute__ parts when parsing fields types.

An example from Arch linux kernel 4.2:

  $ cat /sys/kernel/.../sys_enter_io_submit/format
  ...
  field:struct iocb __attribute__((user)) * ...
                    ^^^

This fix adds support for fields types C attributes parsing to event_read_fields function. When it sees __attribute__ ((attribute-list)) expression it skips it.

Fixes: https://bugzilla.kernel.org/show_bug.cgi?id=205857
Base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf.git/

---
 tools/lib/traceevent/event-parse.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
tools/lib/traceevent/event-parse.c +22 -1
index beaa8b8c08ff..fbc1ea536742 100644
@@ -1486,6 +1486,28 @@ static int event_read_fields(struct tep_event *event, struct tep_format_field **
                            (event->flags & TEP_EVENT_FL_ISFTRACE &&
                             type == TEP_EVENT_OP && strcmp(token, ".") == 0)) {
 
+                               /* ignore C attributes: __attribute__((expr)) */
+                               if (strcmp(token, "__attribute__") == 0) {
+                                       free(token);
+                                       for (int i = 0; i < 2; i++) {
+                                               if (read_expected_item(TEP_EVENT_DELIM, "(") < 0) {
+                                                       goto fail_expect;
+                                               }
+                                       }
+                                       for (int brackets = 2; brackets > 0;) {
+                                               if (read_token(&token) == TEP_EVENT_NONE) {
+                                                       do_warning_event(event, "%s: __attribute__ not full", __func__);
+                                                       goto fail_expect;
+                                               }
+                                               if (strcmp(token, "(") == 0)
+                                                       brackets++;
+                                               else if (strcmp(token, ")") == 0)
+                                                       brackets--;
+                                               free(token);
+                                       }
+                                       continue;
+                               }
+
                                if (strcmp(token, "*") == 0)
                                        field->flags |= TEP_FIELD_IS_POINTER;
 
--

2.24.1

Signature · 1 line
-- Ivan
#5 lore raw

Re: [PATCH bpf] traceevent: ignore __attribute__ in fields format

Quoted 4 lines · On Wed, 15 Jan 2020 20:47:47 +0200 John Koepi <[email protected]> wrote:

I am not sure this is the right mailing list (I was not sure about @netdev). If it is not - I am sorry, I can resend it. The test for this change is at https://github.com/sitano/traceevent_attribute.

It's fine to send it here. It's in my queue to look at.

Thanks!

-- Steve