Skip to contentSkip to conversation

perf dso: prevent null dsos from being added and add comparator checks

PATCH In review Anubhav Shelat 2 participants 5 messages Mar 30 – Mar 31 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf dso: prevent null dsos from being added and add comparator checks

Currently comparator functions in dsos.c don't check if the dsos array entry contains a null value. This can lead to undefined behavior when doing the comparison. So add null checks to comparator functions. Also prevent __dsos__add from adding a null dso to the dso array.

---
 tools/perf/util/dsos.c | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)
tools/perf/util/dsos.c +22 -2
index 0a7645c7fae7..fcfed7dd37ff 100644
@@ -118,7 +118,12 @@ static int __dso__cmp_long_name(const char *long_name, const struct dso_id *id,
 static int __dso__cmp_short_name(const char *short_name, const struct dso_id *id,
 				 const struct dso *b)
 {
-	int rc = strcmp(short_name, dso__short_name(b));
+	int rc;
+
+	if (!b)
+		return -1;
+
+	rc = strcmp(short_name, dso__short_name(b));
 	return rc ?: dso_id__cmp(id, dso__id_const(b));
 }
 
@@ -126,7 +131,16 @@ static int dsos__cmp_long_name_id_short_name(const void *va, const void *vb)
 {
 	const struct dso *a = *((const struct dso **)va);
 	const struct dso *b = *((const struct dso **)vb);
-	int rc = strcmp(dso__long_name(a), dso__long_name(b));
+	int rc;
+
+	if (!a && !b)
+		return 0;
+	if (!a)
+		return -1;
+	if (!b)
+		return 1;
+
+	rc = strcmp(dso__long_name(a), dso__long_name(b));
 
 	if (!rc) {
 		rc = dso_id__cmp(dso__id_const(a), dso__id_const(b));
@@ -146,6 +160,9 @@ static int dsos__cmp_key_long_name_id(const void *vkey, const void *vdso)
 	const struct dsos__key *key = vkey;
 	const struct dso *dso = *((const struct dso **)vdso);
 
+	if (!dso)
+		return -1;
+
 	return __dso__cmp_long_name(key->long_name, key->id, dso);
 }
 
@@ -196,6 +213,9 @@ static struct dso *__dsos__find_by_longname_id(struct dsos *dsos,
 
 int __dsos__add(struct dsos *dsos, struct dso *dso)
 {
+	if (!dso)
+		return -EINVAL;
+
 	if (dsos->cnt == dsos->allocated) {
 		unsigned int to_allocate = 2;
 		struct dso **temp;
Signature · 1 line
2.52.0
reviewer
#1 lore raw
Quoted 5 lines · Anubhav Shelat

Currently comparator functions in dsos.c don't check if the dsos array entry contains a null value. This can lead to undefined behavior when doing the comparison. So add null checks to comparator functions. Also prevent __dsos__add from adding a null dso to the dso array.

Do you have a reproduction for this? The entries in the dsos array should never be null, so the check is redundant. If this is out-of-memory related the check in __dsos__add should suffice.

Thanks,
Ian

Quoted 64 lines
---
 tools/perf/util/dsos.c | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)
tools/perf/util/dsos.c +22 -3
index 0a7645c7fae7..fcfed7dd37ff 100644
@@ -118,7 +118,12 @@ static int __dso__cmp_long_name(const char *long_name, const struct dso_id *id,
 static int __dso__cmp_short_name(const char *short_name, const struct dso_id *id,
                                 const struct dso *b)
 {
-       int rc = strcmp(short_name, dso__short_name(b));
+       int rc;
+
+       if (!b)
+               return -1;
+
+       rc = strcmp(short_name, dso__short_name(b));
        return rc ?: dso_id__cmp(id, dso__id_const(b));
 }
 
@@ -126,7 +131,16 @@ static int dsos__cmp_long_name_id_short_name(const void *va, const void *vb)
 {
        const struct dso *a = *((const struct dso **)va);
        const struct dso *b = *((const struct dso **)vb);
-       int rc = strcmp(dso__long_name(a), dso__long_name(b));
+       int rc;
+
+       if (!a && !b)
+               return 0;
+       if (!a)
+               return -1;
+       if (!b)
+               return 1;
+
+       rc = strcmp(dso__long_name(a), dso__long_name(b));
 
        if (!rc) {
                rc = dso_id__cmp(dso__id_const(a), dso__id_const(b));
@@ -146,6 +160,9 @@ static int dsos__cmp_key_long_name_id(const void *vkey, const void *vdso)
        const struct dsos__key *key = vkey;
        const struct dso *dso = *((const struct dso **)vdso);
 
+       if (!dso)
+               return -1;
+
        return __dso__cmp_long_name(key->long_name, key->id, dso);
 }
 
@@ -196,6 +213,9 @@ static struct dso *__dsos__find_by_longname_id(struct dsos *dsos,
 
 int __dsos__add(struct dsos *dsos, struct dso *dso)
 {
+       if (!dso)
+               return -EINVAL;
+
        if (dsos->cnt == dsos->allocated) {
                unsigned int to_allocate = 2;
                struct dso **temp;
--

2.52.0

#2 lore raw
Quoted 4 lines · Ian Rogers

Do you have a reproduction for this? The entries in the dsos array should never be null, so the check is redundant. If this is out-of-memory related the check in __dsos__add should suffice.

We don't have reproduction, but we got a coredump with the system crashing in dsos__cmp_long_name_id_short_name. This is the stack trace of the culprit thread:
Stack trace of thread 12717:

            #0  0x0000aaaad2905cb0 n/a (perf + 0x1e5cb0)

            #1  0x0000ffffa7090b38 msort_with_tmp.part.0 (libc.so.6 +

0x3eb38)

            #2  0x0000ffffa70909f0 msort_with_tmp.part.0 (libc.so.6 +

0x3e9f0)

            #3  0x0000ffffa70909f0 msort_with_tmp.part.0 (libc.so.6 +

0x3e9f0)

            #4  0x0000ffffa70909f0 msort_with_tmp.part.0 (libc.so.6 +

0x3e9f0)

            #5  0x0000ffffa70909f0 msort_with_tmp.part.0 (libc.so.6 +

0x3e9f0)

            #6  0x0000ffffa70909f0 msort_with_tmp.part.0 (libc.so.6 +

0x3e9f0)

            #7  0x0000ffffa70909f0 msort_with_tmp.part.0 (libc.so.6 +

0x3e9f0)

            #8  0x0000ffffa7090dc8 qsort_r (libc.so.6 + 0x3edc8)

            #9  0x0000aaaad2905e28 n/a (perf + 0x1e5e28)

            #10 0x0000aaaad2905eac n/a (perf + 0x1e5eac)

            #11 0x0000aaaad2906764 dsos__find (perf + 0x1e6764)

            #12 0x0000aaaad2928438 map__new (perf + 0x208438)

            #13 0x0000aaaad292765c machine__process_mmap2_event (perf

+ 0x20765c)

            #14 0x0000aaaad2973b94 perf_tool__process_synth_event

(perf + 0x253b94)

            #15 0x0000aaaad2974874 perf_event__synthesize_mmap_events

(perf + 0x254874)

            #16 0x0000aaaad2975200 n/a (perf + 0x255200)

            #17 0x0000aaaad2975428 n/a (perf + 0x255428)

            #18 0x0000aaaad2975504 n/a (perf + 0x255504)

            #19 0x0000ffffa70d2e00 start_thread (libc.so.6 + 0x80e00)

            #20 0x0000ffffa713d49c thread_start (libc.so.6 + 0xeb49c)

It looks out-of-memory related and I agree the comparator checks aren't strictly necessary, but I think keeping them would be a good redundancy against some future issue that would cause a similar crash.

Anubhav

reviewer
#3 lore raw
Quoted 64 lines · Anubhav Shelat
Quoted 4 lines · Ian Rogers

Do you have a reproduction for this? The entries in the dsos array should never be null, so the check is redundant. If this is out-of-memory related the check in __dsos__add should suffice.

We don't have reproduction, but we got a coredump with the system crashing in dsos__cmp_long_name_id_short_name. This is the stack trace of the culprit thread:
Stack trace of thread 12717:

            #0  0x0000aaaad2905cb0 n/a (perf + 0x1e5cb0)

            #1  0x0000ffffa7090b38 msort_with_tmp.part.0 (libc.so.6 +

0x3eb38)

            #2  0x0000ffffa70909f0 msort_with_tmp.part.0 (libc.so.6 +

