From 9a5d504bb3e2e754ff700b8a9415f70285c93f53 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Sat, 19 Oct 2019 00:20:09 +0200 Subject: [PATCH 1/5] Fix alignment for ARMv8 code --- src/jit_compiler_a64_static.S | 1 + 1 file changed, 1 insertion(+) diff --git a/src/jit_compiler_a64_static.S b/src/jit_compiler_a64_static.S index 660721c..0a4d006 100644 --- a/src/jit_compiler_a64_static.S +++ b/src/jit_compiler_a64_static.S @@ -100,6 +100,7 @@ # v30 -> E 'or' mask = 0x3*00000000******3*00000000****** # v31 -> scale mask = 0x81f000000000000081f0000000000000 + .balign 4 randomx_program_aarch64: # Save callee-saved registers sub sp, sp, 192 From f3ded36061ab50f34a3f890462a419079d0bf7e5 Mon Sep 17 00:00:00 2001 From: ston1th Date: Thu, 17 Oct 2019 22:25:51 +0200 Subject: [PATCH 2/5] Fixed mmap on OpenBSD OpenBSD's mmap does not support `MAP_HUGETLB` and `MAP_POPULATE`. --- src/virtual_memory.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/virtual_memory.cpp b/src/virtual_memory.cpp index 375ea14..fc46ae9 100644 --- a/src/virtual_memory.cpp +++ b/src/virtual_memory.cpp @@ -141,6 +141,8 @@ void* allocLargePagesMemory(std::size_t bytes) { mem = mmap(nullptr, bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, VM_FLAGS_SUPERPAGE_SIZE_2MB, 0); #elif defined(__FreeBSD__) mem = mmap(nullptr, bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_ALIGNED_SUPER, -1, 0); +#elif defined(__OpenBSD__) + mem = MAP_FAILED; // OpenBSD does not support huge pages #else mem = mmap(nullptr, bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_POPULATE, -1, 0); #endif From 068f65339ab273023f56aee84de0b5ea8684ba8d Mon Sep 17 00:00:00 2001 From: wepeng Date: Sun, 20 Oct 2019 18:27:54 +0800 Subject: [PATCH 3/5] modify Datset to Dataset --- doc/specs.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/specs.md b/doc/specs.md index 4f04142..f2ab8b2 100644 --- a/doc/specs.md +++ b/doc/specs.md @@ -934,7 +934,7 @@ The item data is represented by 8 64-bit integer registers: `r0`-`r7`. 1. XOR all registers with the 64 bytes loaded in step 4 (8 bytes per column in order `r0`-`r7`). 1. Set `cacheIndex` to the value of the register that has the longest dependency chain in the SuperscalarHash function executed in step 5. 1. Set `i = i + 1` and go back to step 4 if `i < RANDOMX_CACHE_ACCESSES`. -1. Concatenate registers `r0`-`r7` in little endian format to get the final Datset item data. +1. Concatenate registers `r0`-`r7` in little endian format to get the final Dataset item data. The constants used to initialize register values in step 1 were determined as follows: From 5c0486bd33068f59104e68dcba0465708e2087e0 Mon Sep 17 00:00:00 2001 From: ston1th Date: Sat, 19 Oct 2019 18:41:16 +0200 Subject: [PATCH 4/5] Fixed tests on OpenBSD Since OpenBSD 6.0 W^X is enforced. Added `RANDOMX_FLAG_SECURE` in tests and benchmarks. Updated comment. Excluded `cpu_set_t` since it is not defined on OpenBSD. --- src/randomx.cpp | 5 +++++ src/randomx.h | 1 + src/tests/affinity.cpp | 2 +- src/tests/benchmark.cpp | 5 +++++ src/tests/tests.cpp | 16 ++++++++++++++-- 5 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/randomx.cpp b/src/randomx.cpp index 73fd6e6..90fc46a 100644 --- a/src/randomx.cpp +++ b/src/randomx.cpp @@ -42,6 +42,11 @@ extern "C" { randomx_flags randomx_get_flags() { randomx_flags flags = RANDOMX_HAVE_COMPILER ? RANDOMX_FLAG_JIT : RANDOMX_FLAG_DEFAULT; randomx::Cpu cpu; +#ifdef __OpenBSD__ + if (flags == RANDOMX_FLAG_JIT) { + flags |= RANDOMX_FLAG_SECURE; + } +#endif if (HAVE_AES && cpu.hasAes()) { flags |= RANDOMX_FLAG_HARD_AES; } diff --git a/src/randomx.h b/src/randomx.h index fd46d7b..c06002b 100644 --- a/src/randomx.h +++ b/src/randomx.h @@ -83,6 +83,7 @@ extern "C" { * RANDOMX_FLAG_FULL_MEM * RANDOMX_FLAG_SECURE * These flags must be added manually if desired. + * On OpenBSD RANDOMX_FLAG_SECURE is enabled by default in JIT mode as W^X is enforced by the OS. */ RANDOMX_EXPORT randomx_flags randomx_get_flags(void); diff --git a/src/tests/affinity.cpp b/src/tests/affinity.cpp index 33a95e7..a2e37aa 100644 --- a/src/tests/affinity.cpp +++ b/src/tests/affinity.cpp @@ -65,7 +65,7 @@ set_thread_affinity(std::thread::native_handle_type thread, (thread_policy_t)&policy, 1); #elif defined(_WIN32) || defined(__CYGWIN__) rc = SetThreadAffinityMask(reinterpret_cast(thread), 1ULL << cpuid) == 0 ? -2 : 0; -#else +#elif !defined(__OpenBSD__) cpu_set_t cs; CPU_ZERO(&cs); CPU_SET(cpuid, &cs); diff --git a/src/tests/benchmark.cpp b/src/tests/benchmark.cpp index c8e45d0..60ba5fa 100644 --- a/src/tests/benchmark.cpp +++ b/src/tests/benchmark.cpp @@ -190,6 +190,9 @@ int main(int argc, char** argv) { } if (jit) { flags |= RANDOMX_FLAG_JIT; +#ifdef __OpenBSD__ + flags |= RANDOMX_FLAG_SECURE; +#endif } } @@ -199,9 +202,11 @@ int main(int argc, char** argv) { if (miningMode) { flags |= RANDOMX_FLAG_FULL_MEM; } +#ifndef __OpenBSD__ if (secure) { flags |= RANDOMX_FLAG_SECURE; } +#endif if (flags & RANDOMX_FLAG_ARGON2_AVX2) { std::cout << " - Argon2 implementation: AVX2" << std::endl; diff --git a/src/tests/tests.cpp b/src/tests/tests.cpp index 2773a88..65b2c00 100644 --- a/src/tests/tests.cpp +++ b/src/tests/tests.cpp @@ -143,7 +143,11 @@ int main() { randomx::JitCompiler jit; jit.generateSuperscalarHash(cache->programs, cache->reciprocalCache); jit.generateDatasetInitCode(); +#ifdef __OpenBSD__ + jit.enableExecution(); +#else jit.enableAll(); +#endif uint64_t datasetItem[8]; jit.getDatasetInitFunc()(cache, (uint8_t*)&datasetItem, 0, 1); assert(datasetItem[0] == 0x680588a85ae222db); @@ -950,7 +954,11 @@ int main() { assert(ibc.memMask == randomx::ScratchpadL3Mask); }); +#ifdef __OpenBSD__ + vm = randomx_create_vm(RANDOMX_FLAG_DEFAULT | RANDOMX_FLAG_SECURE, cache, nullptr); +#else vm = randomx_create_vm(RANDOMX_FLAG_DEFAULT, cache, nullptr); +#endif auto test_a = [&] { char hash[RANDOMX_HASH_SIZE]; @@ -1001,7 +1009,11 @@ int main() { vm = nullptr; cache = randomx_alloc_cache(RANDOMX_FLAG_JIT); initCache("test key 000"); - vm = randomx_create_vm(RANDOMX_FLAG_JIT, cache, nullptr); +#ifdef __OpenBSD__ + vm = randomx_create_vm(RANDOMX_FLAG_DEFAULT | RANDOMX_FLAG_SECURE, cache, nullptr); +#else + vm = randomx_create_vm(RANDOMX_FLAG_DEFAULT, cache, nullptr); +#endif } runTest("Hash test 2a (compiler)", RANDOMX_HAVE_COMPILER && stringsEqual(RANDOMX_ARGON_SALT, "RandomX\x03"), test_a); @@ -1048,4 +1060,4 @@ int main() { if (skipped) { std::cout << skipped << " tests were SKIPPED due to incompatible configuration (see above)" << std::endl; } -} \ No newline at end of file +} From 941dfed647eef58cc569e9b69c5145ea938aeaa0 Mon Sep 17 00:00:00 2001 From: "Vladimir (Alkenso)" Date: Wed, 23 Oct 2019 17:47:30 +0300 Subject: [PATCH 5/5] Update CMakeLists.txt (#140) Changed file type of src/jit_compiler_a64_static.S to 'ASM' --- CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index aa3777c..40f0354 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -100,6 +100,7 @@ if (ARCH_ID STREQUAL "x86_64" OR ARCH_ID STREQUAL "x86-64" OR ARCH_ID STREQUAL " src/jit_compiler_x86.cpp) # cheat because cmake and ccache hate each other set_property(SOURCE src/jit_compiler_x86_static.S PROPERTY LANGUAGE C) + set_property(SOURCE src/jit_compiler_x86_static.S PROPERTY XCODE_EXPLICIT_FILE_TYPE sourcecode.asm) if(ARCH STREQUAL "native") add_flag("-march=native") @@ -132,6 +133,7 @@ if (ARM_ID STREQUAL "aarch64" OR ARM_ID STREQUAL "arm64" OR ARM_ID STREQUAL "arm src/jit_compiler_a64.cpp) # cheat because cmake and ccache hate each other set_property(SOURCE src/jit_compiler_a64_static.S PROPERTY LANGUAGE C) + set_property(SOURCE src/jit_compiler_x86_static.S PROPERTY XCODE_EXPLICIT_FILE_TYPE sourcecode.asm) # not sure if this check is needed include(CheckIncludeFile)