[PATCH] viridentity: Fix ref/unref imbalance in VIR_IDENTITY_AUTORESTORE

Michal Privoznik posted 1 patch 2 years, 11 months ago
Test syntax-check failed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/e19e678e2b4ed31889f3d74638bdc4d10d301b2a.1621267851.git.mprivozn@redhat.com
src/util/viridentity.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
[PATCH] viridentity: Fix ref/unref imbalance in VIR_IDENTITY_AUTORESTORE
Posted by Michal Privoznik 2 years, 11 months ago
The basic use case of VIR_IDENTITY_AUTORESTORE() is in
conjunction with virIdentityElevateCurrent(). What happens is
that virIdentityElevateCurrent() gets current identity (which
increases the refcounter of thread local virIdentity object) and
returns a pointer to it. Later, when the variable goes out of
scope the virIdentityRestoreHelper() is called which calls
virIdentitySetCurrent() over the old identity. But this means
that the refcounter is increased again.

Therefore, we have to explicitly decrease the refcounter by
calling g_object_unref().

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
---

I've observed this imbalance whilst running qemuxml2argvtest under
valgrind:

==10412== 949 (40 direct, 909 indirect) bytes in 1 blocks are definitely lost in loss record 524 of 539
==10412==    at 0x48397EF: malloc (vg_replace_malloc.c:307)
==10412==    by 0x50806F8: g_malloc (in /usr/lib64/libglib-2.0.so.0.6600.7)
==10412==    by 0x50992FD: g_slice_alloc (in /usr/lib64/libglib-2.0.so.0.6600.7)
==10412==    by 0x50999B9: g_slice_alloc0 (in /usr/lib64/libglib-2.0.so.0.6600.7)
==10412==    by 0x518D9AB: g_type_create_instance (in /usr/lib64/libgobject-2.0.so.0.6600.7)
==10412==    by 0x51739DC: g_object_new_internal (in /usr/lib64/libgobject-2.0.so.0.6600.7)
==10412==    by 0x5174F5C: g_object_new_with_properties (in /usr/lib64/libgobject-2.0.so.0.6600.7)
==10412==    by 0x5175978: g_object_new (in /usr/lib64/libgobject-2.0.so.0.6600.7)
==10412==    by 0x496C3C6: virIdentityNew (viridentity.c:407)
==10412==    by 0x496BF1A: virIdentityGetSystem (viridentity.c:318)
==10412==    by 0x117CEF: testCompareXMLToArgv (qemuxml2argvtest.c:653)
==10412==    by 0x148505: virTestRun (testutils.c:142)

Test #315 doesn't tickle the bug, while test #316 does.

 src/util/viridentity.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/src/util/viridentity.c b/src/util/viridentity.c
index e7e5c31241..eb77f69e2e 100644
--- a/src/util/viridentity.c
+++ b/src/util/viridentity.c
@@ -197,8 +197,12 @@ void virIdentityRestoreHelper(virIdentity **identptr)
 {
     virIdentity *ident = *identptr;
 
-    if (ident != NULL)
+    if (ident != NULL) {
         virIdentitySetCurrent(ident);
+        /* virIdentitySetCurrent() grabs its own reference.
+         * We don't need ours anymore. */
+        g_object_unref(ident);
+    }
 }
 
 #define TOKEN_BYTES 16
-- 
2.26.3

Re: [PATCH] viridentity: Fix ref/unref imbalance in VIR_IDENTITY_AUTORESTORE
Posted by Daniel P. Berrangé 2 years, 11 months ago
On Mon, May 17, 2021 at 06:12:56PM +0200, Michal Privoznik wrote:
> The basic use case of VIR_IDENTITY_AUTORESTORE() is in
> conjunction with virIdentityElevateCurrent(). What happens is
> that virIdentityElevateCurrent() gets current identity (which
> increases the refcounter of thread local virIdentity object) and
> returns a pointer to it. Later, when the variable goes out of
> scope the virIdentityRestoreHelper() is called which calls
> virIdentitySetCurrent() over the old identity. But this means
> that the refcounter is increased again.
> 
> Therefore, we have to explicitly decrease the refcounter by
> calling g_object_unref().

Opps, yes, i remember thinking about this and clearly forgot
it again.

> 
> Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
> ---
> 
> I've observed this imbalance whilst running qemuxml2argvtest under
> valgrind:
> 
> ==10412== 949 (40 direct, 909 indirect) bytes in 1 blocks are definitely lost in loss record 524 of 539
> ==10412==    at 0x48397EF: malloc (vg_replace_malloc.c:307)
> ==10412==    by 0x50806F8: g_malloc (in /usr/lib64/libglib-2.0.so.0.6600.7)
> ==10412==    by 0x50992FD: g_slice_alloc (in /usr/lib64/libglib-2.0.so.0.6600.7)
> ==10412==    by 0x50999B9: g_slice_alloc0 (in /usr/lib64/libglib-2.0.so.0.6600.7)
> ==10412==    by 0x518D9AB: g_type_create_instance (in /usr/lib64/libgobject-2.0.so.0.6600.7)
> ==10412==    by 0x51739DC: g_object_new_internal (in /usr/lib64/libgobject-2.0.so.0.6600.7)
> ==10412==    by 0x5174F5C: g_object_new_with_properties (in /usr/lib64/libgobject-2.0.so.0.6600.7)
> ==10412==    by 0x5175978: g_object_new (in /usr/lib64/libgobject-2.0.so.0.6600.7)
> ==10412==    by 0x496C3C6: virIdentityNew (viridentity.c:407)
> ==10412==    by 0x496BF1A: virIdentityGetSystem (viridentity.c:318)
> ==10412==    by 0x117CEF: testCompareXMLToArgv (qemuxml2argvtest.c:653)
> ==10412==    by 0x148505: virTestRun (testutils.c:142)
> 
> Test #315 doesn't tickle the bug, while test #316 does.
> 
>  src/util/viridentity.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>


Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|