[PATCH 33/61] dyndbg: check DYNDBG_CLASSMAP_DEFINE args at compile-time
Jim Cromie
jim.cromie at gmail.com
Thu Oct 10 16:14:33 UTC 2024
Validate DYNDBG_CLASSMAP_DEFINE() args at compile:
0 <= _base < 63
class_names is not empty
class_names[0] is a string
(class_names.length + _base) < 63
These compile-time checks will prevent some misuses; 4 such examples
are added to test_dynamic_debug_submod.ko, and will fail compilation
if -DDD_MACRO_ARGCHECK is added to cflags.
They're implemented with 3 static_asserts in DYNDBG_CLASSMAP_DEFINE(),
checking the args. The BUILD_BUG* options all appear to employ a
do-while-0 construct thats illegal at file-scope, which is the proper
usage of this macro (since it exports the classmap).
test-dynamic-debug-submod, when built with -DDD_MACRO_ARGCHECK, gets 4
_DEFINEs to break compilation when the 1st 3 rules above are violated.
Signed-off-by: Jim Cromie <jim.cromie at gmail.com>
---
include/linux/dynamic_debug.h | 4 ++++
lib/test_dynamic_debug.c | 19 +++++++++++++++++++
2 files changed, 23 insertions(+)
diff --git a/include/linux/dynamic_debug.h b/include/linux/dynamic_debug.h
index 28437c5fae20..e3d746a98c33 100644
--- a/include/linux/dynamic_debug.h
+++ b/include/linux/dynamic_debug.h
@@ -111,7 +111,11 @@ struct ddebug_class_map {
* to the classmap.
*/
#define __DYNDBG_CLASSMAP_DEFINE(_var, _maptype, _base, ...) \
+ static_assert(((_base) >= 0 && (_base) < _DPRINTK_CLASS_DFLT)); \
static const char *_var##_classnames[] = { __VA_ARGS__ }; \
+ static_assert(ARRAY_SIZE(_var##_classnames) > 0); \
+ static_assert((ARRAY_SIZE(_var##_classnames) + _base) < \
+ _DPRINTK_CLASS_DFLT); \
extern struct ddebug_class_map _var; \
struct ddebug_class_map __aligned(8) __used \
__section("__dyndbg_classes") _var = { \
diff --git a/lib/test_dynamic_debug.c b/lib/test_dynamic_debug.c
index 8aa10e1e6ba5..bb310e53d603 100644
--- a/lib/test_dynamic_debug.c
+++ b/lib/test_dynamic_debug.c
@@ -118,8 +118,27 @@ DYNDBG_CLASSMAP_PARAM(p_level_num, map_level_num, p);
DYNDBG_CLASSMAP_USE(map_disjoint_bits);
DYNDBG_CLASSMAP_USE(map_level_num);
+#if defined(FORCE_CLASSID_CONFLICT_MODPROBE)
+/*
+ * enable to test overlapping class-id range detection.
+ * This should break on modprobe (of submod)
+ */
+DYNDBG_CLASSMAP_DEFINE(classid_range_conflict_submod, 0, D2_CORE + 1, "D3_SUB");
#endif
+#if defined(DD_MACRO_ARGCHECK)
+/*
+ * Exersize compile-time arg-checks in the macro.
+ * These will break compilation.
+ */
+DYNDBG_CLASSMAP_DEFINE(fail_base_neg, 0, -1, "NEGATIVE_BASE_ARG");
+DYNDBG_CLASSMAP_DEFINE(fail_base_big, 0, 100, "TOOBIG_BASE_ARG");
+DYNDBG_CLASSMAP_DEFINE(fail_str_type, 0, 0, 1 /* not a string */);
+DYNDBG_CLASSMAP_DEFINE(fail_emptyclass, 0, 0 /* ,empty */);
+#endif
+
+#endif /* TEST_DYNAMIC_DEBUG_SUBMOD */
+
/* stand-in for all pr_debug etc */
#define prdbg(SYM) __pr_debug_cls(SYM, #SYM " msg\n")
--
2.46.2
More information about the Intel-gfx-trybot
mailing list