Coverage Report

Created: 2026-07-14 18:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
1.42M
CSHA512& operator<<(CSHA512& hasher, const T& data) {
78
1.42M
    static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want");
79
1.42M
    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.42M
    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.42M
    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.42M
    hasher.Write((const unsigned char*)&data, sizeof(data));
83
1.42M
    return hasher;
84
1.42M
}
randomenv.cpp:CSHA512& (anonymous namespace)::operator<< <stat>(CSHA512&, stat const&)
Line
Count
Source
77
670k
CSHA512& operator<<(CSHA512& hasher, const T& data) {
78
670k
    static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want");
79
670k
    static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
80
670k
    static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want");
81
670k
    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
670k
    hasher.Write((const unsigned char*)&data, sizeof(data));
83
670k
    return hasher;
84
670k
}
randomenv.cpp:CSHA512& (anonymous namespace)::operator<< <unsigned int>(CSHA512&, unsigned int const&)
Line
Count
Source
77
10.4k
CSHA512& operator<<(CSHA512& hasher, const T& data) {
78
10.4k
    static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want");
79
10.4k
    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.4k
    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.4k
    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.4k
    hasher.Write((const unsigned char*)&data, sizeof(data));
83
10.4k
    return hasher;
84
10.4k
}
randomenv.cpp:CSHA512& (anonymous namespace)::operator<< <timespec>(CSHA512&, timespec const&)
Line
Count
Source
77
223k
CSHA512& operator<<(CSHA512& hasher, const T& data) {
78
223k
    static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want");
79
223k
    static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
80
223k
    static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want");
81
223k
    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
223k
    hasher.Write((const unsigned char*)&data, sizeof(data));
83
223k
    return hasher;
84
223k
}
randomenv.cpp:CSHA512& (anonymous namespace)::operator<< <timeval>(CSHA512&, timeval const&)
Line
Count
Source
77
74.3k
CSHA512& operator<<(CSHA512& hasher, const T& data) {
78
74.3k
    static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want");
79
74.3k
    static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
80
74.3k
    static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want");
81
74.3k
    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
74.3k
    hasher.Write((const unsigned char*)&data, sizeof(data));
83
74.3k
    return hasher;
84
74.3k
}
randomenv.cpp:CSHA512& (anonymous namespace)::operator<< <long>(CSHA512&, long const&)
Line
Count
Source
77
223k
CSHA512& operator<<(CSHA512& hasher, const T& data) {
78
223k
    static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want");
79
223k
    static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
80
223k
    static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want");
81
223k
    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
223k
    hasher.Write((const unsigned char*)&data, sizeof(data));
83
223k
    return hasher;
84
223k
}
randomenv.cpp:CSHA512& (anonymous namespace)::operator<< <rusage>(CSHA512&, rusage const&)
Line
Count
Source
77
74.3k
CSHA512& operator<<(CSHA512& hasher, const T& data) {
78
74.3k
    static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want");
79
74.3k
    static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
80
74.3k
    static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want");
81
74.3k
    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
74.3k
    hasher.Write((const unsigned char*)&data, sizeof(data));
83
74.3k
    return hasher;
84
74.3k
}
randomenv.cpp:CSHA512& (anonymous namespace)::operator<< <void**>(CSHA512&, void** const&)
Line
Count
Source
77
74.4k
CSHA512& operator<<(CSHA512& hasher, const T& data) {
78
74.4k
    static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want");
79
74.4k
    static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
80
74.4k
    static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want");
81
74.4k
    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
74.4k
    hasher.Write((const unsigned char*)&data, sizeof(data));
83
74.4k
    return hasher;
84
74.4k
}
randomenv.cpp:CSHA512& (anonymous namespace)::operator<< <void*>(CSHA512&, void* const&)
Line
Count
Source
77
74.4k
CSHA512& operator<<(CSHA512& hasher, const T& data) {
78
74.4k
    static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want");
79
74.4k
    static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
80
74.4k
    static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want");
81
74.4k
    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
74.4k
    hasher.Write((const unsigned char*)&data, sizeof(data));
83
74.4k
    return hasher;
84
74.4k
}
randomenv.cpp:CSHA512& (anonymous namespace)::operator<< <bool>(CSHA512&, bool const&)
Line
Count
Source
77
27
CSHA512& operator<<(CSHA512& hasher, const T& data) {
78
27
    static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want");
79
27
    static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
80
27
    static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want");
81
27
    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
27
    hasher.Write((const unsigned char*)&data, sizeof(data));
83
27
    return hasher;
84
27
}
randomenv.cpp:CSHA512& (anonymous namespace)::operator<< <unsigned long>(CSHA512&, unsigned long const&)
Line
Count
Source
77
135
CSHA512& operator<<(CSHA512& hasher, const T& data) {
78
135
    static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want");
79
135
    static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
80
135
    static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want");
81
135
    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
135
    hasher.Write((const unsigned char*)&data, sizeof(data));
83
135
    return hasher;
84
135
}
randomenv.cpp:CSHA512& (anonymous namespace)::operator<< <int>(CSHA512&, int const&)
Line
Count
Source
77
243
CSHA512& operator<<(CSHA512& hasher, const T& data) {
78
243
    static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want");
79
243
    static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
80
243
    static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want");
81
243
    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
243
    hasher.Write((const unsigned char*)&data, sizeof(data));
83
243
    return hasher;
84
243
}
randomenv.cpp:CSHA512& (anonymous namespace)::operator<< <CSHA512*>(CSHA512&, CSHA512* const&)
Line
Count
Source
77
27
CSHA512& operator<<(CSHA512& hasher, const T& data) {
78
27
    static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want");
79
27
    static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
80
27
    static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want");
81
27
    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
27
    hasher.Write((const unsigned char*)&data, sizeof(data));
83
27
    return hasher;
84
27
}
randomenv.cpp:CSHA512& (anonymous namespace)::operator<< <void (*)(CSHA512&)>(CSHA512&, void (* const&)(CSHA512&))
Line
Count
Source
77
27
CSHA512& operator<<(CSHA512& hasher, const T& data) {
78
27
    static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want");
79
27
    static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
80
27
    static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want");
81
27
    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
27
    hasher.Write((const unsigned char*)&data, sizeof(data));
83
27
    return hasher;
84
27
}
randomenv.cpp:CSHA512& (anonymous namespace)::operator<< <void* (*)(unsigned long) noexcept>(CSHA512&, void* (* const&)(unsigned long) noexcept)
Line
Count
Source
77
27
CSHA512& operator<<(CSHA512& hasher, const T& data) {
78
27
    static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want");
79
27
    static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
80
27
    static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want");
81
27
    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
27
    hasher.Write((const unsigned char*)&data, sizeof(data));
83
27
    return hasher;
84
27
}
randomenv.cpp:CSHA512& (anonymous namespace)::operator<< <int*>(CSHA512&, int* const&)
Line
Count
Source
77
27
CSHA512& operator<<(CSHA512& hasher, const T& data) {
78
27
    static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want");
79
27
    static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
80
27
    static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want");
81
27
    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
27
    hasher.Write((const unsigned char*)&data, sizeof(data));
83
27
    return hasher;
84
27
}
randomenv.cpp:CSHA512& (anonymous namespace)::operator<< <char***>(CSHA512&, char*** const&)
Line
Count
Source
77
27
CSHA512& operator<<(CSHA512& hasher, const T& data) {
78
27
    static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want");
79
27
    static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
80
27
    static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want");
81
27
    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
27
    hasher.Write((const unsigned char*)&data, sizeof(data));
83
27
    return hasher;
84
27
}
randomenv.cpp:CSHA512& (anonymous namespace)::operator<< <std::thread::id>(CSHA512&, std::thread::id const&)
Line
Count
Source
77
27
CSHA512& operator<<(CSHA512& hasher, const T& data) {
78
27
    static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want");
79
27
    static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
80
27
    static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want");
81
27
    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
27
    hasher.Write((const unsigned char*)&data, sizeof(data));
83
27
    return hasher;
84
27
}
85
86
#ifndef WIN32
87
void AddSockaddr(CSHA512& hasher, const struct sockaddr *addr)
88
405
{
89
405
    if (addr == nullptr) return;
  Branch (89:9): [True: 81, False: 324]
90
324
    switch (addr->sa_family) {
91
162
    case AF_INET:
  Branch (91:5): [True: 162, False: 162]
92
162
        hasher.Write((const unsigned char*)addr, sizeof(sockaddr_in));
93
162
        break;
94
54
    case AF_INET6:
  Branch (94:5): [True: 54, False: 270]
95
54
        hasher.Write((const unsigned char*)addr, sizeof(sockaddr_in6));
96
54
        break;
97
108
    default:
  Branch (97:5): [True: 108, False: 216]
98
108
        hasher.Write((const unsigned char*)&addr->sa_family, sizeof(addr->sa_family));
99
324
    }
100
324
}
101
102
void AddFile(CSHA512& hasher, const char *path)
103
669k
{
104
669k
    struct stat sb = {};
105
669k
    int f = open(path, O_RDONLY);
106
669k
    size_t total = 0;
107
669k
    if (f != -1) {
  Branch (107:9): [True: 669k, False: 18.4E]
108
669k
        unsigned char fbuf[4096];
109
669k
        int n;
110
669k
        hasher.Write((const unsigned char*)&f, sizeof(f));
111
669k
        if (fstat(f, &sb) == 0) hasher << sb;
  Branch (111:13): [True: 669k, False: 18.4E]
112
893k
        do {
113
893k
            n = read(f, fbuf, sizeof(fbuf));
114
893k
            if (n > 0) hasher.Write(fbuf, n);
  Branch (114:17): [True: 893k, False: 18.4E]
115
893k
            total += n;
116
            /* not bothering with EINTR handling. */
117
893k
        } while (n == sizeof(fbuf) && total < 1_MiB); // Read only the first 1 Mbyte
  Branch (117:18): [True: 223k, False: 669k]
  Branch (117:39): [True: 223k, False: 0]
118
669k
        close(f);
119
669k
    }
120
669k
}
121
122
void AddPath(CSHA512& hasher, const char *path)
123
135
{
124
135
    struct stat sb = {};
125
135
    if (stat(path, &sb) == 0) {
  Branch (125:9): [True: 135, False: 0]
126
135
        hasher.Write((const unsigned char*)path, strlen(path) + 1);
127
135
        hasher << sb;
128
135
    }
129
135
}
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
1.72k
{
153
1.72k
    GetCPUID(leaf, subleaf, ax, bx, cx, dx);
154
1.72k
    hasher << leaf << subleaf << ax << bx << cx << dx;
155
1.72k
}
156
157
void AddAllCPUID(CSHA512& hasher)
158
27
{
159
27
    uint32_t ax, bx, cx, dx;
160
    // Iterate over all standard leaves
161
27
    AddCPUID(hasher, 0, 0, ax, bx, cx, dx); // Returns max leaf in ax
162
27
    uint32_t max = ax;
163
459
    for (uint32_t leaf = 1; leaf <= max && leaf <= 0xFF; ++leaf) {
  Branch (163:29): [True: 432, False: 27]
  Branch (163:44): [True: 432, False: 0]
164
432
        uint32_t maxsub = 0;
165
594
        for (uint32_t subleaf = 0; subleaf <= 0xFF; ++subleaf) {
  Branch (165:36): [True: 594, False: 0]
166
594
            AddCPUID(hasher, leaf, subleaf, ax, bx, cx, dx);
167
            // Iterate subleafs for leaf values 4, 7, 11, 13
168
594
            if (leaf == 4) {
  Branch (168:17): [True: 27, False: 567]
169
27
                if ((ax & 0x1f) == 0) break;
  Branch (169:21): [True: 27, False: 0]
170
567
            } else if (leaf == 7) {
  Branch (170:24): [True: 54, False: 513]
171
54
                if (subleaf == 0) maxsub = ax;
  Branch (171:21): [True: 27, False: 27]
172
54
                if (subleaf == maxsub) break;
  Branch (172:21): [True: 27, False: 27]
173
513
            } else if (leaf == 11) {
  Branch (173:24): [True: 81, False: 432]
174
81
                if ((cx & 0xff00) == 0) break;
  Branch (174:21): [True: 27, False: 54]
175
432
            } else if (leaf == 13) {
  Branch (175:24): [True: 108, False: 324]
176
108
                if (ax == 0 && bx == 0 && cx == 0 && dx == 0) break;
  Branch (176:21): [True: 27, False: 81]
  Branch (176:32): [True: 27, False: 0]
  Branch (176:43): [True: 27, False: 0]
  Branch (176:54): [True: 27, False: 0]
177
324
            } else {
178
                // For any other leaf, stop after subleaf 0.
179
324
                break;
180
324
            }
181
594
        }
182
432
    }
183
    // Iterate over all extended leaves
184
27
    AddCPUID(hasher, 0x80000000, 0, ax, bx, cx, dx); // Returns max extended leaf in ax
185
27
    uint32_t ext_max = ax;
186
1.10k
    for (uint32_t leaf = 0x80000001; leaf <= ext_max && leaf <= 0x800000FF; ++leaf) {
  Branch (186:38): [True: 1.08k, False: 27]
  Branch (186:57): [True: 1.08k, False: 0]
187
1.08k
        AddCPUID(hasher, leaf, 0, ax, bx, cx, dx);
188
1.08k
    }
189
27
}
190
#endif
191
} // namespace
192
193
void RandAddDynamicEnv(CSHA512& hasher)
194
74.3k
{
195
    // Various clocks
196
#ifdef WIN32
197
    FILETIME ftime;
198
    GetSystemTimeAsFileTime(&ftime);
199
    hasher << ftime;
200
#else
201
74.3k
    struct timespec ts = {};
202
74.3k
#    ifdef CLOCK_MONOTONIC
203
74.3k
    clock_gettime(CLOCK_MONOTONIC, &ts);
204
74.3k
    hasher << ts;
205
74.3k
#    endif
206
74.3k
#    ifdef CLOCK_REALTIME
207
74.3k
    clock_gettime(CLOCK_REALTIME, &ts);
208
74.3k
    hasher << ts;
209
74.3k
#    endif
210
74.3k
#    ifdef CLOCK_BOOTTIME
211
74.3k
    clock_gettime(CLOCK_BOOTTIME, &ts);
212
74.3k
    hasher << ts;
213
74.3k
#    endif
214
    // gettimeofday is available on all UNIX systems, but only has microsecond precision.
215
74.3k
    struct timeval tv = {};
216
74.3k
    gettimeofday(&tv, nullptr);
217
74.3k
    hasher << tv;
218
74.3k
#endif
219
    // Probably redundant, but also use all the standard library clocks:
220
74.3k
    hasher << std::chrono::system_clock::now().time_since_epoch().count();
221
74.3k
    hasher << std::chrono::steady_clock::now().time_since_epoch().count();
222
74.3k
    hasher << std::chrono::high_resolution_clock::now().time_since_epoch().count();
223
224
74.3k
#ifndef WIN32
225
    // Current resource usage.
226
74.3k
    struct rusage usage = {};
227
74.3k
    if (getrusage(RUSAGE_SELF, &usage) == 0) hasher << usage;
  Branch (227:9): [True: 74.3k, False: 18.4E]
228
74.3k
#endif
229
230
74.3k
#ifdef __linux__
231
74.3k
    AddFile(hasher, "/proc/diskstats");
232
74.3k
    AddFile(hasher, "/proc/vmstat");
233
74.3k
    AddFile(hasher, "/proc/schedstat");
234
74.3k
    AddFile(hasher, "/proc/zoneinfo");
235
74.3k
    AddFile(hasher, "/proc/meminfo");
236
74.3k
    AddFile(hasher, "/proc/softirqs");
237
74.3k
    AddFile(hasher, "/proc/stat");
238
74.3k
    AddFile(hasher, "/proc/self/schedstat");
239
74.3k
    AddFile(hasher, "/proc/self/status");
240
74.3k
#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
74.3k
    void* addr = malloc(4097);
268
74.3k
    hasher << &addr << addr;
269
74.3k
    free(addr);
270
74.3k
}
271
272
void RandAddStaticEnv(CSHA512& hasher)
273
27
{
274
    // Some compile-time static properties
275
27
    hasher << (CHAR_MIN < 0) << sizeof(void*) << sizeof(long) << sizeof(int);
276
27
#if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
277
27
    hasher << __GNUC__ << __GNUC_MINOR__ << __GNUC_PATCHLEVEL__;
278
27
#endif
279
#ifdef _MSC_VER
280
    hasher << _MSC_VER;
281
#endif
282
27
    hasher << __cplusplus;
283
27
#ifdef _XOPEN_VERSION
284
27
    hasher << _XOPEN_VERSION;
285
27
#endif
286
27
#ifdef __VERSION__
287
27
    const char* COMPILER_VERSION = __VERSION__;
288
27
    hasher.Write((const unsigned char*)COMPILER_VERSION, strlen(COMPILER_VERSION) + 1);
289
27
#endif
290
291
    // Bitcoin client version
292
27
    hasher << CLIENT_VERSION;
293
294
27
#if defined(HAVE_STRONG_GETAUXVAL)
295
    // Information available through getauxval()
296
27
#  ifdef AT_HWCAP
297
27
    hasher << getauxval(AT_HWCAP);
298
27
#  endif
299
27
#  ifdef AT_HWCAP2
300
27
    hasher << getauxval(AT_HWCAP2);
301
27
#  endif
302
27
#  ifdef AT_RANDOM
303
27
    const unsigned char* random_aux = (const unsigned char*)getauxval(AT_RANDOM);
304
27
    if (random_aux) hasher.Write(random_aux, 16);
  Branch (304:9): [True: 27, False: 0]
305
27
#  endif
306
27
#  ifdef AT_PLATFORM
307
27
    const char* platform_str = (const char*)getauxval(AT_PLATFORM);
308
27
    if (platform_str) hasher.Write((const unsigned char*)platform_str, strlen(platform_str) + 1);
  Branch (308:9): [True: 27, False: 0]
309
27
#  endif
310
27
#  ifdef AT_EXECFN
311
27
    const char* exec_str = (const char*)getauxval(AT_EXECFN);
312
27
    if (exec_str) hasher.Write((const unsigned char*)exec_str, strlen(exec_str) + 1);
  Branch (312:9): [True: 27, False: 0]
313
27
#  endif
314
27
#endif // HAVE_STRONG_GETAUXVAL
315
316
27
#ifdef HAVE_GETCPUID
317
27
    AddAllCPUID(hasher);
318
27
#endif
319
320
    // Memory locations
321
27
    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
27
    char hname[256];
333
27
    if (gethostname(hname, 256) == 0) {
  Branch (333:9): [True: 27, False: 0]
334
27
        hasher.Write((const unsigned char*)hname, strnlen(hname, 256));
335
27
    }
336
27
#endif
337
338
27
#ifdef HAVE_IFADDRS
339
    // Network interfaces
340
27
    struct ifaddrs *ifad = nullptr;
341
27
    getifaddrs(&ifad);
342
27
    struct ifaddrs *ifit = ifad;
343
162
    while (ifit != nullptr) {
  Branch (343:12): [True: 135, False: 27]
344
135
        hasher.Write((const unsigned char*)&ifit, sizeof(ifit));
345
135
        hasher.Write((const unsigned char*)ifit->ifa_name, strlen(ifit->ifa_name) + 1);
346
135
        hasher.Write((const unsigned char*)&ifit->ifa_flags, sizeof(ifit->ifa_flags));
347
135
        AddSockaddr(hasher, ifit->ifa_addr);
348
135
        AddSockaddr(hasher, ifit->ifa_netmask);
349
135
        AddSockaddr(hasher, ifit->ifa_dstaddr);
350
135
        ifit = ifit->ifa_next;
351
135
    }
352
27
    freeifaddrs(ifad);
353
27
#endif
354
355
27
#ifndef WIN32
356
    // UNIX kernel information
357
27
    struct utsname name;
358
27
    if (uname(&name) != -1) {
  Branch (358:9): [True: 27, False: 0]
359
27
        hasher.Write((const unsigned char*)&name.sysname, strlen(name.sysname) + 1);
360
27
        hasher.Write((const unsigned char*)&name.nodename, strlen(name.nodename) + 1);
361
27
        hasher.Write((const unsigned char*)&name.release, strlen(name.release) + 1);
362
27
        hasher.Write((const unsigned char*)&name.version, strlen(name.version) + 1);
363
27
        hasher.Write((const unsigned char*)&name.machine, strlen(name.machine) + 1);
364
27
    }
365
366
    /* Path and filesystem provided data */
367
27
    AddPath(hasher, "/");
368
27
    AddPath(hasher, ".");
369
27
    AddPath(hasher, "/tmp");
370
27
    AddPath(hasher, "/home");
371
27
    AddPath(hasher, "/proc");
372
27
#ifdef __linux__
373
27
    AddFile(hasher, "/proc/cmdline");
374
27
    AddFile(hasher, "/proc/cpuinfo");
375
27
    AddFile(hasher, "/proc/version");
376
27
#endif
377
27
    AddFile(hasher, "/etc/passwd");
378
27
    AddFile(hasher, "/etc/group");
379
27
    AddFile(hasher, "/etc/hosts");
380
27
    AddFile(hasher, "/etc/resolv.conf");
381
27
    AddFile(hasher, "/etc/timezone");
382
27
    AddFile(hasher, "/etc/localtime");
383
27
#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
27
    if (environ) {
  Branch (461:9): [True: 27, False: 0]
462
351
        for (size_t i = 0; environ[i]; ++i) {
  Branch (462:28): [True: 324, False: 27]
463
324
            hasher.Write((const unsigned char*)environ[i], strlen(environ[i]));
464
324
        }
465
27
    }
466
467
    // Process, thread, user, session, group, ... ids.
468
#ifdef WIN32
469
    hasher << GetCurrentProcessId() << GetCurrentThreadId();
470
#else
471
27
    hasher << getpid() << getppid() << getsid(0) << getpgid(0) << getuid() << geteuid() << getgid() << getegid();
472
27
#endif
473
27
    hasher << std::this_thread::get_id();
474
27
}