Fix: Double free in bt_context_remove_trace().
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 11 lines · * Jérémie Galarneau ([email protected]) wrote:
ctf_close_tracewas being called twice when callingbt_context_remove_tracethus causingfree()to be called on an invalid pointer.Calling
bt_context_remove_trace()would callctf_close_trace()once via theclose_handlecallback registered on the ctf format struct and a second call would take place frombt_trace_handle_destroy()which is registered as thevalue_destroy_funcon thetrace_handleshash table of the current context.The first explicit call to
handle->format->close_traceis unnecessary.The crash is reproducible by invoking the
tests-python.pyscript.
I think we should leave the close_trace calls in both locations, but set handle->td to -1 when we close it, and test for negative handle ID before doing the close.
Thanks,
Mathieu
Quoted 57 lines
Signed-off-by Jérémie Galarneau <[email protected]>--- lib/context.c | 8 +------- lib/trace-handle.c | 5 ++++- 2 files changed, 5 insertions(+), 8 deletions(-)lib/context.c +1
-7@@ -156,7 +156,6 @@ end:int bt_context_remove_trace (struct bt_context *ctx,int handle_id) {struct bt_trace_handle *handle; -int ret;if (!ctx)return -EINVAL ;@@ -168,12 +167,7 @@ int bt_context_remove_trace(struct bt_context *ctx, int handle_id)/* Remove from containers */ trace_collection_remove (ctx->tc, handle->td); -/* Close the trace */ - ret = handle->format->close_trace (handle->td); -if (ret) { -fprintf (stderr,"Error in close_trace callback\n" ); -return ret; - } +/* Remove and free the handle */ g_hash_table_remove (ctx->trace_handles, (gpointer ) (unsigned long ) handle_id);lib/trace-handle.c +4
-1@@ -49,7 +49,10 @@ struct bt_trace_handle *bt_trace_handle_create(struct bt_context *ctx)void bt_trace_handle_destroy (struct bt_trace_handle *th) { - th->format->close_trace (th->td); +if (th->format->close_trace (th->td)) { +fprintf (stderr,"Error in close_trace callback\n" ); + } +g_free (th); }Signature · 7 lines
1.8.1.1 _______________________________________________ lttng-dev mailing list [email protected] http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com
A message that is not in the archive (sent off-list or not yet received): 20130121193225.GA8005@Krystal
Quoted 9 lines · Mathieu Desnoyers
Quoted 8 lines · * Jérémie Galarneau ([email protected]) wrote:
ctf_close_tracewas being called twice when callingbt_context_remove_tracethus causingfree()to be called on an invalid pointer.Calling
bt_context_remove_trace()would callctf_close_trace()once via theclose_handlecallback registered on the ctf format struct and a second call would take place frombt_trace_handle_destroy()which is registered as thevalue_destroy_funcon thetrace_handleshash table of the current context.
Quoted 7 lines
Quoted 3 lines
The first explicit call to
handle->format->close_traceis unnecessary.The crash is reproducible by invoking the
tests-python.pyscript.I think we should leave the
close_tracecalls in both locations, but sethandle->tdto -1 when we close it, and test for negative handle ID before doing the close.
I agree, we need the two calls to close, one is called when we remove a trace manually and the other one is called when we destroy the context. Setting and testing the td to -1 will solve the problem and will avoid calling close_trace at all if not needed.
Thanks,
Julien
Quoted 63 lines
Thanks,
Mathieu
Quoted 57 lines
Signed-off-by Jérémie Galarneau <[email protected]>--- lib/context.c | 8 +------- lib/trace-handle.c | 5 ++++- 2 files changed, 5 insertions(+), 8 deletions(-)lib/context.c +1
-7@@ -156,7 +156,6 @@ end:int bt_context_remove_trace (struct bt_context *ctx,int handle_id) {struct bt_trace_handle *handle; -int ret;if (!ctx)return -EINVAL ;@@ -168,12 +167,7 @@ int bt_context_remove_trace(struct bt_context *ctx, int handle_id)/* Remove from containers */ trace_collection_remove (ctx->tc, handle->td); -/* Close the trace */ - ret = handle->format->close_trace (handle->td); -if (ret) { -fprintf (stderr,"Error in close_trace callback\n" ); -return ret; - } +/* Remove and free the handle */ g_hash_table_remove (ctx->trace_handles, (gpointer ) (unsigned long ) handle_id);lib/trace-handle.c +4
-1@@ -49,7 +49,10 @@ struct bt_trace_handle *bt_trace_handle_create(struct bt_context *ctx)void bt_trace_handle_destroy (struct bt_trace_handle *th) { - th->format->close_trace (th->td); +if (th->format->close_trace (th->td)) { +fprintf (stderr,"Error in close_trace callback\n" ); + } +g_free (th); }Signature · 7 lines
1.8.1.1 _______________________________________________ lttng-dev mailing list [email protected] http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 14 lines · * Julien Desfossez ([email protected]) wrote:
Quoted 13 lines · Mathieu Desnoyers
Quoted 11 lines · * Jérémie Galarneau ([email protected]) wrote:
ctf_close_tracewas being called twice when callingbt_context_remove_tracethus causingfree()to be called on an invalid pointer.Calling
bt_context_remove_trace()would callctf_close_trace()once via theclose_handlecallback registered on the ctf format struct and a second call would take place frombt_trace_handle_destroy()which is registered as thevalue_destroy_funcon thetrace_handleshash table of the current context.The first explicit call to
handle->format->close_traceis unnecessary.The crash is reproducible by invoking the
tests-python.pyscript.
Quoted 8 lines
Quoted 3 lines
I think we should leave the
close_tracecalls in both locations, but sethandle->tdto -1 when we close it, and test for negative handle ID before doing the close.I agree, we need the two calls to close, one is called when we remove a trace manually and the other one is called when we destroy the context. Setting and testing the td to -1 will solve the problem and will avoid calling
close_traceat all if not needed.
Can we ever call bt_trace_handle_destroy() manually on a handle that needs the trace to be closed ? After careful review, I don't think so... (see patch v2 from Jérémie).
Thanks,
Mathieu
Quoted 68 lines
Thanks,
Julien
Quoted 63 lines
Thanks,
Mathieu
Quoted 57 lines
Signed-off-by Jérémie Galarneau <[email protected]>--- lib/context.c | 8 +------- lib/trace-handle.c | 5 ++++- 2 files changed, 5 insertions(+), 8 deletions(-)lib/context.c +1
-7@@ -156,7 +156,6 @@ end:int bt_context_remove_trace (struct bt_context *ctx,int handle_id) {struct bt_trace_handle *handle; -int ret;if (!ctx)return -EINVAL ;@@ -168,12 +167,7 @@ int bt_context_remove_trace(struct bt_context *ctx, int handle_id)/* Remove from containers */ trace_collection_remove (ctx->tc, handle->td); -/* Close the trace */ - ret = handle->format->close_trace (handle->td); -if (ret) { -fprintf (stderr,"Error in close_trace callback\n" ); -return ret; - } +/* Remove and free the handle */ g_hash_table_remove (ctx->trace_handles, (gpointer ) (unsigned long ) handle_id);lib/trace-handle.c +4
-1@@ -49,7 +49,10 @@ struct bt_trace_handle *bt_trace_handle_create(struct bt_context *ctx)void bt_trace_handle_destroy (struct bt_trace_handle *th) { - th->format->close_trace (th->td); +if (th->format->close_trace (th->td)) { +fprintf (stderr,"Error in close_trace callback\n" ); + } +g_free (th); }Signature · 7 lines
1.8.1.1 _______________________________________________ lttng-dev mailing list [email protected] http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com