aboutsummaryrefslogtreecommitdiff
path: root/src/compat/glibc_sanity.cpp
diff options
context:
space:
mode:
authorWladimir J. van der Laan <laanwj@gmail.com>2014-06-18 09:18:21 +0200
committerWladimir J. van der Laan <laanwj@gmail.com>2014-06-18 09:52:50 +0200
commitb8e56aa8effdf586491b341cae355948cbc77e6a (patch)
treeac8b7e51aae69e71af21468245798ec9b8be3a65 /src/compat/glibc_sanity.cpp
parent5a514c35bcff262c1759a29e21678d0cd79bd92e (diff)
parent92a6220711b3f98c3daad8a8dcdf13f09ce484fd (diff)
downloadbitcoin-b8e56aa8effdf586491b341cae355948cbc77e6a.tar.xz
Merge pull request #4339
92a6220 sanity: hook up sanity checks (Cory Fields) 679240d sanity: add libc/stdlib sanity checks (Cory Fields) 11404af sanity: autoconf check for sys/select.h (Cory Fields)
Diffstat (limited to 'src/compat/glibc_sanity.cpp')
-rw-r--r--src/compat/glibc_sanity.cpp61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/compat/glibc_sanity.cpp b/src/compat/glibc_sanity.cpp
new file mode 100644
index 0000000000..1f64df9e33
--- /dev/null
+++ b/src/compat/glibc_sanity.cpp
@@ -0,0 +1,61 @@
+#include "bitcoin-config.h"
+
+#include <cstddef>
+#if defined(HAVE_SYS_SELECT_H)
+#include <sys/select.h>
+#endif
+
+extern "C" void* memcpy(void* a, const void* b, size_t c);
+void* memcpy_int(void* a, const void* b, size_t c)
+{
+ return memcpy(a,b,c);
+}
+
+namespace {
+// trigger: Use the memcpy_int wrapper which calls our internal memcpy.
+// A direct call to memcpy may be optimized away by the compiler.
+// test: Fill an array with a sequence of integers. memcpy to a new empty array.
+// Verify that the arrays are equal. Use an odd size to decrease the odds of
+// the call being optimized away.
+template <unsigned int T>
+bool sanity_test_memcpy()
+{
+ unsigned int memcpy_test[T];
+ unsigned int memcpy_verify[T] = {};
+ for (unsigned int i = 0; i != T; ++i)
+ memcpy_test[i] = i;
+
+ memcpy_int(memcpy_verify,memcpy_test,sizeof(memcpy_test));
+
+ for (unsigned int i = 0; i != T; ++i)
+ {
+ if(memcpy_verify[i] != i)
+ return false;
+ }
+ return true;
+}
+
+#if defined(HAVE_SYS_SELECT_H)
+// trigger: Call FD_SET to trigger __fdelt_chk. FORTIFY_SOURCE must be defined
+// as >0 and optimizations must be set to at least -O2.
+// test: Add a file descriptor to an empty fd_set. Verify that it has been
+// correctly added.
+bool sanity_test_fdelt()
+{
+ fd_set fds;
+ FD_ZERO(&fds);
+ FD_SET(0, &fds);
+ return FD_ISSET(0,&fds);
+}
+#endif
+
+} // anon namespace
+
+bool glibc_sanity_test()
+{
+#if defined(HAVE_SYS_SELECT_H)
+ if (!sanity_test_fdelt())
+ return false;
+#endif
+ return sanity_test_memcpy<1025>();
+}