tracing/histogram: Don't use strlen to find length of stacktrace variables
authorTom Zanussi <zanussi@kernel.org>
Fri, 10 Feb 2023 21:33:03 +0000 (15:33 -0600)
committerSteven Rostedt (Google) <rostedt@goodmis.org>
Thu, 16 Feb 2023 00:59:09 +0000 (19:59 -0500)
Because stacktraces are saved in dynamic strings,
trace_event_raw_event_synth() uses strlen to determine the length of
the stack.  Stacktraces may contain 0-bytes, though, in the saved
addresses, so the length found and passed to reserve() will be too
small.

Fix this by using the first unsigned long in the stack variables to
store the actual number of elements in the stack and have
trace_event_raw_event_synth() use that to determine the length of the
stack.

Link: https://lkml.kernel.org/r/1ed6906cd9d6477ef2bd8e63c61de20a9ffe64d7.1676063532.git.zanussi@kernel.org
Signed-off-by: Tom Zanussi <zanussi@kernel.org>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
kernel/trace/trace_events_hist.c
kernel/trace/trace_events_synth.c

index 7f3e6ca..f21e42d 100644 (file)
@@ -3137,13 +3137,15 @@ static inline void __update_field_vars(struct tracing_map_elt *elt,
                                size = min(val->size, STR_VAR_LEN_MAX);
                                strscpy(str, val_str, size);
                        } else {
+                               char *stack_start = str + sizeof(unsigned long);
                                int e;
 
-                               e = stack_trace_save((void *)str,
+                               e = stack_trace_save((void *)stack_start,
                                                     HIST_STACKTRACE_DEPTH,
                                                     HIST_STACKTRACE_SKIP);
                                if (e < HIST_STACKTRACE_DEPTH - 1)
-                                       ((unsigned long *)str)[e] = 0;
+                                       ((unsigned long *)stack_start)[e] = 0;
+                               *((unsigned long *)str) = e;
                        }
                        var_val = (u64)(uintptr_t)str;
                }
@@ -5135,13 +5137,15 @@ static void hist_trigger_elt_update(struct hist_trigger_data *hist_data,
                                        size = min(hist_field->size, STR_VAR_LEN_MAX);
                                        strscpy(str, val_str, size);
                                } else {
+                                       char *stack_start = str + sizeof(unsigned long);
                                        int e;
 
-                                       e = stack_trace_save((void *)str,
+                                       e = stack_trace_save((void *)stack_start,
                                                             HIST_STACKTRACE_DEPTH,
                                                             HIST_STACKTRACE_SKIP);
                                        if (e < HIST_STACKTRACE_DEPTH - 1)
-                                               ((unsigned long *)str)[e] = 0;
+                                               ((unsigned long *)stack_start)[e] = 0;
+                                       *((unsigned long *)str) = e;
                                }
                                hist_val = (u64)(uintptr_t)str;
                        }
index 306c89e..70bddb2 100644 (file)
@@ -538,7 +538,12 @@ static notrace void trace_event_raw_event_synth(void *__data,
                val_idx = var_ref_idx[field_pos];
                str_val = (char *)(long)var_ref_vals[val_idx];
 
-               len = kern_fetch_store_strlen((unsigned long)str_val);
+               if (event->dynamic_fields[i]->is_stack) {
+                       len = *((unsigned long *)str_val);
+                       len *= sizeof(unsigned long);
+               } else {
+                       len = kern_fetch_store_strlen((unsigned long)str_val);
+               }
 
                fields_size += len;
        }