From d4f19030befaa331ead2414c649e812cf72a2b5d Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Wed, 5 May 2021 09:52:13 -0400 Subject: [PATCH] lib: enforce preconditions when getting packet message clock snapshots MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit The functions: - bt_message_packet_beginning_borrow_default_clock_snapshot_const - bt_message_packet_end_borrow_default_clock_snapshot_const respectively have the preconditions: - The packets of the stream class of message have a beginning default clock snapshot. - The packets of the stream class of message have an end default clock snapshot. These preconditions are currently not enforced by the code. A user of these functions violating these preconditions in a dev build currently gets NULL back, which is not an expected return value. Add some checks for this. This helps for example when using the trimmer component on a trace without packet beginning/end default clock snapshots, like the one here [1]. Without this patch, we hit a precondition failure when we pass the unexpected NULL value to another function: $ ./src/cli/babeltrace2 nvctf --timerange="05:50:53,05:50:55" 05-05 10:02:15.110 3642287 3642287 F LIB/ASSERT-COND bt_lib_assert_cond_failed@assert-cond.c:64 Babeltrace 2 library precondition not satisfied. 05-05 10:02:15.110 3642287 3642287 F LIB/ASSERT-COND bt_lib_assert_cond_failed@assert-cond.c:66 ------------------------------------------------------------------------ 05-05 10:02:15.110 3642287 3642287 F LIB/ASSERT-COND bt_lib_assert_cond_failed@assert-cond.c:67 Condition ID: `pre:clock-snapshot-get-ns-from-origin:not-null:clock-snapshot`. 05-05 10:02:15.110 3642287 3642287 F LIB/ASSERT-COND bt_lib_assert_cond_failed@assert-cond.c:69 Function: bt_clock_snapshot_get_ns_from_origin(). 05-05 10:02:15.110 3642287 3642287 F LIB/ASSERT-COND bt_lib_assert_cond_failed@assert-cond.c:70 ------------------------------------------------------------------------ 05-05 10:02:15.110 3642287 3642287 F LIB/ASSERT-COND bt_lib_assert_cond_failed@assert-cond.c:71 Error is: 05-05 10:02:15.110 3642287 3642287 F LIB/ASSERT-COND bt_lib_assert_cond_failed@assert-cond.c:73 Clock snapshot is NULL. 05-05 10:02:15.110 3642287 3642287 F LIB/ASSERT-COND bt_lib_assert_cond_failed@assert-cond.c:76 Aborting... With this patch, the problem is caught a little bit earlier, so closer to the root of the problem: $ ./src/cli/babeltrace2 nvctf --timerange="05:50:53,05:50:55" 05-05 10:02:30.784 3647078 3647078 F LIB/ASSERT-COND bt_lib_assert_cond_failed@assert-cond.c:64 Babeltrace 2 library precondition not satisfied. 05-05 10:02:30.784 3647078 3647078 F LIB/ASSERT-COND bt_lib_assert_cond_failed@assert-cond.c:66 ------------------------------------------------------------------------ 05-05 10:02:30.784 3647078 3647078 F LIB/ASSERT-COND bt_lib_assert_cond_failed@assert-cond.c:67 Condition ID: `pre:message-packet-beginning-borrow-default-clock-snapshot-const:msg-stream-class-packets-have-beginning-default-clock-snapshot`. 05-05 10:02:30.784 3647078 3647078 F LIB/ASSERT-COND bt_lib_assert_cond_failed@assert-cond.c:69 Function: bt_message_packet_beginning_borrow_default_clock_snapshot_const(). 05-05 10:02:30.784 3647078 3647078 F LIB/ASSERT-COND bt_lib_assert_cond_failed@assert-cond.c:70 ------------------------------------------------------------------------ 05-05 10:02:30.784 3647078 3647078 F LIB/ASSERT-COND bt_lib_assert_cond_failed@assert-cond.c:71 Error is: 05-05 10:02:30.784 3647078 3647078 F LIB/ASSERT-COND bt_lib_assert_cond_failed@assert-cond.c:73 Message's stream class packets don't have beginning default clock snapshot. 05-05 10:02:30.784 3647078 3647078 F LIB/ASSERT-COND bt_lib_assert_cond_failed@assert-cond.c:76 Aborting... [1] https://lists.lttng.org/pipermail/lttng-dev/2021-May/029955.html Change-Id: I420bb71ab3a5139ff15b524f09c818a1c2aafa81 Signed-off-by: Simon Marchi Reviewed-on: https://review.lttng.org/c/babeltrace/+/5688 Tested-by: jenkins Reviewed-by: Philippe Proulx Reviewed-by: Jérémie Galarneau --- src/lib/graph/message/packet.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/lib/graph/message/packet.c b/src/lib/graph/message/packet.c index 86452b0c..e27c766c 100644 --- a/src/lib/graph/message/packet.c +++ b/src/lib/graph/message/packet.c @@ -34,6 +34,22 @@ BT_ASSERT_PRE_DEV_MSG_HAS_TYPE("message", (_msg), \ "packet-end", BT_MESSAGE_TYPE_PACKET_END) +#define BT_ASSERT_PRE_DEV_MSG_STREAM_CLASS_PACKETS_HAVE_BEGINNING_DEF_CS(_msg) \ + BT_ASSERT_PRE_DEV("msg-stream-class-packets-have-beginning-default-clock-snapshot", ({ \ + const struct bt_packet *_packet = bt_message_packet_beginning_borrow_packet_const(_msg); \ + const struct bt_stream *_stream = bt_packet_borrow_stream_const(_packet); \ + const struct bt_stream_class *_stream_class = bt_stream_borrow_class_const(_stream); \ + bt_stream_class_packets_have_beginning_default_clock_snapshot(_stream_class); \ + }), "Packets of the message's stream don't have a beginning default clock snapshot.") + +#define BT_ASSERT_PRE_DEV_MSG_STREAM_CLASS_PACKETS_HAVE_END_DEF_CS(_msg) \ + BT_ASSERT_PRE_DEV("msg-stream-class-packets-have-end-default-clock-snapshot", ({ \ + const struct bt_packet *_packet = bt_message_packet_end_borrow_packet_const(_msg); \ + const struct bt_stream *_stream = bt_packet_borrow_stream_const(_packet); \ + const struct bt_stream_class *_stream_class = bt_stream_borrow_class_const(_stream); \ + bt_stream_class_packets_have_end_default_clock_snapshot(_stream_class); \ + }), "Packets of the message's stream don't have an end default clock snapshot.") + static inline struct bt_message *new_packet_message(struct bt_graph *graph, enum bt_message_type type, bt_object_release_func recycle_func) @@ -329,6 +345,7 @@ bt_message_packet_beginning_borrow_default_clock_snapshot_const( BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg); BT_ASSERT_PRE_DEV_MSG_IS_PACKET_BEGINNING(msg); BT_ASSERT_PRE_DEV_FOR_BORROW_DEF_CS(msg); + BT_ASSERT_PRE_DEV_MSG_STREAM_CLASS_PACKETS_HAVE_BEGINNING_DEF_CS(msg); return borrow_packet_message_default_clock_snapshot_const(msg); } @@ -339,6 +356,7 @@ bt_message_packet_end_borrow_default_clock_snapshot_const( BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg); BT_ASSERT_PRE_DEV_MSG_IS_PACKET_END(msg); BT_ASSERT_PRE_DEV_FOR_BORROW_DEF_CS(msg); + BT_ASSERT_PRE_DEV_MSG_STREAM_CLASS_PACKETS_HAVE_END_DEF_CS(msg); return borrow_packet_message_default_clock_snapshot_const(msg); } -- 2.34.1