From: Mathieu Desnoyers Date: Thu, 15 Apr 2021 21:26:32 +0000 (-0400) Subject: tracepoint: validate provider/event name length with static assert X-Git-Tag: v2.13.0-rc1~93 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=81b16412749579b79a4d829ff87e170d0e1ebd0d;p=lttng-ust.git tracepoint: validate provider/event name length with static assert 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 Change-Id: Ia2e9898ef2f7a5708c692476aff7df6113bccb90 --- diff --git a/include/lttng/tracepoint.h b/include/lttng/tracepoint.h index b47b1e0e..f85db068 100644 --- a/include/lttng/tracepoint.h +++ b/include/lttng/tracepoint.h @@ -20,6 +20,8 @@ #include #include +#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"))) = \ diff --git a/include/lttng/ust-tracepoint-event.h b/include/lttng/ust-tracepoint-event.h index ea0e9564..c50047d0 100644 --- a/include/lttng/ust-tracepoint-event.h +++ b/include/lttng/ust-tracepoint-event.h @@ -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 diff --git a/src/lib/lttng-ust-tracepoint/tracepoint.c b/src/lib/lttng-ust-tracepoint/tracepoint.c index 03e1e0bb..e43c1aeb 100644 --- a/src/lib/lttng-ust-tracepoint/tracepoint.c +++ b/src/lib/lttng-ust-tracepoint/tracepoint.c @@ -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) {