Skip to contentSkip to conversation

Memory leak fixes

PATCH Applied Marc-André Lureau 10 participants 20 messages May 3, 2017 – Jun 1, 2017

lore
Share & tools
lore
#0 lore raw

v5 cover Memory leak fixes

Hi,

A new series of leaks spotted by ASAN. Mostly after introducing of the test-hmp. Would it be useful having a configure --enable-asan, and enabled by default with --enable-debug?

Marc-André Lureau (5):
test-keyval: fix leaks
audio: fix capture buffer leaks
audio: fix WAVState leak
slirp: fix leak
dump: fix memory_mapping_filter leak

 audio/audio.c       | 2 ++
 audio/wavcapture.c  | 1 +
 memory_mapping.c    | 1 +
 slirp/socket.c      | 3 +++
 tests/test-keyval.c | 4 ++++
 5 files changed, 11 insertions(+)
Signature · 1 line
2.12.0.191.gc5d8de91d
#1 lore raw

PATCH v5 1/5 test-keyval: fix leaks

Spotted by ASAN.

---
 tests/test-keyval.c | 4 ++++
 1 file changed, 4 insertions(+)
tests/test-keyval.c +4 -0
index ba19560a22..c556b1b117 100644
@@ -628,6 +628,7 @@ static void test_keyval_visit_alternate(void)
     visit_type_AltNumStr(v, "a", &ans, &error_abort);
     g_assert_cmpint(ans->type, ==, QTYPE_QSTRING);
     g_assert_cmpstr(ans->u.s, ==, "1");
+    qapi_free_AltNumStr(ans);
     visit_type_AltNumInt(v, "a", &ani, &err);
     error_free_or_abort(&err);
     visit_end_struct(v, NULL);
@@ -651,9 +652,12 @@ static void test_keyval_visit_any(void)
     g_assert(qlist);
     qstr = qobject_to_qstring(qlist_pop(qlist));
     g_assert_cmpstr(qstring_get_str(qstr), ==, "null");
+    QDECREF(qstr);
     qstr = qobject_to_qstring(qlist_pop(qlist));
     g_assert_cmpstr(qstring_get_str(qstr), ==, "1");
     g_assert(qlist_empty(qlist));
+    QDECREF(qstr);
+    qobject_decref(any);
     visit_check_struct(v, &error_abort);
     visit_end_struct(v, NULL);
     visit_free(v);
Signature · 1 line
2.12.0.191.gc5d8de91d
Reviewed-by on v5 1/5
#2 lore raw

Re: [Qemu-devel] [PATCH v5 1/5] test-keyval: fix leaks

Quoted 6 lines · Marc-André Lureau

Spotted by ASAN.

---
 tests/test-keyval.c | 4 ++++
 1 file changed, 4 insertions(+)
Quoted 27 lines
tests/test-keyval.c +4 -0
index ba19560a22..c556b1b117 100644
@@ -628,6 +628,7 @@ static void test_keyval_visit_alternate(void)
     visit_type_AltNumStr(v, "a", &ans, &error_abort);
     g_assert_cmpint(ans->type, ==, QTYPE_QSTRING);
     g_assert_cmpstr(ans->u.s, ==, "1");
+    qapi_free_AltNumStr(ans);
     visit_type_AltNumInt(v, "a", &ani, &err);
     error_free_or_abort(&err);
     visit_end_struct(v, NULL);
@@ -651,9 +652,12 @@ static void test_keyval_visit_any(void)
     g_assert(qlist);
     qstr = qobject_to_qstring(qlist_pop(qlist));
     g_assert_cmpstr(qstring_get_str(qstr), ==, "null");
+    QDECREF(qstr);
     qstr = qobject_to_qstring(qlist_pop(qlist));
     g_assert_cmpstr(qstring_get_str(qstr), ==, "1");
     g_assert(qlist_empty(qlist));
+    QDECREF(qstr);
+    qobject_decref(any);
     visit_check_struct(v, &error_abort);
     visit_end_struct(v, NULL);
     visit_free(v);
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#4 lore raw

Re: [Qemu-devel] [PATCH v5 1/5] test-keyval: fix leaks

Quoted 5 lines · Markus Armbruster
Quoted 3 lines · Eric Blake
Quoted 2 lines · Marc-André Lureau

