RandomWOW/src/randomx.cpp

339 lines
10 KiB
C++
Raw Normal View History

2019-04-20 09:08:01 +00:00
/*
Copyright (c) 2018-2019, tevador <tevador@gmail.com>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2019-04-20 09:08:01 +00:00
*/
#include "randomx.h"
#include "dataset.hpp"
2019-04-20 14:53:06 +00:00
#include "vm_interpreted.hpp"
#include "vm_interpreted_light.hpp"
#include "vm_compiled.hpp"
#include "vm_compiled_light.hpp"
2019-04-20 09:08:01 +00:00
#include "blake2/blake2.h"
2019-06-29 16:53:49 +00:00
#include <cassert>
#include <limits>
2019-04-20 09:08:01 +00:00
extern "C" {
randomx_cache *randomx_alloc_cache(randomx_flags flags) {
2019-06-29 16:53:49 +00:00
randomx_cache *cache;
2019-04-20 09:08:01 +00:00
try {
2019-06-29 16:53:49 +00:00
cache = new randomx_cache();
2019-04-21 13:04:17 +00:00
switch (flags & (RANDOMX_FLAG_JIT | RANDOMX_FLAG_LARGE_PAGES)) {
case RANDOMX_FLAG_DEFAULT:
cache->dealloc = &randomx::deallocCache<randomx::DefaultAllocator>;
cache->jit = nullptr;
cache->initialize = &randomx::initCache;
cache->datasetInit = &randomx::initDataset;
cache->memory = (uint8_t*)randomx::DefaultAllocator::allocMemory(randomx::CacheSize);
2019-04-21 13:04:17 +00:00
break;
case RANDOMX_FLAG_JIT:
cache->dealloc = &randomx::deallocCache<randomx::DefaultAllocator>;
cache->jit = new randomx::JitCompiler();
cache->initialize = &randomx::initCacheCompile;
cache->datasetInit = cache->jit->getDatasetInitFunc();
cache->memory = (uint8_t*)randomx::DefaultAllocator::allocMemory(randomx::CacheSize);
2019-04-21 13:04:17 +00:00
break;
case RANDOMX_FLAG_LARGE_PAGES:
cache->dealloc = &randomx::deallocCache<randomx::LargePageAllocator>;
cache->jit = nullptr;
cache->initialize = &randomx::initCache;
cache->datasetInit = &randomx::initDataset;
cache->memory = (uint8_t*)randomx::LargePageAllocator::allocMemory(randomx::CacheSize);
2019-04-21 13:04:17 +00:00
break;
case RANDOMX_FLAG_JIT | RANDOMX_FLAG_LARGE_PAGES:
cache->dealloc = &randomx::deallocCache<randomx::LargePageAllocator>;
cache->jit = new randomx::JitCompiler();
cache->initialize = &randomx::initCacheCompile;
cache->datasetInit = cache->jit->getDatasetInitFunc();
cache->memory = (uint8_t*)randomx::LargePageAllocator::allocMemory(randomx::CacheSize);
2019-04-21 13:04:17 +00:00
break;
default:
UNREACHABLE;
}
}
catch (std::exception &ex) {
2019-06-29 16:53:49 +00:00
if (cache != nullptr) {
randomx_release_cache(cache);
cache = nullptr;
}
2019-04-20 09:08:01 +00:00
}
return cache;
}
2019-05-01 12:40:13 +00:00
void randomx_init_cache(randomx_cache *cache, const void *key, size_t keySize) {
2019-06-29 16:53:49 +00:00
assert(cache != nullptr);
assert(keySize == 0 || key != nullptr);
std::string cacheKey;
cacheKey.assign((const char *)key, keySize);
if (cache->cacheKey != cacheKey || !cache->isInitialized()) {
cache->initialize(cache, key, keySize);
cache->cacheKey = cacheKey;
}
2019-04-20 09:08:01 +00:00
}
void randomx_release_cache(randomx_cache* cache) {
2019-06-29 16:53:49 +00:00
assert(cache != nullptr);
cache->dealloc(cache);
2019-04-20 09:08:01 +00:00
delete cache;
}
randomx_dataset *randomx_alloc_dataset(randomx_flags flags) {
//fail on 32-bit systems if DatasetSize is >= 4 GiB
if (randomx::DatasetSize > std::numeric_limits<size_t>::max()) {
return nullptr;
}
2019-06-29 16:53:49 +00:00
randomx_dataset *dataset;
try {
2019-06-29 16:53:49 +00:00
dataset = new randomx_dataset();
2019-04-21 13:04:17 +00:00
if (flags & RANDOMX_FLAG_LARGE_PAGES) {
dataset->dealloc = &randomx::deallocDataset<randomx::LargePageAllocator>;
dataset->memory = (uint8_t*)randomx::LargePageAllocator::allocMemory(randomx::DatasetSize);
2019-04-21 13:04:17 +00:00
}
else {
dataset->dealloc = &randomx::deallocDataset<randomx::DefaultAllocator>;
dataset->memory = (uint8_t*)randomx::DefaultAllocator::allocMemory(randomx::DatasetSize);
2019-04-21 13:04:17 +00:00
}
}
catch (std::exception &ex) {
2019-06-29 16:53:49 +00:00
if (dataset != nullptr) {
randomx_release_dataset(dataset);
dataset = nullptr;
}
2019-04-20 09:08:01 +00:00
}
return dataset;
}
2019-06-29 16:53:49 +00:00
constexpr unsigned long DatasetItemCount = randomx::DatasetSize / RANDOMX_DATASET_ITEM_SIZE;
unsigned long randomx_dataset_item_count() {
2019-06-29 16:53:49 +00:00
return DatasetItemCount;
}
void randomx_init_dataset(randomx_dataset *dataset, randomx_cache *cache, unsigned long startItem, unsigned long itemCount) {
2019-06-29 16:53:49 +00:00
assert(dataset != nullptr);
assert(cache != nullptr);
assert(startItem < DatasetItemCount && itemCount <= DatasetItemCount);
assert(startItem + itemCount <= DatasetItemCount);
cache->datasetInit(cache, dataset->memory + startItem * randomx::CacheLineSize, startItem, startItem + itemCount);
2019-04-20 09:08:01 +00:00
}
void *randomx_get_dataset_memory(randomx_dataset *dataset) {
2019-06-29 16:53:49 +00:00
assert(dataset != nullptr);
return dataset->memory;
}
2019-04-20 09:08:01 +00:00
void randomx_release_dataset(randomx_dataset *dataset) {
2019-06-29 16:53:49 +00:00
assert(dataset != nullptr);
dataset->dealloc(dataset);
2019-04-20 09:08:01 +00:00
delete dataset;
}
randomx_vm *randomx_create_vm(randomx_flags flags, randomx_cache *cache, randomx_dataset *dataset) {
2019-06-29 16:53:49 +00:00
assert(cache != nullptr || (flags & RANDOMX_FLAG_FULL_MEM));
assert(cache == nullptr || cache->isInitialized());
assert(dataset != nullptr || !(flags & RANDOMX_FLAG_FULL_MEM));
2019-04-21 13:04:17 +00:00
randomx_vm *vm = nullptr;
try {
switch (flags & (RANDOMX_FLAG_FULL_MEM | RANDOMX_FLAG_JIT | RANDOMX_FLAG_HARD_AES | RANDOMX_FLAG_LARGE_PAGES)) {
case RANDOMX_FLAG_DEFAULT:
2019-04-21 13:04:17 +00:00
vm = new randomx::InterpretedLightVmDefault();
break;
case RANDOMX_FLAG_FULL_MEM:
2019-04-21 13:04:17 +00:00
vm = new randomx::InterpretedVmDefault();
break;
case RANDOMX_FLAG_JIT:
if (flags & RANDOMX_FLAG_SECURE) {
vm = new randomx::CompiledLightVmDefaultSecure();
}
else {
vm = new randomx::CompiledLightVmDefault();
}
2019-04-21 13:04:17 +00:00
break;
case RANDOMX_FLAG_FULL_MEM | RANDOMX_FLAG_JIT:
if (flags & RANDOMX_FLAG_SECURE) {
vm = new randomx::CompiledVmDefaultSecure();
}
else {
vm = new randomx::CompiledVmDefault();
}
2019-04-21 13:04:17 +00:00
break;
case RANDOMX_FLAG_HARD_AES:
2019-04-21 13:04:17 +00:00
vm = new randomx::InterpretedLightVmHardAes();
break;
2019-04-20 09:08:01 +00:00
case RANDOMX_FLAG_FULL_MEM | RANDOMX_FLAG_HARD_AES:
2019-04-21 13:04:17 +00:00
vm = new randomx::InterpretedVmHardAes();
break;
case RANDOMX_FLAG_JIT | RANDOMX_FLAG_HARD_AES:
if (flags & RANDOMX_FLAG_SECURE) {
vm = new randomx::CompiledLightVmHardAesSecure();
}
else {
vm = new randomx::CompiledLightVmHardAes();
}
2019-04-21 13:04:17 +00:00
break;
case RANDOMX_FLAG_FULL_MEM | RANDOMX_FLAG_JIT | RANDOMX_FLAG_HARD_AES:
if (flags & RANDOMX_FLAG_SECURE) {
vm = new randomx::CompiledVmHardAesSecure();
}
else {
vm = new randomx::CompiledVmHardAes();
}
2019-04-21 13:04:17 +00:00
break;
case RANDOMX_FLAG_LARGE_PAGES:
2019-04-21 13:04:17 +00:00
vm = new randomx::InterpretedLightVmLargePage();
break;
case RANDOMX_FLAG_FULL_MEM | RANDOMX_FLAG_LARGE_PAGES:
2019-04-21 13:04:17 +00:00
vm = new randomx::InterpretedVmLargePage();
break;
case RANDOMX_FLAG_JIT | RANDOMX_FLAG_LARGE_PAGES:
if (flags & RANDOMX_FLAG_SECURE) {
vm = new randomx::CompiledLightVmLargePageSecure();
}
else {
vm = new randomx::CompiledLightVmLargePage();
}
2019-04-21 13:04:17 +00:00
break;
case RANDOMX_FLAG_FULL_MEM | RANDOMX_FLAG_JIT | RANDOMX_FLAG_LARGE_PAGES:
if (flags & RANDOMX_FLAG_SECURE) {
vm = new randomx::CompiledVmLargePageSecure();
}
else {
vm = new randomx::CompiledVmLargePage();
}
2019-04-21 13:04:17 +00:00
break;
case RANDOMX_FLAG_HARD_AES | RANDOMX_FLAG_LARGE_PAGES:
2019-04-21 13:04:17 +00:00
vm = new randomx::InterpretedLightVmLargePageHardAes();
break;
case RANDOMX_FLAG_FULL_MEM | RANDOMX_FLAG_HARD_AES | RANDOMX_FLAG_LARGE_PAGES:
2019-04-21 13:04:17 +00:00
vm = new randomx::InterpretedVmLargePageHardAes();
break;
case RANDOMX_FLAG_JIT | RANDOMX_FLAG_HARD_AES | RANDOMX_FLAG_LARGE_PAGES:
if (flags & RANDOMX_FLAG_SECURE) {
vm = new randomx::CompiledLightVmLargePageHardAesSecure();
}
else {
vm = new randomx::CompiledLightVmLargePageHardAes();
}
2019-04-21 13:04:17 +00:00
break;
case RANDOMX_FLAG_FULL_MEM | RANDOMX_FLAG_JIT | RANDOMX_FLAG_HARD_AES | RANDOMX_FLAG_LARGE_PAGES:
if (flags & RANDOMX_FLAG_SECURE) {
vm = new randomx::CompiledVmLargePageHardAesSecure();
}
else {
vm = new randomx::CompiledVmLargePageHardAes();
}
2019-04-21 13:04:17 +00:00
break;
default:
UNREACHABLE;
}
if(cache != nullptr) {
vm->setCache(cache);
vm->cacheKey = cache->cacheKey;
}
if(dataset != nullptr)
vm->setDataset(dataset);
2019-04-21 13:04:17 +00:00
vm->allocate();
}
catch (std::exception &ex) {
2019-04-20 09:08:01 +00:00
delete vm;
vm = nullptr;
}
return vm;
}
void randomx_vm_set_cache(randomx_vm *machine, randomx_cache* cache) {
2019-06-29 16:53:49 +00:00
assert(machine != nullptr);
assert(cache != nullptr && cache->isInitialized());
if (machine->cacheKey != cache->cacheKey) {
machine->setCache(cache);
machine->cacheKey = cache->cacheKey;
}
2019-04-20 09:08:01 +00:00
}
void randomx_vm_set_dataset(randomx_vm *machine, randomx_dataset *dataset) {
2019-06-29 16:53:49 +00:00
assert(machine != nullptr);
assert(dataset != nullptr);
2019-04-20 09:08:01 +00:00
machine->setDataset(dataset);
}
void randomx_destroy_vm(randomx_vm *machine) {
2019-06-29 16:53:49 +00:00
assert(machine != nullptr);
2019-04-20 09:08:01 +00:00
delete machine;
}
void randomx_calculate_hash(randomx_vm *machine, const void *input, size_t inputSize, void *output) {
2019-06-29 16:53:49 +00:00
assert(machine != nullptr);
assert(inputSize == 0 || input != nullptr);
assert(output != nullptr);
2019-04-21 13:04:17 +00:00
alignas(16) uint64_t tempHash[8];
2019-06-29 16:53:49 +00:00
int blakeResult = blake2b(tempHash, sizeof(tempHash), input, inputSize, nullptr, 0);
assert(blakeResult == 0);
2019-04-21 13:04:17 +00:00
machine->initScratchpad(&tempHash);
2019-04-20 09:08:01 +00:00
machine->resetRoundingMode();
for (int chain = 0; chain < RANDOMX_PROGRAM_COUNT - 1; ++chain) {
2019-04-21 13:04:17 +00:00
machine->run(&tempHash);
2019-06-29 16:53:49 +00:00
blakeResult = blake2b(tempHash, sizeof(tempHash), machine->getRegisterFile(), sizeof(randomx::RegisterFile), nullptr, 0);
assert(blakeResult == 0);
2019-04-20 09:08:01 +00:00
}
2019-04-21 13:04:17 +00:00
machine->run(&tempHash);
machine->getFinalResult(output, RANDOMX_HASH_SIZE);
2019-04-20 09:08:01 +00:00
}
}