LCOV - code coverage report
Current view: top level - src - randomenv.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 98.0 % 151 148
Test Date: 2024-08-28 04:44:32 Functions: 100.0 % 24 24
Branches: 69.8 % 63 44

             Branch data     Line data    Source code
       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 <config/bitcoin-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                 :             : #include <winreg.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                 :             : #if HAVE_DECL_GETIFADDRS && HAVE_DECL_FREEIFADDRS
      43                 :             : #include <ifaddrs.h>
      44                 :             : #endif
      45                 :             : #ifdef HAVE_SYSCTL
      46                 :             : #include <sys/sysctl.h>
      47                 :             : #ifdef HAVE_VM_VM_PARAM_H
      48                 :             : #include <vm/vm_param.h>
      49                 :             : #endif
      50                 :             : #ifdef HAVE_SYS_RESOURCES_H
      51                 :             : #include <sys/resources.h>
      52                 :             : #endif
      53                 :             : #ifdef HAVE_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                 :             : #ifndef _MSC_VER
      62                 :             : extern char** environ; // NOLINT(readability-redundant-declaration): Necessary on some platforms
      63                 :             : #endif
      64                 :             : 
      65                 :             : namespace {
      66                 :             : 
      67                 :             : void RandAddSeedPerfmon(CSHA512& hasher)
      68                 :             : {
      69                 :             : #ifdef WIN32
      70                 :             :     // Seed with the entire set of perfmon data
      71                 :             : 
      72                 :             :     // This can take up to 2 seconds, so only do it every 10 minutes.
      73                 :             :     // Initialize last_perfmon to 0 seconds, we don't skip the first call.
      74                 :             :     static std::atomic<SteadyClock::time_point> last_perfmon{SteadyClock::time_point{0s}};
      75                 :             :     auto last_time = last_perfmon.load();
      76                 :             :     auto current_time = SteadyClock::now();
      77                 :             :     if (current_time < last_time + 10min) return;
      78                 :             :     last_perfmon = current_time;
      79                 :             : 
      80                 :             :     std::vector<unsigned char> vData(250000, 0);
      81                 :             :     long ret = 0;
      82                 :             :     unsigned long nSize = 0;
      83                 :             :     const size_t nMaxSize = 10000000; // Bail out at more than 10MB of performance data
      84                 :             :     while (true) {
      85                 :             :         nSize = vData.size();
      86                 :             :         ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", nullptr, nullptr, vData.data(), &nSize);
      87                 :             :         if (ret != ERROR_MORE_DATA || vData.size() >= nMaxSize)
      88                 :             :             break;
      89                 :             :         vData.resize(std::min((vData.size() * 3) / 2, nMaxSize)); // Grow size of buffer exponentially
      90                 :             :     }
      91                 :             :     RegCloseKey(HKEY_PERFORMANCE_DATA);
      92                 :             :     if (ret == ERROR_SUCCESS) {
      93                 :             :         hasher.Write(vData.data(), nSize);
      94                 :             :         memory_cleanse(vData.data(), nSize);
      95                 :             :     } else {
      96                 :             :         // Performance data is only a best-effort attempt at improving the
      97                 :             :         // situation when the OS randomness (and other sources) aren't
      98                 :             :         // adequate. As a result, failure to read it is isn't considered critical,
      99                 :             :         // so we don't call RandFailure().
     100                 :             :         // TODO: Add logging when the logger is made functional before global
     101                 :             :         // constructors have been invoked.
     102                 :             :     }
     103                 :             : #endif
     104                 :             : }
     105                 :             : 
     106                 :             : /** Helper to easily feed data into a CSHA512.
     107                 :             :  *
     108                 :             :  * Note that this does not serialize the passed object (like stream.h's << operators do).
     109                 :             :  * Its raw memory representation is used directly.
     110                 :             :  */
     111                 :             : template<typename T>
     112                 :       50518 : CSHA512& operator<<(CSHA512& hasher, const T& data) {
     113                 :             :     static_assert(!std::is_same<typename std::decay<T>::type, char*>::value, "Calling operator<<(CSHA512, char*) is probably not what you want");
     114                 :             :     static_assert(!std::is_same<typename std::decay<T>::type, unsigned char*>::value, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
     115                 :             :     static_assert(!std::is_same<typename std::decay<T>::type, const char*>::value, "Calling operator<<(CSHA512, const char*) is probably not what you want");
     116                 :             :     static_assert(!std::is_same<typename std::decay<T>::type, const unsigned char*>::value, "Calling operator<<(CSHA512, const unsigned char*) is probably not what you want");
     117                 :       50518 :     hasher.Write((const unsigned char*)&data, sizeof(data));
     118                 :       50518 :     return hasher;
     119                 :             : }
     120                 :             : 
     121                 :             : #ifndef WIN32
     122                 :        1608 : void AddSockaddr(CSHA512& hasher, const struct sockaddr *addr)
     123                 :             : {
     124         [ +  + ]:        1608 :     if (addr == nullptr) return;
     125      [ +  -  + ]:        1340 :     switch (addr->sa_family) {
     126                 :         804 :     case AF_INET:
     127                 :         804 :         hasher.Write((const unsigned char*)addr, sizeof(sockaddr_in));
     128                 :         804 :         break;
     129                 :           0 :     case AF_INET6:
     130                 :           0 :         hasher.Write((const unsigned char*)addr, sizeof(sockaddr_in6));
     131                 :           0 :         break;
     132                 :         536 :     default:
     133                 :         536 :         hasher.Write((const unsigned char*)&addr->sa_family, sizeof(addr->sa_family));
     134                 :             :     }
     135                 :             : }
     136                 :             : 
     137                 :        2412 : void AddFile(CSHA512& hasher, const char *path)
     138                 :             : {
     139                 :        2412 :     struct stat sb = {};
     140                 :        2412 :     int f = open(path, O_RDONLY);
     141                 :        2412 :     size_t total = 0;
     142         [ +  - ]:        2412 :     if (f != -1) {
     143                 :        2412 :         unsigned char fbuf[4096];
     144                 :        2412 :         int n;
     145                 :        2412 :         hasher.Write((const unsigned char*)&f, sizeof(f));
     146         [ +  - ]:        2412 :         if (fstat(f, &sb) == 0) hasher << sb;
     147                 :        2680 :         do {
     148                 :        2680 :             n = read(f, fbuf, sizeof(fbuf));
     149         [ +  - ]:        2680 :             if (n > 0) hasher.Write(fbuf, n);
     150                 :        2680 :             total += n;
     151                 :             :             /* not bothering with EINTR handling. */
     152         [ +  + ]:        2680 :         } while (n == sizeof(fbuf) && total < 1048576); // Read only the first 1 Mbyte
     153                 :        2412 :         close(f);
     154                 :             :     }
     155                 :        2412 : }
     156                 :             : 
     157                 :         670 : void AddPath(CSHA512& hasher, const char *path)
     158                 :             : {
     159                 :         670 :     struct stat sb = {};
     160         [ +  - ]:         670 :     if (stat(path, &sb) == 0) {
     161                 :         670 :         hasher.Write((const unsigned char*)path, strlen(path) + 1);
     162                 :         670 :         hasher << sb;
     163                 :             :     }
     164                 :         670 : }
     165                 :             : #endif
     166                 :             : 
     167                 :             : #ifdef HAVE_SYSCTL
     168                 :             : template<int... S>
     169                 :             : void AddSysctl(CSHA512& hasher)
     170                 :             : {
     171                 :             :     int CTL[sizeof...(S)] = {S...};
     172                 :             :     unsigned char buffer[65536];
     173                 :             :     size_t siz = 65536;
     174                 :             :     int ret = sysctl(CTL, sizeof...(S), buffer, &siz, nullptr, 0);
     175                 :             :     if (ret == 0 || (ret == -1 && errno == ENOMEM)) {
     176                 :             :         hasher << sizeof(CTL);
     177                 :             :         hasher.Write((const unsigned char*)CTL, sizeof(CTL));
     178                 :             :         if (siz > sizeof(buffer)) siz = sizeof(buffer);
     179                 :             :         hasher << siz;
     180                 :             :         hasher.Write(buffer, siz);
     181                 :             :     }
     182                 :             : }
     183                 :             : #endif
     184                 :             : 
     185                 :             : #ifdef HAVE_GETCPUID
     186                 :        7102 : void inline AddCPUID(CSHA512& hasher, uint32_t leaf, uint32_t subleaf, uint32_t& ax, uint32_t& bx, uint32_t& cx, uint32_t& dx)
     187                 :             : {
     188                 :        7102 :     GetCPUID(leaf, subleaf, ax, bx, cx, dx);
     189                 :        7102 :     hasher << leaf << subleaf << ax << bx << cx << dx;
     190                 :        7102 : }
     191                 :             : 
     192                 :         134 : void AddAllCPUID(CSHA512& hasher)
     193                 :             : {
     194                 :         134 :     uint32_t ax, bx, cx, dx;
     195                 :             :     // Iterate over all standard leaves
     196                 :         134 :     AddCPUID(hasher, 0, 0, ax, bx, cx, dx); // Returns max leaf in ax
     197                 :         134 :     uint32_t max = ax;
     198         [ +  + ]:        1876 :     for (uint32_t leaf = 1; leaf <= max && leaf <= 0xFF; ++leaf) {
     199                 :             :         uint32_t maxsub = 0;
     200         [ +  - ]:        2412 :         for (uint32_t subleaf = 0; subleaf <= 0xFF; ++subleaf) {
     201                 :        2412 :             AddCPUID(hasher, leaf, subleaf, ax, bx, cx, dx);
     202                 :             :             // Iterate subleafs for leaf values 4, 7, 11, 13
     203         [ +  + ]:        2412 :             if (leaf == 4) {
     204         [ -  + ]:         134 :                 if ((ax & 0x1f) == 0) break;
     205         [ +  + ]:        2278 :             } else if (leaf == 7) {
     206         [ +  - ]:         134 :                 if (subleaf == 0) maxsub = ax;
     207         [ -  + ]:         134 :                 if (subleaf == maxsub) break;
     208         [ +  + ]:        2144 :             } else if (leaf == 11) {
     209         [ +  + ]:         402 :                 if ((cx & 0xff00) == 0) break;
     210         [ +  + ]:        1742 :             } else if (leaf == 13) {
     211   [ +  +  +  -  :         536 :                 if (ax == 0 && bx == 0 && cx == 0 && dx == 0) break;
             +  -  -  + ]
     212                 :             :             } else {
     213                 :             :                 // For any other leaf, stop after subleaf 0.
     214                 :             :                 break;
     215                 :             :             }
     216                 :             :         }
     217                 :             :     }
     218                 :             :     // Iterate over all extended leaves
     219                 :         134 :     AddCPUID(hasher, 0x80000000, 0, ax, bx, cx, dx); // Returns max extended leaf in ax
     220                 :         134 :     uint32_t ext_max = ax;
     221         [ +  + ]:        4556 :     for (uint32_t leaf = 0x80000001; leaf <= ext_max && leaf <= 0x800000FF; ++leaf) {
     222                 :        4422 :         AddCPUID(hasher, leaf, 0, ax, bx, cx, dx);
     223                 :             :     }
     224                 :         134 : }
     225                 :             : #endif
     226                 :             : } // namespace
     227                 :             : 
     228                 :         134 : void RandAddDynamicEnv(CSHA512& hasher)
     229                 :             : {
     230                 :         134 :     RandAddSeedPerfmon(hasher);
     231                 :             : 
     232                 :             :     // Various clocks
     233                 :             : #ifdef WIN32
     234                 :             :     FILETIME ftime;
     235                 :             :     GetSystemTimeAsFileTime(&ftime);
     236                 :             :     hasher << ftime;
     237                 :             : #else
     238                 :         134 :     struct timespec ts = {};
     239                 :             : #    ifdef CLOCK_MONOTONIC
     240                 :         134 :     clock_gettime(CLOCK_MONOTONIC, &ts);
     241                 :         134 :     hasher << ts;
     242                 :             : #    endif
     243                 :             : #    ifdef CLOCK_REALTIME
     244                 :         134 :     clock_gettime(CLOCK_REALTIME, &ts);
     245                 :         134 :     hasher << ts;
     246                 :             : #    endif
     247                 :             : #    ifdef CLOCK_BOOTTIME
     248                 :         134 :     clock_gettime(CLOCK_BOOTTIME, &ts);
     249                 :         134 :     hasher << ts;
     250                 :             : #    endif
     251                 :             :     // gettimeofday is available on all UNIX systems, but only has microsecond precision.
     252                 :         134 :     struct timeval tv = {};
     253                 :         134 :     gettimeofday(&tv, nullptr);
     254                 :         134 :     hasher << tv;
     255                 :             : #endif
     256                 :             :     // Probably redundant, but also use all the standard library clocks:
     257                 :         134 :     hasher << std::chrono::system_clock::now().time_since_epoch().count();
     258                 :         134 :     hasher << std::chrono::steady_clock::now().time_since_epoch().count();
     259                 :         134 :     hasher << std::chrono::high_resolution_clock::now().time_since_epoch().count();
     260                 :             : 
     261                 :             : #ifndef WIN32
     262                 :             :     // Current resource usage.
     263                 :         134 :     struct rusage usage = {};
     264         [ +  - ]:         134 :     if (getrusage(RUSAGE_SELF, &usage) == 0) hasher << usage;
     265                 :             : #endif
     266                 :             : 
     267                 :             : #ifdef __linux__
     268                 :         134 :     AddFile(hasher, "/proc/diskstats");
     269                 :         134 :     AddFile(hasher, "/proc/vmstat");
     270                 :         134 :     AddFile(hasher, "/proc/schedstat");
     271                 :         134 :     AddFile(hasher, "/proc/zoneinfo");
     272                 :         134 :     AddFile(hasher, "/proc/meminfo");
     273                 :         134 :     AddFile(hasher, "/proc/softirqs");
     274                 :         134 :     AddFile(hasher, "/proc/stat");
     275                 :         134 :     AddFile(hasher, "/proc/self/schedstat");
     276                 :         134 :     AddFile(hasher, "/proc/self/status");
     277                 :             : #endif
     278                 :             : 
     279                 :             : #ifdef HAVE_SYSCTL
     280                 :             : #  ifdef CTL_KERN
     281                 :             : #    if defined(KERN_PROC) && defined(KERN_PROC_ALL)
     282                 :             :     AddSysctl<CTL_KERN, KERN_PROC, KERN_PROC_ALL>(hasher);
     283                 :             : #    endif
     284                 :             : #  endif
     285                 :             : #  ifdef CTL_HW
     286                 :             : #    ifdef HW_DISKSTATS
     287                 :             :     AddSysctl<CTL_HW, HW_DISKSTATS>(hasher);
     288                 :             : #    endif
     289                 :             : #  endif
     290                 :             : #  ifdef CTL_VM
     291                 :             : #    ifdef VM_LOADAVG
     292                 :             :     AddSysctl<CTL_VM, VM_LOADAVG>(hasher);
     293                 :             : #    endif
     294                 :             : #    ifdef VM_TOTAL
     295                 :             :     AddSysctl<CTL_VM, VM_TOTAL>(hasher);
     296                 :             : #    endif
     297                 :             : #    ifdef VM_METER
     298                 :             :     AddSysctl<CTL_VM, VM_METER>(hasher);
     299                 :             : #    endif
     300                 :             : #  endif
     301                 :             : #endif
     302                 :             : 
     303                 :             :     // Stack and heap location
     304                 :         134 :     void* addr = malloc(4097);
     305                 :         134 :     hasher << &addr << addr;
     306                 :         134 :     free(addr);
     307                 :         134 : }
     308                 :             : 
     309                 :         134 : void RandAddStaticEnv(CSHA512& hasher)
     310                 :             : {
     311                 :             :     // Some compile-time static properties
     312                 :         134 :     hasher << (CHAR_MIN < 0) << sizeof(void*) << sizeof(long) << sizeof(int);
     313                 :             : #if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
     314                 :         134 :     hasher << __GNUC__ << __GNUC_MINOR__ << __GNUC_PATCHLEVEL__;
     315                 :             : #endif
     316                 :             : #ifdef _MSC_VER
     317                 :             :     hasher << _MSC_VER;
     318                 :             : #endif
     319                 :         134 :     hasher << __cplusplus;
     320                 :             : #ifdef _XOPEN_VERSION
     321                 :         134 :     hasher << _XOPEN_VERSION;
     322                 :             : #endif
     323                 :             : #ifdef __VERSION__
     324                 :         134 :     const char* COMPILER_VERSION = __VERSION__;
     325                 :         134 :     hasher.Write((const unsigned char*)COMPILER_VERSION, strlen(COMPILER_VERSION) + 1);
     326                 :             : #endif
     327                 :             : 
     328                 :             :     // Bitcoin client version
     329                 :         134 :     hasher << CLIENT_VERSION;
     330                 :             : 
     331                 :             : #if defined(HAVE_STRONG_GETAUXVAL)
     332                 :             :     // Information available through getauxval()
     333                 :             : #  ifdef AT_HWCAP
     334                 :         134 :     hasher << getauxval(AT_HWCAP);
     335                 :             : #  endif
     336                 :             : #  ifdef AT_HWCAP2
     337                 :         134 :     hasher << getauxval(AT_HWCAP2);
     338                 :             : #  endif
     339                 :             : #  ifdef AT_RANDOM
     340                 :         134 :     const unsigned char* random_aux = (const unsigned char*)getauxval(AT_RANDOM);
     341         [ +  - ]:         134 :     if (random_aux) hasher.Write(random_aux, 16);
     342                 :             : #  endif
     343                 :             : #  ifdef AT_PLATFORM
     344                 :         134 :     const char* platform_str = (const char*)getauxval(AT_PLATFORM);
     345         [ +  - ]:         134 :     if (platform_str) hasher.Write((const unsigned char*)platform_str, strlen(platform_str) + 1);
     346                 :             : #  endif
     347                 :             : #  ifdef AT_EXECFN
     348                 :         134 :     const char* exec_str = (const char*)getauxval(AT_EXECFN);
     349         [ +  - ]:         134 :     if (exec_str) hasher.Write((const unsigned char*)exec_str, strlen(exec_str) + 1);
     350                 :             : #  endif
     351                 :             : #endif // HAVE_STRONG_GETAUXVAL
     352                 :             : 
     353                 :             : #ifdef HAVE_GETCPUID
     354                 :         134 :     AddAllCPUID(hasher);
     355                 :             : #endif
     356                 :             : 
     357                 :             :     // Memory locations
     358                 :         134 :     hasher << &hasher << &RandAddStaticEnv << &malloc << &errno << &environ;
     359                 :             : 
     360                 :             :     // Hostname
     361                 :             : #ifdef WIN32
     362                 :             :     constexpr DWORD max_size = MAX_COMPUTERNAME_LENGTH + 1;
     363                 :             :     char hname[max_size];
     364                 :             :     DWORD size = max_size;
     365                 :             :     if (GetComputerNameA(hname, &size) != 0) {
     366                 :             :         hasher.Write(UCharCast(hname), size);
     367                 :             :     }
     368                 :             : #else
     369                 :         134 :     char hname[256];
     370         [ +  - ]:         134 :     if (gethostname(hname, 256) == 0) {
     371                 :         134 :         hasher.Write((const unsigned char*)hname, strnlen(hname, 256));
     372                 :             :     }
     373                 :             : #endif
     374                 :             : 
     375                 :             : #if HAVE_DECL_GETIFADDRS && HAVE_DECL_FREEIFADDRS
     376                 :             :     // Network interfaces
     377                 :         134 :     struct ifaddrs *ifad = nullptr;
     378                 :         134 :     getifaddrs(&ifad);
     379                 :         134 :     struct ifaddrs *ifit = ifad;
     380         [ +  + ]:         670 :     while (ifit != nullptr) {
     381                 :         536 :         hasher.Write((const unsigned char*)&ifit, sizeof(ifit));
     382                 :         536 :         hasher.Write((const unsigned char*)ifit->ifa_name, strlen(ifit->ifa_name) + 1);
     383                 :         536 :         hasher.Write((const unsigned char*)&ifit->ifa_flags, sizeof(ifit->ifa_flags));
     384                 :         536 :         AddSockaddr(hasher, ifit->ifa_addr);
     385                 :         536 :         AddSockaddr(hasher, ifit->ifa_netmask);
     386                 :         536 :         AddSockaddr(hasher, ifit->ifa_dstaddr);
     387                 :         536 :         ifit = ifit->ifa_next;
     388                 :             :     }
     389                 :         134 :     freeifaddrs(ifad);
     390                 :             : #endif
     391                 :             : 
     392                 :             : #ifndef WIN32
     393                 :             :     // UNIX kernel information
     394                 :         134 :     struct utsname name;
     395         [ +  - ]:         134 :     if (uname(&name) != -1) {
     396                 :         134 :         hasher.Write((const unsigned char*)&name.sysname, strlen(name.sysname) + 1);
     397                 :         134 :         hasher.Write((const unsigned char*)&name.nodename, strlen(name.nodename) + 1);
     398                 :         134 :         hasher.Write((const unsigned char*)&name.release, strlen(name.release) + 1);
     399                 :         134 :         hasher.Write((const unsigned char*)&name.version, strlen(name.version) + 1);
     400                 :         134 :         hasher.Write((const unsigned char*)&name.machine, strlen(name.machine) + 1);
     401                 :             :     }
     402                 :             : 
     403                 :             :     /* Path and filesystem provided data */
     404                 :         134 :     AddPath(hasher, "/");
     405                 :         134 :     AddPath(hasher, ".");
     406                 :         134 :     AddPath(hasher, "/tmp");
     407                 :         134 :     AddPath(hasher, "/home");
     408                 :         134 :     AddPath(hasher, "/proc");
     409                 :             : #ifdef __linux__
     410                 :         134 :     AddFile(hasher, "/proc/cmdline");
     411                 :         134 :     AddFile(hasher, "/proc/cpuinfo");
     412                 :         134 :     AddFile(hasher, "/proc/version");
     413                 :             : #endif
     414                 :         134 :     AddFile(hasher, "/etc/passwd");
     415                 :         134 :     AddFile(hasher, "/etc/group");
     416                 :         134 :     AddFile(hasher, "/etc/hosts");
     417                 :         134 :     AddFile(hasher, "/etc/resolv.conf");
     418                 :         134 :     AddFile(hasher, "/etc/timezone");
     419                 :         134 :     AddFile(hasher, "/etc/localtime");
     420                 :             : #endif
     421                 :             : 
     422                 :             :     // For MacOS/BSDs, gather data through sysctl instead of /proc. Not all of these
     423                 :             :     // will exist on every system.
     424                 :             : #ifdef HAVE_SYSCTL
     425                 :             : #  ifdef CTL_HW
     426                 :             : #    ifdef HW_MACHINE
     427                 :             :     AddSysctl<CTL_HW, HW_MACHINE>(hasher);
     428                 :             : #    endif
     429                 :             : #    ifdef HW_MODEL
     430                 :             :     AddSysctl<CTL_HW, HW_MODEL>(hasher);
     431                 :             : #    endif
     432                 :             : #    ifdef HW_NCPU
     433                 :             :     AddSysctl<CTL_HW, HW_NCPU>(hasher);
     434                 :             : #    endif
     435                 :             : #    ifdef HW_PHYSMEM
     436                 :             :     AddSysctl<CTL_HW, HW_PHYSMEM>(hasher);
     437                 :             : #    endif
     438                 :             : #    ifdef HW_USERMEM
     439                 :             :     AddSysctl<CTL_HW, HW_USERMEM>(hasher);
     440                 :             : #    endif
     441                 :             : #    ifdef HW_MACHINE_ARCH
     442                 :             :     AddSysctl<CTL_HW, HW_MACHINE_ARCH>(hasher);
     443                 :             : #    endif
     444                 :             : #    ifdef HW_REALMEM
     445                 :             :     AddSysctl<CTL_HW, HW_REALMEM>(hasher);
     446                 :             : #    endif
     447                 :             : #    ifdef HW_CPU_FREQ
     448                 :             :     AddSysctl<CTL_HW, HW_CPU_FREQ>(hasher);
     449                 :             : #    endif
     450                 :             : #    ifdef HW_BUS_FREQ
     451                 :             :     AddSysctl<CTL_HW, HW_BUS_FREQ>(hasher);
     452                 :             : #    endif
     453                 :             : #    ifdef HW_CACHELINE
     454                 :             :     AddSysctl<CTL_HW, HW_CACHELINE>(hasher);
     455                 :             : #    endif
     456                 :             : #  endif
     457                 :             : #  ifdef CTL_KERN
     458                 :             : #    ifdef KERN_BOOTFILE
     459                 :             :      AddSysctl<CTL_KERN, KERN_BOOTFILE>(hasher);
     460                 :             : #    endif
     461                 :             : #    ifdef KERN_BOOTTIME
     462                 :             :      AddSysctl<CTL_KERN, KERN_BOOTTIME>(hasher);
     463                 :             : #    endif
     464                 :             : #    ifdef KERN_CLOCKRATE
     465                 :             :      AddSysctl<CTL_KERN, KERN_CLOCKRATE>(hasher);
     466                 :             : #    endif
     467                 :             : #    ifdef KERN_HOSTID
     468                 :             :      AddSysctl<CTL_KERN, KERN_HOSTID>(hasher);
     469                 :             : #    endif
     470                 :             : #    ifdef KERN_HOSTUUID
     471                 :             :      AddSysctl<CTL_KERN, KERN_HOSTUUID>(hasher);
     472                 :             : #    endif
     473                 :             : #    ifdef KERN_HOSTNAME
     474                 :             :      AddSysctl<CTL_KERN, KERN_HOSTNAME>(hasher);
     475                 :             : #    endif
     476                 :             : #    ifdef KERN_OSRELDATE
     477                 :             :      AddSysctl<CTL_KERN, KERN_OSRELDATE>(hasher);
     478                 :             : #    endif
     479                 :             : #    ifdef KERN_OSRELEASE
     480                 :             :      AddSysctl<CTL_KERN, KERN_OSRELEASE>(hasher);
     481                 :             : #    endif
     482                 :             : #    ifdef KERN_OSREV
     483                 :             :      AddSysctl<CTL_KERN, KERN_OSREV>(hasher);
     484                 :             : #    endif
     485                 :             : #    ifdef KERN_OSTYPE
     486                 :             :      AddSysctl<CTL_KERN, KERN_OSTYPE>(hasher);
     487                 :             : #    endif
     488                 :             : #    ifdef KERN_POSIX1
     489                 :             :      AddSysctl<CTL_KERN, KERN_OSREV>(hasher);
     490                 :             : #    endif
     491                 :             : #    ifdef KERN_VERSION
     492                 :             :      AddSysctl<CTL_KERN, KERN_VERSION>(hasher);
     493                 :             : #    endif
     494                 :             : #  endif
     495                 :             : #endif
     496                 :             : 
     497                 :             :     // Env variables
     498         [ +  - ]:         134 :     if (environ) {
     499         [ +  + ]:        8680 :         for (size_t i = 0; environ[i]; ++i) {
     500                 :        8546 :             hasher.Write((const unsigned char*)environ[i], strlen(environ[i]));
     501                 :             :         }
     502                 :             :     }
     503                 :             : 
     504                 :             :     // Process, thread, user, session, group, ... ids.
     505                 :             : #ifdef WIN32
     506                 :             :     hasher << GetCurrentProcessId() << GetCurrentThreadId();
     507                 :             : #else
     508                 :         134 :     hasher << getpid() << getppid() << getsid(0) << getpgid(0) << getuid() << geteuid() << getgid() << getegid();
     509                 :             : #endif
     510                 :         134 :     hasher << std::this_thread::get_id();
     511                 :         134 : }
        

Generated by: LCOV version 2.0-1