bugs.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. // Copyright © 2018-2019 Ariadne Devos
  3. /* sHT -- in case of a bug, crash graceful */
  4. #ifndef _sHT_BUGS_H
  5. #define _sHT_BUGS_H
  6. #include <stddef.h>
  7. #include <sys/types.h>
  8. #include <limits.h>
  9. #include <sHT/compiler.h>
  10. /** Bug catching
  11. This is for detecting bugs of the class that cause memory corruption,
  12. where trying to continue only amplifies it. It is also for bugs that
  13. cause a smoke test to fail, in which case the application isn't useful
  14. at all until the bug isn't fixed.
  15. The functions are: @var{sHT_halt}, @var{sHT_assert}, @var{sHT_require}
  16. and @var{sHT_todo}. The last one is intentionally left undocumented,
  17. for a obvious reason here documented: it should not be needed outside
  18. proof-of-concepts. */
  19. /** Check that a precondition is true.
  20. This is only for catching bugs. In particular, it may speculatively
  21. be ignored. */
  22. #define sHT_require(b) \
  23. do { \
  24. if (sHT_unlikely(!(b))) \
  25. sHT_halt(#b); \
  26. } while (0)
  27. /** Check that an invariant is true.
  28. This is only for catching bugs. In particular, it may speculatively
  29. be ignored. */
  30. #define sHT_assert(b) sHT_require(b)
  31. /* Last entry has .code == 0, and is the default */
  32. struct sHT_bug_option {
  33. const char *msg;
  34. int code;
  35. };
  36. __attribute__((cold)) __attribute__((nonnull (1)))
  37. _Noreturn void
  38. sHT_bug(const struct sHT_bug_option *options, int code);
  39. #define sHT_todo(b) do { if ((b)) __builtin_trap(); } while (0)
  40. #endif