Skip to contentSkip to conversation

perf mem2node: avoid double free related to realloc

PATCH Superseded Ian Rogers 2 participants 5 messages Mar 15, 2020 – Mar 20, 2020 perf events

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

PATCH 1/1 perf mem2node: avoid double free related to realloc

Realloc of size zero is a free not an error, avoid this causing a double free. Caught by clang's address sanitizer:

==2634==ERROR: AddressSanitizer: attempting double-free on 0x6020000015f0 in thread T0:

    #0 0x5649659297fd in free llvm/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:123:3
    #1 0x5649659e9251 in __zfree tools/lib/zalloc.c:13:2
    #2 0x564965c0f92c in mem2node__exit tools/perf/util/mem2node.c:114:2
    #3 0x564965a08b4c in perf_c2c__report tools/perf/builtin-c2c.c:2867:2
    #4 0x564965a0616a in cmd_c2c tools/perf/builtin-c2c.c:2989:10
    #5 0x564965944348 in run_builtin tools/perf/perf.c:312:11
    #6 0x564965943235 in handle_internal_command tools/perf/perf.c:364:8
    #7 0x5649659440c4 in run_argv tools/perf/perf.c:408:2
    #8 0x564965942e41 in main tools/perf/perf.c:538:3

0x6020000015f0 is located 0 bytes inside of 1-byte region [0x6020000015f0,0x6020000015f1) freed by thread T0 here:

    #0 0x564965929da3 in realloc third_party/llvm/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:164:3
    #1 0x564965c0f55e in mem2node__init tools/perf/util/mem2node.c:97:16
    #2 0x564965a08956 in perf_c2c__report tools/perf/builtin-c2c.c:2803:8
    #3 0x564965a0616a in cmd_c2c tools/perf/builtin-c2c.c:2989:10
    #4 0x564965944348 in run_builtin tools/perf/perf.c:312:11
    #5 0x564965943235 in handle_internal_command tools/perf/perf.c:364:8
    #6 0x5649659440c4 in run_argv tools/perf/perf.c:408:2
    #7 0x564965942e41 in main tools/perf/perf.c:538:3

previously allocated by thread T0 here:

    #0 0x564965929c42 in calloc third_party/llvm/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:154:3
    #1 0x5649659e9220 in zalloc tools/lib/zalloc.c:8:9
    #2 0x564965c0f32d in mem2node__init tools/perf/util/mem2node.c:61:12
    #3 0x564965a08956 in perf_c2c__report tools/perf/builtin-c2c.c:2803:8
    #4 0x564965a0616a in cmd_c2c tools/perf/builtin-c2c.c:2989:10
    #5 0x564965944348 in run_builtin tools/perf/perf.c:312:11
    #6 0x564965943235 in handle_internal_command tools/perf/perf.c:364:8
    #7 0x5649659440c4 in run_argv tools/perf/perf.c:408:2
    #8 0x564965942e41 in main tools/perf/perf.c:538:3
---
 tools/perf/util/mem2node.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/mem2node.c +1 -1
index 797d86a1ab09..7f97aa69eb65 100644
@@ -95,7 +95,7 @@ int mem2node__init(struct mem2node *map, struct perf_env *env)
 
 	/* Cut unused entries, due to merging. */
 	tmp_entries = realloc(entries, sizeof(*entries) * j);
