index-nospec.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. // Copyright © 2018-2019 Ariadne Devos
  3. /* sHT -- test index masking */
  4. /* Derived from <tests/inc-saturated.c>
  5. This is important to test, because it is mostly only used for
  6. during a speculative execution -- where most of the results
  7. are thrown away, except for some side-channels. */
  8. #include <limits.h>
  9. #include <stddef.h>
  10. #include <stdint.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <sHT/nospec.h>
  14. #ifdef SIZE_C
  15. #elif SIZE_MAX == UINT16_MAX
  16. # define SIZE_C(a) UINT16_C(a)
  17. #elif SIZE_MAX == UINT32_MAX
  18. # define SIZE_C(a) UINT32_C(a)
  19. #elif SIZE_MAX == UINT64_MAX
  20. # define SIZE_C(a) UINT64_C(a)
  21. #elif SIZE_MAX == UINT128_MAX
  22. # define SIZE_C(a) UINT128_C(a)
  23. #elif
  24. # error unsupported architecture
  25. #endif
  26. static void
  27. test(size_t old_i, size_t n)
  28. {
  29. size_t i = sHT_index_nospec(old_i, n);
  30. if (n == 0) {
  31. if (i == 0)
  32. return;
  33. goto error;
  34. }
  35. if (old_i < n) {
  36. if (i == old_i)
  37. return;
  38. goto error;
  39. }
  40. /* old_i >= n, n != 0 */
  41. if (i >= n)
  42. goto error;
  43. return;
  44. error:
  45. if (puts("FAIL: sHT_index_nospec") < 0)
  46. exit(2);
  47. exit(1);
  48. }
  49. int
  50. main(void)
  51. {
  52. #define HALFWORK SIZE_C(1024)
  53. /* Don't use sHT_index_iterate, as that would be create a rather
  54. circular situation.
  55. Exploit semantics of wrap-around arithmetic for easy high and low
  56. values, without 2^128 iterations. */
  57. for (size_t n = SIZE_MAX - HALFWORK; n != HALFWORK; n++) {
  58. for (size_t i = SIZE_MAX - HALFWORK; i != HALFWORK; i++) {
  59. test(i, n);
  60. test(i + SIZE_MAX/2, n);
  61. test(i + SIZE_MAX/2, n + SIZE_MAX/2);
  62. test(i, n + SIZE_MAX/2);
  63. }
  64. }
  65. if (puts("PASS: sHT/index_nospec") < 0)
  66. exit(2);
  67. exit(0);
  68. }