Skip to contentSkip to conversation

Fix unit tests and APIs for loading saved mappings

PATCH New Tzvetomir Stoyanov (VMware) 1 participant 4 messages Apr 12, 2021

lore
Share & tools
lore
#0 lore raw

cover Fix unit tests and APIs for loading saved mappings

Some last minute changes broke the unit tests of the library. While running the tests, noticed also the problem in the logic that loads saved command lines, kallsyms and printk formats.

Tzvetomir Stoyanov (VMware) (3):
libtracefs: Fix trace options unit test
libtracefs: Fix loading of saved maps
libtracefs: Fixed trace marker unit test

 src/tracefs-events.c  |  6 +++---
 utest/tracefs-utest.c | 25 +++++--------------------
 2 files changed, 8 insertions(+), 23 deletions(-)
Signature · 1 line
2.30.2
#1 lore raw

PATCH 1/3 libtracefs: Fix trace options unit test

Changes to tracefs library APIs were introduced by this patch set:
https://lore.kernel.org/linux-trace-devel/[email protected]/
which make the option mask immutable and controlled by the library. However, the unit test was not updated and fails.
Fixed the trace options unit test, to reflect the changes in the API.

---
 utest/tracefs-utest.c | 21 +++------------------
 1 file changed, 3 insertions(+), 18 deletions(-)
utest/tracefs-utest.c +3 -18
index 1f48e6f..04b5e81 100644
@@ -679,21 +679,11 @@ out:
 	return ret;
 }
 
-static bool check_options_mask_empty(struct tracefs_options_mask *mask)
-{
-	int i;
-
-	for (i = 1; i < TRACEFS_OPTION_MAX; i++) {
-		if (tracefs_option_mask_is_set(mask, i))
-			return false;
-	}
-	return true;
-}
-
 static void test_instance_tracing_options(struct tracefs_instance *instance)
 {
-	struct tracefs_options_mask *enabled;
-	struct tracefs_options_mask *all, *all_copy;
+	const struct tracefs_options_mask *enabled;
+	const struct tracefs_options_mask *all_copy;
+	const struct tracefs_options_mask *all;
 	enum tracefs_option_id i = 1;
 	char file[PATH_MAX];
 	const char *name;
@@ -743,11 +733,6 @@ static void test_instance_tracing_options(struct tracefs_instance *instance)
 			CU_TEST(check_option(instance, i, true, 0));
 		}
 	}
-	CU_TEST(check_options_mask_empty(all));
-	CU_TEST(check_options_mask_empty(enabled));
-
-	free(all);
-	free(enabled);
 }
 
 static void test_tracing_options(void)
Signature · 1 line
2.30.2
#2 lore raw

PATCH 2/3 libtracefs: Fix loading of saved maps

In reply to Tzvetomir Stoyanov (VMware)

The internal helper function, used for loading saved printk formats, kallsyms and command lines retuns 0 in case the given file exist, but is empty. In that case a buffer is not allocated and size 0 is returned. Logic for loading those mappings does not handle that case, which leads to memory corrupion freeing invalid memory.

---
 src/tracefs-events.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
src/tracefs-events.c +3 -3
index 94573b3..3a6196b 100644
@@ -603,7 +603,7 @@ static void load_kallsyms(struct tep_handle *tep)
 {
 	char *buf;
 
-	if (str_read_file("/proc/kallsyms", &buf, false) < 0)
+	if (str_read_file("/proc/kallsyms", &buf, false) <= 0)
 		return;
 
 	tep_parse_kallsyms(tep, buf);
@@ -623,7 +623,7 @@ static int load_saved_cmdlines(const char *tracing_dir,
 
 	ret = str_read_file(path, &buf, false);
 	free(path);
-	if (ret < 0)
+	if (ret <= 0)
 		return -1;
 
 	ret = tep_parse_saved_cmdlines(tep, buf);
@@ -645,7 +645,7 @@ static void load_printk_formats(const char *tracing_dir,
 
 	ret = str_read_file(path, &buf, false);
 	free(path);
-	if (ret < 0)
+	if (ret <= 0)
 		return;
 
 	tep_parse_printk_formats(tep, buf);
Signature · 1 line
2.30.2
#3 lore raw

PATCH 3/3 libtracefs: Fixed trace marker unit test

In reply to Tzvetomir Stoyanov (VMware)

The tracefs_printf() and tracefs_vprintf() APIs do not write the terminating NULL characted of the string in the trace marker, as it is not required by the kernel interafce. Fixed the unit test to not expect that terminating NULL while verifying the result of these APIs.

---
 utest/tracefs-utest.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
utest/tracefs-utest.c +2 -2
index 04b5e81..d675c16 100644
@@ -287,14 +287,14 @@ static void test_instance_ftrace_marker(struct tracefs_instance *instance)
 		CU_TEST(tracefs_printf(instance, "Test marker: %s 0x%X", string, data) == 0);
 	}
 	asprintf(&str, "Test marker: %s 0x%X", string, data);
-	CU_TEST(find_test_marker(instance, str, strlen(str) + 1, MARKERS_WRITE_COUNT, false));
+	CU_TEST(find_test_marker(instance, str, strlen(str), MARKERS_WRITE_COUNT, false));
 	free(str);
 
 	for (i = 0; i < MARKERS_WRITE_COUNT; i++) {
 		CU_TEST(marker_vprint(instance, "Test marker V: %s 0x%X", string, data) == 0);
 	}
 	asprintf(&str, "Test marker V: %s 0x%X", string, data);
-	CU_TEST(find_test_marker(instance, str, strlen(str) + 1, MARKERS_WRITE_COUNT, false));
+	CU_TEST(find_test_marker(instance, str, strlen(str), MARKERS_WRITE_COUNT, false));
 	free(str);
 
 	tracefs_print_close(instance);
Signature · 1 line
2.30.2