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