Spotted by ASAN.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
---
 tests/test-keyval.c | 4 ++++
 1 file changed, 4 insertions(+)

This is in qapi-next already. I'm adding the R-by now.

Actually, I'm replacing the commit in my tree by this one, because that results in a simpler, cleaner commit message: I don't have to note the tweaks I applied in my tree, and you in your v5. Thanks!

#5 lore raw

PATCH v5 2/5 audio: fix capture buffer leaks

In reply to Marc-André Lureau

Spotted by ASAN.

---
 audio/audio.c | 2 ++
 1 file changed, 2 insertions(+)
audio/audio.c +2 -0
index c8898d8422..beafed209b 100644
@@ -2028,6 +2028,8 @@ void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque)
                     sw = sw1;
                 }
                 QLIST_REMOVE (cap, entries);
+                g_free (cap->hw.mix_buf);
+                g_free (cap->buf);
                 g_free (cap);
             }
             return;
Signature · 1 line
2.12.0.191.gc5d8de91d
#6 lore raw

Re: [Qemu-devel] [PATCH v5 2/5] audio: fix capture buffer leaks

Gerd already sent this one few days ago ;)

http://lists.nongnu.org/archive/html/qemu-devel/2017-04/msg05587.html

Quoted 21 lines · Marc-André Lureau

Spotted by ASAN.

---
 audio/audio.c | 2 ++
 1 file changed, 2 insertions(+)
audio/audio.c +2 -0
index c8898d8422..beafed209b 100644
@@ -2028,6 +2028,8 @@ void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque)
                     sw = sw1;
                 }
                 QLIST_REMOVE (cap, entries);
+                g_free (cap->hw.mix_buf);
+                g_free (cap->buf);
                 g_free (cap);
             }
             return;
#7 lore raw

PATCH v5 3/5 audio: fix WAVState leak

In reply to Marc-André Lureau

Spotted by ASAN.

---
 audio/wavcapture.c | 1 +
 1 file changed, 1 insertion(+)
audio/wavcapture.c +1 -0
index 8bfb9e7654..5863803584 100644
@@ -88,6 +88,7 @@ static void wav_capture_destroy (void *opaque)
     WAVState *wav = opaque;
 
     AUD_del_capture (wav->cap, wav);
+    g_free (wav);
 }
 
 static void wav_capture_info (void *opaque)
Signature · 1 line
2.12.0.191.gc5d8de91d
Reviewed-by on v5 3/5
#8 lore raw

Re: [Qemu-devel] [PATCH v5 3/5] audio: fix WAVState leak

Quoted 3 lines · Marc-André Lureau

Spotted by ASAN.

Quoted 17 lines
---
 audio/wavcapture.c | 1 +
 1 file changed, 1 insertion(+)
audio/wavcapture.c +1 -0
index 8bfb9e7654..5863803584 100644
@@ -88,6 +88,7 @@ static void wav_capture_destroy (void *opaque)
     WAVState *wav = opaque;
 
     AUD_del_capture (wav->cap, wav);
+    g_free (wav);
 }
 
 static void wav_capture_info (void *opaque)
#9 lore raw

PATCH v5 4/5 slirp: fix leak

In reply to Marc-André Lureau

Spotted by ASAN:

/x86_64/hmp/pc-0.12:
=================================================================
==22538==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 224 byte(s) in 1 object(s) allocated from:

    #0 0x7f0f63cdee60 in malloc (/lib64/libasan.so.3+0xc6e60)
    #1 0x556f11ff32d7 in tcp_newtcpcb /home/elmarco/src/qemu/slirp/tcp_subr.c:250
    #2 0x556f11fdb1d1 in tcp_listen /home/elmarco/src/qemu/slirp/socket.c:688
    #3 0x556f11fca9d5 in slirp_add_hostfwd /home/elmarco/src/qemu/slirp/slirp.c:1052
    #4 0x556f11f8db41 in slirp_hostfwd /home/elmarco/src/qemu/net/slirp.c:506
    #5 0x556f11f8dd83 in hmp_hostfwd_add /home/elmarco/src/qemu/net/slirp.c:535

There might be a better way to fix this, but calling slirp tcp_close() doesn't work.

---
 slirp/socket.c | 3 +++
 1 file changed, 3 insertions(+)
