bugs.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* s2 - in case of a bug, crash graceful
  2. Copyright (C) 2018 Ariadne Devos
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #ifndef _sHT_BUGS_H
  14. #define _sHT_BUGS_H
  15. #include <stddef.h>
  16. #include <sys/types.h>
  17. #include <limits.h>
  18. #include <sHT/compiler.h>
  19. /** Bug catching
  20. This is for detecting bugs of the class that cause memory corruption,
  21. where trying to continue only amplifies it. It is also for bugs that
  22. cause a smoke test to fail, in which case the application isn't useful
  23. at all until the bug isn't fixed.
  24. The functions are: @var{sHT_halt}, @var{sHT_assert}, @var{sHT_require}
  25. and @var{sHT_todo}. The last one is intentionally left undocumented,
  26. for a obvious reason here documented: it should not be needed outside
  27. proof-of-concepts. */
  28. /** Check that a precondition is true.
  29. This is only for catching bugs. In particular, it may speculatively
  30. be ignored. */
  31. #define sHT_require(b) \
  32. do { \
  33. if (sHT_unlikely(!(b))) \
  34. sHT_halt(#b); \
  35. } while (0)
  36. /** Check that an invariant is true.
  37. This is only for catching bugs. In particular, it may speculatively
  38. be ignored. */
  39. #define sHT_assert(b) sHT_require(b)
  40. /* Last entry has .code == 0, and is the default */
  41. struct sHT_bug_option {
  42. const char *msg;
  43. int code;
  44. };
  45. __attribute__((cold)) __attribute__((nonnull (1)))
  46. _Noreturn void
  47. sHT_bug(const struct sHT_bug_option *options, int code);
  48. #define sHT_todo(b) do { if ((b)) __builtin_trap(); } while (0)
  49. #endif