diff mbox series

[1/8] kernel-shark: Add more sanity checks for model misbehavior detection

Message ID 20190213161216.14438-2-ykaradzhov@vmware.com (mailing list archive)
State Accepted
Headers show
Series Various modifications toward KS 1.0 | expand

Commit Message

Yordan Karadzhov Feb. 13, 2019, 4:12 p.m. UTC
I found that those checks are very useful for early detection of
misbehavior (bugs) of the visualization model. In particular those
checks helped me a lot when developing the multi-stream  branch of
KernelShark (future version 2.0).

Signed-off-by: Yordan Karadzhov <ykaradzhov@vmware.com>
---
 kernel-shark/src/libkshark-model.c | 28 +++++++++++++++++++---------
 kernel-shark/src/libkshark.c       |  7 ++++++-
 2 files changed, 25 insertions(+), 10 deletions(-)
diff mbox series

Patch

diff --git a/kernel-shark/src/libkshark-model.c b/kernel-shark/src/libkshark-model.c
index 2094795..a4041c3 100644
--- a/kernel-shark/src/libkshark-model.c
+++ b/kernel-shark/src/libkshark-model.c
@@ -299,6 +299,7 @@  static void ksmodel_set_next_bin_edge(struct kshark_trace_histo *histo,
 static void ksmodel_set_bin_counts(struct kshark_trace_histo *histo)
 {
 	int i = 0, prev_not_empty;
+	ssize_t count_tmp;
 
 	histo->tot_count = 0;
 	memset(&histo->bin_count[0], 0,
@@ -329,12 +330,18 @@  static void ksmodel_set_bin_counts(struct kshark_trace_histo *histo)
 			 * empty bin, which will give us the number of data
 			 * rows in the "prev_not_empty" bin.
 			 */
-			histo->bin_count[prev_not_empty] =
-				histo->map[i] - histo->map[prev_not_empty];
+			count_tmp = histo->map[i] - histo->map[prev_not_empty];
+
+			/*
+			 * We will do a sanity check. The number of data rows
+			 * in the previous not empty bin must be greater than
+			 * zero.
+			 */
+			assert(count_tmp > 0);
+			histo->bin_count[prev_not_empty] = count_tmp;
 
 			if (prev_not_empty != LOB(histo))
-				histo->tot_count +=
-					histo->bin_count[prev_not_empty];
+				histo->tot_count += count_tmp;
 
 			prev_not_empty = i;
 		}
@@ -346,19 +353,22 @@  static void ksmodel_set_bin_counts(struct kshark_trace_histo *histo)
 		 * The Upper Overflow bin is empty. Use the size of the dataset
 		 * to calculate the content of the previouse not empty bin.
 		 */
-		histo->bin_count[prev_not_empty] = histo->data_size -
-						   histo->map[prev_not_empty];
+		count_tmp = histo->data_size - histo->map[prev_not_empty];
 	} else {
 		/*
 		 * Use the index of the first entry inside the Upper Overflow
 		 * bin to calculate the content of the previouse not empty
 		 * bin.
 		 */
-		histo->bin_count[prev_not_empty] = histo->map[UOB(histo)] -
-						   histo->map[prev_not_empty];
+		count_tmp = histo->map[UOB(histo)] - histo->map[prev_not_empty];
 	}
 
-	histo->tot_count += histo->bin_count[prev_not_empty];
+	/*
+	 * We will do a sanity check. The number of data rows in the last not
+	 * empty bin must be greater than zero.
+	 */
+	assert(count_tmp > 0);
+	histo->tot_count += histo->bin_count[prev_not_empty] = count_tmp;
 }
 
 /**
diff --git a/kernel-shark/src/libkshark.c b/kernel-shark/src/libkshark.c
index 5033e47..9a41945 100644
--- a/kernel-shark/src/libkshark.c
+++ b/kernel-shark/src/libkshark.c
@@ -1552,7 +1552,7 @@  const struct kshark_entry dummy_entry = {
 static const struct kshark_entry *
 get_entry(const struct kshark_entry_request *req,
           struct kshark_entry **data,
-          ssize_t *index, size_t start, ssize_t end, int inc)
+          ssize_t *index, ssize_t start, ssize_t end, int inc)
 {
 	struct kshark_context *kshark_ctx = NULL;
 	const struct kshark_entry *e = NULL;
@@ -1564,6 +1564,11 @@  get_entry(const struct kshark_entry_request *req,
 	if (!kshark_instance(&kshark_ctx))
 		return e;
 
+	/*
+	 * We will do a sanity check in order to protect against infinite
+	 * loops.
+	 */
+	assert((inc > 0 && start < end) || (inc < 0 && start > end));
 	for (i = start; i != end; i += inc) {
 		if (req->cond(kshark_ctx, data[i], req->val)) {
 			/*