[PATCH v2 1/2] libtracefs: Add random number to keep synthetic variables unique

From: Steven Rostedt
Date: Thu Aug 12 2021 - 11:50:41 EST


From: "Steven Rostedt (VMware)" <rostedt@xxxxxxxxxxx>

The 'hist' triggers expect that all variables are unique. If two synthetic
events are created, it is possible that they will use the same variable
names, and this can break the logic for synthetic events. Add a random
number to the argument names that will help prevent that from happening.
There's no guarantee that there wont be collisions, but the chances of
that happening is 1 in 32768. If this is a problem, we could possibly look
for variables that are already in use.

Link: https://lore.kernel.org/linux-trace-devel/20210812005546.910833-2-rostedt@xxxxxxxxxxx/

Signed-off-by: Steven Rostedt (VMware) <rostedt@xxxxxxxxxxx>
---
src/tracefs-hist.c | 23 ++++++++++++++++++++++-
1 file changed, 22 insertions(+), 1 deletion(-)

diff --git a/src/tracefs-hist.c b/src/tracefs-hist.c
index 7f9cf3820611..9de70579a871 100644
--- a/src/tracefs-hist.c
+++ b/src/tracefs-hist.c
@@ -13,6 +13,8 @@
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
+#include <sys/time.h>
+#include <sys/types.h>

#include "tracefs.h"
#include "tracefs-local.h"
@@ -558,6 +560,7 @@ struct tracefs_synth {
unsigned int end_parens;
unsigned int end_state;
int *start_type;
+ char arg_name[16];
int arg_cnt;
};

@@ -951,13 +954,31 @@ int tracefs_synth_add_match_field(struct tracefs_synth *synth,
return -1;
}

+static unsigned int make_rand(void)
+{
+ struct timeval tv;
+ unsigned long seed;
+
+ gettimeofday(&tv, NULL);
+ seed = (tv.tv_sec + tv.tv_usec) + gettid();
+
+ /* taken from the rand(3) man page */
+ seed = seed * 1103515245 + 12345;
+ return((unsigned)(seed/65536) % 32768);
+}
+
static char *new_arg(struct tracefs_synth *synth)
{
int cnt = synth->arg_cnt + 1;
char *arg;
int ret;

- ret = asprintf(&arg, "__arg__%d", cnt);
+ /* Create a unique argument name */
+ if (!synth->arg_name[0]) {
+ /* make_rand() returns at most 32768 (total 13 bytes in use) */
+ sprintf(synth->arg_name, "__arg_%u_", make_rand());
+ }
+ ret = asprintf(&arg, "%s%d", synth->arg_name, cnt);
if (ret < 0)
return NULL;

--
2.30.2