Sanity checks (#88)

This commit is contained in:
tevador 2019-06-29 18:53:49 +02:00 committed by GitHub
parent c6b5ec12b6
commit aaa6e4e881
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 54 additions and 10 deletions

View File

@ -50,6 +50,10 @@ struct randomx_cache {
randomx::DatasetInitFunc* datasetInit; randomx::DatasetInitFunc* datasetInit;
randomx::SuperscalarProgram programs[RANDOMX_CACHE_ACCESSES]; randomx::SuperscalarProgram programs[RANDOMX_CACHE_ACCESSES];
std::vector<uint64_t> reciprocalCache; std::vector<uint64_t> reciprocalCache;
bool isInitialized() {
return programs[0].getSize() != 0;
}
}; };
//A pointer to a standard-layout struct object points to its initial member //A pointer to a standard-layout struct object points to its initial member

View File

@ -33,13 +33,15 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "vm_compiled.hpp" #include "vm_compiled.hpp"
#include "vm_compiled_light.hpp" #include "vm_compiled_light.hpp"
#include "blake2/blake2.h" #include "blake2/blake2.h"
#include <cassert>
extern "C" { extern "C" {
randomx_cache *randomx_alloc_cache(randomx_flags flags) { randomx_cache *randomx_alloc_cache(randomx_flags flags) {
randomx_cache *cache = new randomx_cache(); randomx_cache *cache;
try { try {
cache = new randomx_cache();
switch (flags & (RANDOMX_FLAG_JIT | RANDOMX_FLAG_LARGE_PAGES)) { switch (flags & (RANDOMX_FLAG_JIT | RANDOMX_FLAG_LARGE_PAGES)) {
case RANDOMX_FLAG_DEFAULT: case RANDOMX_FLAG_DEFAULT:
cache->dealloc = &randomx::deallocCache<randomx::DefaultAllocator>; cache->dealloc = &randomx::deallocCache<randomx::DefaultAllocator>;
@ -78,26 +80,32 @@ extern "C" {
} }
} }
catch (std::exception &ex) { catch (std::exception &ex) {
if (cache != nullptr) {
randomx_release_cache(cache); randomx_release_cache(cache);
cache = nullptr; cache = nullptr;
} }
}
return cache; return cache;
} }
void randomx_init_cache(randomx_cache *cache, const void *key, size_t keySize) { void randomx_init_cache(randomx_cache *cache, const void *key, size_t keySize) {
assert(cache != nullptr);
assert(keySize == 0 || key != nullptr);
cache->initialize(cache, key, keySize); cache->initialize(cache, key, keySize);
} }
void randomx_release_cache(randomx_cache* cache) { void randomx_release_cache(randomx_cache* cache) {
assert(cache != nullptr);
cache->dealloc(cache); cache->dealloc(cache);
delete cache; delete cache;
} }
randomx_dataset *randomx_alloc_dataset(randomx_flags flags) { randomx_dataset *randomx_alloc_dataset(randomx_flags flags) {
randomx_dataset *dataset = new randomx_dataset(); randomx_dataset *dataset;
try { try {
dataset = new randomx_dataset();
if (flags & RANDOMX_FLAG_LARGE_PAGES) { if (flags & RANDOMX_FLAG_LARGE_PAGES) {
dataset->dealloc = &randomx::deallocDataset<randomx::LargePageAllocator>; dataset->dealloc = &randomx::deallocDataset<randomx::LargePageAllocator>;
dataset->memory = (uint8_t*)randomx::LargePageAllocator::allocMemory(randomx::DatasetSize); dataset->memory = (uint8_t*)randomx::LargePageAllocator::allocMemory(randomx::DatasetSize);
@ -108,31 +116,45 @@ extern "C" {
} }
} }
catch (std::exception &ex) { catch (std::exception &ex) {
if (dataset != nullptr) {
randomx_release_dataset(dataset); randomx_release_dataset(dataset);
dataset = nullptr; dataset = nullptr;
} }
}
return dataset; return dataset;
} }
constexpr unsigned long DatasetItemCount = randomx::DatasetSize / RANDOMX_DATASET_ITEM_SIZE;
unsigned long randomx_dataset_item_count() { unsigned long randomx_dataset_item_count() {
return randomx::DatasetSize / RANDOMX_DATASET_ITEM_SIZE; return DatasetItemCount;
} }
void randomx_init_dataset(randomx_dataset *dataset, randomx_cache *cache, unsigned long startItem, unsigned long itemCount) { void randomx_init_dataset(randomx_dataset *dataset, randomx_cache *cache, unsigned long startItem, unsigned long itemCount) {
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); cache->datasetInit(cache, dataset->memory + startItem * randomx::CacheLineSize, startItem, startItem + itemCount);
} }
void *randomx_get_dataset_memory(randomx_dataset *dataset) { void *randomx_get_dataset_memory(randomx_dataset *dataset) {
assert(dataset != nullptr);
return dataset->memory; return dataset->memory;
} }
void randomx_release_dataset(randomx_dataset *dataset) { void randomx_release_dataset(randomx_dataset *dataset) {
assert(dataset != nullptr);
dataset->dealloc(dataset); dataset->dealloc(dataset);
delete dataset; delete dataset;
} }
randomx_vm *randomx_create_vm(randomx_flags flags, randomx_cache *cache, randomx_dataset *dataset) { randomx_vm *randomx_create_vm(randomx_flags flags, randomx_cache *cache, randomx_dataset *dataset) {
assert(cache != nullptr || (flags & RANDOMX_FLAG_FULL_MEM));
assert(cache == nullptr || cache->isInitialized());
assert(dataset != nullptr || !(flags & RANDOMX_FLAG_FULL_MEM));
randomx_vm *vm = nullptr; randomx_vm *vm = nullptr;
try { try {
@ -222,25 +244,35 @@ extern "C" {
} }
void randomx_vm_set_cache(randomx_vm *machine, randomx_cache* cache) { void randomx_vm_set_cache(randomx_vm *machine, randomx_cache* cache) {
assert(machine != nullptr);
assert(cache != nullptr && cache->isInitialized());
machine->setCache(cache); machine->setCache(cache);
} }
void randomx_vm_set_dataset(randomx_vm *machine, randomx_dataset *dataset) { void randomx_vm_set_dataset(randomx_vm *machine, randomx_dataset *dataset) {
assert(machine != nullptr);
assert(dataset != nullptr);
machine->setDataset(dataset); machine->setDataset(dataset);
} }
void randomx_destroy_vm(randomx_vm *machine) { void randomx_destroy_vm(randomx_vm *machine) {
assert(machine != nullptr);
delete machine; delete machine;
} }
void randomx_calculate_hash(randomx_vm *machine, const void *input, size_t inputSize, void *output) { void randomx_calculate_hash(randomx_vm *machine, const void *input, size_t inputSize, void *output) {
assert(machine != nullptr);
assert(inputSize == 0 || input != nullptr);
assert(output != nullptr);
alignas(16) uint64_t tempHash[8]; alignas(16) uint64_t tempHash[8];
blake2b(tempHash, sizeof(tempHash), input, inputSize, nullptr, 0); int blakeResult = blake2b(tempHash, sizeof(tempHash), input, inputSize, nullptr, 0);
assert(blakeResult == 0);
machine->initScratchpad(&tempHash); machine->initScratchpad(&tempHash);
machine->resetRoundingMode(); machine->resetRoundingMode();
for (int chain = 0; chain < RANDOMX_PROGRAM_COUNT - 1; ++chain) { for (int chain = 0; chain < RANDOMX_PROGRAM_COUNT - 1; ++chain) {
machine->run(&tempHash); machine->run(&tempHash);
blake2b(tempHash, sizeof(tempHash), machine->getRegisterFile(), sizeof(randomx::RegisterFile), nullptr, 0); blakeResult = blake2b(tempHash, sizeof(tempHash), machine->getRegisterFile(), sizeof(randomx::RegisterFile), nullptr, 0);
assert(blakeResult == 0);
} }
machine->run(&tempHash); machine->run(&tempHash);
machine->getFinalResult(output, RANDOMX_HASH_SIZE); machine->getFinalResult(output, RANDOMX_HASH_SIZE);

View File

@ -26,6 +26,7 @@ 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. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include <assert.h>
#include "reciprocal.h" #include "reciprocal.h"
/* /*
@ -45,6 +46,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
uint64_t randomx_reciprocal(uint64_t divisor) { uint64_t randomx_reciprocal(uint64_t divisor) {
assert(divisor != 0);
const uint64_t p2exp63 = 1ULL << 63; const uint64_t p2exp63 = 1ULL << 63;
uint64_t quotient = p2exp63 / divisor, remainder = p2exp63 % divisor; uint64_t quotient = p2exp63 / divisor, remainder = p2exp63 % divisor;

View File

@ -57,7 +57,11 @@ namespace randomx {
} }
Instruction programBuffer[SuperscalarMaxSize]; Instruction programBuffer[SuperscalarMaxSize];
uint32_t size; uint32_t size
#ifndef NDEBUG
= 0
#endif
;
int addrReg; int addrReg;
double ipc; double ipc;
int codeSize; int codeSize;

View File

@ -1011,6 +1011,7 @@ int main() {
cache = randomx_alloc_cache(RANDOMX_FLAG_JIT); cache = randomx_alloc_cache(RANDOMX_FLAG_JIT);
currentKey.size = 0; currentKey.size = 0;
randomx_destroy_vm(vm); randomx_destroy_vm(vm);
initCache("test key 000");
vm = randomx_create_vm(RANDOMX_FLAG_JIT, cache, nullptr); vm = randomx_create_vm(RANDOMX_FLAG_JIT, cache, nullptr);
runTest("Hash test 2a (compiler)", RANDOMX_HAVE_COMPILER && stringsEqual(RANDOMX_ARGON_SALT, "RandomX\x03"), test_a); runTest("Hash test 2a (compiler)", RANDOMX_HAVE_COMPILER && stringsEqual(RANDOMX_ARGON_SALT, "RandomX\x03"), test_a);