0x3e9f0)

            #3  0x0000ffffa70909f0 msort_with_tmp.part.0 (libc.so.6 +

0x3e9f0)

            #4  0x0000ffffa70909f0 msort_with_tmp.part.0 (libc.so.6 +

0x3e9f0)

            #5  0x0000ffffa70909f0 msort_with_tmp.part.0 (libc.so.6 +

0x3e9f0)

            #6  0x0000ffffa70909f0 msort_with_tmp.part.0 (libc.so.6 +

0x3e9f0)

            #7  0x0000ffffa70909f0 msort_with_tmp.part.0 (libc.so.6 +

0x3e9f0)

            #8  0x0000ffffa7090dc8 qsort_r (libc.so.6 + 0x3edc8)

            #9  0x0000aaaad2905e28 n/a (perf + 0x1e5e28)

            #10 0x0000aaaad2905eac n/a (perf + 0x1e5eac)

            #11 0x0000aaaad2906764 dsos__find (perf + 0x1e6764)

            #12 0x0000aaaad2928438 map__new (perf + 0x208438)

            #13 0x0000aaaad292765c machine__process_mmap2_event (perf

+ 0x20765c)

            #14 0x0000aaaad2973b94 perf_tool__process_synth_event

(perf + 0x253b94)

            #15 0x0000aaaad2974874 perf_event__synthesize_mmap_events

(perf + 0x254874)

            #16 0x0000aaaad2975200 n/a (perf + 0x255200)

            #17 0x0000aaaad2975428 n/a (perf + 0x255428)

            #18 0x0000aaaad2975504 n/a (perf + 0x255504)

            #19 0x0000ffffa70d2e00 start_thread (libc.so.6 + 0x80e00)

            #20 0x0000ffffa713d49c thread_start (libc.so.6 + 0xeb49c)
Quoted 3 lines

It looks out-of-memory related and I agree the comparator checks aren't strictly necessary, but I think keeping them would be a good redundancy against some future issue that would cause a similar crash.

Thanks Anubhav! I think adding null checks on elements in the array could cause null checks everywhere. How about we keep:

```
 int __dsos__add(struct dsos *dsos, struct dso *dso)
 {
+       if (!dso)
+               return -EINVAL;
```

and drop the others? This clarifies the invariant on the array clear, avoids bugs and eliminates the need for additional checks throughout the code.

Thanks,
Ian

Quoted 2 lines

Anubhav

#4 lore raw
Quoted 11 lines · Ian Rogers

Thanks Anubhav! I think adding null checks on elements in the array could cause null checks everywhere. How about we keep:
```
int __dsos__add(struct dsos *dsos, struct dso *dso)
{

tools/perf/util/dsos.c +2 -0
+       if (!dso)
+               return -EINVAL;

```
and drop the others? This clarifies the invariant on the array clear, avoids bugs and eliminates the need for additional checks throughout the code.

Sure, sounds good!
Thanks,
Anubhav