Skip to contentSkip to conversation

perf data convert: Fix segfault when converting to json on arm64

PATCH Superseded Ilkka Koskinen 3 participants 4 messages Jan 11, 2024 – Jan 17, 2024 perf events

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf data convert: Fix segfault when converting to json on arm64

Arm64 doesn't have Model in /proc/cpuinfo and, thus, cpu_desc doesn't get assigned.

Running

	$ perf data convert --to-json perf.data.json

ends up calling output_json_string() with NULL pointer, which causes a segmentation fault.

---
 tools/perf/util/data-convert-json.c | 5 +++++
 1 file changed, 5 insertions(+)
tools/perf/util/data-convert-json.c +5 -0
index 5bb3c2ba95ca..5d6de1cef546 100644
@@ -97,6 +97,11 @@ static void output_json_format(FILE *out, bool comma, int depth, const char *for
 static void output_json_key_string(FILE *out, bool comma, int depth,
 		const char *key, const char *value)
 {
+	if (!value) {
+		pr_info("No value set for key %s\n", key);
+		return;
+	}
+
 	output_json_delimiters(out, comma, depth);
 	output_json_string(out, key);
 	fputs(": ", out);
Signature · 1 line
2.43.0
#1 lore raw
Quoted 20 lines · Ilkka Koskinen

Arm64 doesn't have Model in /proc/cpuinfo and, thus, cpu_desc doesn't get assigned.

Running

	$ perf data convert --to-json perf.data.json

ends up calling output_json_string() with NULL pointer, which causes a segmentation fault.

---
 tools/perf/util/data-convert-json.c | 5 +++++
 1 file changed, 5 insertions(+)
tools/perf/util/data-convert-json.c +0 -0
index 5bb3c2ba95ca..5d6de1cef546 100644
@@ -97,6 +97,11 @@ static void output_json_format(FILE *out, bool comma, int depth, const char *for
 static void output_json_key_string(FILE *out, bool comma, int depth,

tools/perf/util/data-convert-json.c static void output_json_format(FILE *out, bool comma, int depth, const char *for · line 107

Quoted 10 lines
tools/perf/util/data-convert-json.c +5 -0
 		const char *key, const char *value)
 {
+	if (!value) {
+		pr_info("No value set for key %s\n", key);
+		return;
+	}
+
 	output_json_delimiters(out, comma, depth);
 	output_json_string(out, key);
 	fputs(": ", out);

It looks like this would hide new errors on any of the other fields that output_json_key_string() is called on. Maybe it would be better to only wrap the call to output cpu_desc with the if? If that's the only one that we think is optional, and even better only do it for arm64.

I mention this because the test for 'perf data convert' only checks for valid json syntax, but not any fields. So we might want to avoid others going missing.

Thanks
James

maintainer
#2 lore raw

Hello,

Quoted 41 lines · James Clark
Quoted 20 lines · Ilkka Koskinen

Arm64 doesn't have Model in /proc/cpuinfo and, thus, cpu_desc doesn't get assigned.

Running

      $ perf data convert --to-json perf.data.json

ends up calling output_json_string() with NULL pointer, which causes a segmentation fault.

---
 tools/perf/util/data-convert-json.c | 5 +++++
 1 file changed, 5 insertions(+)
tools/perf/util/data-convert-json.c +0 -0
index 5bb3c2ba95ca..5d6de1cef546 100644
@@ -97,6 +97,11 @@ static void output_json_format(FILE *out, bool comma, int depth, const char *for
 static void output_json_key_string(FILE *out, bool comma, int depth,

tools/perf/util/data-convert-json.c static void output_json_format(FILE *out, bool comma, int depth, const char *for · line 107

Quoted 10 lines
tools/perf/util/data-convert-json.c +5 -0
              const char *key, const char *value)
 {
+     if (!value) {
+             pr_info("No value set for key %s\n", key);
+             return;
+     }
+
      output_json_delimiters(out, comma, depth);
      output_json_string(out, key);
      fputs(": ", out);

It looks like this would hide new errors on any of the other fields that output_json_key_string() is called on. Maybe it would be better to only wrap the call to output cpu_desc with the if? If that's the only one that we think is optional, and even better only do it for arm64.

Quoted 3 lines

I mention this because the test for 'perf data convert' only checks for valid json syntax, but not any fields. So we might want to avoid others going missing.

Makes sense. Ilkka, can you send v2 with this?

Thanks,
Namhyung

#3 lore raw
Quoted 44 lines · Namhyung Kim

Hello,

Quoted 41 lines · James Clark
Quoted 20 lines · Ilkka Koskinen

Arm64 doesn't have Model in /proc/cpuinfo and, thus, cpu_desc doesn't get assigned.

Running

      $ perf data convert --to-json perf.data.json

ends up calling output_json_string() with NULL pointer, which causes a segmentation fault.

---
 tools/perf/util/data-convert-json.c | 5 +++++
 1 file changed, 5 insertions(+)
tools/perf/util/data-convert-json.c +0 -0
index 5bb3c2ba95ca..5d6de1cef546 100644
@@ -97,6 +97,11 @@ static void output_json_format(FILE *out, bool comma, int depth, const char *for
 static void output_json_key_string(FILE *out, bool comma, int depth,

tools/perf/util/data-convert-json.c static void output_json_format(FILE *out, bool comma, int depth, const char *for · line 107

Quoted 10 lines
tools/perf/util/data-convert-json.c +5 -0
              const char *key, const char *value)
 {
+     if (!value) {
+             pr_info("No value set for key %s\n", key);
+             return;
+     }
+
      output_json_delimiters(out, comma, depth);
      output_json_string(out, key);
      fputs(": ", out);

It looks like this would hide new errors on any of the other fields that output_json_key_string() is called on. Maybe it would be better to only wrap the call to output cpu_desc with the if? If that's the only one that we think is optional, and even better only do it for arm64.

Quoted 5 lines
Quoted 3 lines

I mention this because the test for 'perf data convert' only checks for valid json syntax, but not any fields. So we might want to avoid others going missing.

Makes sense. Ilkka, can you send v2 with this?

I initially considered the choice James suggested but I kind of thought that pr_info() might be enough. However, I don't have strong prefence on either. I'll send an updated version soon.

Cheers, Ilkka

Quoted 4 lines

Thanks,
Namhyung