check.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2020 Brian Smith.
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
  8. // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
  10. // SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef RING_CHECK_H
  15. #define RING_CHECK_H
  16. // |debug_assert_nonsecret| is like |assert| and should be used (only) when the
  17. // assertion does not have any potential to leak a secret. |NDEBUG| controls this
  18. // exactly like |assert|. It is emulated when there is no assert.h to make
  19. // cross-building easier.
  20. //
  21. // When reviewing uses of |debug_assert_nonsecret|, verify that the check
  22. // really does not have potential to leak a secret.
  23. #if !defined(GFp_NOSTDLIBINC)
  24. # include <assert.h>
  25. # define debug_assert_nonsecret(x) assert(x)
  26. #else
  27. # if !defined(NDEBUG)
  28. # define debug_assert_nonsecret(x) ((x) ? ((void)0) : __builtin_trap())
  29. # else
  30. # define debug_assert_nonsecret(x) ((void)0)
  31. # endif
  32. #endif
  33. // |dev_assert_secret| is like |assert| and should be used (only) when the
  34. // assertion operates on secret data in a way that has the potential to leak
  35. // the secret. |dev_assert_secret| can only be enabled by changing the |#if 0|
  36. // here to |#if 1| (or equivalent) when |NDEBUG| is not defined. This is not
  37. // controlled only through |NDEBUG| so that such checks do not leak into debug
  38. // builds that may make it into production use.
  39. //
  40. // When reviewing uses of |dev_assert_secret|, verify that the check really
  41. // does have the potential to leak a secret.
  42. #if 0 // DO NOT COMMIT CHANGES TO THIS LINE.
  43. # define dev_assert_secret debug_assert_nonsecret
  44. #else
  45. # define dev_assert_secret(x) ((void)0)
  46. #endif
  47. #endif // RING_CHECK_H