diff --git a/sys/sys/kassert.h b/sys/sys/kassert.h --- a/sys/sys/kassert.h +++ b/sys/sys/kassert.h @@ -38,6 +38,36 @@ extern bool panicked; #define KERNEL_PANICKED() __predict_false(panicked) +/* + * Trap accesses going through a pointer. Moreover if kasan is available trap + * reading the pointer itself. + * + * Sample usage: you have a struct with numerous fields and by API contract + * only some of them get populated, even if the implementation temporary writes + * to them. You can DEBUG_POISON_POINTER so the consumer which should no be + * looking at the field gets caught. + * + * DEBUG_POISON_POINTER(obj->ptr); + * .... + * if (obj->ptr != NULL) // traps with kasan, does not trap otherwise + * .... + * obj->ptr->field // traps with and without kasan + */ +#ifdef INVARIANTS + +#include + +#ifdef DEBUG_POISON_POINTER_VALUE +#define DEBUG_POISON_POINTER(x) ({ \ + x = (void *)(DEBUG_POISON_POINTER_VALUE); \ + kasan_mark(&x, 0, sizeof(x), KASAN_GENERIC_REDZONE); \ +}) +#else +#define DEBUG_POISON_POINTER(x) +#endif + +#endif + #ifdef INVARIANTS /* The option is always available */ #define VNASSERT(exp, vp, msg) do { \ if (__predict_false(!(exp))) { \