Skip to contentSkip to conversation

libtraceeval: Add size checks to insert and query functions

RFC PATCH In review Steven Rostedt 1 participant 4 messages Aug 16, 2023

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 libtraceeval: Add size checks to insert and query functions

From: "Steven Rostedt (Google)" <[email protected]>

Currently, there's nothing that checks the size of the keys/values being passed into traceeval_query() and traceeval_insert(). Make functions that take the size of those arrays:

traceeval_query_size()
traceeval_insert_size()

and convert the traceeval_query() and traceeval_insert() into macros that calculate the size of the fields to pass in:

  #define traceeval_query(teval, keys, results) \
     traceeval_query_size(teval, sizeof(keys) / sizeof(keys[0]), results)

and this will allow the code to check to make sure the size matches what is expected (note, that currently is not done, but can be added).

If the keys or vals is not an array, but instead a pointer, if one of the macros is used, the compiler will complain with:

warning: division sizeof (const struct traceeval_data *) / sizeof (const struct traceeval_data)
does not compute the number of array elements [-Wsizeof-pointer-div]
190 | sizeof(keys) / sizeof(keys[0]), results);

Then the user would need to call the "_size()" function directly (as is done in the sample code, where the keys received by an iterator is
used for a query)

---

This is an RFC patch. I wonder if we should have this to make the code more robust?

 include/traceeval-hist.h | 18 +++++++++++++-----
 samples/task-eval.c      |  2 +-
 src/histograms.c         | 10 +++++-----
 3 files changed, 19 insertions(+), 11 deletions(-)
