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