Skip to contentSkip to conversation

xl: free common_domname on exit

PATCH In review Ian Campbell 2 participants 3 messages Jun 12, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xl: free common_domname on exit

The split between xl.c and xl_cmdimpl.c made this a bit odd, I chose to add a destructor to xl_cmdimpl.c which is called from xl.c instead of making the variable non-static since that seems like a nicer encapsulation.

==5915== 6 bytes in 1 blocks are still reachable in loss record 1 of 1

==5915==    at 0x4025755: malloc (vg_replace_malloc.c:291)
==5915==    by 0x424FDF3: read_message (xs.c:1145)
==5915==    by 0x425017E: xs_talkv (xs.c:428)
==5915==    by 0x4250445: xs_single (xs.c:546)
==5915==    by 0x42504CE: xs_read (xs.c:592)
==5915==    by 0x4074E2C: libxl_domid_to_name (libxl_utils.c:54)
==5915==    by 0x804E3F2: find_domain (xl_cmdimpl.c:186)
==5915==    by 0x8055B7E: main_destroy (xl_cmdimpl.c:3942)
==5915==    by 0x804DD60: main (xl.c:371)
---
 tools/libxl/xl.c         |    2 ++
 tools/libxl/xl.h         |    2 ++
 tools/libxl/xl_cmdimpl.c |    7 +++++--
 3 files changed, 9 insertions(+), 2 deletions(-)
tools/libxl/xl.c +2 -0
index 0f3acb9..3201180 100644
@@ -291,6 +291,8 @@ static void xl_ctx_free(void)
         free(default_gatewaydev);
         default_gatewaydev = NULL;
     }
+
+    xl_cmdimpl_atexit();
 }
 
 int main(int argc, char **argv)
tools/libxl/xl.h +2 -0
index 771b4af..5ae8d11 100644
@@ -113,6 +113,8 @@ extern int cmdtable_len;
 /* Look up a command in the table, allowing unambiguous truncation */
 struct cmd_spec *cmdtable_lookup(const char *s);
 
+extern void xl_cmdimpl_atexit(void);
+
 extern libxl_ctx *ctx;
 extern xentoollog_logger_stdiostream *logger;
 
tools/libxl/xl_cmdimpl.c +5 -2
index c1a969b..325b257 100644
@@ -68,7 +68,7 @@ libxl_ctx *ctx;
 xlchild children[child_max];
 
 #define INVALID_DOMID ~0
-static const char *common_domname;
+static char *common_domname = NULL;
 static int fd_lock = -1;
 
 /* Stash for specific vcpu to pcpu mappping */
@@ -139,7 +139,10 @@ struct domain_create {
     char **migration_domname_r; /* from malloc */
 };
 
-
+void xl_cmdimpl_atexit(void)
+{
+    free(common_domname); common_domname = NULL;
+}
 
 static int qualifier_to_id(const char *p, uint32_t *id_r)
 {
Signature · 1 line
1.7.2.5
#1 lore raw
Quoted 54 lines · Ian Campbell

The split between xl.c and xl_cmdimpl.c made this a bit odd, I chose to add a destructor to xl_cmdimpl.c which is called from xl.c instead of making the variable non-static since that seems like a nicer encapsulation.

==5915== 6 bytes in 1 blocks are still reachable in loss record 1 of 1

==5915==    at 0x4025755: malloc (vg_replace_malloc.c:291)
==5915==    by 0x424FDF3: read_message (xs.c:1145)
==5915==    by 0x425017E: xs_talkv (xs.c:428)
==5915==    by 0x4250445: xs_single (xs.c:546)
==5915==    by 0x42504CE: xs_read (xs.c:592)
==5915==    by 0x4074E2C: libxl_domid_to_name (libxl_utils.c:54)
==5915==    by 0x804E3F2: find_domain (xl_cmdimpl.c:186)
==5915==    by 0x8055B7E: main_destroy (xl_cmdimpl.c:3942)
==5915==    by 0x804DD60: main (xl.c:371)
---
 tools/libxl/xl.c         |    2 ++
 tools/libxl/xl.h         |    2 ++
 tools/libxl/xl_cmdimpl.c |    7 +++++--
 3 files changed, 9 insertions(+), 2 deletions(-)
tools/libxl/xl.c +2 -0
index 0f3acb9..3201180 100644
@@ -291,6 +291,8 @@ static void xl_ctx_free(void)
         free(default_gatewaydev);
         default_gatewaydev = NULL;
     }
+
+    xl_cmdimpl_atexit();
 }
 
 int main(int argc, char **argv)
tools/libxl/xl.h +2 -0
index 771b4af..5ae8d11 100644
@@ -113,6 +113,8 @@ extern int cmdtable_len;
 /* Look up a command in the table, allowing unambiguous truncation */
 struct cmd_spec *cmdtable_lookup(const char *s);
 
+extern void xl_cmdimpl_atexit(void);
+
 extern libxl_ctx *ctx;
 extern xentoollog_logger_stdiostream *logger;
 
tools/libxl/xl_cmdimpl.c +0 -0
index c1a969b..325b257 100644

tools/libxl/xl_cmdimpl.c libxl_ctx *ctx; · line 71

Quoted 5 lines
tools/libxl/xl_cmdimpl.c +1 -1
@@ -68,7 +68,7 @@ libxl_ctx *ctx;
 xlchild children[child_max];
 
 #define INVALID_DOMID ~0
-static const char *common_domname;
+static char *common_domname = NULL;

Why is "const" being removed?

-George

#2 lore raw
Quoted 7 lines · George Dunlap
Quoted 7 lines
tools/libxl/xl_cmdimpl.c +0 -0
index c1a969b..325b257 100644
@@ -68,7 +68,7 @@ libxl_ctx *ctx;
 xlchild children[child_max];
 
Quoted 5 lines

tools/libxl/xl_cmdimpl.c libxl_ctx *ctx; · line 71

Quoted 3 lines
tools/libxl/xl_cmdimpl.c +1 -1
 #define INVALID_DOMID ~0
-static const char *common_domname;
+static char *common_domname = NULL;

Why is "const" being removed?

free takes a non-const void *, so it has to be non-const to free it.

I had checked that all the places which assigned here were dynamically allocated and not e.g. string literals but double checking that I missed that the one in create_domain takes it from the c_info without dupping it, which would lead to a double free. There's probably a leak of the original name in there too.

With that in mind I nack my own patch ;-)

Quoted 1 line

From what I can tell the right fix is too big for 4.3 so I'll leave this

until 4.4.

Ian.