Fix RandomX validation exploit with difficulty reset, improve build system

- One-time difficulty reset to minimum for 17 blocks at activation height
- Separate RandomX build dirs for linux/win64 cross-compilation
- Add build target tracking to auto-clean on target switch
- Automated win64 release packaging to release-win64/
- Add build artifacts to .gitignore
- Miner memory diagnostics and large-page dataset fallback
This commit is contained in:
dan_s
2026-02-19 22:07:38 -06:00
parent 16149aed5a
commit 8a0eb77692
11 changed files with 395 additions and 37 deletions

View File

@@ -23,6 +23,7 @@
#include "pow/tromp/equi_miner.h"
#endif
#include <atomic>
#include "amount.h"
#include "chainparams.h"
#include "consensus/consensus.h"
@@ -1015,6 +1016,56 @@ enum RandomXSolverCancelCheck
int GetRandomXInterval();
int GetRandomXBlockLag();
#ifdef _WIN32
#include <windows.h>
static void LogProcessMemory(const char* label) {
// Use K32GetProcessMemoryInfo from kernel32.dll (available on Win7+)
// to avoid linking psapi.lib
typedef struct {
DWORD cb;
DWORD PageFaultCount;
SIZE_T PeakWorkingSetSize;
SIZE_T WorkingSetSize;
SIZE_T QuotaPeakPagedPoolUsage;
SIZE_T QuotaPagedPoolUsage;
SIZE_T QuotaPeakNonPagedPoolUsage;
SIZE_T QuotaNonPagedPoolUsage;
SIZE_T PagefileUsage;
SIZE_T PeakPagefileUsage;
SIZE_T PrivateUsage;
} PMC_EX;
typedef BOOL (WINAPI *PFN)(HANDLE, PMC_EX*, DWORD);
static PFN pfn = (PFN)GetProcAddress(GetModuleHandleA("kernel32.dll"), "K32GetProcessMemoryInfo");
if (pfn) {
PMC_EX pmc = {};
pmc.cb = sizeof(pmc);
if (pfn(GetCurrentProcess(), &pmc, sizeof(pmc))) {
LogPrintf("MemDiag [%s]: WorkingSet=%.1fMB, PrivateUsage=%.1fMB, PagefileUsage=%.1fMB\n",
label,
pmc.WorkingSetSize / (1024.0 * 1024.0),
pmc.PrivateUsage / (1024.0 * 1024.0),
pmc.PagefileUsage / (1024.0 * 1024.0));
}
}
}
#else
static void LogProcessMemory(const char* label) {
// Linux: read /proc/self/status
FILE *f = fopen("/proc/self/status", "r");
if (f) {
char line[256];
while (fgets(line, sizeof(line), f)) {
if (strncmp(line, "VmRSS:", 6) == 0 || strncmp(line, "VmSize:", 7) == 0) {
// Remove newline
line[strlen(line)-1] = '\0';
LogPrintf("MemDiag [%s]: %s\n", label, line);
}
}
fclose(f);
}
}
#endif
// Shared RandomX dataset manager — all miner threads share a single ~2GB dataset
// instead of each allocating their own. The dataset is read-only after initialization
// and RandomX explicitly supports multiple VMs sharing one dataset.
@@ -1036,6 +1087,12 @@ struct RandomXDatasetManager {
if (initialized) return true;
flags |= RANDOMX_FLAG_FULL_MEM;
LogPrintf("RandomXDatasetManager: flags=0x%x (JIT=%d, HARD_AES=%d, FULL_MEM=%d, LARGE_PAGES=%d)\n",
(int)flags,
!!(flags & RANDOMX_FLAG_JIT), !!(flags & RANDOMX_FLAG_HARD_AES),
!!(flags & RANDOMX_FLAG_FULL_MEM), !!(flags & RANDOMX_FLAG_LARGE_PAGES));
LogProcessMemory("before cache alloc");
cache = randomx_alloc_cache(flags | RANDOMX_FLAG_LARGE_PAGES | RANDOMX_FLAG_SECURE);
if (cache == nullptr) {
@@ -1043,25 +1100,39 @@ struct RandomXDatasetManager {
cache = randomx_alloc_cache(flags | RANDOMX_FLAG_SECURE);
if (cache == nullptr) {
LogPrintf("RandomXDatasetManager: cache alloc failed with secure, trying basic...\n");
cache = randomx_alloc_cache(flags);
if (cache == nullptr) {
LogPrintf("RandomXDatasetManager: cannot allocate cache!\n");
return false;
}
}
cache = randomx_alloc_cache(flags);
if (cache == nullptr) {
LogPrintf("RandomXDatasetManager: cannot allocate cache!\n");
}
LogProcessMemory("after cache alloc");
// Try to allocate dataset with large pages first for better performance
dataset = randomx_alloc_dataset(flags | RANDOMX_FLAG_LARGE_PAGES);
if (dataset == nullptr) {
LogPrintf("RandomXDatasetManager: dataset alloc failed with large pages, trying without...\n");
dataset = randomx_alloc_dataset(flags);
if (dataset == nullptr) {
LogPrintf("RandomXDatasetManager: cannot allocate dataset!\n");
randomx_release_cache(cache);
cache = nullptr;
return false;
}
}
dataset = randomx_alloc_dataset(flags);
if (dataset == nullptr) {
LogPrintf("RandomXDatasetManager: cannot allocate dataset!\n");
randomx_release_cache(cache);
cache = nullptr;
return false;
}
datasetItemCount = randomx_dataset_item_count();
initialized = true;
LogPrintf("RandomXDatasetManager: allocated shared cache + dataset (%lu items)\n", datasetItemCount);
LogProcessMemory("after dataset alloc");
// Log the actual memory addresses to help diagnose sharing issues
uint8_t *datasetMemory = (uint8_t*)randomx_get_dataset_memory(dataset);
size_t datasetSize = datasetItemCount * RANDOMX_DATASET_ITEM_SIZE;
LogPrintf("RandomXDatasetManager: allocated shared dataset:\n");
LogPrintf(" - Dataset struct at: %p\n", (void*)dataset);
LogPrintf(" - Dataset memory at: %p (size: %.2f GB)\n", (void*)datasetMemory, datasetSize / (1024.0 * 1024.0 * 1024.0));
LogPrintf(" - Items: %lu, Item size: %d bytes\n", datasetItemCount, RANDOMX_DATASET_ITEM_SIZE);
LogPrintf(" - Expected total process memory: ~%.2f GB + ~2MB per mining thread\n", datasetSize / (1024.0 * 1024.0 * 1024.0));
return true;
}
@@ -1104,12 +1175,24 @@ struct RandomXDatasetManager {
randomx_init_dataset(dataset, cache, 0, datasetItemCount);
}
LogPrintf("RandomXDatasetManager: dataset rebuilt\n");
LogProcessMemory("after dataset init");
}
// Creates a per-thread VM using the shared dataset.
// Caller must hold a shared lock on datasetMtx.
// The VM itself is small (~2MB scratchpad) - the 2GB dataset is shared via pointer.
randomx_vm *CreateVM() {
return randomx_create_vm(flags, nullptr, dataset);
static std::atomic<int> vmCount{0};
LogProcessMemory("before CreateVM");
randomx_vm *vm = randomx_create_vm(flags, nullptr, dataset);
if (vm != nullptr) {
int id = ++vmCount;
uint8_t *datasetMemory = (uint8_t*)randomx_get_dataset_memory(dataset);
LogPrintf("RandomXDatasetManager: VM #%d created - VM at %p, shared dataset memory at %p\n",
id, (void*)vm, (void*)datasetMemory);
LogProcessMemory("after CreateVM");
}
return vm;
}
void Shutdown() {