linux-next: build failure after merge of the kspp tree
Subsystems (2)
Hi all,
After merging the kspp tree, today's linux-next build (powerpc allmodconfig) failed like this:
In file included from include/linux/string.h:253,
from include/linux/bitmap.h:11,
from include/linux/cpumask.h:12,
from include/linux/mm_types_task.h:14,
from include/linux/mm_types.h:5,
from include/linux/buildid.h:5,
from include/linux/module.h:14,
from samples/trace_events/trace-events-sample.c:2:
In function '__fortify_strcpy',
inlined from 'perf_trace_foo_rel_loc' at samples/trace_events/./trace-events-sample.h:519:1:include/linux/fortify-string.h:47:33: error: '__builtin_strcpy' offset 12 is out of the bounds [0, 4] [-Werror=array-bounds]
47 | #define __underlying_strcpy __builtin_strcpy
| ^
include/linux/fortify-string.h:445:24: note: in expansion of macro '__underlying_strcpy'
445 | return __underlying_strcpy(p, q);
| ^~~~~~~~~~~~~~~~~~~Exposed by (probably) commit
602670289b69 ("fortify: Detect struct member overflows in memcpy() at compile-time")
Introduced by commit
b466b1332164 ("samples/trace_event: Add '__rel_loc' using sample event")
I have reverted that latter commit for today.
Signature · 2 lines
Cheers, Stephen Rothwell
Quoted 5 lines · Stephen Rothwell
Hi all,
After merging the kspp tree, today's linux-next build (powerpc allmodconfig) failed like this:
Quoted 11 lines
In file included from
include/linux/string.h:253,
frominclude/linux/bitmap.h:11,
frominclude/linux/cpumask.h:12,
frominclude/linux/mm_types_task.h:14,
frominclude/linux/mm_types.h:5,
frominclude/linux/buildid.h:5,
frominclude/linux/module.h:14,
fromsamples/trace_events/trace-events-sample.c:2:
In function '__fortify_strcpy',
inlined from 'perf_trace_foo_rel_loc' atsamples/trace_events/./trace-events-sample.h:519:1:include/linux/fortify-string.h:47:33: error: '__builtin_strcpy' offset 12 is out of the bounds [0, 4] [-Werror=array-bounds]
-Warray-bounds thinks something is trying to get at offset 12 of an object it thinks is only 4 bytes in size.
Quoted 8 lines
47 | #define __underlying_strcpy __builtin_strcpy | ^ include/linux/fortify-string.h:445:24: note: in expansion of macro '__underlying_strcpy' 445 | return __underlying_strcpy(p, q); | ^~~~~~~~~~~~~~~~~~~Exposed by (probably) commit
Quoted 7 lines
602670289b69("fortify: Detect struct member overflows inmemcpy()at compile-time")Introduced by commit
b466b1332164("samples/trace_event: Add '__rel_loc' using sample event")I have reverted that latter commit for today.
Digging through the macros, I end up reconstructing this:
strcpy( (char *)((void *)(&__entry->__rel_loc_foo) + sizeof(__entry->__rel_loc_foo) + (__entry->__rel_loc_foo & 0xffff)), foo ? (const char *)(foo) : "(null)");
I couldn't figure out how __entry is being allocated, but it seemed maybe related to this note:
/*
* struct trace_event_data_offsets_<call> {
* u32 <item1>; * u32 <item2>; * [...]
* };
*
* The __dynamic_array() macro will create each u32 <item>, this is
* to keep the offset of each array from the beginning of the event.
* The size of an array is also encoded, in the higher 16 bits of
* <item>.
*/
So, I think -Warray-bounds is refusing to see the destination as anything except a u32, but being accessed at 4 (sizeof(u32)) + 8 (address && 0xffff) (?)
But if this is true, I would imagine there would be plenty of other warnings? I'm currently stumped.
Reading 55de2c0b5610 ("tracing: Add '__rel_loc' using trace event macros") did not help me. ;)
-Kees
Signature · 1 line
Kees Cook
Masami Hiramatsu
maintainerHi Kees,
Quoted 39 lines · On Mon, 24 Jan 2022 23:44:05 -0800 Kees Cook <[email protected]> wrote:
Quoted 5 lines · Stephen Rothwell
Hi all,
After merging the kspp tree, today's linux-next build (powerpc allmodconfig) failed like this:
Quoted 11 lines
In file included from
include/linux/string.h:253,
frominclude/linux/bitmap.h:11,
frominclude/linux/cpumask.h:12,
frominclude/linux/mm_types_task.h:14,
frominclude/linux/mm_types.h:5,
frominclude/linux/buildid.h:5,
frominclude/linux/module.h:14,
fromsamples/trace_events/trace-events-sample.c:2:
In function '__fortify_strcpy',
inlined from 'perf_trace_foo_rel_loc' atsamples/trace_events/./trace-events-sample.h:519:1:include/linux/fortify-string.h:47:33: error: '__builtin_strcpy' offset 12 is out of the bounds [0, 4] [-Werror=array-bounds]-Warray-bounds thinks something is trying to get at offset 12 of an object it thinks is only 4 bytes in size.
Quoted 8 lines
47 | #define __underlying_strcpy __builtin_strcpy | ^ include/linux/fortify-string.h:445:24: note: in expansion of macro '__underlying_strcpy' 445 | return __underlying_strcpy(p, q); | ^~~~~~~~~~~~~~~~~~~Exposed by (probably) commit
Quoted 7 lines
602670289b69("fortify: Detect struct member overflows inmemcpy()at compile-time")Introduced by commit
b466b1332164("samples/trace_event: Add '__rel_loc' using sample event")I have reverted that latter commit for today.
Digging through the macros, I end up reconstructing this:
Quoted 7 lines
strcpy( (char *)((void *)(&__entry->__rel_loc_foo) + sizeof(__entry->__rel_loc_foo) + (__entry->__rel_loc_foo & 0xffff)), foo ? (const char *)(foo) : "(null)");I couldn't figure out how
__entryis being allocated, but it seemed maybe related to this note:
The __entry is the trace-event entry on the trace ring_buffer. This reserved an entry (area) on the ring_buffer and fills it with given traced data. "__rel_loc_foo" is the a field on the entry, which type is u32. This should be something like this.
struct {
...
u32 __rel_loc_foo;
...
} *__entry;
Quoted 14 lines
/*
* struct trace_event_data_offsets_<call> {* u32 <item1>; * u32 <item2>; * [...]* };
*
* The__dynamic_array()macro will create each u32 <item>, this is
* to keep the offset of each array from the beginning of the event.
* The size of an array is also encoded, in the higher 16 bits of
* <item>.
*/
Quoted 3 lines
So, I think -Warray-bounds is refusing to see the destination as anything except a u32, but being accessed at 4 (sizeof(u32)) + 8 (address &&
0xffff) (?)
Ah, I got it. Yes, that's right. __data_loc() will access the data from the __entry, but the __rel_loc() points the same address from the encoded field ("__rel_loc_foo" in this case) itself. This is introduced for the user application event, which doesn't know the actual __entry size because the __entry includes some kernel internal defined fields.
Quoted 2 lines
But if this is true, I would imagine there would be plenty of other warnings? I'm currently stumped.
That is because __rel_loc is used only in the sample code in the kernel for testing. Other use-cases comes from user-space.
Hmm, can we skip this boundary check for this example?
Thank you,
Quoted 8 lines
Reading
55de2c0b5610("tracing: Add '__rel_loc' using trace event macros") did not help me. ;)-Kees
Signature · 1 line
Kees Cook
Signature · 1 line
Masami Hiramatsu <[email protected]>
Steven Rostedt
maintainerQuoted 6 lines · On Tue, 25 Jan 2022 22:27:32 +0900 Masami Hiramatsu <[email protected]> wrote:
Quoted 2 lines
But if this is true, I would imagine there would be plenty of other warnings? I'm currently stumped.
That is because
__rel_locis used only in the sample code in the kernel for testing. Other use-cases comes from user-space.
Hmm, can we skip this boundary check for this example?
Is this only checked when __CHECKER__ is defined? If so, would this work?
-- Steve
samples/trace_events/trace-events-sample.h +6 -0
@@ -506,6 +506,8 @@ DEFINE_EVENT_PRINT(foo_template, foo_with_template_print,TP_ARGS (foo, bar), TP_printk("bar %s %d", __get_str(foo), __entry->bar)); +/* rel_loc is for user defined events, the checker will trigger bugs on it */ +#ifndef __CHECKER__/* * There are yet another __rel_loc dynamic data attribute. If you * use __rel_dynamic_array() and __rel_string() etc. macros, you
@@ -539,6 +541,10 @@ TRACE_EVENT(foo_rel_loc,TP_printk ("foo_rel_loc %s, %d, %s" ,__get_rel_str (foo), __entry->bar,__get_rel_bitmask (bitmask)) ); +#else +#define trace_foo_rel_loc (foo, bar, mask) do { } while (0) +#endif /* __CHECKER __ */ +#endif /***** NOTICE! The #if protection ends here. *****/
Masami Hiramatsu
maintainerQuoted 24 lines · On Tue, 25 Jan 2022 22:27:32 +0900 Masami Hiramatsu <[email protected]> wrote:
Quoted 13 lines
/*
* struct trace_event_data_offsets_<call> {* u32 <item1>; * u32 <item2>; * [...]* };
*
* The__dynamic_array()macro will create each u32 <item>, this is
* to keep the offset of each array from the beginning of the event.
* The size of an array is also encoded, in the higher 16 bits of
* <item>.
*/Quoted 3 lines
So, I think -Warray-bounds is refusing to see the destination as anything except a u32, but being accessed at 4 (sizeof(u32)) + 8 (address &&
0xffff) (?)Ah, I got it. Yes, that's right.
__data_loc()will access the data from the__entry, but the__rel_loc()points the same address from the encoded field ("__rel_loc_foo" in this case) itself. This is introduced for the user application event, which doesn't know the actual__entrysize because the__entryincludes some kernel internal defined fields.
Quoted 6 lines
Quoted 2 lines
But if this is true, I would imagine there would be plenty of other warnings? I'm currently stumped.
That is because
__rel_locis used only in the sample code in the kernel for testing. Other use-cases comes from user-space.
Hmm, can we skip this boundary check for this example?
If the -Warray-bounds determines the destination array size from the type of given pointer, we can just change the macro as below;
#define __get_rel_dynamic_array(field) ((void *)__entry + \ offsetof(typeof(*__entry), __rel_loc_##field) + \ sizeof(__entry->__rel_loc_##field) + \ (__entry->__rel_loc_##field & 0xffff))
This must works same as __get_dynamic_array() macro.
Could you try this patch?
From 2982ba01367ec1f746a4f128512436e5325a7f9d Mon Sep 17 00:00:00 2001
From: Masami Hiramatsu <[email protected]>
Date: Tue, 25 Jan 2022 23:19:30 +0900
Subject: [PATCH] tracing: Avoid -Warray-bounds warning for __rel_loc macro
Since -Warray-bounds checks the destination size from the type of given pointer, __assign_rel_str() macro gets warned because it passes the pointer to the 'u32' field instead of 'trace_event_raw_*' data structure.
Pass the data address calculated from the 'trace_event_raw_*' instead of 'u32' __rel_loc field.
--- include/trace/trace_events.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
include/trace/trace_events.h +4 -3
@@ -318,9 +318,10 @@ TRACE_MAKE_SYSTEM_STR();#define __get_str (field) ((char *)__get_dynamic_array(field))#undef __get_rel_dynamic_array -#define __get_rel_dynamic_array (field) \ - ((void *)(&__entry->__rel_loc_##field) + \ - sizeof(__entry->__rel_loc_##field) + \ +#define __get_rel_dynamic_array (field) \ + ((void *)__entry + \ + offsetof(typeof(*__entry), __rel_loc_##field) + \ + sizeof(__entry->__rel_loc_##field) + \ (__entry->__rel_loc_##field & 0xffff))#undef __get_rel_dynamic_array_len
Signature · 5 lines
2.25.1 -- Masami Hiramatsu <[email protected]>
Steven Rostedt
maintainerQuoted 14 lines · On Tue, 25 Jan 2022 23:31:54 +0900 Masami Hiramatsu <[email protected]> wrote:
Could you try this patch?
Quoted 1 line
From
2982ba01367ec1f746a4f128512436e5325a7f9dMon Sep 17 00:00:00 2001From: Masami Hiramatsu <[email protected]>
Date: Tue, 25 Jan 2022 23:19:30 +0900
Subject: [PATCH] tracing: Avoid -Warray-bounds warning for__rel_locmacroSince -Warray-bounds checks the destination size from the type of given pointer,
__assign_rel_str()macro gets warned because it passes the pointer to the 'u32' field instead of 'trace_event_raw_*' data structure.
Pass the data address calculated from the 'trace_event_raw_*' instead of 'u32'__rel_locfield.
Quoted 3 lines
Signed-off-by Masami Hiramatsu <[email protected]>Reported-by Stephen Rothwell <[email protected]>Cc Kees Cook <[email protected]>
If this works, I'll add it instead of the one that just turns it off it __CHECKER__ is defined.
Can you please let me know if it fixes the issue for you. I have a test run waiting on it.
Thanks,
-- Steve
Quoted 11 lines · Steven Rostedt
Quoted 6 lines · On Tue, 25 Jan 2022 22:27:32 +0900 Masami Hiramatsu <[email protected]> wrote:
Quoted 2 lines
But if this is true, I would imagine there would be plenty of other warnings? I'm currently stumped.
That is because
__rel_locis used only in the sample code in the kernel for testing. Other use-cases comes from user-space.
Hmm, can we skip this boundary check for this example?Is this only checked when
__CHECKER__is defined? If so, would this work?
__CHECKER__ is only for sparse. This is from re-enabling -Warray-bounds for gcc.
Signature · 1 line
Kees Cook
Quoted 43 lines · Masami Hiramatsu
Quoted 24 lines · On Tue, 25 Jan 2022 22:27:32 +0900 Masami Hiramatsu <[email protected]> wrote:
Quoted 13 lines
/*
* struct trace_event_data_offsets_<call> {* u32 <item1>; * u32 <item2>; * [...]* };
*
* The__dynamic_array()macro will create each u32 <item>, this is
* to keep the offset of each array from the beginning of the event.
* The size of an array is also encoded, in the higher 16 bits of
* <item>.
*/Quoted 3 lines
So, I think -Warray-bounds is refusing to see the destination as anything except a u32, but being accessed at 4 (sizeof(u32)) + 8 (address &&
0xffff) (?)Ah, I got it. Yes, that's right.
__data_loc()will access the data from the__entry, but the__rel_loc()points the same address from the encoded field ("__rel_loc_foo" in this case) itself. This is introduced for the user application event, which doesn't know the actual__entrysize because the__entryincludes some kernel internal defined fields.Quoted 6 lines
Quoted 2 lines
But if this is true, I would imagine there would be plenty of other warnings? I'm currently stumped.
That is because
__rel_locis used only in the sample code in the kernel for testing. Other use-cases comes from user-space.
Hmm, can we skip this boundary check for this example?If the -Warray-bounds determines the destination array size from the type of given pointer, we can just change the macro as below;
#define __get_rel_dynamic_array(field) ((void *)__entry + \ offsetof(typeof(*__entry), __rel_loc_##field) + \ sizeof(__entry->__rel_loc_##field) + \ (__entry->__rel_loc_##field & 0xffff))
Quoted 3 lines
This must works same as
__get_dynamic_array()macro.Could you try this patch?
Thanks, I'll give this a spin. I need to reproduce sfr's warning first, but now that I've fetched next-20220125, it should be easy. *famous last words*
Signature · 1 line
Kees Cook
Steven Rostedt
maintainerQuoted 3 lines · On Tue, 25 Jan 2022 09:54:14 -0800 Kees Cook <[email protected]> wrote:
Thanks, I'll give this a spin. I need to reproduce sfr's warning first, but now that I've fetched next-20220125, it should be easy. *famous last words*
I'll start my tests with this patch, and when/if you test it, please send a Tested-by tag.
Thanks,
-- Steve
Quoted 74 lines · Masami Hiramatsu
Quoted 24 lines · On Tue, 25 Jan 2022 22:27:32 +0900 Masami Hiramatsu <[email protected]> wrote:
Quoted 13 lines
/*
* struct trace_event_data_offsets_<call> {* u32 <item1>; * u32 <item2>; * [...]* };
*
* The__dynamic_array()macro will create each u32 <item>, this is
* to keep the offset of each array from the beginning of the event.
* The size of an array is also encoded, in the higher 16 bits of
* <item>.
*/Quoted 3 lines
So, I think -Warray-bounds is refusing to see the destination as anything except a u32, but being accessed at 4 (sizeof(u32)) + 8 (address &&
0xffff) (?)Ah, I got it. Yes, that's right.
__data_loc()will access the data from the__entry, but the__rel_loc()points the same address from the encoded field ("__rel_loc_foo" in this case) itself. This is introduced for the user application event, which doesn't know the actual__entrysize because the__entryincludes some kernel internal defined fields.Quoted 6 lines
Quoted 2 lines
But if this is true, I would imagine there would be plenty of other warnings? I'm currently stumped.
That is because
__rel_locis used only in the sample code in the kernel for testing. Other use-cases comes from user-space.
Hmm, can we skip this boundary check for this example?If the -Warray-bounds determines the destination array size from the type of given pointer, we can just change the macro as below;
#define __get_rel_dynamic_array(field) ((void *)__entry + \ offsetof(typeof(*__entry), __rel_loc_##field) + \ sizeof(__entry->__rel_loc_##field) + \ (__entry->__rel_loc_##field & 0xffff))This must works same as
__get_dynamic_array()macro.Could you try this patch?
From
2982ba01367ec1f746a4f128512436e5325a7f9dMon Sep 17 00:00:00 2001
From: Masami Hiramatsu <[email protected]>
Date: Tue, 25 Jan 2022 23:19:30 +0900
Subject: [PATCH] tracing: Avoid -Warray-bounds warning for__rel_locmacroSince -Warray-bounds checks the destination size from the type of given pointer,
__assign_rel_str()macro gets warned because it passes the pointer to the 'u32' field instead of 'trace_event_raw_*' data structure.
Pass the data address calculated from the 'trace_event_raw_*' instead of 'u32'__rel_locfield.Signed-off-by Masami Hiramatsu <[email protected]>Reported-by Stephen Rothwell <[email protected]>Cc Kees Cook <[email protected]>--- include/trace/trace_events.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)include/trace/trace_events.h +0
-0@@ -318,9 +318,10 @@ TRACE_MAKE_SYSTEM_STR();#define __get_str (field) ((char *)__get_dynamic_array(field))#undef __get_rel_dynamic_array
include/trace/trace_events.h TRACE_MAKE_SYSTEM_STR(); · line 327
Quoted 10 lines
include/trace/trace_events.h +4
-3-#define __get_rel_dynamic_array (field) \ - ((void *)(&__entry->__rel_loc_##field) + \ - sizeof(__entry->__rel_loc_##field) + \ +#define __get_rel_dynamic_array (field) \ + ((void *)__entry + \ + offsetof(typeof(*__entry), __rel_loc_##field) + \ + sizeof(__entry->__rel_loc_##field) + \ (__entry->__rel_loc_##field & 0xffff))#undef __get_rel_dynamic_array_len
This patch doesn't silence the warning, but now that I see the shape of things more clearly, let me see if I can find the right combo.
Thanks!
-Kees
Signature · 1 line
Kees Cook
Steven Rostedt
maintainerQuoted 84 lines · On Tue, 25 Jan 2022 12:57:24 -0800 Kees Cook <[email protected]> wrote:
Quoted 83 lines · Masami Hiramatsu
Quoted 24 lines · On Tue, 25 Jan 2022 22:27:32 +0900 Masami Hiramatsu <[email protected]> wrote:
Quoted 13 lines
/*
* struct trace_event_data_offsets_<call> {* u32 <item1>; * u32 <item2>; * [...]* };
*
* The__dynamic_array()macro will create each u32 <item>, this is
* to keep the offset of each array from the beginning of the event.
* The size of an array is also encoded, in the higher 16 bits of
* <item>.
*/Quoted 3 lines
So, I think -Warray-bounds is refusing to see the destination as anything except a u32, but being accessed at 4 (sizeof(u32)) + 8 (address &&
0xffff) (?)Ah, I got it. Yes, that's right.
__data_loc()will access the data from the__entry, but the__rel_loc()points the same address from the encoded field ("__rel_loc_foo" in this case) itself. This is introduced for the user application event, which doesn't know the actual__entrysize because the__entryincludes some kernel internal defined fields.Quoted 6 lines
Quoted 2 lines
But if this is true, I would imagine there would be plenty of other warnings? I'm currently stumped.
That is because
__rel_locis used only in the sample code in the kernel for testing. Other use-cases comes from user-space.
Hmm, can we skip this boundary check for this example?If the -Warray-bounds determines the destination array size from the type of given pointer, we can just change the macro as below;
#define __get_rel_dynamic_array(field) ((void *)__entry + \ offsetof(typeof(*__entry), __rel_loc_##field) + \ sizeof(__entry->__rel_loc_##field) + \ (__entry->__rel_loc_##field & 0xffff))This must works same as
__get_dynamic_array()macro.Could you try this patch?
From
2982ba01367ec1f746a4f128512436e5325a7f9dMon Sep 17 00:00:00 2001
From: Masami Hiramatsu <[email protected]>
Date: Tue, 25 Jan 2022 23:19:30 +0900
Subject: [PATCH] tracing: Avoid -Warray-bounds warning for__rel_locmacroSince -Warray-bounds checks the destination size from the type of given pointer,
__assign_rel_str()macro gets warned because it passes the pointer to the 'u32' field instead of 'trace_event_raw_*' data structure.
Pass the data address calculated from the 'trace_event_raw_*' instead of 'u32'__rel_locfield.Signed-off-by Masami Hiramatsu <[email protected]>Reported-by Stephen Rothwell <[email protected]>Cc Kees Cook <[email protected]>--- include/trace/trace_events.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)include/trace/trace_events.h +4
-3@@ -318,9 +318,10 @@ TRACE_MAKE_SYSTEM_STR();#define __get_str (field) ((char *)__get_dynamic_array(field))#undef __get_rel_dynamic_array -#define __get_rel_dynamic_array (field) \ - ((void *)(&__entry->__rel_loc_##field) + \ - sizeof(__entry->__rel_loc_##field) + \ +#define __get_rel_dynamic_array (field) \ + ((void *)__entry + \ + offsetof(typeof(*__entry), __rel_loc_##field) + \ + sizeof(__entry->__rel_loc_##field) + \ (__entry->__rel_loc_##field & 0xffff))
Quoted 4 lines
include/trace/trace_events.h TRACE_MAKE_SYSTEM_STR(); · line 327
Quoted 1 line
include/trace/trace_events.h +0
-0#undef __get_rel_dynamic_array_lenThis patch doesn't silence the warning, but now that I see the shape of things more clearly, let me see if I can find the right combo.
Hmm, could the zero size array cause an issues here. That is, does this help?
include/trace/trace_events.h +1 -1
@@ -128,7 +128,7 @@ TRACE_MAKE_SYSTEM_STR(); struct trace_event_raw_##name { \ struct trace_entry ent; \ tstruct \ - char __data[0]; \ + char __data[]; \ }; \ \ static struct trace_event_class event_class_##name;
-- Steve
Steven Rostedt
maintainerQuoted 10 lines · On Tue, 25 Jan 2022 16:23:26 -0500 Steven Rostedt <[email protected]> wrote:
Quoted 10 lines
Quoted 10 lines
#undef
__get_rel_dynamic_array(quoted diff) +4
-3-#define __get_rel_dynamic_array(field) \ - ((void *)(&__entry->__rel_loc_##field) + \ - sizeof(__entry->__rel_loc_##field) + \ +#define __get_rel_dynamic_array(field) \ + ((void *)__entry + \ + offsetof(typeof(*__entry), __rel_loc_##field) + \ + sizeof(__entry->__rel_loc_##field) + \ (__entry->__rel_loc_##field & 0xffff))
Quoted 2 lines
Quoted 2 lines
Quoted 1 line
#undef
__get_rel_dynamic_array_len
I also do not like the the inconsistency between sizeof(__entry->__rel_loc_##field) and sizeof(u32) that is used in the calculation part:
#define __rel_dynamic_array(type, item, len) \ __item_length = (len) * sizeof(type); \ __data_offsets->item = __data_size + \ offsetof(typeof(*entry), __data) - \ offsetof(typeof(*entry), __rel_loc_##item) - \ sizeof(u32); \ __data_offsets->item |= __item_length << 16; \ __data_size += __item_length;
Why is one using sizeof(u32) and the other using the size of the field?
Just to let you know what is happening. As dynamic elements of the trace event needs to be appended at the end of the event, the above macros are defined and then run through the TRACE_EVENT() macro, where the TP_STRUCT__entry() is parsed to calculate where each item will be for that event.
static inline notrace int trace_event_get_offsets_##call( \
struct trace_event_data_offsets_##call *__data_offsets, proto) \
{ \
int __data_size = 0; \
int __maybe_unused __item_length; \
struct trace_event_raw_##call __maybe_unused *entry; \
\
tstruct; \
\
return __data_size; \
}The tstruct is the TP_STRUCT__entry() and for each __rel_dynamic_array() or __dynamic_array(), the __data_size gets updated and saved into the __data_offsets that holds where each item is.
The rel versions sets the offset from its location to the data, where as the non rel versions sets the offset from the beginning of the event to the data.
-- Steve
Quoted 100 lines · Steven Rostedt
Quoted 84 lines · On Tue, 25 Jan 2022 12:57:24 -0800 Kees Cook <[email protected]> wrote:
Quoted 83 lines · Masami Hiramatsu
Quoted 24 lines · On Tue, 25 Jan 2022 22:27:32 +0900 Masami Hiramatsu <[email protected]> wrote:
Quoted 13 lines
/*
* struct trace_event_data_offsets_<call> {* u32 <item1>; * u32 <item2>; * [...]* };
*
* The__dynamic_array()macro will create each u32 <item>, this is
* to keep the offset of each array from the beginning of the event.
* The size of an array is also encoded, in the higher 16 bits of
* <item>.
*/Quoted 3 lines
So, I think -Warray-bounds is refusing to see the destination as anything except a u32, but being accessed at 4 (sizeof(u32)) + 8 (address &&
0xffff) (?)Ah, I got it. Yes, that's right.
__data_loc()will access the data from the__entry, but the__rel_loc()points the same address from the encoded field ("__rel_loc_foo" in this case) itself. This is introduced for the user application event, which doesn't know the actual__entrysize because the__entryincludes some kernel internal defined fields.Quoted 6 lines
Quoted 2 lines
But if this is true, I would imagine there would be plenty of other warnings? I'm currently stumped.
That is because
__rel_locis used only in the sample code in the kernel for testing. Other use-cases comes from user-space.
Hmm, can we skip this boundary check for this example?If the -Warray-bounds determines the destination array size from the type of given pointer, we can just change the macro as below;
#define __get_rel_dynamic_array(field) ((void *)__entry + \ offsetof(typeof(*__entry), __rel_loc_##field) + \ sizeof(__entry->__rel_loc_##field) + \ (__entry->__rel_loc_##field & 0xffff))This must works same as
__get_dynamic_array()macro.Could you try this patch?
From
2982ba01367ec1f746a4f128512436e5325a7f9dMon Sep 17 00:00:00 2001
From: Masami Hiramatsu <[email protected]>
Date: Tue, 25 Jan 2022 23:19:30 +0900
Subject: [PATCH] tracing: Avoid -Warray-bounds warning for__rel_locmacroSince -Warray-bounds checks the destination size from the type of given pointer,
__assign_rel_str()macro gets warned because it passes the pointer to the 'u32' field instead of 'trace_event_raw_*' data structure.
Pass the data address calculated from the 'trace_event_raw_*' instead of 'u32'__rel_locfield.Signed-off-by Masami Hiramatsu <[email protected]>Reported-by Stephen Rothwell <[email protected]>Cc Kees Cook <[email protected]>--- include/trace/trace_events.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)include/trace/trace_events.h +4
-3@@ -318,9 +318,10 @@ TRACE_MAKE_SYSTEM_STR();#define __get_str (field) ((char *)__get_dynamic_array(field))#undef __get_rel_dynamic_array -#define __get_rel_dynamic_array (field) \ - ((void *)(&__entry->__rel_loc_##field) + \ - sizeof(__entry->__rel_loc_##field) + \ +#define __get_rel_dynamic_array (field) \ + ((void *)__entry + \ + offsetof(typeof(*__entry), __rel_loc_##field) + \ + sizeof(__entry->__rel_loc_##field) + \ (__entry->__rel_loc_##field & 0xffff))Quoted 4 lines
include/trace/trace_events.h TRACE_MAKE_SYSTEM_STR(); · line 327
Quoted 1 line
include/trace/trace_events.h +0
-0#undef __get_rel_dynamic_array_lenThis patch doesn't silence the warning, but now that I see the shape of things more clearly, let me see if I can find the right combo.
Hmm, could the zero size array cause an issues here. That is, does this help?
include/trace/trace_events.h +0
-0
include/trace/trace_events.h TRACE_MAKE_SYSTEM_STR(); · line 134
Quoted 8 lines
include/trace/trace_events.h +1
-1@@ -128,7 +128,7 @@ TRACE_MAKE_SYSTEM_STR(); struct trace_event_raw_##name { \ struct trace_entry ent; \ tstruct \ - char __data[0]; \ + char __data[]; \ }; \ \ static struct trace_event_class event_class_##name;
I changed this too, just to future-proof it, and in an attempt to base the address off of __data[] (which turned out not to be needed).
It turns out that there was still a version of the __get_rel_dyanmic_array in perf.h that was the actual culprit.
Signature · 1 line
Kees Cook
Quoted 30 lines · Steven Rostedt
Quoted 10 lines · On Tue, 25 Jan 2022 16:23:26 -0500 Steven Rostedt <[email protected]> wrote:
Quoted 10 lines
Quoted 10 lines
#undef
__get_rel_dynamic_array(quoted diff) +4
-3-#define __get_rel_dynamic_array(field) \ - ((void *)(&__entry->__rel_loc_##field) + \ - sizeof(__entry->__rel_loc_##field) + \ +#define __get_rel_dynamic_array(field) \ + ((void *)__entry + \ + offsetof(typeof(*__entry), __rel_loc_##field) + \ + sizeof(__entry->__rel_loc_##field) + \ (__entry->__rel_loc_##field & 0xffff))Quoted 2 lines
Quoted 2 lines
Quoted 1 line
#undef
__get_rel_dynamic_array_lenI also do not like the the inconsistency between
sizeof(__entry->__rel_loc_##field)and sizeof(u32) that is used in the calculation part:#define __rel_dynamic_array(type, item, len) \ __item_length = (len) * sizeof(type); \ __data_offsets->item = __data_size + \ offsetof(typeof(*entry), __data) - \ offsetof(typeof(*entry), __rel_loc_##item) - \ sizeof(u32); \ __data_offsets->item |= __item_length << 16; \ __data_size += __item_length;
Quoted 1 line
Why is one using sizeof(u32) and the other using the size of the field?
It might make more sense to calculate everything as an offset within __data[] instead of from the start of __entry. The patch I sent just did in perf.h exactly what Masami did in trace_event.h. That worked. I had an earlier version that did this horrible thing which could probably be significantly improved, since I just subtract the offset of __data:
#undef __get_rel_dynamic_array
#define __get_rel_dynamic_array(field) \
- ((void *)(&__entry->__rel_loc_##field) + \
- sizeof(__entry->__rel_loc_##field) + \
- (__entry->__rel_loc_##field & 0xffff))
+ ((void *)&__entry->__data[ \
+ offsetof(typeof(*__entry), __rel_loc_##field) \
+ + sizeof(__entry->__rel_loc_##field) \
+ + (__entry->__rel_loc_##field & 0xffff) \
+ - offsetof(typeof(*__entry), __data) \
+ ])
Quoted 19 lines
Just to let you know what is happening. As dynamic elements of the trace event needs to be appended at the end of the event, the above macros are defined and then run through the
TRACE_EVENT()macro, where theTP_STRUCT__entry()is parsed to calculate where each item will be for that event.static inline notrace int trace_event_get_offsets_##call( \ struct trace_event_data_offsets_##call *__data_offsets, proto) \ { \ int __data_size = 0; \ int __maybe_unused __item_length; \ struct trace_event_raw_##call __maybe_unused *entry; \ \ tstruct; \ \ return __data_size; \ }
Quoted 7 lines
The tstruct is the
TP_STRUCT__entry()and for each__rel_dynamic_array()or__dynamic_array(), the__data_sizegets updated and saved into the__data_offsetsthat holds where each item is.The rel versions sets the offset from its location to the data, where as the non rel versions sets the offset from the beginning of the event to the data.
Could this just be
#define __get_rel_dynamic_array(field) \ ((void *)(&__entry->data[__entry->__rel_loc_##field & 0xffff])
?
Signature · 1 line
Kees Cook
Steven Rostedt
maintainerQuoted 8 lines · On Tue, 25 Jan 2022 14:07:14 -0800 Kees Cook <[email protected]> wrote:
Quoted 7 lines
The tstruct is the
TP_STRUCT__entry()and for each__rel_dynamic_array()or__dynamic_array(), the__data_sizegets updated and saved into the__data_offsetsthat holds where each item is.The rel versions sets the offset from its location to the data, where as the non rel versions sets the offset from the beginning of the event to the data.
Quoted 6 lines
Could this just be
#define __get_rel_dynamic_array(field) \ ((void *)(&__entry->data[__entry->__rel_loc_##field & 0xffff])?
This is currently user space defined. But since the only user of the rel_* version hasn't been upstreamed yet, we could change it. But it also requires changing libtraceevent as it depends on this code too.
I'm surprised that it doesn't break with the __get_dynamic_array() versions, or is that because it's based off of __entry?
-- Steve
Masami Hiramatsu
maintainerHi Steve,
(Ccing Beau)
Quoted 13 lines · On Tue, 25 Jan 2022 17:21:14 -0500 Steven Rostedt <[email protected]> wrote:
Quoted 10 lines · On Tue, 25 Jan 2022 14:07:14 -0800 Kees Cook <[email protected]> wrote:
Quoted 7 lines
The tstruct is the
TP_STRUCT__entry()and for each__rel_dynamic_array()or__dynamic_array(), the__data_sizegets updated and saved into the__data_offsetsthat holds where each item is.The rel versions sets the offset from its location to the data, where as the non rel versions sets the offset from the beginning of the event to the data.
Could this just be
Quoted 8 lines
Quoted 4 lines
#define __get_rel_dynamic_array(field) \ ((void *)(&__entry->data[__entry->__rel_loc_##field & 0xffff])?
This is currently user space defined. But since the only user of the rel_* version hasn't been upstreamed yet, we could change it. But it also requires changing libtraceevent as it depends on this code too.
I think Kees' idea seems better. If you and Beau are good, I will update the macros for __rel_loc. (This requires to change some user-space application which Beau is making too.)
Quoted 3 lines
I'm surprised that it doesn't break with the
__get_dynamic_array()versions, or is that because it's based off of__entry?
I think so. Gcc seems to check the size of the data structure where the original base address points.
Thank you,
Quoted 2 lines
-- Steve
Signature · 1 line
Masami Hiramatsu <[email protected]>
Steven Rostedt
maintainerQuoted 3 lines · On Wed, 26 Jan 2022 09:35:38 +0900 Masami Hiramatsu <[email protected]> wrote:
I think Kees' idea seems better. If you and Beau are good, I will update the macros for
__rel_loc. (This requires to change some user-space application which Beau is making too.)
If Beau is OK with it, I'm OK with it too. I need to release a new version of libtraceevent anyway, and I can make the update for that too.
Who's adding the patch (if Beau says it's OK), you or Kees?
-- Steve
Quoted 33 lines · Masami Hiramatsu
Hi Steve,
(Ccing Beau)Quoted 13 lines · On Tue, 25 Jan 2022 17:21:14 -0500 Steven Rostedt <[email protected]> wrote:
Quoted 10 lines · On Tue, 25 Jan 2022 14:07:14 -0800 Kees Cook <[email protected]> wrote:
Quoted 7 lines
The tstruct is the
TP_STRUCT__entry()and for each__rel_dynamic_array()or__dynamic_array(), the__data_sizegets updated and saved into the__data_offsetsthat holds where each item is.The rel versions sets the offset from its location to the data, where as the non rel versions sets the offset from the beginning of the event to the data.
Could this just be
Quoted 8 lines
Quoted 4 lines
#define __get_rel_dynamic_array(field) \ ((void *)(&__entry->data[__entry->__rel_loc_##field & 0xffff])?
This is currently user space defined. But since the only user of the rel_* version hasn't been upstreamed yet, we could change it. But it also requires changing libtraceevent as it depends on this code too.
I think Kees' idea seems better. If you and Beau are good, I will update the macros for
__rel_loc. (This requires to change some user-space application which Beau is making too.)Quoted 1 line
Quoted 5 lines
Quoted 2 lines
I'm surprised that it doesn't break with the
__get_dynamic_array()versions, or is that because it's based off of__entry?I think so. Gcc seems to check the size of the data structure where the original base address points.
Right, and it's pretty good these days at navigating through casts and inlines, etc, which is appreciated in all the cases where it has found real bugs. :)
The "offset from __entry" solution works as well as "offset from __entry->data", so I don't care which is used. If "offset from data" makes more sense for this API, yeah, I guess change it now while it's possible. :)
Thanks for helping track this down!
Signature · 1 line
Kees Cook
Quoted 11 lines · Steven Rostedt
Quoted 3 lines · On Wed, 26 Jan 2022 09:35:38 +0900 Masami Hiramatsu <[email protected]> wrote:
I think Kees' idea seems better. If you and Beau are good, I will update the macros for
__rel_loc. (This requires to change some user-space application which Beau is making too.)If Beau is OK with it, I'm OK with it too. I need to release a new version of libtraceevent anyway, and I can make the update for that too.
Who's adding the patch (if Beau says it's OK), you or Kees?
I don't know anything about libtraceevent, so hopefully not me! :) The patches Masami and I already sent fix the warning, so I leave it you y'all to decide if you want to make the internals a bit simpler.
Signature · 1 line
Kees Cook
Steven Rostedt
maintainerQuoted 8 lines · On Tue, 25 Jan 2022 19:18:53 -0800 Kees Cook <[email protected]> wrote:
Quoted 7 lines · Steven Rostedt
Quoted 3 lines · On Wed, 26 Jan 2022 09:35:38 +0900 Masami Hiramatsu <[email protected]> wrote:
I think Kees' idea seems better. If you and Beau are good, I will update the macros for
__rel_loc. (This requires to change some user-space application which Beau is making too.)
Quoted 9 lines
Quoted 4 lines
If Beau is OK with it, I'm OK with it too. I need to release a new version of libtraceevent anyway, and I can make the update for that too.
Who's adding the patch (if Beau says it's OK), you or Kees?
I don't know anything about libtraceevent, so hopefully not me! :) The patches Masami and I already sent fix the warning, so I leave it you y'all to decide if you want to make the internals a bit simpler.
Thinking about this more. I may just take both your patches, and leave it as an offset from the location of the descriptor. It's closer to the meaning of "relative" than doing it as a fixed offset from data.
And then no other patches or user space needs to be changed.
Thanks,
-- Steve
Masami Hiramatsu
maintainerQuoted 11 lines · On Tue, 25 Jan 2022 20:16:34 -0500 Steven Rostedt <[email protected]> wrote:
Quoted 3 lines · On Wed, 26 Jan 2022 09:35:38 +0900 Masami Hiramatsu <[email protected]> wrote:
I think Kees' idea seems better. If you and Beau are good, I will update the macros for
__rel_loc. (This requires to change some user-space application which Beau is making too.)If Beau is OK with it, I'm OK with it too. I need to release a new version of libtraceevent anyway, and I can make the update for that too.
Who's adding the patch (if Beau says it's OK), you or Kees?
This will update the __rel_loc spec, so I'll do it :)
Thank you,
Quoted 2 lines
-- Steve
Signature · 1 line
Masami Hiramatsu <[email protected]>
Steven Rostedt
maintainerQuoted 7 lines · On Wed, 26 Jan 2022 12:52:52 +0900 Masami Hiramatsu <[email protected]> wrote:
Quoted 7 lines
Quoted 3 lines · On Wed, 26 Jan 2022 09:35:38 +0900 Masami Hiramatsu <[email protected]> wrote:
I think Kees' idea seems better. If you and Beau are good, I will update the macros for
__rel_loc. (This requires to change some user-space application which Beau is making too.)
Quoted 6 lines
Quoted 4 lines
If Beau is OK with it, I'm OK with it too. I need to release a new version of libtraceevent anyway, and I can make the update for that too.
Who's adding the patch (if Beau says it's OK), you or Kees?
This will update the
__rel_locspec, so I'll do it :)
As I replied to the other email, I don't think we want to do this.
As "rel" means relative, I think it makes more sense to keep it as is, and not have it just be from the data section. That may be even more confusing.
But I'll wait for Beau to answer.
Note, I added both your and Kees patches to my queue and I'm currently testing it. You can see what I'm testing here:
https://git.kernel.org/pub/scm/linux/kernel/git/rostedt/linux-trace.git/log/?h=ftrace/core
-- Steve
Quoted 10 lines · Steven Rostedt
Quoted 7 lines · On Tue, 25 Jan 2022 19:18:53 -0800 Kees Cook <[email protected]> wrote:
Quoted 6 lines · Steven Rostedt
Quoted 3 lines · On Wed, 26 Jan 2022 09:35:38 +0900 Masami Hiramatsu <[email protected]> wrote:
I think Kees' idea seems better. If you and Beau are good, I will update the macros for
__rel_loc. (This requires to change some user-space application which Beau is making too.)
I like the simplicity of using offset from the start of data, however, we would have to save the start of data somewhere for this. The user trace processing side won't know the size of the common header (unless I missed something elsewhere got added).
Due to this, I would *prefer* to have the offset still be the offset from the __rel_loc entry. That makes it absolutely clear to both user and kernel without requiring any additional information where to find it.
Quoted 12 lines
Quoted 10 lines
Quoted 5 lines
If Beau is OK with it, I'm OK with it too. I need to release a new version of libtraceevent anyway, and I can make the update for that too.
Who's adding the patch (if Beau says it's OK), you or Kees?
I don't know anything about libtraceevent, so hopefully not me! :) The patches Masami and I already sent fix the warning, so I leave it you y'all to decide if you want to make the internals a bit simpler.
Quoted 6 lines
Thinking about this more. I may just take both your patches, and leave it as an offset from the location of the descriptor. It's closer to the meaning of "relative" than doing it as a fixed offset from data.
And then no other patches or user space needs to be changed.
Right, and on the user side we will not know the start of the "data" / fixed offset without carrying back that information somehow, due to the common header size not being a fixed length forever.
Quoted 3 lines
Thanks,
-- Steve
Thanks,
-Beau
Steven Rostedt
maintainerQuoted 3 lines · On Wed, 26 Jan 2022 11:41:38 -0800 Beau Belgrave <[email protected]>…
Right, and on the user side we will not know the start of the "data" / fixed offset without carrying back that information somehow, due to the common header size not being a fixed length forever.
Good point! The data is not exported to user space via the format files like the __ref_loc field is, so there's no guarantee of knowing where data is.
Let's leave it as is.
-- Steve