You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

glibc_sanity.cpp 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright (c) 2009-2014 The Starwels developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #if defined(HAVE_CONFIG_H)
  5. #include "config/starwels-config.h"
  6. #endif
  7. #include <cstddef>
  8. #if defined(HAVE_SYS_SELECT_H)
  9. #include <sys/select.h>
  10. #endif
  11. extern "C" void* memcpy(void* a, const void* b, size_t c);
  12. void* memcpy_int(void* a, const void* b, size_t c)
  13. {
  14. return memcpy(a, b, c);
  15. }
  16. namespace
  17. {
  18. // trigger: Use the memcpy_int wrapper which calls our internal memcpy.
  19. // A direct call to memcpy may be optimized away by the compiler.
  20. // test: Fill an array with a sequence of integers. memcpy to a new empty array.
  21. // Verify that the arrays are equal. Use an odd size to decrease the odds of
  22. // the call being optimized away.
  23. template <unsigned int T>
  24. bool sanity_test_memcpy()
  25. {
  26. unsigned int memcpy_test[T];
  27. unsigned int memcpy_verify[T] = {};
  28. for (unsigned int i = 0; i != T; ++i)
  29. memcpy_test[i] = i;
  30. memcpy_int(memcpy_verify, memcpy_test, sizeof(memcpy_test));
  31. for (unsigned int i = 0; i != T; ++i) {
  32. if (memcpy_verify[i] != i)
  33. return false;
  34. }
  35. return true;
  36. }
  37. #if defined(HAVE_SYS_SELECT_H)
  38. // trigger: Call FD_SET to trigger __fdelt_chk. FORTIFY_SOURCE must be defined
  39. // as >0 and optimizations must be set to at least -O2.
  40. // test: Add a file descriptor to an empty fd_set. Verify that it has been
  41. // correctly added.
  42. bool sanity_test_fdelt()
  43. {
  44. fd_set fds;
  45. FD_ZERO(&fds);
  46. FD_SET(0, &fds);
  47. return FD_ISSET(0, &fds);
  48. }
  49. #endif
  50. } // namespace
  51. bool glibc_sanity_test()
  52. {
  53. #if defined(HAVE_SYS_SELECT_H)
  54. if (!sanity_test_fdelt())
  55. return false;
  56. #endif
  57. return sanity_test_memcpy<1025>();
  58. }