constant_flow.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * \file constant_flow.h
  3. *
  4. * \brief This file contains tools to ensure tested code has constant flow.
  5. */
  6. /*
  7. * Copyright The Mbed TLS Contributors
  8. * SPDX-License-Identifier: Apache-2.0
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  11. * not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  18. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. */
  22. #ifndef TEST_CONSTANT_FLOW_H
  23. #define TEST_CONSTANT_FLOW_H
  24. #include "mbedtls/build_info.h"
  25. /*
  26. * This file defines the two macros
  27. *
  28. * #define TEST_CF_SECRET(ptr, size)
  29. * #define TEST_CF_PUBLIC(ptr, size)
  30. *
  31. * that can be used in tests to mark a memory area as secret (no branch or
  32. * memory access should depend on it) or public (default, only needs to be
  33. * marked explicitly when it was derived from secret data).
  34. *
  35. * Arguments:
  36. * - ptr: a pointer to the memory area to be marked
  37. * - size: the size in bytes of the memory area
  38. *
  39. * Implementation:
  40. * The basic idea is that of ctgrind <https://github.com/agl/ctgrind>: we can
  41. * re-use tools that were designed for checking use of uninitialized memory.
  42. * This file contains two implementations: one based on MemorySanitizer, the
  43. * other on valgrind's memcheck. If none of them is enabled, dummy macros that
  44. * do nothing are defined for convenience.
  45. *
  46. * \note #TEST_CF_SECRET must be called directly from within a .function file,
  47. * not indirectly via a macro defined under tests/include or a function
  48. * under tests/src. This is because we only run Valgrind for constant
  49. * flow on test suites that have greppable annotations inside them (see
  50. * `skip_suites_without_constant_flow` in `tests/scripts/all.sh`).
  51. */
  52. #if defined(MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN)
  53. #include <sanitizer/msan_interface.h>
  54. /* Use macros to avoid messing up with origin tracking */
  55. #define TEST_CF_SECRET __msan_allocated_memory
  56. // void __msan_allocated_memory(const volatile void* data, size_t size);
  57. #define TEST_CF_PUBLIC __msan_unpoison
  58. // void __msan_unpoison(const volatile void *a, size_t size);
  59. #elif defined(MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND)
  60. #include <valgrind/memcheck.h>
  61. #define TEST_CF_SECRET VALGRIND_MAKE_MEM_UNDEFINED
  62. // VALGRIND_MAKE_MEM_UNDEFINED(_qzz_addr, _qzz_len)
  63. #define TEST_CF_PUBLIC VALGRIND_MAKE_MEM_DEFINED
  64. // VALGRIND_MAKE_MEM_DEFINED(_qzz_addr, _qzz_len)
  65. #else /* MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN ||
  66. MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND */
  67. #define TEST_CF_SECRET(ptr, size)
  68. #define TEST_CF_PUBLIC(ptr, size)
  69. #endif /* MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN ||
  70. MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND */
  71. #endif /* TEST_CONSTANT_FLOW_H */