From aaa6e4e881fe121478ee31d3386c220629c0ee31 Mon Sep 17 00:00:00 2001 From: tevador <37503146+tevador@users.noreply.github.com> Date: Sat, 29 Jun 2019 18:53:49 +0200 Subject: [PATCH] Sanity checks (#88) --- src/dataset.hpp | 4 +++ src/randomx.cpp | 50 ++++++++++++++++++++++++++++++------- src/reciprocal.c | 3 +++ src/superscalar_program.hpp | 6 ++++- src/tests/tests.cpp | 1 + 5 files changed, 54 insertions(+), 10 deletions(-) diff --git a/src/dataset.hpp b/src/dataset.hpp index 58a67f4..192d63a 100644 --- a/src/dataset.hpp +++ b/src/dataset.hpp @@ -50,6 +50,10 @@ struct randomx_cache { randomx::DatasetInitFunc* datasetInit; randomx::SuperscalarProgram programs[RANDOMX_CACHE_ACCESSES]; std::vector reciprocalCache; + + bool isInitialized() { + return programs[0].getSize() != 0; + } }; //A pointer to a standard-layout struct object points to its initial member diff --git a/src/randomx.cpp b/src/randomx.cpp index 8598e6e..6feafde 100644 --- a/src/randomx.cpp +++ b/src/randomx.cpp @@ -33,13 +33,15 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "vm_compiled.hpp" #include "vm_compiled_light.hpp" #include "blake2/blake2.h" +#include extern "C" { randomx_cache *randomx_alloc_cache(randomx_flags flags) { - randomx_cache *cache = new randomx_cache(); + randomx_cache *cache; try { + cache = new randomx_cache(); switch (flags & (RANDOMX_FLAG_JIT | RANDOMX_FLAG_LARGE_PAGES)) { case RANDOMX_FLAG_DEFAULT: cache->dealloc = &randomx::deallocCache; @@ -78,26 +80,32 @@ extern "C" { } } catch (std::exception &ex) { - randomx_release_cache(cache); - cache = nullptr; + if (cache != nullptr) { + randomx_release_cache(cache); + cache = nullptr; + } } return cache; } 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); } void randomx_release_cache(randomx_cache* cache) { + assert(cache != nullptr); cache->dealloc(cache); delete cache; } randomx_dataset *randomx_alloc_dataset(randomx_flags flags) { - randomx_dataset *dataset = new randomx_dataset(); + randomx_dataset *dataset; try { + dataset = new randomx_dataset(); if (flags & RANDOMX_FLAG_LARGE_PAGES) { dataset->dealloc = &randomx::deallocDataset; dataset->memory = (uint8_t*)randomx::LargePageAllocator::allocMemory(randomx::DatasetSize); @@ -108,31 +116,45 @@ extern "C" { } } catch (std::exception &ex) { - randomx_release_dataset(dataset); - dataset = nullptr; + if (dataset != nullptr) { + randomx_release_dataset(dataset); + dataset = nullptr; + } } return dataset; } + constexpr unsigned long DatasetItemCount = randomx::DatasetSize / RANDOMX_DATASET_ITEM_SIZE; + 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) { + 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); } void *randomx_get_dataset_memory(randomx_dataset *dataset) { + assert(dataset != nullptr); return dataset->memory; } void randomx_release_dataset(randomx_dataset *dataset) { + assert(dataset != nullptr); dataset->dealloc(dataset); delete 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; try { @@ -222,25 +244,35 @@ extern "C" { } void randomx_vm_set_cache(randomx_vm *machine, randomx_cache* cache) { + assert(machine != nullptr); + assert(cache != nullptr && cache->isInitialized()); machine->setCache(cache); } void randomx_vm_set_dataset(randomx_vm *machine, randomx_dataset *dataset) { + assert(machine != nullptr); + assert(dataset != nullptr); machine->setDataset(dataset); } void randomx_destroy_vm(randomx_vm *machine) { + assert(machine != nullptr); delete machine; } 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]; - 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->resetRoundingMode(); for (int chain = 0; chain < RANDOMX_PROGRAM_COUNT - 1; ++chain) { 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->getFinalResult(output, RANDOMX_HASH_SIZE); diff --git a/src/reciprocal.c b/src/reciprocal.c index 5552821..22620f5 100644 --- a/src/reciprocal.c +++ b/src/reciprocal.c @@ -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. */ +#include #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) { + assert(divisor != 0); + const uint64_t p2exp63 = 1ULL << 63; uint64_t quotient = p2exp63 / divisor, remainder = p2exp63 % divisor; diff --git a/src/superscalar_program.hpp b/src/superscalar_program.hpp index 38c2ae4..7bcd484 100644 --- a/src/superscalar_program.hpp +++ b/src/superscalar_program.hpp @@ -57,7 +57,11 @@ namespace randomx { } Instruction programBuffer[SuperscalarMaxSize]; - uint32_t size; + uint32_t size +#ifndef NDEBUG + = 0 +#endif + ; int addrReg; double ipc; int codeSize; diff --git a/src/tests/tests.cpp b/src/tests/tests.cpp index 2e8e8a2..29cdb92 100644 --- a/src/tests/tests.cpp +++ b/src/tests/tests.cpp @@ -1011,6 +1011,7 @@ int main() { cache = randomx_alloc_cache(RANDOMX_FLAG_JIT); currentKey.size = 0; randomx_destroy_vm(vm); + initCache("test key 000"); 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);