tracing: silence GCC 9 array bounds warning
PATCH 1/1 tracing: silence GCC 9 array bounds warning
Starting with GCC 9, -Warray-bounds detects cases when memset is called starting on a member of a struct but the size to be cleared ends up writing over further members.
Such a call happens in the trace code to clear, at once, all members after and including seq on struct trace_iterator:
In function 'memset',
inlined from 'ftrace_dump' at kernel/trace/trace.c:8914:3:
./include/linux/string.h:344:9: warning: '__builtin_memset' offset [8505, 8560] from the object at 'iter' is out of the bounds of referenced subobject 'seq' with type 'struct trace_seq' at offset 4368 [-Warray-bounds]
344 | return __builtin_memset(p, c, size);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
In order to avoid GCC complaining about it, we compute the address ourselves by adding the offsetof distance instead of referring directly to the member.
Since there are two places doing this clear (trace.c and trace_kdb.c), take the chance to move the workaround into a single place in the internal header.
--- kernel/trace/trace.c | 7 +------ kernel/trace/trace.h | 14 ++++++++++++++ kernel/trace/trace_kdb.c | 7 +------ 3 files changed, 16 insertions(+), 12 deletions(-)
kernel/trace/trace.c +1 -6
@@ -8627,12 +8627,7 @@ void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) cnt++; -/* reset all but tr, trace, and overruns */ -memset (&iter.seq,0 , -sizeof (struct trace_iterator ) - - offsetof(struct trace_iterator , seq)); - iter.iter_flags |=TRACE_FILE_LAT_FMT ; - iter.pos =-1 ; +trace_iterator_reset (&iter);if (trace_find_next_entry_inc (&iter) !=NULL ) {int ret;
kernel/trace/trace.h +14 -0
@@ -1964,4 +1964,18 @@ static inline void tracer_hardirqs_off(unsigned long a0, unsigned long a1) { }extern struct trace_iterator *tracepoint_print_iter; +/* reset all but tr, trace, and overruns */ +static __always_inline void trace_iterator_reset(struct trace_iterator * iter) +{ +/* +* Equivalent to &iter->seq, but avoids GCC 9 complaining about +* overwriting more members than just iter->seq (-Warray-bounds) +*/ +memset ((char *)(iter) + offsetof(struct trace_iterator , seq),0 , +sizeof (struct trace_iterator ) - + offsetof(struct trace_iterator , seq)); + iter->iter_flags |=TRACE_FILE_LAT_FMT ; + iter->pos =-1 ; +} +#endif /* _LINUX_KERNEL_TRACE_H */
kernel/trace/trace_kdb.c +1 -6
@@ -41,12 +41,7 @@ static void ftrace_dump_buf(int skip_lines, long cpu_file)kdb_printf ("Dumping ftrace buffer:\n" ); -/* reset all but tr, trace, and overruns */ -memset (&iter.seq,0 , -sizeof (struct trace_iterator ) - - offsetof(struct trace_iterator , seq)); - iter.iter_flags |=TRACE_FILE_LAT_FMT ; - iter.pos =-1 ; +trace_iterator_reset (&iter);if (cpu_file ==RING_BUFFER_ALL_CPUS ) {for_each_tracing_cpu (cpu) {
Signature · 1 line
2.17.1
Steven Rostedt
maintainerQuoted 16 lines · On Fri, 17 May 2019 11:25:02 +0200 Miguel Ojeda…
Starting with GCC 9, -Warray-bounds detects cases when memset is called starting on a member of a struct but the size to be cleared ends up writing over further members.
Such a call happens in the trace code to clear, at once, all members after and including
seqon structtrace_iterator:In function 'memset',
inlined from 'ftrace_dump' atkernel/trace/trace.c:8914:3:
./include/linux/string.h:344:9: warning: '__builtin_memset' offset [8505, 8560] from the object at 'iter' is out of the bounds of referenced subobject 'seq' with type 'structtrace_seq' at offset 4368 [-Warray-bounds]
344 | return__builtin_memset(p, c, size);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
Quoted 7 lines
In order to avoid GCC complaining about it, we compute the address ourselves by adding the offsetof distance instead of referring directly to the member.
Since there are two places doing this clear (
trace.candtrace_kdb.c), take the chance to move the workaround into a single place in the internal header.
Hi Miguel,
Linus mentioned this too.
https://lore.kernel.org/lkml/CAHk-=wihYB8w__YQjgYjYZsVniu5CtkTcFycmCGdqVg8GUje7g@mail.gmail.com/T/#u
I was going to do a helper function, and put it in the queue for the next merge window (as it isn't really a bug, just gcc complaining a little more aggressively). But since you already did the patch, I'll use yours. But I have some nits about it below.
Add here:
Quoted 13 lines
Signed-off-by Miguel Ojeda <[email protected]>--- kernel/trace/trace.c | 7 +------ kernel/trace/trace.h | 14 ++++++++++++++ kernel/trace/trace_kdb.c | 7 +------ 3 files changed, 16 insertions(+), 12 deletions(-)kernel/trace/trace.c +0
-0
kernel/trace/trace.c void ftrace_dump(enum ftrace_dump_mode · line 8634
Quoted 8 lines
kernel/trace/trace.c +0
-5@@ -8627,12 +8627,7 @@ void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) cnt++; -/* reset all but tr, trace, and overruns */ -memset (&iter.seq,0 , -sizeof (struct trace_iterator ) - - offsetof(struct trace_iterator , seq)); - iter.iter_flags |=TRACE_FILE_LAT_FMT ;
Setting the LAT_FMT isn't something a function called "reset" should do.
Quoted 21 lines
kernel/trace/trace.c +1
-1- iter.pos =-1 ; +trace_iterator_reset (&iter);if (trace_find_next_entry_inc (&iter) !=NULL ) {int ret;kernel/trace/trace.h +4
-0@@ -1964,4 +1964,18 @@ static inline void tracer_hardirqs_off(unsigned long a0, unsigned long a1) { }extern struct trace_iterator *tracepoint_print_iter; +/* reset all but tr, trace, and overruns */ +static __always_inline void trace_iterator_reset(struct trace_iterator * iter) +{ +/* +* Equivalent to &iter->seq, but avoids GCC 9 complaining about
kernel/trace/trace.h +1
-0+* overwriting more members than just iter->seq (-Warray-bounds)
kernel/trace/trace.h
Quoted 2 lines
kernel/trace/trace.h +2
-0+*/ + memset((char *)(iter) + offsetof(struct trace_iterator,
Why (char *)? Please use (void *).
kernel/trace/trace.h static inline void tracer_hardirqs_off(unsigned long a0, unsigned long a1) { } · line 1976
Quoted 3 lines
seq), 0,
kernel/trace/trace.h +2
-0+sizeof (struct trace_iterator ) - + offsetof(struct trace_iterator, seq));
Make a variable for offset and reuse that (see Linus's email).
kernel/trace/trace.h static inline void tracer_hardirqs_off(unsigned long a0, unsigned long a1) { } · line 1977
Quoted 1 line
kernel/trace/trace.h +1
-0+ iter->iter_flags |=TRACE_FILE_LAT_FMT ;
Again, leave the LAT_FMT change in the other locations.
Please send a v2 version with these updates.
Thanks!
-- Steve
Quoted 22 lines
kernel/trace/trace.h +3
-0+ iter->pos = -1; +} +#endif /* _LINUX_KERNEL_TRACE_H */ kernel/trace/trace_kdb.c +1
-6@@ -41,12 +41,7 @@ static void ftrace_dump_buf(int skip_lines, long cpu_file) kdb_printf("Dumping ftrace buffer:\n"); -/* reset all but tr, trace, and overruns */ -memset (&iter.seq,0 , -sizeof (struct trace_iterator ) - - offsetof(struct trace_iterator , seq)); - iter.iter_flags |=TRACE_FILE_LAT_FMT ; - iter.pos =-1 ; +trace_iterator_reset (&iter);if (cpu_file ==RING_BUFFER_ALL_CPUS ) {for_each_tracing_cpu (cpu) {
kernel/trace/trace.h static inline void tracer_hardirqs_off(unsigned long a0, unsigned long a1) { } · line 1976
Quoted 4 lines · Miguel Ojeda
kernel/trace/trace.h +3
-0+memset ((char *)(iter) + offsetof(struct trace_iterator , seq),0 , +sizeof (struct trace_iterator ) - + offsetof(struct trace_iterator , seq));
Honestly, the above is nasty.
Whenever you have to split an expression or statement over several lines, you should ask yourself why it's so complicated.
This can be trivially rewritten as
const unsigned int offset = offsetof(struct trace_iterator, seq);
memset(offset + (void *)iter, 0, sizeof(*iter) - offset);and now we have two much simpler expressions that are each much easier to read and don't need multiple lines.
In particular using that "offset" variable means that you can trivially see that "oh, we're starting the memset at that offset, and we're using the 'sizeof() - offset' thing to limit the size". It's a lot clearer to use the same trivial 'offset' thing in both the offset and the size, than to have a more complex expression that it duplicated twice and is not at all as visually obvious that it's the exact same thing simply because it's bigger and more complex.
Also, the while 'offset' is a variable, any compiler will immediately see that it's a constant value, so it's not like this will affect the generated code at all. Unless you compile with something crazy like '-O0', which is not a supported configuration exactly because we expect compilers to not be terminally stupid.
So you get simpler and clearer source, with no downsides.
Linus
Hi Steven,
Quoted 6 lines · Steven Rostedt
Hi Miguel,
Linus mentioned this too.
https://lore.kernel.org/lkml/CAHk-=wihYB8w__YQjgYjYZsVniu5CtkTcFycmCGdqVg8GUje7g@mail.gmail.com/T/#u
Ah, I didn't see that. We were discussing here [1] backporting to 4.19 some cleanups we did for GCC 9 a few months ago, so I was testing that Linus' master was still clean a few hours ago. When I found that couple of warnings I quickly made a patch before I forgot. I should probably have added RFC :-)
[1] https://lore.kernel.org/lkml/CANiq72=fsL5m2_e+bNovFCHy3=YVf53EKGtGE_sWvsAD=ONHuQ@mail.gmail.com/
Quoted 1 line
Setting the
LAT_FMTisn't something a function called "reset" should do.
I added the surrounding code heuristically since it was in a single block with the comment above and since it was repeated in both places, but I had no idea on the semantics. :-)
Quoted 3 lines
Quoted 1 line
+ memset((char *)(iter) + offsetof(struct
trace_iterator,Why (char *)? Please use (void *).
We are adding a byte-sized offset, so char * is the one that makes sense. Doing it with void * works, although it is a GNU extension. But as you prefer :-)
Cheers,
Miguel
Quoted 4 lines · Linus Torvalds
Quoted 1 line · Miguel Ojeda
Quoted 8 lines
kernel/trace/trace.h static inline void tracer_hardirqs_off(unsigned long a0, unsigned long a1) { } · line 1976
Quoted 3 lines
kernel/trace/trace.h +3
-0+memset ((char *)(iter) + offsetof(struct trace_iterator , seq),0 , +sizeof (struct trace_iterator ) - + offsetof(struct trace_iterator , seq));Honestly, the above is nasty.
Whenever you have to split an expression or statement over several lines, you should ask yourself why it's so complicated.
Will do -- I was trying to keep the code as closely to the original as possible (I simply replaced the &iter.seq expression :-)
By the way, how do you all feel about moving this as a generic facility to zero out the suffix/prefix of an structure? In particular, since we won't have the LAT* stuff according to Steven.
Quoted 3 lines
Also, the while 'offset' is a variable, any compiler will immediately see that it's a constant value, so it's not like this will affect the generated code at all.
I like C++'s constexpr (for variable defs), maybe one day we will get it on C; it is useful to cleanly annotate compile-time values like this.
Quoted 3 lines
Unless you compile with something crazy like
'-O0', which is not a supported configuration exactly because we expect compilers to not be terminally stupid.
Fun fact: it seems clang folds some of these even under -O0. In godbolt I see it folding the third argument completely. The first one isn't, but it is computed on the function prologue, leaving the 'offset' variable unused.
Cheers,
Miguel
Quoted 3 lines · Miguel Ojeda
./include/linux/string.h:344:9: warning: '__builtin_memset' offset [8505, 8560] from the object at 'iter' is out of the bounds of
By the way, I noticed these offsets of the new warning seem to be off by 1, reported here:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90525
Cheers,
Miguel
Steven Rostedt
maintainerQuoted 3 lines · On Fri, 17 May 2019 21:09:21 +0200 Miguel Ojeda…
By the way, how do you all feel about moving this as a generic facility to zero out the suffix/prefix of an structure? In particular, since we won't have the LAT* stuff according to Steven.
Is this done in other places? If so, how many.
Note, the LAT* update doesn't belong in the iterator reset function, but the pos = -1 still does.
Thanks!
-- Steve