/tmp/bitcoin/src/randomenv.cpp
Line | Count | Source |
1 | | // Copyright (c) 2009-2010 Satoshi Nakamoto |
2 | | // Copyright (c) 2009-present The Bitcoin 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 | | #include <bitcoin-build-config.h> // IWYU pragma: keep |
7 | | |
8 | | #include <randomenv.h> |
9 | | |
10 | | #include <clientversion.h> |
11 | | #include <compat/compat.h> |
12 | | #include <compat/cpuid.h> |
13 | | #include <crypto/sha512.h> |
14 | | #include <span.h> |
15 | | #include <support/cleanse.h> |
16 | | #include <util/byte_units.h> |
17 | | #include <util/time.h> |
18 | | |
19 | | #include <algorithm> |
20 | | #include <atomic> |
21 | | #include <cstdint> |
22 | | #include <cstring> |
23 | | #include <chrono> |
24 | | #include <climits> |
25 | | #include <thread> |
26 | | #include <vector> |
27 | | |
28 | | #include <sys/types.h> // must go before a number of other headers |
29 | | |
30 | | #ifdef WIN32 |
31 | | #include <windows.h> |
32 | | #else |
33 | | #include <fcntl.h> |
34 | | #include <netinet/in.h> |
35 | | #include <sys/resource.h> |
36 | | #include <sys/socket.h> |
37 | | #include <sys/stat.h> |
38 | | #include <sys/time.h> |
39 | | #include <sys/utsname.h> |
40 | | #include <unistd.h> |
41 | | #endif |
42 | | #ifdef HAVE_IFADDRS |
43 | | #include <ifaddrs.h> |
44 | | #endif |
45 | | #ifdef HAVE_SYSCTL |
46 | | #include <sys/sysctl.h> |
47 | | #if __has_include(<vm/vm_param.h>) |
48 | | #include <vm/vm_param.h> |
49 | | #endif |
50 | | #if __has_include(<sys/resources.h>) |
51 | | #include <sys/resources.h> |
52 | | #endif |
53 | | #if __has_include(<sys/vmmeter.h>) |
54 | | #include <sys/vmmeter.h> |
55 | | #endif |
56 | | #endif |
57 | | #if defined(HAVE_STRONG_GETAUXVAL) |
58 | | #include <sys/auxv.h> |
59 | | #endif |
60 | | |
61 | | #if defined(__APPLE__) || \ |
62 | | defined(__FreeBSD__) || \ |
63 | | defined(__NetBSD__) || \ |
64 | | defined(__OpenBSD__) || \ |
65 | | defined(__illumos__) |
66 | | extern char** environ; // Necessary on the above platforms |
67 | | #endif |
68 | | |
69 | | namespace { |
70 | | |
71 | | /** Helper to easily feed data into a CSHA512. |
72 | | * |
73 | | * Note that this does not serialize the passed object (like stream.h's << operators do). |
74 | | * Its raw memory representation is used directly. |
75 | | */ |
76 | | template<typename T> |
77 | 70.0k | CSHA512& operator<<(CSHA512& hasher, const T& data) { |
78 | 70.0k | static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want"); |
79 | 70.0k | static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want"); |
80 | 70.0k | static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want"); |
81 | 70.0k | static_assert(!std::is_same_v<std::decay_t<T>, const unsigned char*>, "Calling operator<<(CSHA512, const unsigned char*) is probably not what you want"); |
82 | 70.0k | hasher.Write((const unsigned char*)&data, sizeof(data)); |
83 | 70.0k | return hasher; |
84 | 70.0k | } randomenv.cpp:CSHA512& (anonymous namespace)::operator<<<stat>(CSHA512&, stat const&) Line | Count | Source | 77 | 26.7k | CSHA512& operator<<(CSHA512& hasher, const T& data) { | 78 | 26.7k | static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want"); | 79 | 26.7k | static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want"); | 80 | 26.7k | static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want"); | 81 | 26.7k | static_assert(!std::is_same_v<std::decay_t<T>, const unsigned char*>, "Calling operator<<(CSHA512, const unsigned char*) is probably not what you want"); | 82 | 26.7k | hasher.Write((const unsigned char*)&data, sizeof(data)); | 83 | 26.7k | return hasher; | 84 | 26.7k | } |
randomenv.cpp:CSHA512& (anonymous namespace)::operator<<<timespec>(CSHA512&, timespec const&) Line | Count | Source | 77 | 3.78k | CSHA512& operator<<(CSHA512& hasher, const T& data) { | 78 | 3.78k | static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want"); | 79 | 3.78k | static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want"); | 80 | 3.78k | static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want"); | 81 | 3.78k | static_assert(!std::is_same_v<std::decay_t<T>, const unsigned char*>, "Calling operator<<(CSHA512, const unsigned char*) is probably not what you want"); | 82 | 3.78k | hasher.Write((const unsigned char*)&data, sizeof(data)); | 83 | 3.78k | return hasher; | 84 | 3.78k | } |
randomenv.cpp:CSHA512& (anonymous namespace)::operator<<<timeval>(CSHA512&, timeval const&) Line | Count | Source | 77 | 1.26k | CSHA512& operator<<(CSHA512& hasher, const T& data) { | 78 | 1.26k | static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want"); | 79 | 1.26k | static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want"); | 80 | 1.26k | static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want"); | 81 | 1.26k | static_assert(!std::is_same_v<std::decay_t<T>, const unsigned char*>, "Calling operator<<(CSHA512, const unsigned char*) is probably not what you want"); | 82 | 1.26k | hasher.Write((const unsigned char*)&data, sizeof(data)); | 83 | 1.26k | return hasher; | 84 | 1.26k | } |
randomenv.cpp:CSHA512& (anonymous namespace)::operator<<<long>(CSHA512&, long const&) Line | Count | Source | 77 | 4.96k | CSHA512& operator<<(CSHA512& hasher, const T& data) { | 78 | 4.96k | static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want"); | 79 | 4.96k | static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want"); | 80 | 4.96k | static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want"); | 81 | 4.96k | static_assert(!std::is_same_v<std::decay_t<T>, const unsigned char*>, "Calling operator<<(CSHA512, const unsigned char*) is probably not what you want"); | 82 | 4.96k | hasher.Write((const unsigned char*)&data, sizeof(data)); | 83 | 4.96k | return hasher; | 84 | 4.96k | } |
randomenv.cpp:CSHA512& (anonymous namespace)::operator<<<rusage>(CSHA512&, rusage const&) Line | Count | Source | 77 | 1.26k | CSHA512& operator<<(CSHA512& hasher, const T& data) { | 78 | 1.26k | static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want"); | 79 | 1.26k | static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want"); | 80 | 1.26k | static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want"); | 81 | 1.26k | static_assert(!std::is_same_v<std::decay_t<T>, const unsigned char*>, "Calling operator<<(CSHA512, const unsigned char*) is probably not what you want"); | 82 | 1.26k | hasher.Write((const unsigned char*)&data, sizeof(data)); | 83 | 1.26k | return hasher; | 84 | 1.26k | } |
randomenv.cpp:CSHA512& (anonymous namespace)::operator<<<void**>(CSHA512&, void** const&) Line | Count | Source | 77 | 1.26k | CSHA512& operator<<(CSHA512& hasher, const T& data) { | 78 | 1.26k | static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want"); | 79 | 1.26k | static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want"); | 80 | 1.26k | static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want"); | 81 | 1.26k | static_assert(!std::is_same_v<std::decay_t<T>, const unsigned char*>, "Calling operator<<(CSHA512, const unsigned char*) is probably not what you want"); | 82 | 1.26k | hasher.Write((const unsigned char*)&data, sizeof(data)); | 83 | 1.26k | return hasher; | 84 | 1.26k | } |
randomenv.cpp:CSHA512& (anonymous namespace)::operator<<<void*>(CSHA512&, void* const&) Line | Count | Source | 77 | 1.26k | CSHA512& operator<<(CSHA512& hasher, const T& data) { | 78 | 1.26k | static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want"); | 79 | 1.26k | static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want"); | 80 | 1.26k | static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want"); | 81 | 1.26k | static_assert(!std::is_same_v<std::decay_t<T>, const unsigned char*>, "Calling operator<<(CSHA512, const unsigned char*) is probably not what you want"); | 82 | 1.26k | hasher.Write((const unsigned char*)&data, sizeof(data)); | 83 | 1.26k | return hasher; | 84 | 1.26k | } |
randomenv.cpp:CSHA512& (anonymous namespace)::operator<<<bool>(CSHA512&, bool const&) Line | Count | Source | 77 | 1.18k | CSHA512& operator<<(CSHA512& hasher, const T& data) { | 78 | 1.18k | static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want"); | 79 | 1.18k | static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want"); | 80 | 1.18k | static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want"); | 81 | 1.18k | static_assert(!std::is_same_v<std::decay_t<T>, const unsigned char*>, "Calling operator<<(CSHA512, const unsigned char*) is probably not what you want"); | 82 | 1.18k | hasher.Write((const unsigned char*)&data, sizeof(data)); | 83 | 1.18k | return hasher; | 84 | 1.18k | } |
randomenv.cpp:CSHA512& (anonymous namespace)::operator<<<unsigned long>(CSHA512&, unsigned long const&) Line | Count | Source | 77 | 5.91k | CSHA512& operator<<(CSHA512& hasher, const T& data) { | 78 | 5.91k | static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want"); | 79 | 5.91k | static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want"); | 80 | 5.91k | static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want"); | 81 | 5.91k | static_assert(!std::is_same_v<std::decay_t<T>, const unsigned char*>, "Calling operator<<(CSHA512, const unsigned char*) is probably not what you want"); | 82 | 5.91k | hasher.Write((const unsigned char*)&data, sizeof(data)); | 83 | 5.91k | return hasher; | 84 | 5.91k | } |
randomenv.cpp:CSHA512& (anonymous namespace)::operator<<<int>(CSHA512&, int const&) Line | Count | Source | 77 | 10.6k | CSHA512& operator<<(CSHA512& hasher, const T& data) { | 78 | 10.6k | static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want"); | 79 | 10.6k | static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want"); | 80 | 10.6k | static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want"); | 81 | 10.6k | static_assert(!std::is_same_v<std::decay_t<T>, const unsigned char*>, "Calling operator<<(CSHA512, const unsigned char*) is probably not what you want"); | 82 | 10.6k | hasher.Write((const unsigned char*)&data, sizeof(data)); | 83 | 10.6k | return hasher; | 84 | 10.6k | } |
randomenv.cpp:CSHA512& (anonymous namespace)::operator<<<CSHA512*>(CSHA512&, CSHA512* const&) Line | Count | Source | 77 | 1.18k | CSHA512& operator<<(CSHA512& hasher, const T& data) { | 78 | 1.18k | static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want"); | 79 | 1.18k | static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want"); | 80 | 1.18k | static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want"); | 81 | 1.18k | static_assert(!std::is_same_v<std::decay_t<T>, const unsigned char*>, "Calling operator<<(CSHA512, const unsigned char*) is probably not what you want"); | 82 | 1.18k | hasher.Write((const unsigned char*)&data, sizeof(data)); | 83 | 1.18k | return hasher; | 84 | 1.18k | } |
randomenv.cpp:CSHA512& (anonymous namespace)::operator<<<void (*)(CSHA512&)>(CSHA512&, void (* const&)(CSHA512&)) Line | Count | Source | 77 | 1.18k | CSHA512& operator<<(CSHA512& hasher, const T& data) { | 78 | 1.18k | static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want"); | 79 | 1.18k | static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want"); | 80 | 1.18k | static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want"); | 81 | 1.18k | static_assert(!std::is_same_v<std::decay_t<T>, const unsigned char*>, "Calling operator<<(CSHA512, const unsigned char*) is probably not what you want"); | 82 | 1.18k | hasher.Write((const unsigned char*)&data, sizeof(data)); | 83 | 1.18k | return hasher; | 84 | 1.18k | } |
randomenv.cpp:CSHA512& (anonymous namespace)::operator<<<void* (*)(unsigned long) noexcept>(CSHA512&, void* (* const&)(unsigned long) noexcept) Line | Count | Source | 77 | 1.18k | CSHA512& operator<<(CSHA512& hasher, const T& data) { | 78 | 1.18k | static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want"); | 79 | 1.18k | static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want"); | 80 | 1.18k | static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want"); | 81 | 1.18k | static_assert(!std::is_same_v<std::decay_t<T>, const unsigned char*>, "Calling operator<<(CSHA512, const unsigned char*) is probably not what you want"); | 82 | 1.18k | hasher.Write((const unsigned char*)&data, sizeof(data)); | 83 | 1.18k | return hasher; | 84 | 1.18k | } |
randomenv.cpp:CSHA512& (anonymous namespace)::operator<<<int*>(CSHA512&, int* const&) Line | Count | Source | 77 | 1.18k | CSHA512& operator<<(CSHA512& hasher, const T& data) { | 78 | 1.18k | static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want"); | 79 | 1.18k | static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want"); | 80 | 1.18k | static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want"); | 81 | 1.18k | static_assert(!std::is_same_v<std::decay_t<T>, const unsigned char*>, "Calling operator<<(CSHA512, const unsigned char*) is probably not what you want"); | 82 | 1.18k | hasher.Write((const unsigned char*)&data, sizeof(data)); | 83 | 1.18k | return hasher; | 84 | 1.18k | } |
randomenv.cpp:CSHA512& (anonymous namespace)::operator<<<char***>(CSHA512&, char*** const&) Line | Count | Source | 77 | 1.18k | CSHA512& operator<<(CSHA512& hasher, const T& data) { | 78 | 1.18k | static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want"); | 79 | 1.18k | static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want"); | 80 | 1.18k | static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want"); | 81 | 1.18k | static_assert(!std::is_same_v<std::decay_t<T>, const unsigned char*>, "Calling operator<<(CSHA512, const unsigned char*) is probably not what you want"); | 82 | 1.18k | hasher.Write((const unsigned char*)&data, sizeof(data)); | 83 | 1.18k | return hasher; | 84 | 1.18k | } |
randomenv.cpp:CSHA512& (anonymous namespace)::operator<<<unsigned int>(CSHA512&, unsigned int const&) Line | Count | Source | 77 | 4.73k | CSHA512& operator<<(CSHA512& hasher, const T& data) { | 78 | 4.73k | static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want"); | 79 | 4.73k | static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want"); | 80 | 4.73k | static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want"); | 81 | 4.73k | static_assert(!std::is_same_v<std::decay_t<T>, const unsigned char*>, "Calling operator<<(CSHA512, const unsigned char*) is probably not what you want"); | 82 | 4.73k | hasher.Write((const unsigned char*)&data, sizeof(data)); | 83 | 4.73k | return hasher; | 84 | 4.73k | } |
randomenv.cpp:CSHA512& (anonymous namespace)::operator<<<std::thread::id>(CSHA512&, std::thread::id const&) Line | Count | Source | 77 | 1.18k | CSHA512& operator<<(CSHA512& hasher, const T& data) { | 78 | 1.18k | static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want"); | 79 | 1.18k | static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want"); | 80 | 1.18k | static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want"); | 81 | 1.18k | static_assert(!std::is_same_v<std::decay_t<T>, const unsigned char*>, "Calling operator<<(CSHA512, const unsigned char*) is probably not what you want"); | 82 | 1.18k | hasher.Write((const unsigned char*)&data, sizeof(data)); | 83 | 1.18k | return hasher; | 84 | 1.18k | } |
|
85 | | |
86 | | #ifndef WIN32 |
87 | | void AddSockaddr(CSHA512& hasher, const struct sockaddr *addr) |
88 | 28.3k | { |
89 | 28.3k | if (addr == nullptr) return; |
90 | 22.4k | switch (addr->sa_family) { |
91 | 10.6k | case AF_INET: |
92 | 10.6k | hasher.Write((const unsigned char*)addr, sizeof(sockaddr_in)); |
93 | 10.6k | break; |
94 | 4.73k | case AF_INET6: |
95 | 4.73k | hasher.Write((const unsigned char*)addr, sizeof(sockaddr_in6)); |
96 | 4.73k | break; |
97 | 7.09k | default: |
98 | 7.09k | hasher.Write((const unsigned char*)&addr->sa_family, sizeof(addr->sa_family)); |
99 | 22.4k | } |
100 | 22.4k | } |
101 | | |
102 | | void AddFile(CSHA512& hasher, const char *path) |
103 | 21.9k | { |
104 | 21.9k | struct stat sb = {}; |
105 | 21.9k | int f = open(path, O_RDONLY); |
106 | 21.9k | size_t total = 0; |
107 | 21.9k | if (f != -1) { |
108 | 20.8k | unsigned char fbuf[4096]; |
109 | 20.8k | int n; |
110 | 20.8k | hasher.Write((const unsigned char*)&f, sizeof(f)); |
111 | 20.8k | if (fstat(f, &sb) == 0) hasher << sb; |
112 | 22.0k | do { |
113 | 22.0k | n = read(f, fbuf, sizeof(fbuf)); |
114 | 22.0k | if (n > 0) hasher.Write(fbuf, n); |
115 | 22.0k | total += n; |
116 | | /* not bothering with EINTR handling. */ |
117 | 22.0k | } while (n == sizeof(fbuf) && total < 1_MiB); // Read only the first 1 Mbyte |
118 | 20.8k | close(f); |
119 | 20.8k | } |
120 | 21.9k | } |
121 | | |
122 | | void AddPath(CSHA512& hasher, const char *path) |
123 | 5.91k | { |
124 | 5.91k | struct stat sb = {}; |
125 | 5.91k | if (stat(path, &sb) == 0) { |
126 | 5.91k | hasher.Write((const unsigned char*)path, strlen(path) + 1); |
127 | 5.91k | hasher << sb; |
128 | 5.91k | } |
129 | 5.91k | } |
130 | | #endif |
131 | | |
132 | | #ifdef HAVE_SYSCTL |
133 | | template<int... S> |
134 | | void AddSysctl(CSHA512& hasher) |
135 | | { |
136 | | int CTL[sizeof...(S)] = {S...}; |
137 | | unsigned char buffer[65536]; |
138 | | size_t siz = 65536; |
139 | | int ret = sysctl(CTL, sizeof...(S), buffer, &siz, nullptr, 0); |
140 | | if (ret == 0 || (ret == -1 && errno == ENOMEM)) { |
141 | | hasher << sizeof(CTL); |
142 | | hasher.Write((const unsigned char*)CTL, sizeof(CTL)); |
143 | | if (siz > sizeof(buffer)) siz = sizeof(buffer); |
144 | | hasher << siz; |
145 | | hasher.Write(buffer, siz); |
146 | | } |
147 | | } |
148 | | #endif |
149 | | |
150 | | #ifdef HAVE_GETCPUID |
151 | | void inline AddCPUID(CSHA512& hasher, uint32_t leaf, uint32_t subleaf, uint32_t& ax, uint32_t& bx, uint32_t& cx, uint32_t& dx) |
152 | | { |
153 | | GetCPUID(leaf, subleaf, ax, bx, cx, dx); |
154 | | hasher << leaf << subleaf << ax << bx << cx << dx; |
155 | | } |
156 | | |
157 | | void AddAllCPUID(CSHA512& hasher) |
158 | | { |
159 | | uint32_t ax, bx, cx, dx; |
160 | | // Iterate over all standard leaves |
161 | | AddCPUID(hasher, 0, 0, ax, bx, cx, dx); // Returns max leaf in ax |
162 | | uint32_t max = ax; |
163 | | for (uint32_t leaf = 1; leaf <= max && leaf <= 0xFF; ++leaf) { |
164 | | uint32_t maxsub = 0; |
165 | | for (uint32_t subleaf = 0; subleaf <= 0xFF; ++subleaf) { |
166 | | AddCPUID(hasher, leaf, subleaf, ax, bx, cx, dx); |
167 | | // Iterate subleafs for leaf values 4, 7, 11, 13 |
168 | | if (leaf == 4) { |
169 | | if ((ax & 0x1f) == 0) break; |
170 | | } else if (leaf == 7) { |
171 | | if (subleaf == 0) maxsub = ax; |
172 | | if (subleaf == maxsub) break; |
173 | | } else if (leaf == 11) { |
174 | | if ((cx & 0xff00) == 0) break; |
175 | | } else if (leaf == 13) { |
176 | | if (ax == 0 && bx == 0 && cx == 0 && dx == 0) break; |
177 | | } else { |
178 | | // For any other leaf, stop after subleaf 0. |
179 | | break; |
180 | | } |
181 | | } |
182 | | } |
183 | | // Iterate over all extended leaves |
184 | | AddCPUID(hasher, 0x80000000, 0, ax, bx, cx, dx); // Returns max extended leaf in ax |
185 | | uint32_t ext_max = ax; |
186 | | for (uint32_t leaf = 0x80000001; leaf <= ext_max && leaf <= 0x800000FF; ++leaf) { |
187 | | AddCPUID(hasher, leaf, 0, ax, bx, cx, dx); |
188 | | } |
189 | | } |
190 | | #endif |
191 | | } // namespace |
192 | | |
193 | | void RandAddDynamicEnv(CSHA512& hasher) |
194 | 1.26k | { |
195 | | // Various clocks |
196 | | #ifdef WIN32 |
197 | | FILETIME ftime; |
198 | | GetSystemTimeAsFileTime(&ftime); |
199 | | hasher << ftime; |
200 | | #else |
201 | 1.26k | struct timespec ts = {}; |
202 | 1.26k | # ifdef CLOCK_MONOTONIC |
203 | 1.26k | clock_gettime(CLOCK_MONOTONIC, &ts); |
204 | 1.26k | hasher << ts; |
205 | 1.26k | # endif |
206 | 1.26k | # ifdef CLOCK_REALTIME |
207 | 1.26k | clock_gettime(CLOCK_REALTIME, &ts); |
208 | 1.26k | hasher << ts; |
209 | 1.26k | # endif |
210 | 1.26k | # ifdef CLOCK_BOOTTIME |
211 | 1.26k | clock_gettime(CLOCK_BOOTTIME, &ts); |
212 | 1.26k | hasher << ts; |
213 | 1.26k | # endif |
214 | | // gettimeofday is available on all UNIX systems, but only has microsecond precision. |
215 | 1.26k | struct timeval tv = {}; |
216 | 1.26k | gettimeofday(&tv, nullptr); |
217 | 1.26k | hasher << tv; |
218 | 1.26k | #endif |
219 | | // Probably redundant, but also use all the standard library clocks: |
220 | 1.26k | hasher << std::chrono::system_clock::now().time_since_epoch().count(); |
221 | 1.26k | hasher << std::chrono::steady_clock::now().time_since_epoch().count(); |
222 | 1.26k | hasher << std::chrono::high_resolution_clock::now().time_since_epoch().count(); |
223 | | |
224 | 1.26k | #ifndef WIN32 |
225 | | // Current resource usage. |
226 | 1.26k | struct rusage usage = {}; |
227 | 1.26k | if (getrusage(RUSAGE_SELF, &usage) == 0) hasher << usage; |
228 | 1.26k | #endif |
229 | | |
230 | 1.26k | #ifdef __linux__ |
231 | 1.26k | AddFile(hasher, "/proc/diskstats"); |
232 | 1.26k | AddFile(hasher, "/proc/vmstat"); |
233 | 1.26k | AddFile(hasher, "/proc/schedstat"); |
234 | 1.26k | AddFile(hasher, "/proc/zoneinfo"); |
235 | 1.26k | AddFile(hasher, "/proc/meminfo"); |
236 | 1.26k | AddFile(hasher, "/proc/softirqs"); |
237 | 1.26k | AddFile(hasher, "/proc/stat"); |
238 | 1.26k | AddFile(hasher, "/proc/self/schedstat"); |
239 | 1.26k | AddFile(hasher, "/proc/self/status"); |
240 | 1.26k | #endif |
241 | | |
242 | | #ifdef HAVE_SYSCTL |
243 | | # ifdef CTL_KERN |
244 | | # if defined(KERN_PROC) && defined(KERN_PROC_ALL) |
245 | | AddSysctl<CTL_KERN, KERN_PROC, KERN_PROC_ALL>(hasher); |
246 | | # endif |
247 | | # endif |
248 | | # ifdef CTL_HW |
249 | | # ifdef HW_DISKSTATS |
250 | | AddSysctl<CTL_HW, HW_DISKSTATS>(hasher); |
251 | | # endif |
252 | | # endif |
253 | | # ifdef CTL_VM |
254 | | # ifdef VM_LOADAVG |
255 | | AddSysctl<CTL_VM, VM_LOADAVG>(hasher); |
256 | | # endif |
257 | | # ifdef VM_TOTAL |
258 | | AddSysctl<CTL_VM, VM_TOTAL>(hasher); |
259 | | # endif |
260 | | # ifdef VM_METER |
261 | | AddSysctl<CTL_VM, VM_METER>(hasher); |
262 | | # endif |
263 | | # endif |
264 | | #endif |
265 | | |
266 | | // Stack and heap location |
267 | 1.26k | void* addr = malloc(4097); |
268 | 1.26k | hasher << &addr << addr; |
269 | 1.26k | free(addr); |
270 | 1.26k | } |
271 | | |
272 | | void RandAddStaticEnv(CSHA512& hasher) |
273 | 1.18k | { |
274 | | // Some compile-time static properties |
275 | 1.18k | hasher << (CHAR_MIN < 0) << sizeof(void*) << sizeof(long) << sizeof(int); |
276 | 1.18k | #if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__) |
277 | 1.18k | hasher << __GNUC__ << __GNUC_MINOR__ << __GNUC_PATCHLEVEL__; |
278 | 1.18k | #endif |
279 | | #ifdef _MSC_VER |
280 | | hasher << _MSC_VER; |
281 | | #endif |
282 | 1.18k | hasher << __cplusplus; |
283 | 1.18k | #ifdef _XOPEN_VERSION |
284 | 1.18k | hasher << _XOPEN_VERSION; |
285 | 1.18k | #endif |
286 | 1.18k | #ifdef __VERSION__ |
287 | 1.18k | const char* COMPILER_VERSION = __VERSION__; |
288 | 1.18k | hasher.Write((const unsigned char*)COMPILER_VERSION, strlen(COMPILER_VERSION) + 1); |
289 | 1.18k | #endif |
290 | | |
291 | | // Bitcoin client version |
292 | 1.18k | hasher << CLIENT_VERSION; |
293 | | |
294 | 1.18k | #if defined(HAVE_STRONG_GETAUXVAL) |
295 | | // Information available through getauxval() |
296 | 1.18k | # ifdef AT_HWCAP |
297 | 1.18k | hasher << getauxval(AT_HWCAP); |
298 | 1.18k | # endif |
299 | 1.18k | # ifdef AT_HWCAP2 |
300 | 1.18k | hasher << getauxval(AT_HWCAP2); |
301 | 1.18k | # endif |
302 | 1.18k | # ifdef AT_RANDOM |
303 | 1.18k | const unsigned char* random_aux = (const unsigned char*)getauxval(AT_RANDOM); |
304 | 1.18k | if (random_aux) hasher.Write(random_aux, 16); |
305 | 1.18k | # endif |
306 | 1.18k | # ifdef AT_PLATFORM |
307 | 1.18k | const char* platform_str = (const char*)getauxval(AT_PLATFORM); |
308 | 1.18k | if (platform_str) hasher.Write((const unsigned char*)platform_str, strlen(platform_str) + 1); |
309 | 1.18k | # endif |
310 | 1.18k | # ifdef AT_EXECFN |
311 | 1.18k | const char* exec_str = (const char*)getauxval(AT_EXECFN); |
312 | 1.18k | if (exec_str) hasher.Write((const unsigned char*)exec_str, strlen(exec_str) + 1); |
313 | 1.18k | # endif |
314 | 1.18k | #endif // HAVE_STRONG_GETAUXVAL |
315 | | |
316 | | #ifdef HAVE_GETCPUID |
317 | | AddAllCPUID(hasher); |
318 | | #endif |
319 | | |
320 | | // Memory locations |
321 | 1.18k | hasher << &hasher << &RandAddStaticEnv << &malloc << &errno << &environ; |
322 | | |
323 | | // Hostname |
324 | | #ifdef WIN32 |
325 | | constexpr DWORD max_size = MAX_COMPUTERNAME_LENGTH + 1; |
326 | | char hname[max_size]; |
327 | | DWORD size = max_size; |
328 | | if (GetComputerNameA(hname, &size) != 0) { |
329 | | hasher.Write(UCharCast(hname), size); |
330 | | } |
331 | | #else |
332 | 1.18k | char hname[256]; |
333 | 1.18k | if (gethostname(hname, 256) == 0) { |
334 | 1.18k | hasher.Write((const unsigned char*)hname, strnlen(hname, 256)); |
335 | 1.18k | } |
336 | 1.18k | #endif |
337 | | |
338 | 1.18k | #ifdef HAVE_IFADDRS |
339 | | // Network interfaces |
340 | 1.18k | struct ifaddrs *ifad = nullptr; |
341 | 1.18k | getifaddrs(&ifad); |
342 | 1.18k | struct ifaddrs *ifit = ifad; |
343 | 10.6k | while (ifit != nullptr) { |
344 | 9.46k | hasher.Write((const unsigned char*)&ifit, sizeof(ifit)); |
345 | 9.46k | hasher.Write((const unsigned char*)ifit->ifa_name, strlen(ifit->ifa_name) + 1); |
346 | 9.46k | hasher.Write((const unsigned char*)&ifit->ifa_flags, sizeof(ifit->ifa_flags)); |
347 | 9.46k | AddSockaddr(hasher, ifit->ifa_addr); |
348 | 9.46k | AddSockaddr(hasher, ifit->ifa_netmask); |
349 | 9.46k | AddSockaddr(hasher, ifit->ifa_dstaddr); |
350 | 9.46k | ifit = ifit->ifa_next; |
351 | 9.46k | } |
352 | 1.18k | freeifaddrs(ifad); |
353 | 1.18k | #endif |
354 | | |
355 | 1.18k | #ifndef WIN32 |
356 | | // UNIX kernel information |
357 | 1.18k | struct utsname name; |
358 | 1.18k | if (uname(&name) != -1) { |
359 | 1.18k | hasher.Write((const unsigned char*)&name.sysname, strlen(name.sysname) + 1); |
360 | 1.18k | hasher.Write((const unsigned char*)&name.nodename, strlen(name.nodename) + 1); |
361 | 1.18k | hasher.Write((const unsigned char*)&name.release, strlen(name.release) + 1); |
362 | 1.18k | hasher.Write((const unsigned char*)&name.version, strlen(name.version) + 1); |
363 | 1.18k | hasher.Write((const unsigned char*)&name.machine, strlen(name.machine) + 1); |
364 | 1.18k | } |
365 | | |
366 | | /* Path and filesystem provided data */ |
367 | 1.18k | AddPath(hasher, "/"); |
368 | 1.18k | AddPath(hasher, "."); |
369 | 1.18k | AddPath(hasher, "/tmp"); |
370 | 1.18k | AddPath(hasher, "/home"); |
371 | 1.18k | AddPath(hasher, "/proc"); |
372 | 1.18k | #ifdef __linux__ |
373 | 1.18k | AddFile(hasher, "/proc/cmdline"); |
374 | 1.18k | AddFile(hasher, "/proc/cpuinfo"); |
375 | 1.18k | AddFile(hasher, "/proc/version"); |
376 | 1.18k | #endif |
377 | 1.18k | AddFile(hasher, "/etc/passwd"); |
378 | 1.18k | AddFile(hasher, "/etc/group"); |
379 | 1.18k | AddFile(hasher, "/etc/hosts"); |
380 | 1.18k | AddFile(hasher, "/etc/resolv.conf"); |
381 | 1.18k | AddFile(hasher, "/etc/timezone"); |
382 | 1.18k | AddFile(hasher, "/etc/localtime"); |
383 | 1.18k | #endif |
384 | | |
385 | | // For MacOS/BSDs, gather data through sysctl instead of /proc. Not all of these |
386 | | // will exist on every system. |
387 | | #ifdef HAVE_SYSCTL |
388 | | # ifdef CTL_HW |
389 | | # ifdef HW_MACHINE |
390 | | AddSysctl<CTL_HW, HW_MACHINE>(hasher); |
391 | | # endif |
392 | | # ifdef HW_MODEL |
393 | | AddSysctl<CTL_HW, HW_MODEL>(hasher); |
394 | | # endif |
395 | | # ifdef HW_NCPU |
396 | | AddSysctl<CTL_HW, HW_NCPU>(hasher); |
397 | | # endif |
398 | | # ifdef HW_PHYSMEM |
399 | | AddSysctl<CTL_HW, HW_PHYSMEM>(hasher); |
400 | | # endif |
401 | | # ifdef HW_USERMEM |
402 | | AddSysctl<CTL_HW, HW_USERMEM>(hasher); |
403 | | # endif |
404 | | # ifdef HW_MACHINE_ARCH |
405 | | AddSysctl<CTL_HW, HW_MACHINE_ARCH>(hasher); |
406 | | # endif |
407 | | # ifdef HW_REALMEM |
408 | | AddSysctl<CTL_HW, HW_REALMEM>(hasher); |
409 | | # endif |
410 | | # ifdef HW_CPU_FREQ |
411 | | AddSysctl<CTL_HW, HW_CPU_FREQ>(hasher); |
412 | | # endif |
413 | | # ifdef HW_BUS_FREQ |
414 | | AddSysctl<CTL_HW, HW_BUS_FREQ>(hasher); |
415 | | # endif |
416 | | # ifdef HW_CACHELINE |
417 | | AddSysctl<CTL_HW, HW_CACHELINE>(hasher); |
418 | | # endif |
419 | | # endif |
420 | | # ifdef CTL_KERN |
421 | | # ifdef KERN_BOOTFILE |
422 | | AddSysctl<CTL_KERN, KERN_BOOTFILE>(hasher); |
423 | | # endif |
424 | | # ifdef KERN_BOOTTIME |
425 | | AddSysctl<CTL_KERN, KERN_BOOTTIME>(hasher); |
426 | | # endif |
427 | | # ifdef KERN_CLOCKRATE |
428 | | AddSysctl<CTL_KERN, KERN_CLOCKRATE>(hasher); |
429 | | # endif |
430 | | # ifdef KERN_HOSTID |
431 | | AddSysctl<CTL_KERN, KERN_HOSTID>(hasher); |
432 | | # endif |
433 | | # ifdef KERN_HOSTUUID |
434 | | AddSysctl<CTL_KERN, KERN_HOSTUUID>(hasher); |
435 | | # endif |
436 | | # ifdef KERN_HOSTNAME |
437 | | AddSysctl<CTL_KERN, KERN_HOSTNAME>(hasher); |
438 | | # endif |
439 | | # ifdef KERN_OSRELDATE |
440 | | AddSysctl<CTL_KERN, KERN_OSRELDATE>(hasher); |
441 | | # endif |
442 | | # ifdef KERN_OSRELEASE |
443 | | AddSysctl<CTL_KERN, KERN_OSRELEASE>(hasher); |
444 | | # endif |
445 | | # ifdef KERN_OSREV |
446 | | AddSysctl<CTL_KERN, KERN_OSREV>(hasher); |
447 | | # endif |
448 | | # ifdef KERN_OSTYPE |
449 | | AddSysctl<CTL_KERN, KERN_OSTYPE>(hasher); |
450 | | # endif |
451 | | # ifdef KERN_POSIX1 |
452 | | AddSysctl<CTL_KERN, KERN_OSREV>(hasher); |
453 | | # endif |
454 | | # ifdef KERN_VERSION |
455 | | AddSysctl<CTL_KERN, KERN_VERSION>(hasher); |
456 | | # endif |
457 | | # endif |
458 | | #endif |
459 | | |
460 | | // Env variables |
461 | 1.18k | if (environ) { |
462 | 38.9k | for (size_t i = 0; environ[i]; ++i) { |
463 | 37.7k | hasher.Write((const unsigned char*)environ[i], strlen(environ[i])); |
464 | 37.7k | } |
465 | 1.18k | } |
466 | | |
467 | | // Process, thread, user, session, group, ... ids. |
468 | | #ifdef WIN32 |
469 | | hasher << GetCurrentProcessId() << GetCurrentThreadId(); |
470 | | #else |
471 | 1.18k | hasher << getpid() << getppid() << getsid(0) << getpgid(0) << getuid() << geteuid() << getgid() << getegid(); |
472 | 1.18k | #endif |
473 | 1.18k | hasher << std::this_thread::get_id(); |
474 | 1.18k | } |