Skip to contentSkip to conversation

trace-cruncher: Fix segfault in PyTraceHist_read()

PATCH New Yordan Karadzhov (VMware) 1 participant 2 messages Dec 16, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 trace-cruncher: Fix segfault in PyTraceHist_read()

A NULL pointer can be returned when reading from the 'hist' file of the event. This may happen if the histogram is not initialized.

---
 src/ftracepy-utils.c | 3 +++
 1 file changed, 3 insertions(+)
src/ftracepy-utils.c +3 -0
index a735d88..1b1f350 100644
@@ -978,6 +978,9 @@ PyObject *PyTraceHist_read(PyTraceHist *self, PyObject *args,
 					      PyObject *kwargs)
 {
 	char *data = hist_read(self, args, kwargs);
+	if (!data)
+		return NULL;
+
 	PyObject *ret = PyUnicode_FromString(data);
 
 	free(data);
Signature · 1 line
2.32.0
#1 lore raw

PATCH 2/2 trace-cruncher: Add more histogram tests

Testing the handling of couple of error cases.

---
 .../tests/1_unit/test_01_ftracepy_unit.py      | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
tracecruncher/tests/1_unit/test_01_ftracepy_unit.py +18 -0
index 7b344e2..7e5b20e 100644
@@ -582,5 +582,23 @@ class HistTestCase(unittest.TestCase):
         self.assertTrue('Available triggers:' in h_buff)
         f.close()
 
+    def test_hist_err(self):
+        inst = ft.create_instance(instance_name)
+        hist = ft.hist(system='kmem', event='kmalloc',
+                       axes={'call_site': 'sym',
+                             'bytes_alloc': 'n'})
+        err = 'Failed read data from histogram'
+        with self.assertRaises(Exception) as context:
+            hist.read(inst)
+        self.assertTrue(err in str(context.exception))
+
+        hist.start(inst)
+        err = 'Failed to start filling the histogram'
+        with self.assertRaises(Exception) as context:
+            hist.start(inst)
+        self.assertTrue(err in str(context.exception))
+        hist.close(inst)
+
+
 if __name__ == '__main__':
     unittest.main()
Signature · 1 line
2.32.0