Coverage Report

Created: 2025-06-10 13:21

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-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
4.91M
CSHA512& operator<<(CSHA512& hasher, const T& data) {
73
4.91M
    static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want");
74
4.91M
    static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
75
4.91M
    static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want");
76
4.91M
    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
4.91M
    hasher.Write((const unsigned char*)&data, sizeof(data));
78
4.91M
    return hasher;
79
4.91M
}
randomenv.cpp:CSHA512& (anonymous namespace)::operator<< <stat>(CSHA512&, stat const&)
Line
Count
Source
72
255k
CSHA512& operator<<(CSHA512& hasher, const T& data) {
73
255k
    static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want");
74
255k
    static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
75
255k
    static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want");
76
255k
    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
255k
    hasher.Write((const unsigned char*)&data, sizeof(data));
78
255k
    return hasher;
79
255k
}
randomenv.cpp:CSHA512& (anonymous namespace)::operator<< <unsigned int>(CSHA512&, unsigned int const&)
Line
Count
Source
72
4.30M
CSHA512& operator<<(CSHA512& hasher, const T& data) {
73
4.30M
    static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want");
74
4.30M
    static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
75
4.30M
    static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want");
76
4.30M
    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
4.30M
    hasher.Write((const unsigned char*)&data, sizeof(data));
78
4.30M
    return hasher;
79
4.30M
}
randomenv.cpp:CSHA512& (anonymous namespace)::operator<< <timespec>(CSHA512&, timespec const&)
Line
Count
Source
72
33.2k
CSHA512& operator<<(CSHA512& hasher, const T& data) {
73
33.2k
    static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want");
74
33.2k
    static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
75
33.2k
    static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want");
76
33.2k
    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
33.2k
    hasher.Write((const unsigned char*)&data, sizeof(data));
78
33.2k
    return hasher;
79
33.2k
}
randomenv.cpp:CSHA512& (anonymous namespace)::operator<< <timeval>(CSHA512&, timeval const&)
Line
Count
Source
72
11.0k
CSHA512& operator<<(CSHA512& hasher, const T& data) {
73
11.0k
    static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want");
74
11.0k
    static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
75
11.0k
    static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want");
76
11.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");
77
11.0k
    hasher.Write((const unsigned char*)&data, sizeof(data));
78
11.0k
    return hasher;
79
11.0k
}
randomenv.cpp:CSHA512& (anonymous namespace)::operator<< <long>(CSHA512&, long const&)
Line
Count
Source
72
44.3k
CSHA512& operator<<(CSHA512& hasher, const T& data) {
73
44.3k
    static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want");
74
44.3k
    static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
75
44.3k
    static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want");
76
44.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");
77
44.3k
    hasher.Write((const unsigned char*)&data, sizeof(data));
78
44.3k
    return hasher;
79
44.3k
}
randomenv.cpp:CSHA512& (anonymous namespace)::operator<< <rusage>(CSHA512&, rusage const&)
Line
Count
Source
72
11.0k
CSHA512& operator<<(CSHA512& hasher, const T& data) {
73
11.0k
    static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want");
74
11.0k
    static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
75
11.0k
    static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want");
76
11.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");
77
11.0k
    hasher.Write((const unsigned char*)&data, sizeof(data));
78
11.0k
    return hasher;
79
11.0k
}
randomenv.cpp:CSHA512& (anonymous namespace)::operator<< <void**>(CSHA512&, void** const&)
Line
Count
Source
72
11.0k
CSHA512& operator<<(CSHA512& hasher, const T& data) {
73
11.0k
    static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want");
74
11.0k
    static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
75
11.0k
    static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want");
76
11.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");
77
11.0k
    hasher.Write((const unsigned char*)&data, sizeof(data));
78
11.0k
    return hasher;
79
11.0k
}
randomenv.cpp:CSHA512& (anonymous namespace)::operator<< <void*>(CSHA512&, void* const&)
Line
Count
Source
72
11.0k
CSHA512& operator<<(CSHA512& hasher, const T& data) {
73
11.0k
    static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want");
74
11.0k
    static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
75
11.0k
    static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want");
76
11.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");
77
11.0k
    hasher.Write((const unsigned char*)&data, sizeof(data));
78
11.0k
    return hasher;
79
11.0k
}
randomenv.cpp:CSHA512& (anonymous namespace)::operator<< <bool>(CSHA512&, bool const&)
Line
Count
Source
72
11.0k
CSHA512& operator<<(CSHA512& hasher, const T& data) {
73
11.0k
    static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want");
74
11.0k
    static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
75
11.0k
    static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want");
76
11.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");
77
11.0k
    hasher.Write((const unsigned char*)&data, sizeof(data));
78
11.0k
    return hasher;
79
11.0k
}
randomenv.cpp:CSHA512& (anonymous namespace)::operator<< <unsigned long>(CSHA512&, unsigned long const&)
Line
Count
Source
72
55.4k
CSHA512& operator<<(CSHA512& hasher, const T& data) {
73
55.4k
    static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want");
74
55.4k
    static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
75
55.4k
    static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want");
76
55.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");
77
55.4k
    hasher.Write((const unsigned char*)&data, sizeof(data));
78
55.4k
    return hasher;
79
55.4k
}
randomenv.cpp:CSHA512& (anonymous namespace)::operator<< <int>(CSHA512&, int const&)
Line
Count
Source
72
99.8k
CSHA512& operator<<(CSHA512& hasher, const T& data) {
73
99.8k
    static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want");
74
99.8k
    static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
75
99.8k
    static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want");
76
99.8k
    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
99.8k
    hasher.Write((const unsigned char*)&data, sizeof(data));
78
99.8k
    return hasher;
79
99.8k
}
randomenv.cpp:CSHA512& (anonymous namespace)::operator<< <CSHA512*>(CSHA512&, CSHA512* const&)
Line
Count
Source
72
11.0k
CSHA512& operator<<(CSHA512& hasher, const T& data) {
73
11.0k
    static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want");
74
11.0k
    static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
75
11.0k
    static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want");
76
11.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");
77
11.0k
    hasher.Write((const unsigned char*)&data, sizeof(data));
78
11.0k
    return hasher;
79
11.0k
}
randomenv.cpp:CSHA512& (anonymous namespace)::operator<< <void (*)(CSHA512&)>(CSHA512&, void (* const&)(CSHA512&))
Line
Count
Source
72
11.0k
CSHA512& operator<<(CSHA512& hasher, const T& data) {
73
11.0k
    static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want");
74
11.0k
    static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
75
11.0k
    static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want");
76
11.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");
77
11.0k
    hasher.Write((const unsigned char*)&data, sizeof(data));
78
11.0k
    return hasher;
79
11.0k
}
randomenv.cpp:CSHA512& (anonymous namespace)::operator<< <void* (*)(unsigned long) noexcept>(CSHA512&, void* (* const&)(unsigned long) noexcept)
Line
Count
Source
72
11.0k
CSHA512& operator<<(CSHA512& hasher, const T& data) {
73
11.0k
    static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want");
74
11.0k
    static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
75
11.0k
    static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want");
76
11.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");
77
11.0k
    hasher.Write((const unsigned char*)&data, sizeof(data));
78
11.0k
    return hasher;
79
11.0k
}
randomenv.cpp:CSHA512& (anonymous namespace)::operator<< <int*>(CSHA512&, int* const&)
Line
Count
Source
72
11.0k
CSHA512& operator<<(CSHA512& hasher, const T& data) {
73
11.0k
    static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want");
74
11.0k
    static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
75
11.0k
    static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want");
76
11.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");
77
11.0k
    hasher.Write((const unsigned char*)&data, sizeof(data));
78
11.0k
    return hasher;
79
11.0k
}
randomenv.cpp:CSHA512& (anonymous namespace)::operator<< <char***>(CSHA512&, char*** const&)
Line
Count
Source
72
11.0k
CSHA512& operator<<(CSHA512& hasher, const T& data) {
73
11.0k
    static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want");
74
11.0k
    static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
75
11.0k
    static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want");
76
11.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");
77
11.0k
    hasher.Write((const unsigned char*)&data, sizeof(data));
78
11.0k
    return hasher;
79
11.0k
}
randomenv.cpp:CSHA512& (anonymous namespace)::operator<< <std::thread::id>(CSHA512&, std::thread::id const&)
Line
Count
Source
72
11.0k
CSHA512& operator<<(CSHA512& hasher, const T& data) {
73
11.0k
    static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want");
74
11.0k
    static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
75
11.0k
    static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want");
76
11.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");
77
11.0k
    hasher.Write((const unsigned char*)&data, sizeof(data));
78
11.0k
    return hasher;
79
11.0k
}
80
81
#ifndef WIN32
82
void AddSockaddr(CSHA512& hasher, const struct sockaddr *addr)
83
166k
{
84
166k
    if (addr == nullptr) return;
  Branch (84:9): [True: 33.2k, False: 133k]
85
133k
    switch (addr->sa_family) {
86
66.5k
    case AF_INET:
  Branch (86:5): [True: 66.5k, False: 66.5k]
87
66.5k
        hasher.Write((const unsigned char*)addr, sizeof(sockaddr_in));
88
66.5k
        break;
89
22.1k
    case AF_INET6:
  Branch (89:5): [True: 22.1k, False: 110k]
90
22.1k
        hasher.Write((const unsigned char*)addr, sizeof(sockaddr_in6));
91
22.1k
        break;
92
44.3k
    default:
  Branch (92:5): [True: 44.3k, False: 88.7k]
93
44.3k
        hasher.Write((const unsigned char*)&addr->sa_family, sizeof(addr->sa_family));
94
133k
    }
95
133k
}
96
97
void AddFile(CSHA512& hasher, const char *path)
98
199k
{
99
199k
    struct stat sb = {};
100
199k
    int f = open(path, O_RDONLY);
101
199k
    size_t total = 0;
102
199k
    if (f != -1) {
  Branch (102:9): [True: 199k, False: 0]
103
199k
        unsigned char fbuf[4096];
104
199k
        int n;
105
199k
        hasher.Write((const unsigned char*)&f, sizeof(f));
106
199k
        if (fstat(f, &sb) == 0) hasher << sb;
  Branch (106:13): [True: 199k, False: 0]
107
244k
        do {
108
244k
            n = read(f, fbuf, sizeof(fbuf));
109
244k
            if (n > 0) hasher.Write(fbuf, n);
  Branch (109:17): [True: 244k, False: 0]
110
244k
            total += n;
111
            /* not bothering with EINTR handling. */
112
244k
        } while (n == sizeof(fbuf) && total < 1048576); // Read only the first 1 Mbyte
  Branch (112:18): [True: 44.3k, False: 199k]
  Branch (112:39): [True: 44.3k, False: 0]
113
199k
        close(f);
114
199k
    }
115
199k
}
116
117
void AddPath(CSHA512& hasher, const char *path)
118
55.4k
{
119
55.4k
    struct stat sb = {};
120
55.4k
    if (stat(path, &sb) == 0) {
  Branch (120:9): [True: 55.4k, False: 0]
121
55.4k
        hasher.Write((const unsigned char*)path, strlen(path) + 1);
122
55.4k
        hasher << sb;
123
55.4k
    }
124
55.4k
}
125
#endif
126
127
#ifdef HAVE_SYSCTL
128
template<int... S>
129
void AddSysctl(CSHA512& hasher)
130
{
131
    int CTL[sizeof...(S)] = {S...};
132
    unsigned char buffer[65536];
133
    size_t siz = 65536;
134
    int ret = sysctl(CTL, sizeof...(S), buffer, &siz, nullptr, 0);
135
    if (ret == 0 || (ret == -1 && errno == ENOMEM)) {
136
        hasher << sizeof(CTL);
137
        hasher.Write((const unsigned char*)CTL, sizeof(CTL));
138
        if (siz > sizeof(buffer)) siz = sizeof(buffer);
139
        hasher << siz;
140
        hasher.Write(buffer, siz);
141
    }
142
}
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
709k
{
148
709k
    GetCPUID(leaf, subleaf, ax, bx, cx, dx);
149
709k
    hasher << leaf << subleaf << ax << bx << cx << dx;
150
709k
}
151
152
void AddAllCPUID(CSHA512& hasher)
153
11.0k
{
154
11.0k
    uint32_t ax, bx, cx, dx;
155
    // Iterate over all standard leaves
156
11.0k
    AddCPUID(hasher, 0, 0, ax, bx, cx, dx); // Returns max leaf in ax
157
11.0k
    uint32_t max = ax;
158
188k
    for (uint32_t leaf = 1; leaf <= max && leaf <= 0xFF; ++leaf) {
  Branch (158:29): [True: 177k, False: 11.0k]
  Branch (158:44): [True: 177k, False: 0]
159
177k
        uint32_t maxsub = 0;
160
244k
        for (uint32_t subleaf = 0; subleaf <= 0xFF; ++subleaf) {
  Branch (160:36): [True: 244k, False: 0]
161
244k
            AddCPUID(hasher, leaf, subleaf, ax, bx, cx, dx);
162
            // Iterate subleafs for leaf values 4, 7, 11, 13
163
244k
            if (leaf == 4) {
  Branch (163:17): [True: 11.0k, False: 232k]
164
11.0k
                if ((ax & 0x1f) == 0) break;
  Branch (164:21): [True: 11.0k, False: 0]
165
232k
            } else if (leaf == 7) {
  Branch (165:24): [True: 22.1k, False: 210k]
166
22.1k
                if (subleaf == 0) maxsub = ax;
  Branch (166:21): [True: 11.0k, False: 11.0k]
167
22.1k
                if (subleaf == maxsub) break;
  Branch (167:21): [True: 11.0k, False: 11.0k]
168
210k
            } else if (leaf == 11) {
  Branch (168:24): [True: 33.2k, False: 177k]
169
33.2k
                if ((cx & 0xff00) == 0) break;
  Branch (169:21): [True: 11.0k, False: 22.1k]
170
177k
            } else if (leaf == 13) {
  Branch (170:24): [True: 44.3k, False: 133k]
171
44.3k
                if (ax == 0 && bx == 0 && cx == 0 && dx == 0) break;
  Branch (171:21): [True: 11.0k, False: 33.2k]
  Branch (171:32): [True: 11.0k, False: 0]
  Branch (171:43): [True: 11.0k, False: 0]
  Branch (171:54): [True: 11.0k, False: 0]
172
133k
            } else {
173
                // For any other leaf, stop after subleaf 0.
174
133k
                break;
175
133k
            }
176
244k
        }
177
177k
    }
178
    // Iterate over all extended leaves
179
11.0k
    AddCPUID(hasher, 0x80000000, 0, ax, bx, cx, dx); // Returns max extended leaf in ax
180
11.0k
    uint32_t ext_max = ax;
181
454k
    for (uint32_t leaf = 0x80000001; leaf <= ext_max && leaf <= 0x800000FF; ++leaf) {
  Branch (181:38): [True: 443k, False: 11.0k]
  Branch (181:57): [True: 443k, False: 0]
182
443k
        AddCPUID(hasher, leaf, 0, ax, bx, cx, dx);
183
443k
    }
184
11.0k
}
185
#endif
186
} // namespace
187
188
void RandAddDynamicEnv(CSHA512& hasher)
189
11.0k
{
190
    // Various clocks
191
#ifdef WIN32
192
    FILETIME ftime;
193
    GetSystemTimeAsFileTime(&ftime);
194
    hasher << ftime;
195
#else
196
11.0k
    struct timespec ts = {};
197
11.0k
#    ifdef CLOCK_MONOTONIC
198
11.0k
    clock_gettime(CLOCK_MONOTONIC, &ts);
199
11.0k
    hasher << ts;
200
11.0k
#    endif
201
11.0k
#    ifdef CLOCK_REALTIME
202
11.0k
    clock_gettime(CLOCK_REALTIME, &ts);
203
11.0k
    hasher << ts;
204
11.0k
#    endif
205
11.0k
#    ifdef CLOCK_BOOTTIME
206
11.0k
    clock_gettime(CLOCK_BOOTTIME, &ts);
207
11.0k
    hasher << ts;
208
11.0k
#    endif
209
    // gettimeofday is available on all UNIX systems, but only has microsecond precision.
210
11.0k
    struct timeval tv = {};
211
11.0k
    gettimeofday(&tv, nullptr);
212
11.0k
    hasher << tv;
213
11.0k
#endif
214
    // Probably redundant, but also use all the standard library clocks:
215
11.0k
    hasher << std::chrono::system_clock::now().time_since_epoch().count();
216
11.0k
    hasher << std::chrono::steady_clock::now().time_since_epoch().count();
217
11.0k
    hasher << std::chrono::high_resolution_clock::now().time_since_epoch().count();
218
219
11.0k
#ifndef WIN32
220
    // Current resource usage.
221
11.0k
    struct rusage usage = {};
222
11.0k
    if (getrusage(RUSAGE_SELF, &usage) == 0) hasher << usage;
  Branch (222:9): [True: 11.0k, False: 0]
223
11.0k
#endif
224
225
11.0k
#ifdef __linux__
226
11.0k
    AddFile(hasher, "/proc/diskstats");
227
11.0k
    AddFile(hasher, "/proc/vmstat");
228
11.0k
    AddFile(hasher, "/proc/schedstat");
229
11.0k
    AddFile(hasher, "/proc/zoneinfo");
230
11.0k
    AddFile(hasher, "/proc/meminfo");
231
11.0k
    AddFile(hasher, "/proc/softirqs");
232
11.0k
    AddFile(hasher, "/proc/stat");
233
11.0k
    AddFile(hasher, "/proc/self/schedstat");
234
11.0k
    AddFile(hasher, "/proc/self/status");
235
11.0k
#endif
236
237
#ifdef HAVE_SYSCTL
238
#  ifdef CTL_KERN
239
#    if defined(KERN_PROC) && defined(KERN_PROC_ALL)
240
    AddSysctl<CTL_KERN, KERN_PROC, KERN_PROC_ALL>(hasher);
241
#    endif
242
#  endif
243
#  ifdef CTL_HW
244
#    ifdef HW_DISKSTATS
245
    AddSysctl<CTL_HW, HW_DISKSTATS>(hasher);
246
#    endif
247
#  endif
248
#  ifdef CTL_VM
249
#    ifdef VM_LOADAVG
250
    AddSysctl<CTL_VM, VM_LOADAVG>(hasher);
251
#    endif
252
#    ifdef VM_TOTAL
253
    AddSysctl<CTL_VM, VM_TOTAL>(hasher);
254
#    endif
255
#    ifdef VM_METER
256
    AddSysctl<CTL_VM, VM_METER>(hasher);
257
#    endif
258
#  endif
259
#endif
260
261
    // Stack and heap location
262
11.0k
    void* addr = malloc(4097);
263
11.0k
    hasher << &addr << addr;
264
11.0k
    free(addr);
265
11.0k
}
266
267
void RandAddStaticEnv(CSHA512& hasher)
268
11.0k
{
269
    // Some compile-time static properties
270
11.0k
    hasher << (CHAR_MIN < 0) << sizeof(void*) << sizeof(long) << sizeof(int);
271
11.0k
#if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
272
11.0k
    hasher << __GNUC__ << __GNUC_MINOR__ << __GNUC_PATCHLEVEL__;
273
11.0k
#endif
274
#ifdef _MSC_VER
275
    hasher << _MSC_VER;
276
#endif
277
11.0k
    hasher << __cplusplus;
278
11.0k
#ifdef _XOPEN_VERSION
279
11.0k
    hasher << _XOPEN_VERSION;
280
11.0k
#endif
281
11.0k
#ifdef __VERSION__
282
11.0k
    const char* COMPILER_VERSION = __VERSION__;
283
11.0k
    hasher.Write((const unsigned char*)COMPILER_VERSION, strlen(COMPILER_VERSION) + 1);
284
11.0k
#endif
285
286
    // Bitcoin client version
287
11.0k
    hasher << CLIENT_VERSION;
288
289
11.0k
#if defined(HAVE_STRONG_GETAUXVAL)
290
    // Information available through getauxval()
291
11.0k
#  ifdef AT_HWCAP
292
11.0k
    hasher << getauxval(AT_HWCAP);
293
11.0k
#  endif
294
11.0k
#  ifdef AT_HWCAP2
295
11.0k
    hasher << getauxval(AT_HWCAP2);
296
11.0k
#  endif
297
11.0k
#  ifdef AT_RANDOM
298
11.0k
    const unsigned char* random_aux = (const unsigned char*)getauxval(AT_RANDOM);
299
11.0k
    if (random_aux) hasher.Write(random_aux, 16);
  Branch (299:9): [True: 11.0k, False: 0]
300
11.0k
#  endif
301
11.0k
#  ifdef AT_PLATFORM
302
11.0k
    const char* platform_str = (const char*)getauxval(AT_PLATFORM);
303
11.0k
    if (platform_str) hasher.Write((const unsigned char*)platform_str, strlen(platform_str) + 1);
  Branch (303:9): [True: 11.0k, False: 0]
304
11.0k
#  endif
305
11.0k
#  ifdef AT_EXECFN
306
11.0k
    const char* exec_str = (const char*)getauxval(AT_EXECFN);
307
11.0k
    if (exec_str) hasher.Write((const unsigned char*)exec_str, strlen(exec_str) + 1);
  Branch (307:9): [True: 11.0k, False: 0]
308
11.0k
#  endif
309
11.0k
#endif // HAVE_STRONG_GETAUXVAL
310
311
11.0k
#ifdef HAVE_GETCPUID
312
11.0k
    AddAllCPUID(hasher);
313
11.0k
#endif
314
315
    // Memory locations
316
11.0k
    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
11.0k
    char hname[256];
328
11.0k
    if (gethostname(hname, 256) == 0) {
  Branch (328:9): [True: 11.0k, False: 0]
329
11.0k
        hasher.Write((const unsigned char*)hname, strnlen(hname, 256));
330
11.0k
    }
331
11.0k
#endif
332
333
11.0k
#ifdef HAVE_IFADDRS
334
    // Network interfaces
335
11.0k
    struct ifaddrs *ifad = nullptr;
336
11.0k
    getifaddrs(&ifad);
337
11.0k
    struct ifaddrs *ifit = ifad;
338
66.5k
    while (ifit != nullptr) {
  Branch (338:12): [True: 55.4k, False: 11.0k]
339
55.4k
        hasher.Write((const unsigned char*)&ifit, sizeof(ifit));
340
55.4k
        hasher.Write((const unsigned char*)ifit->ifa_name, strlen(ifit->ifa_name) + 1);
341
55.4k
        hasher.Write((const unsigned char*)&ifit->ifa_flags, sizeof(ifit->ifa_flags));
342
55.4k
        AddSockaddr(hasher, ifit->ifa_addr);
343
55.4k
        AddSockaddr(hasher, ifit->ifa_netmask);
344
55.4k
        AddSockaddr(hasher, ifit->ifa_dstaddr);
345
55.4k
        ifit = ifit->ifa_next;
346
55.4k
    }
347
11.0k
    freeifaddrs(ifad);
348
11.0k
#endif
349
350
11.0k
#ifndef WIN32
351
    // UNIX kernel information
352
11.0k
    struct utsname name;
353
11.0k
    if (uname(&name) != -1) {
  Branch (353:9): [True: 11.0k, False: 0]
354
11.0k
        hasher.Write((const unsigned char*)&name.sysname, strlen(name.sysname) + 1);
355
11.0k
        hasher.Write((const unsigned char*)&name.nodename, strlen(name.nodename) + 1);
356
11.0k
        hasher.Write((const unsigned char*)&name.release, strlen(name.release) + 1);
357
11.0k
        hasher.Write((const unsigned char*)&name.version, strlen(name.version) + 1);
358
11.0k
        hasher.Write((const unsigned char*)&name.machine, strlen(name.machine) + 1);
359
11.0k
    }
360
361
    /* Path and filesystem provided data */
362
11.0k
    AddPath(hasher, "/");
363
11.0k
    AddPath(hasher, ".");
364
11.0k
    AddPath(hasher, "/tmp");
365
11.0k
    AddPath(hasher, "/home");
366
11.0k
    AddPath(hasher, "/proc");
367
11.0k
#ifdef __linux__
368
11.0k
    AddFile(hasher, "/proc/cmdline");
369
11.0k
    AddFile(hasher, "/proc/cpuinfo");
370
11.0k
    AddFile(hasher, "/proc/version");
371
11.0k
#endif
372
11.0k
    AddFile(hasher, "/etc/passwd");
373
11.0k
    AddFile(hasher, "/etc/group");
374
11.0k
    AddFile(hasher, "/etc/hosts");
375
11.0k
    AddFile(hasher, "/etc/resolv.conf");
376
11.0k
    AddFile(hasher, "/etc/timezone");
377
11.0k
    AddFile(hasher, "/etc/localtime");
378
11.0k
#endif
379
380
    // For MacOS/BSDs, gather data through sysctl instead of /proc. Not all of these
381
    // will exist on every system.
382
#ifdef HAVE_SYSCTL
383
#  ifdef CTL_HW
384
#    ifdef HW_MACHINE
385
    AddSysctl<CTL_HW, HW_MACHINE>(hasher);
386
#    endif
387
#    ifdef HW_MODEL
388
    AddSysctl<CTL_HW, HW_MODEL>(hasher);
389
#    endif
390
#    ifdef HW_NCPU
391
    AddSysctl<CTL_HW, HW_NCPU>(hasher);
392
#    endif
393
#    ifdef HW_PHYSMEM
394
    AddSysctl<CTL_HW, HW_PHYSMEM>(hasher);
395
#    endif
396
#    ifdef HW_USERMEM
397
    AddSysctl<CTL_HW, HW_USERMEM>(hasher);
398
#    endif
399
#    ifdef HW_MACHINE_ARCH
400
    AddSysctl<CTL_HW, HW_MACHINE_ARCH>(hasher);
401
#    endif
402
#    ifdef HW_REALMEM
403
    AddSysctl<CTL_HW, HW_REALMEM>(hasher);
404
#    endif
405
#    ifdef HW_CPU_FREQ
406
    AddSysctl<CTL_HW, HW_CPU_FREQ>(hasher);
407
#    endif
408
#    ifdef HW_BUS_FREQ
409
    AddSysctl<CTL_HW, HW_BUS_FREQ>(hasher);
410
#    endif
411
#    ifdef HW_CACHELINE
412
    AddSysctl<CTL_HW, HW_CACHELINE>(hasher);
413
#    endif
414
#  endif
415
#  ifdef CTL_KERN
416
#    ifdef KERN_BOOTFILE
417
     AddSysctl<CTL_KERN, KERN_BOOTFILE>(hasher);
418
#    endif
419
#    ifdef KERN_BOOTTIME
420
     AddSysctl<CTL_KERN, KERN_BOOTTIME>(hasher);
421
#    endif
422
#    ifdef KERN_CLOCKRATE
423
     AddSysctl<CTL_KERN, KERN_CLOCKRATE>(hasher);
424
#    endif
425
#    ifdef KERN_HOSTID
426
     AddSysctl<CTL_KERN, KERN_HOSTID>(hasher);
427
#    endif
428
#    ifdef KERN_HOSTUUID
429
     AddSysctl<CTL_KERN, KERN_HOSTUUID>(hasher);
430
#    endif
431
#    ifdef KERN_HOSTNAME
432
     AddSysctl<CTL_KERN, KERN_HOSTNAME>(hasher);
433
#    endif
434
#    ifdef KERN_OSRELDATE
435
     AddSysctl<CTL_KERN, KERN_OSRELDATE>(hasher);
436
#    endif
437
#    ifdef KERN_OSRELEASE
438
     AddSysctl<CTL_KERN, KERN_OSRELEASE>(hasher);
439
#    endif
440
#    ifdef KERN_OSREV
441
     AddSysctl<CTL_KERN, KERN_OSREV>(hasher);
442
#    endif
443
#    ifdef KERN_OSTYPE
444
     AddSysctl<CTL_KERN, KERN_OSTYPE>(hasher);
445
#    endif
446
#    ifdef KERN_POSIX1
447
     AddSysctl<CTL_KERN, KERN_OSREV>(hasher);
448
#    endif
449
#    ifdef KERN_VERSION
450
     AddSysctl<CTL_KERN, KERN_VERSION>(hasher);
451
#    endif
452
#  endif
453
#endif
454
455
    // Env variables
456
11.0k
    if (environ) {
  Branch (456:9): [True: 11.0k, False: 0]
457
221k
        for (size_t i = 0; environ[i]; ++i) {
  Branch (457:28): [True: 210k, False: 11.0k]
458
210k
            hasher.Write((const unsigned char*)environ[i], strlen(environ[i]));
459
210k
        }
460
11.0k
    }
461
462
    // Process, thread, user, session, group, ... ids.
463
#ifdef WIN32
464
    hasher << GetCurrentProcessId() << GetCurrentThreadId();
465
#else
466
11.0k
    hasher << getpid() << getppid() << getsid(0) << getpgid(0) << getuid() << geteuid() << getgid() << getegid();
467
11.0k
#endif
468
11.0k
    hasher << std::this_thread::get_id();
469
11.0k
}