-	if (tmp_entries)
+	if (j == 0 || tmp_entries)
 		entries = tmp_entries;
 
 	for (i = 0; i < j; i++) {
Signature · 1 line
2.25.1.481.gfbce0eb801-goog
#1 lore raw
Quoted 46 lines · Ian Rogers

Realloc of size zero is a free not an error, avoid this causing a double free. Caught by clang's address sanitizer:

==2634==ERROR: AddressSanitizer: attempting double-free on 0x6020000015f0 in thread T0:

    #0 0x5649659297fd in free llvm/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:123:3
    #1 0x5649659e9251 in __zfree tools/lib/zalloc.c:13:2
    #2 0x564965c0f92c in mem2node__exit tools/perf/util/mem2node.c:114:2
    #3 0x564965a08b4c in perf_c2c__report tools/perf/builtin-c2c.c:2867:2
    #4 0x564965a0616a in cmd_c2c tools/perf/builtin-c2c.c:2989:10
    #5 0x564965944348 in run_builtin tools/perf/perf.c:312:11
    #6 0x564965943235 in handle_internal_command tools/perf/perf.c:364:8
    #7 0x5649659440c4 in run_argv tools/perf/perf.c:408:2
    #8 0x564965942e41 in main tools/perf/perf.c:538:3

0x6020000015f0 is located 0 bytes inside of 1-byte region [0x6020000015f0,0x6020000015f1) freed by thread T0 here:

    #0 0x564965929da3 in realloc third_party/llvm/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:164:3
    #1 0x564965c0f55e in mem2node__init tools/perf/util/mem2node.c:97:16
    #2 0x564965a08956 in perf_c2c__report tools/perf/builtin-c2c.c:2803:8
    #3 0x564965a0616a in cmd_c2c tools/perf/builtin-c2c.c:2989:10
    #4 0x564965944348 in run_builtin tools/perf/perf.c:312:11
    #5 0x564965943235 in handle_internal_command tools/perf/perf.c:364:8
    #6 0x5649659440c4 in run_argv tools/perf/perf.c:408:2
    #7 0x564965942e41 in main tools/perf/perf.c:538:3

previously allocated by thread T0 here:

    #0 0x564965929c42 in calloc third_party/llvm/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:154:3
    #1 0x5649659e9220 in zalloc tools/lib/zalloc.c:8:9
    #2 0x564965c0f32d in mem2node__init tools/perf/util/mem2node.c:61:12
    #3 0x564965a08956 in perf_c2c__report tools/perf/builtin-c2c.c:2803:8
    #4 0x564965a0616a in cmd_c2c tools/perf/builtin-c2c.c:2989:10
    #5 0x564965944348 in run_builtin tools/perf/perf.c:312:11
    #6 0x564965943235 in handle_internal_command tools/perf/perf.c:364:8
    #7 0x5649659440c4 in run_argv tools/perf/perf.c:408:2
    #8 0x564965942e41 in main tools/perf/perf.c:538:3
---
 tools/perf/util/mem2node.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/mem2node.c +0 -0
index 797d86a1ab09..7f97aa69eb65 100644

tools/perf/util/mem2node.c int mem2node__init(struct mem2node *map, struct perf_env *env) · line 98

Quoted 5 lines
tools/perf/util/mem2node.c +1 -1
@@ -95,7 +95,7 @@ int mem2node__init(struct mem2node *map, struct perf_env *env)
 
 	/* Cut unused entries, due to merging. */
 	tmp_entries = realloc(entries, sizeof(*entries) * j);
-	if (tmp_entries)
+	if (j == 0 || tmp_entries)

nice catch.. I wonder if we should fail in here, or at least warn that there're no entris.. which is really strange ;-)

thanks,
jirka

Quoted 6 lines
tools/perf/util/mem2node.c +0 -0
 		entries = tmp_entries;
 
 	for (i = 0; i < j; i++) {
Signature · 1 line
2.25.1.481.gfbce0eb801-goog
author reviewer
#2 lore raw
Quoted 49 lines · Jiri Olsa
Quoted 47 lines · Ian Rogers

Realloc of size zero is a free not an error, avoid this causing a double free. Caught by clang's address sanitizer:

==2634==ERROR: AddressSanitizer: attempting double-free on 0x6020000015f0 in thread T0:

    #0 0x5649659297fd in free llvm/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:123:3
    #1 0x5649659e9251 in __zfree tools/lib/zalloc.c:13:2
    #2 0x564965c0f92c in mem2node__exit tools/perf/util/mem2node.c:114:2
    #3 0x564965a08b4c in perf_c2c__report tools/perf/builtin-c2c.c:2867:2
    #4 0x564965a0616a in cmd_c2c tools/perf/builtin-c2c.c:2989:10
    #5 0x564965944348 in run_builtin tools/perf/perf.c:312:11
    #6 0x564965943235 in handle_internal_command tools/perf/perf.c:364:8
    #7 0x5649659440c4 in run_argv tools/perf/perf.c:408:2
    #8 0x564965942e41 in main tools/perf/perf.c:538:3

0x6020000015f0 is located 0 bytes inside of 1-byte region [0x6020000015f0,0x6020000015f1) freed by thread T0 here:

    #0 0x564965929da3 in realloc third_party/llvm/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:164:3
    #1 0x564965c0f55e in mem2node__init tools/perf/util/mem2node.c:97:16
    #2 0x564965a08956 in perf_c2c__report tools/perf/builtin-c2c.c:2803:8
    #3 0x564965a0616a in cmd_c2c tools/perf/builtin-c2c.c:2989:10
    #4 0x564965944348 in run_builtin tools/perf/perf.c:312:11
    #5 0x564965943235 in handle_internal_command tools/perf/perf.c:364:8
    #6 0x5649659440c4 in run_argv tools/perf/perf.c:408:2
    #7 0x564965942e41 in main tools/perf/perf.c:538:3

previously allocated by thread T0 here:

    #0 0x564965929c42 in calloc third_party/llvm/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:154:3
    #1 0x5649659e9220 in zalloc tools/lib/zalloc.c:8:9
    #2 0x564965c0f32d in mem2node__init tools/perf/util/mem2node.c:61:12
    #3 0x564965a08956 in perf_c2c__report tools/perf/builtin-c2c.c:2803:8
    #4 0x564965a0616a in cmd_c2c tools/perf/builtin-c2c.c:2989:10
    #5 0x564965944348 in run_builtin tools/perf/perf.c:312:11
    #6 0x564965943235 in handle_internal_command tools/perf/perf.c:364:8
    #7 0x5649659440c4 in run_argv tools/perf/perf.c:408:2
    #8 0x564965942e41 in main tools/perf/perf.c:538:3
---
 tools/perf/util/mem2node.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/mem2node.c +0 -0
index 797d86a1ab09..7f97aa69eb65 100644
@@ -95,7 +95,7 @@ int mem2node__init(struct mem2node *map, struct perf_env *env)
 
Quoted 7 lines

tools/perf/util/mem2node.c int mem2node__init(struct mem2node *map, struct perf_env *env) · line 98

Quoted 4 lines
tools/perf/util/mem2node.c +1 -1
      /* Cut unused entries, due to merging. */
      tmp_entries = realloc(entries, sizeof(*entries) * j);
-     if (tmp_entries)
+     if (j == 0 || tmp_entries)

nice catch.. I wonder if we should fail in here, or at least warn that there're no entris.. which is really strange ;-)

The workload was the stream benchmark with perf c2c, but stream isn't particularly long running. Not sure how j became 0, there's 2 possibilities in the code. The worry with a warning is the spam, so I just wanted to make the code correct.

Thanks,
Ian

Quoted 10 lines

thanks,
jirka

Quoted 6 lines
tools/perf/util/mem2node.c +0 -1
              entries = tmp_entries;
 
      for (i = 0; i < j; i++) {
--

2.25.1.481.gfbce0eb801-goog

#3 lore raw
Quoted 58 lines · Ian Rogers
Quoted 49 lines · Jiri Olsa
Quoted 47 lines · Ian Rogers

Realloc of size zero is a free not an error, avoid this causing a double free. Caught by clang's address sanitizer:

==2634==ERROR: AddressSanitizer: attempting double-free on 0x6020000015f0 in thread T0:

    #0 0x5649659297fd in free llvm/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:123:3
    #1 0x5649659e9251 in __zfree tools/lib/zalloc.c:13:2
    #2 0x564965c0f92c in mem2node__exit tools/perf/util/mem2node.c:114:2
    #3 0x564965a08b4c in perf_c2c__report tools/perf/builtin-c2c.c:2867:2
    #4 0x564965a0616a in cmd_c2c tools/perf/builtin-c2c.c:2989:10
    #5 0x564965944348 in run_builtin tools/perf/perf.c:312:11
    #6 0x564965943235 in handle_internal_command tools/perf/perf.c:364:8
    #7 0x5649659440c4 in run_argv tools/perf/perf.c:408:2
    #8 0x564965942e41 in main tools/perf/perf.c:538:3

0x6020000015f0 is located 0 bytes inside of 1-byte region [0x6020000015f0,0x6020000015f1) freed by thread T0 here:

    #0 0x564965929da3 in realloc third_party/llvm/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:164:3
    #1 0x564965c0f55e in mem2node__init tools/perf/util/mem2node.c:97:16
    #2 0x564965a08956 in perf_c2c__report tools/perf/builtin-c2c.c:2803:8
    #3 0x564965a0616a in cmd_c2c tools/perf/builtin-c2c.c:2989:10
    #4 0x564965944348 in run_builtin tools/perf/perf.c:312:11
    #5 0x564965943235 in handle_internal_command tools/perf/perf.c:364:8
    #6 0x5649659440c4 in run_argv tools/perf/perf.c:408:2
    #7 0x564965942e41 in main tools/perf/perf.c:538:3

previously allocated by thread T0 here:

    #0 0x564965929c42 in calloc third_party/llvm/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:154:3
    #1 0x5649659e9220 in zalloc tools/lib/zalloc.c:8:9
    #2 0x564965c0f32d in mem2node__init tools/perf/util/mem2node.c:61:12
    #3 0x564965a08956 in perf_c2c__report tools/perf/builtin-c2c.c:2803:8
    #4 0x564965a0616a in cmd_c2c tools/perf/builtin-c2c.c:2989:10
    #5 0x564965944348 in run_builtin tools/perf/perf.c:312:11
    #6 0x564965943235 in handle_internal_command tools/perf/perf.c:364:8
    #7 0x5649659440c4 in run_argv tools/perf/perf.c:408:2
    #8 0x564965942e41 in main tools/perf/perf.c:538:3
---
 tools/perf/util/mem2node.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/mem2node.c +0 -0
index 797d86a1ab09..7f97aa69eb65 100644
@@ -95,7 +95,7 @@ int mem2node__init(struct mem2node *map, struct perf_env *env)
 
Quoted 7 lines

tools/perf/util/mem2node.c int mem2node__init(struct mem2node *map, struct perf_env *env) · line 98

Quoted 4 lines
tools/perf/util/mem2node.c +1 -1
      /* Cut unused entries, due to merging. */
      tmp_entries = realloc(entries, sizeof(*entries) * j);
-     if (tmp_entries)
+     if (j == 0 || tmp_entries)

nice catch.. I wonder if we should fail in here, or at least warn that there're no entris.. which is really strange ;-)

Quoted 4 lines

The workload was the stream benchmark with perf c2c, but stream isn't particularly long running. Not sure how j became 0, there's 2 possibilities in the code. The worry with a warning is the spam, so I just wanted to make the code correct.

I was wondering if we should fail completely,
but we might break some c2c expected behaviour

how about just WARN_ONCE pn j == 0, and then
the lookup will fail already with:

if (WARN_ONCE(node < 0, "WARNING: failed to find node\n"))
return;

just get enough hints ;-)

jirka

Quoted 15 lines

Thanks,
Ian

Quoted 10 lines

thanks,
jirka

Quoted 6 lines
tools/perf/util/mem2node.c +0 -1
              entries = tmp_entries;
 
      for (i = 0; i < j; i++) {
--

2.25.1.481.gfbce0eb801-goog

author reviewer
#4 lore raw
Quoted 68 lines · Jiri Olsa
Quoted 58 lines · Ian Rogers
Quoted 49 lines · Jiri Olsa
Quoted 47 lines · Ian Rogers

Realloc of size zero is a free not an error, avoid this causing a double free. Caught by clang's address sanitizer:

==2634==ERROR: AddressSanitizer: attempting double-free on 0x6020000015f0 in thread T0:

    #0 0x5649659297fd in free llvm/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:123:3
    #1 0x5649659e9251 in __zfree tools/lib/zalloc.c:13:2
    #2 0x564965c0f92c in mem2node__exit tools/perf/util/mem2node.c:114:2
    #3 0x564965a08b4c in perf_c2c__report tools/perf/builtin-c2c.c:2867:2
    #4 0x564965a0616a in cmd_c2c tools/perf/builtin-c2c.c:2989:10
    #5 0x564965944348 in run_builtin tools/perf/perf.c:312:11
    #6 0x564965943235 in handle_internal_command tools/perf/perf.c:364:8
    #7 0x5649659440c4 in run_argv tools/perf/perf.c:408:2
    #8 0x564965942e41 in main tools/perf/perf.c:538:3

0x6020000015f0 is located 0 bytes inside of 1-byte region [0x6020000015f0,0x6020000015f1) freed by thread T0 here:

    #0 0x564965929da3 in realloc third_party/llvm/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:164:3
    #1 0x564965c0f55e in mem2node__init tools/perf/util/mem2node.c:97:16
    #2 0x564965a08956 in perf_c2c__report tools/perf/builtin-c2c.c:2803:8
    #3 0x564965a0616a in cmd_c2c tools/perf/builtin-c2c.c:2989:10
    #4 0x564965944348 in run_builtin tools/perf/perf.c:312:11
    #5 0x564965943235 in handle_internal_command tools/perf/perf.c:364:8
    #6 0x5649659440c4 in run_argv tools/perf/perf.c:408:2
    #7 0x564965942e41 in main tools/perf/perf.c:538:3

previously allocated by thread T0 here:

    #0 0x564965929c42 in calloc third_party/llvm/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:154:3
    #1 0x5649659e9220 in zalloc tools/lib/zalloc.c:8:9
    #2 0x564965c0f32d in mem2node__init tools/perf/util/mem2node.c:61:12
    #3 0x564965a08956 in perf_c2c__report tools/perf/builtin-c2c.c:2803:8
    #4 0x564965a0616a in cmd_c2c tools/perf/builtin-c2c.c:2989:10
    #5 0x564965944348 in run_builtin tools/perf/perf.c:312:11
    #6 0x564965943235 in handle_internal_command tools/perf/perf.c:364:8
    #7 0x5649659440c4 in run_argv tools/perf/perf.c:408:2
    #8 0x564965942e41 in main tools/perf/perf.c:538:3
---
 tools/perf/util/mem2node.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/mem2node.c +0 -0
index 797d86a1ab09..7f97aa69eb65 100644
@@ -95,7 +95,7 @@ int mem2node__init(struct mem2node *map, struct perf_env *env)
 
Quoted 7 lines

tools/perf/util/mem2node.c int mem2node__init(struct mem2node *map, struct perf_env *env) · line 98

Quoted 4 lines
tools/perf/util/mem2node.c +1 -1
      /* Cut unused entries, due to merging. */
      tmp_entries = realloc(entries, sizeof(*entries) * j);
-     if (tmp_entries)
+     if (j == 0 || tmp_entries)

nice catch.. I wonder if we should fail in here, or at least warn that there're no entris.. which is really strange ;-)

Quoted 4 lines

The workload was the stream benchmark with perf c2c, but stream isn't particularly long running. Not sure how j became 0, there's 2 possibilities in the code. The worry with a warning is the spam, so I just wanted to make the code correct.

I was wondering if we should fail completely,
but we might break some c2c expected behaviour

Quoted 7 lines

how about just WARN_ONCE pn j == 0, and then
the lookup will fail already with:

if (WARN_ONCE(node < 0, "WARNING: failed to find node\n"))
return;

just get enough hints ;-)

Sorry for the delay. I sent a v2 hopefully doing what you asked :-)
https://lkml.org/lkml/2020/3/20/858

Thanks,
Ian

Quoted 18 lines

jirka

Quoted 15 lines

Thanks,
Ian

Quoted 10 lines

thanks,
jirka

Quoted 6 lines
tools/perf/util/mem2node.c +0 -1
              entries = tmp_entries;
 
      for (i = 0; i < j; i++) {
--

2.25.1.481.gfbce0eb801-goog