include/traceeval-hist.h +13 -5
index d0bee7c02aa0..c8c27a51c62b 100644
@@ -171,15 +171,23 @@ struct traceeval *traceeval_init(struct traceeval_type *keys,
 
 void traceeval_release(struct traceeval *teval);
 
-int traceeval_insert(struct traceeval *teval,
-		     const struct traceeval_data *keys,
-		     const struct traceeval_data *vals);
+int traceeval_insert_size(struct traceeval *teval,
+			  const struct traceeval_data *keys, size_t nr_keys,
+			  const struct traceeval_data *vals, size_t nr_vals);
+
+#define traceeval_insert(teval, keys, vals)				\
+	traceeval_insert_size(teval, keys, sizeof(keys) / sizeof(keys[0]), \
+			      vals, sizeof(vals) / sizeof(vals[0]))
 
 int traceeval_remove(struct traceeval *teval,
 		     const struct traceeval_data *keys);
 
-int traceeval_query(struct traceeval *teval, const struct traceeval_data *keys,
-		    const struct traceeval_data **results);
+int traceeval_query_size(struct traceeval *teval, const struct traceeval_data *keys,
+			 size_t nr_keys, const struct traceeval_data **results);
+
+#define traceeval_query(teval, keys, results)				\
+	traceeval_query_size(teval, keys,				\
+			     sizeof(keys) / sizeof(keys[0]), results);
 
 void traceeval_results_release(struct traceeval *teval,
 			       const struct traceeval_data *results);
samples/task-eval.c +1 -1
index c2950b2d64b0..4eaab7ad6e15 100644
@@ -814,7 +814,7 @@ static void display_processes(struct traceeval *teval,
 		struct process_data *pdata = NULL;
 		const char *comm = keys[0].cstring;
 
-		ret = traceeval_query(teval_data, keys, &results);
+		ret = traceeval_query(teval_data, keys, 2, &results);
 		if (ret < 1)
 			continue; /* ?? */
 
src/histograms.c +5 -5
index a7d0643cfbbd..f71a2d557e34 100644
@@ -669,8 +669,8 @@ fail:
  *
  * Returns 1 if found, 0 if not found, and -1 on error.
  */
-int traceeval_query(struct traceeval *teval, const struct traceeval_data *keys,
-		    const struct traceeval_data **results)
+int traceeval_query_size(struct traceeval *teval, const struct traceeval_data *keys,
+			 size_t nr_keys, const struct traceeval_data **results)
 {
 	struct entry *entry;
 	int check;
@@ -909,9 +909,9 @@ unsigned long long traceeval_stat_count(struct traceeval_stat *stat)
  *
  * Returns 0 on success, and -1 on error.
  */
-int traceeval_insert(struct traceeval *teval,
-		     const struct traceeval_data *keys,
-		     const struct traceeval_data *vals)
+int traceeval_insert_size(struct traceeval *teval,
+			  const struct traceeval_data *keys, size_t nr_keys,
+			  const struct traceeval_data *vals, size_t nr_vals)
 {
 	struct entry *entry;
 	int check;
Signature · 1 line
2.40.1
#1 lore raw

samples/task-eval.c static void display_processes(struct traceeval *teval, · line 820

Quoted 11 lines · On Tue, 15 Aug 2023 20:02:34 -0400 Steven Rostedt <[email protected]> wrote:
samples/task-eval.c +1 -1
@@ -814,7 +814,7 @@ static void display_processes(struct traceeval *teval,
 		struct process_data *pdata = NULL;
 		const char *comm = keys[0].cstring;
 
-		ret = traceeval_query(teval_data, keys, &results);
+		ret = traceeval_query(teval_data, keys, 2, &results);
 		if (ret < 1)
 			continue; /* ?? */
 

That should be:

		traceeval_query_size(...)

I removed it to get the warning to copy into the change log, and ended up not putting it back :-p

-- Steve

#2 lore raw
In reply to Steven Rostedt
Quoted 5 lines · On Tue, 15 Aug 2023 20:02:34 -0400 Steven Rostedt <[email protected]> wrote:
include/traceeval-hist.h +0 -1
@@ -171,15 +171,23 @@ struct traceeval *traceeval_init(struct traceeval_type *keys,
 
 void traceeval_release(struct traceeval *teval);
 
-int traceeval_insert(struct traceeval *teval,

include/traceeval-hist.h struct traceeval *traceeval_init(struct traceeval_type *keys, · line 181

Quoted 10 lines
include/traceeval-hist.h +7 -2
-		     const struct traceeval_data *keys,
-		     const struct traceeval_data *vals);
+int traceeval_insert_size(struct traceeval *teval,
+			  const struct traceeval_data *keys, size_t nr_keys,
+			  const struct traceeval_data *vals, size_t nr_vals);
+
+#define traceeval_insert(teval, keys, vals)				\
+	traceeval_insert_size(teval, keys, sizeof(keys) / sizeof(keys[0]), \
+			      vals, sizeof(vals) / sizeof(vals[0]))
 

I just realized that vals may be NULL, so the above needs to be:

#define traceeval_insert(teval, keys, vals)				\
	traceeval_insert_size(teval, keys, sizeof(keys) / sizeof(keys[0]), \
			      vals, vals ? sizeof(vals) / sizeof(vals[0]) : 0)

-- Steve

#3 lore raw
Quoted 10 lines · On Tue, 15 Aug 2023 21:10:52 -0400 Steven Rostedt <[email protected]> wrote:

include/traceeval-hist.h struct traceeval *traceeval_init(struct traceeval_type *keys, · line 180

Quoted 4 lines
include/traceeval-hist.h +3 -0
+#define traceeval_insert(teval, keys, vals)				\
+	traceeval_insert_size(teval, keys, sizeof(keys) / sizeof(keys[0]), \
+			      vals, sizeof(vals) / sizeof(vals[0]))

I just realized that vals may be NULL, so the above needs to be:

#define traceeval_insert(teval, keys, vals)				\
	traceeval_insert_size(teval, keys, sizeof(keys) / sizeof(keys[0]), \
			      vals, vals ? sizeof(vals) / sizeof(vals[0]) : 0)

Nevermind. NULL won't compile (and neither will the above).

-- Steve