slirp/socket.c +3 -0
index 86927722e1..3b49a69a93 100644
@@ -100,6 +100,9 @@ sofree(struct socket *so)
   if(so->so_next && so->so_prev)
     remque(so);  /* crashes if so is not in a queue */
 
+  if (so->so_tcpcb) {
+      free(so->so_tcpcb);
+  }
   free(so);
 }
 
Signature · 1 line
2.12.0.191.gc5d8de91d
#10 lore raw

Re: [Qemu-devel] [PATCH v5 4/5] slirp: fix leak

Applied to my tree, thanks!

#11 lore raw

Re: [Qemu-devel] [PATCH v5 4/5] slirp: fix leak

Quoted 3 lines · Samuel Thibault

Applied to my tree, thanks!

still not in tree, please remove the unnecessary 'if' before merging

thanks!

Signature · 1 line
Marc-André Lureau
#12 lore raw

PATCH v5 5/5 dump: fix memory_mapping_filter leak

In reply to Marc-André Lureau

Spotted by ASAN.

---
 memory_mapping.c | 1 +
 1 file changed, 1 insertion(+)
memory_mapping.c +1 -0
index 6a39d71da2..a5d38552a6 100644
@@ -337,6 +337,7 @@ void memory_mapping_filter(MemoryMappingList *list, int64_t begin,
         if (cur->phys_addr >= begin + length ||
             cur->phys_addr + cur->length <= begin) {
             QTAILQ_REMOVE(&list->head, cur, next);
+            g_free(cur);
             list->num--;
             continue;
         }
Signature · 1 line
2.12.0.191.gc5d8de91d
#13 lore raw

Re: [Qemu-devel] [PATCH v5 5/5] dump: fix memory_mapping_filter leak

Hi

Quoted 6 lines · Marc-André Lureau

Spotted by ASAN.

---
 memory_mapping.c | 1 +

No maintainer for this file, Wen, Paolo?

Quoted 15 lines
 1 file changed, 1 insertion(+)
memory_mapping.c +1 -0
index 6a39d71da2..a5d38552a6 100644
@@ -337,6 +337,7 @@ void memory_mapping_filter(MemoryMappingList *list,
 int64_t begin,
         if (cur->phys_addr >= begin + length ||
             cur->phys_addr + cur->length <= begin) {
             QTAILQ_REMOVE(&list->head, cur, next);
+            g_free(cur);
             list->num--;
             continue;
         }
Quoted 5 lines

--
2.12.0.191.gc5d8de91d

--

Marc-André Lureau

#14 lore raw

Re: [Qemu-devel] [PATCH v5 5/5] dump: fix memory_mapping_filter leak

Quoted 5 lines · Marc-André Lureau

Hi

On Thu, May 4, 2017 at 2:42 AM Marc-André Lureau < [email protected]> wrote:

Quoted 9 lines
Quoted 6 lines

Spotted by ASAN.

---
 memory_mapping.c | 1 +

No maintainer for this file, Wen, Paolo?

CC qemu-trivial

thanks

Quoted 25 lines
Quoted 18 lines


 1 file changed, 1 insertion(+)
memory_mapping.c +1 -0
index 6a39d71da2..a5d38552a6 100644
@@ -337,6 +337,7 @@ void memory_mapping_filter(MemoryMappingList *list,
 int64_t begin,
         if (cur->phys_addr >= begin + length ||
             cur->phys_addr + cur->length <= begin) {
             QTAILQ_REMOVE(&list->head, cur, next);
+            g_free(cur);
             list->num--;
             continue;
         }
Quoted 5 lines

--
2.12.0.191.gc5d8de91d

--

Marc-André Lureau

Signature · 1 line
Marc-André Lureau
#16 lore raw
In reply to Marc-André Lureau

This is actually v1 (confused with git-publish)

----- Original Message -----

Quoted 24 lines

Hi,

A new series of leaks spotted by ASAN. Mostly after introducing of the test-hmp. Would it be useful having a configure --enable-asan, and enabled by default with --enable-debug?

Marc-André Lureau (5):
test-keyval: fix leaks
audio: fix capture buffer leaks
audio: fix WAVState leak
slirp: fix leak
dump: fix memory_mapping_filter leak

 audio/audio.c       | 2 ++
 audio/wavcapture.c  | 1 +
 memory_mapping.c    | 1 +
 slirp/socket.c      | 3 +++
 tests/test-keyval.c | 4 ++++
 5 files changed, 11 insertions(+)

--
2.12.0.191.gc5d8de91d

#17 lore raw
In reply to Marc-André Lureau
This series seems to have some coding style problems. See output below for more information:

Hi,

This series seems to have some coding style problems. See output below for more information:

Subject: [Qemu-devel] [PATCH v5 0/5] Memory leak fixes
Message-id: [email protected]
Type: series

=== TEST SCRIPT BEGIN ===
#!/bin/bash

BASE=base
n=1
total=$(git log --oneline $BASE.. | wc -l)
failed=0

git config --local diff.renamelimit 0
git config --local diff.renames True

commits="$(git log --format=%H --reverse $BASE..)" for c in $commits; do

    echo "Checking PATCH $n/$total: $(git log -n 1 --format=%s $c)..."
    if ! git show $c --format=email | ./scripts/checkpatch.pl --mailback -; then
        failed=1
        echo
    fi
    n=$((n+1))

done

exit $failed
=== TEST SCRIPT END ===

Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384
Switched to a new branch 'test'
84762e2 dump: fix memory_mapping_filter leak
345a5d8 slirp: fix leak
3f1ae28 audio: fix WAVState leak
565e071 audio: fix capture buffer leaks
2ce5984 test-keyval: fix leaks

=== OUTPUT BEGIN ===
Checking PATCH 1/5: test-keyval: fix leaks...
Checking PATCH 2/5: audio: fix capture buffer leaks...
ERROR: space prohibited between function name and open parenthesis '(' #22: FILE: audio/audio.c:2031:
+ g_free (cap->hw.mix_buf);

ERROR: space prohibited between function name and open parenthesis '(' #23: FILE: audio/audio.c:2032:
+ g_free (cap->buf);

total: 2 errors, 0 warnings, 8 lines checked

Your patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.

Checking PATCH 3/5: audio: fix WAVState leak...
ERROR: space prohibited between function name and open parenthesis '(' #22: FILE: audio/wavcapture.c:91:
+ g_free (wav);

total: 1 errors, 0 warnings, 7 lines checked

Your patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.

Checking PATCH 4/5: slirp: fix leak...
ERROR: suspect code indent for conditional statements (2, 6) #37: FILE: slirp/socket.c:103:
+ if (so->so_tcpcb) {
+ free(so->so_tcpcb);

total: 1 errors, 0 warnings, 9 lines checked

Your patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.

Checking PATCH 5/5: dump: fix memory_mapping_filter leak... === OUTPUT END ===

Test command exited with code: 1

---
Email generated automatically by Patchew [http://patchew.org/].
Please send your feedback to [email protected]

#18 lore raw
In reply to Marc-André Lureau
Quoted 4 lines · Marc-André Lureau

Hi,

A new series of leaks spotted by ASAN. Mostly after introducing of the test-hmp. Would it be useful having a configure --enable-asan, and

Yes.

Quoted 1 line

enabled by default with --enable-debug?

Not sure - isn't --enable-debug just for debug symbols and we enable extra stuff like --enable-tcg-debug for additional checking? Given the overhead of asan I still think we need a configure setup for reasonably debugable but still performant.

Quoted 8 lines

Marc-André Lureau (5):
test-keyval: fix leaks
audio: fix capture buffer leaks
audio: fix WAVState leak
slirp: fix leak
dump: fix memory_mapping_filter leak

Quoted 6 lines
 audio/audio.c       | 2 ++
 audio/wavcapture.c  | 1 +
 memory_mapping.c    | 1 +
 slirp/socket.c      | 3 +++
 tests/test-keyval.c | 4 ++++
 5 files changed, 11 insertions(+)

--
Alex Bennée

#19 lore raw
In reply to Marc-André Lureau
Quoted 5 lines · Marc-André Lureau

Hi,

A new series of leaks spotted by ASAN. Mostly after introducing of the test-hmp. Would it be useful having a configure --enable-asan, and enabled by default with --enable-debug?

Yes, --enable-asan would be a nice idea, and we can also add a build target for Docker tests.

Paolo