tracepoint: validate provider/event name length with static assert
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Thu, 15 Apr 2021 21:26:32 +0000 (17:26 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Fri, 16 Apr 2021 13:22:02 +0000 (09:22 -0400)
Validate the provider/event name length with static assert in both
_DEFINE_TRACEPOINT() and within the tracepoint probe.

Validating the length in _DEFINE_TRACEPOINT() ensures that the
tracepoints don't try to register a callsite with a too long name by
mistake. This will enable handling of too long names with rejection of
the tracepoint rather than truncation of the name in a future patch.

The tracepoint probe was already validating the length, but using a
static assert makes the error message clearer.

Introduce LTTNG_UST_TRACEPOINT_NAME_LEN_MAX which is separate from the
communication protocol's LTTNG_UST_ABI_SYM_NAME_LEN to ensure both
communication protocol and instrumentation API can evolve independently.

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Change-Id: Ia2e9898ef2f7a5708c692476aff7df6113bccb90

include/lttng/tracepoint.h
include/lttng/ust-tracepoint-event.h
src/lib/lttng-ust-tracepoint/tracepoint.c

index b47b1e0e1c00ab5eb5d1a79356762b8852b35874..f85db068d378383d941dbc8271ed555a71d1a9b9 100644 (file)
@@ -20,6 +20,8 @@
 #include <lttng/ust-compiler.h>
 #include <lttng/ust-tracer.h>
 
+#define LTTNG_UST_TRACEPOINT_NAME_LEN_MAX      256
+
 #ifdef LTTNG_UST_HAVE_SDT_INTEGRATION
 /*
  * Instead of using SDT_USE_VARIADIC from 'sys/sdt.h', use our own namespaced
@@ -154,6 +156,25 @@ extern "C" {
 #define _TP_ARGS_DATA_VAR(...)         _TP_DATA_VAR_N(_TP_NARGS(0, ##__VA_ARGS__), ##__VA_ARGS__)
 #define _TP_PARAMS(...)                        __VA_ARGS__
 
+/*
+ * sizeof(#_provider) - 1 : length of the provider string (excluding \0).
+ * sizeof(#_name) - 1     : length of the name string (excluding \0).
+ * + 1                    : separator between provider and event name.
+ *
+ * Upper bound (inclusive) is LTTNG_UST_TRACEPOINT_NAME_LEN_MAX - 1 to
+ * account for \0.
+ *
+ * The comparison is:
+ *   left hand side:   sizeof(#_provider) - 1 + sizeof(#_name) - 1 + 1
+ *   right hand side:  LTTNG_UST_TRACEPOINT_NAME_LEN_MAX - 1
+ *   operator:         <=  (inclusive)
+ * Simplified in the code below.
+ */
+#define lttng_ust_tracepoint_validate_name_len(_provider, _name)                                               \
+       lttng_ust_static_assert(sizeof(#_provider) + sizeof(#_name) <= LTTNG_UST_TRACEPOINT_NAME_LEN_MAX,       \
+               "Tracepoint name length is too long",                                                           \
+               Tracepoint_name_length_is_too_long)
+
 /*
  * The tracepoint cb is marked always inline so we can distinguish
  * between caller's ip addresses within the probe using the return
@@ -446,6 +467,7 @@ extern struct lttng_ust_tracepoint * const __stop___tracepoints_ptrs[]
 #define _TP_EXTRACT_STRING(...)        #__VA_ARGS__
 
 #define _DEFINE_TRACEPOINT(_provider, _name, _args)                            \
+       lttng_ust_tracepoint_validate_name_len(_provider, _name);               \
        extern int __tracepoint_provider_##_provider;                           \
        static const char __tp_strtab_##_provider##___##_name[]                 \
                __attribute__((section("__tracepoints_strings"))) =             \
index ea0e9564f66a66b09131dbf9cdbca8b5f0c0ac54..c50047d0baf4c6fc5a262b70b9f2ed72328e1e23 100644 (file)
@@ -109,10 +109,7 @@ void _TP_COMBINE_TOKENS(__tracepoint_provider_check_, TRACEPOINT_PROVIDER)(void)
 
 #undef _TRACEPOINT_EVENT_INSTANCE
 #define _TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
-static const char                                                      \
-       __tp_name_len_check##_provider##___##_name[LTTNG_UST_ABI_SYM_NAME_LEN] \
-       __attribute__((unused)) =                                       \
-               #_provider ":" #_name;
+       lttng_ust_tracepoint_validate_name_len(_provider, _name);
 
 #include TRACEPOINT_INCLUDE
 
index 03e1e0bba807a27219de30a364ff7f3af911f81a..e43c1aeb312967aefaec9e52757dad447fb7034e 100644 (file)
@@ -130,6 +130,10 @@ struct callsite_entry {
        bool tp_entry_callsite_ref; /* Has a tp_entry took a ref on this callsite */
 };
 
+lttng_ust_static_assert(LTTNG_UST_TRACEPOINT_NAME_LEN_MAX == LTTNG_UST_ABI_SYM_NAME_LEN,
+               "Tracepoint name max length mismatch between UST ABI and tracepoint API",
+               Tracepoint_name_max_length_mismatch);
+
 /* coverity[+alloc] */
 static void *allocate_probes(int count)
 {
This page took 0.0283 seconds and 4 git commands to generate.