Merge pull request #187 from tevador/pr-netbsd

Fix compilation and JIT support on NetBSD

1. Disable hugepages (not supported).
2. Force W^X (required).
3. When allocating JIT memory, PROT_EXEC must be reserved
   in order to set the pages executable later.
This commit is contained in:
tevador 2020-06-28 16:35:19 +02:00 committed by GitHub
commit a1c08a2f41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 21 additions and 11 deletions

View File

@ -35,3 +35,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#else #else
#include "jit_compiler_fallback.hpp" #include "jit_compiler_fallback.hpp"
#endif #endif
#if defined(__OpenBSD__) || defined(__NetBSD__)
#define RANDOMX_FORCE_SECURE
#endif

View File

@ -43,7 +43,7 @@ extern "C" {
randomx_flags randomx_get_flags() { randomx_flags randomx_get_flags() {
randomx_flags flags = RANDOMX_HAVE_COMPILER ? RANDOMX_FLAG_JIT : RANDOMX_FLAG_DEFAULT; randomx_flags flags = RANDOMX_HAVE_COMPILER ? RANDOMX_FLAG_JIT : RANDOMX_FLAG_DEFAULT;
randomx::Cpu cpu; randomx::Cpu cpu;
#ifdef __OpenBSD__ #ifdef RANDOMX_FORCE_SECURE
if (flags == RANDOMX_FLAG_JIT) { if (flags == RANDOMX_FLAG_JIT) {
flags |= RANDOMX_FLAG_SECURE; flags |= RANDOMX_FLAG_SECURE;
} }

View File

@ -65,7 +65,7 @@ set_thread_affinity(std::thread::native_handle_type thread,
(thread_policy_t)&policy, 1); (thread_policy_t)&policy, 1);
#elif defined(_WIN32) || defined(__CYGWIN__) #elif defined(_WIN32) || defined(__CYGWIN__)
rc = SetThreadAffinityMask(reinterpret_cast<HANDLE>(thread), 1ULL << cpuid) == 0 ? -2 : 0; rc = SetThreadAffinityMask(reinterpret_cast<HANDLE>(thread), 1ULL << cpuid) == 0 ? -2 : 0;
#elif !defined(__OpenBSD__) && !defined(__FreeBSD__) && !defined(__ANDROID__) #elif !defined(__OpenBSD__) && !defined(__FreeBSD__) && !defined(__ANDROID__) && !defined(__NetBSD__)
cpu_set_t cs; cpu_set_t cs;
CPU_ZERO(&cs); CPU_ZERO(&cs);
CPU_SET(cpuid, &cs); CPU_SET(cpuid, &cs);

View File

@ -40,6 +40,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "../dataset.hpp" #include "../dataset.hpp"
#include "../blake2/endian.h" #include "../blake2/endian.h"
#include "../common.hpp" #include "../common.hpp"
#include "../jit_compiler.hpp"
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h> #include <windows.h>
#include <VersionHelpers.h> #include <VersionHelpers.h>
@ -199,7 +200,7 @@ int main(int argc, char** argv) {
} }
if (jit) { if (jit) {
flags |= RANDOMX_FLAG_JIT; flags |= RANDOMX_FLAG_JIT;
#ifdef __OpenBSD__ #ifdef RANDOMX_FORCE_SECURE
flags |= RANDOMX_FLAG_SECURE; flags |= RANDOMX_FLAG_SECURE;
#endif #endif
} }
@ -211,7 +212,7 @@ int main(int argc, char** argv) {
if (miningMode) { if (miningMode) {
flags |= RANDOMX_FLAG_FULL_MEM; flags |= RANDOMX_FLAG_FULL_MEM;
} }
#ifndef __OpenBSD__ #ifndef RANDOMX_FORCE_SECURE
if (secure) { if (secure) {
flags |= RANDOMX_FLAG_SECURE; flags |= RANDOMX_FLAG_SECURE;
} }

View File

@ -143,7 +143,7 @@ int main() {
randomx::JitCompiler jit; randomx::JitCompiler jit;
jit.generateSuperscalarHash(cache->programs, cache->reciprocalCache); jit.generateSuperscalarHash(cache->programs, cache->reciprocalCache);
jit.generateDatasetInitCode(); jit.generateDatasetInitCode();
#ifdef __OpenBSD__ #ifdef RANDOMX_FORCE_SECURE
jit.enableExecution(); jit.enableExecution();
#else #else
jit.enableAll(); jit.enableAll();
@ -954,7 +954,7 @@ int main() {
assert(ibc.memMask == randomx::ScratchpadL3Mask); assert(ibc.memMask == randomx::ScratchpadL3Mask);
}); });
#ifdef __OpenBSD__ #ifdef RANDOMX_FORCE_SECURE
vm = randomx_create_vm(RANDOMX_FLAG_DEFAULT | RANDOMX_FLAG_SECURE, cache, nullptr); vm = randomx_create_vm(RANDOMX_FLAG_DEFAULT | RANDOMX_FLAG_SECURE, cache, nullptr);
#else #else
vm = randomx_create_vm(RANDOMX_FLAG_DEFAULT, cache, nullptr); vm = randomx_create_vm(RANDOMX_FLAG_DEFAULT, cache, nullptr);
@ -1009,10 +1009,10 @@ int main() {
vm = nullptr; vm = nullptr;
cache = randomx_alloc_cache(RANDOMX_FLAG_JIT); cache = randomx_alloc_cache(RANDOMX_FLAG_JIT);
initCache("test key 000"); initCache("test key 000");
#ifdef __OpenBSD__ #ifdef RANDOMX_FORCE_SECURE
vm = randomx_create_vm(RANDOMX_FLAG_DEFAULT | RANDOMX_FLAG_SECURE, cache, nullptr); vm = randomx_create_vm(RANDOMX_FLAG_JIT | RANDOMX_FLAG_SECURE, cache, nullptr);
#else #else
vm = randomx_create_vm(RANDOMX_FLAG_DEFAULT, cache, nullptr); vm = randomx_create_vm(RANDOMX_FLAG_JIT, cache, nullptr);
#endif #endif
} }

View File

@ -94,7 +94,12 @@ void* allocMemoryPages(std::size_t bytes) {
if (mem == nullptr) if (mem == nullptr)
throw std::runtime_error(getErrorMessage("allocMemoryPages - VirtualAlloc")); throw std::runtime_error(getErrorMessage("allocMemoryPages - VirtualAlloc"));
#else #else
mem = mmap(nullptr, bytes, PAGE_READWRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); #if defined(__NetBSD__)
#define RESERVED_FLAGS PROT_MPROTECT(PROT_EXEC)
#else
#define RESERVED_FLAGS 0
#endif
mem = mmap(nullptr, bytes, PAGE_READWRITE | RESERVED_FLAGS, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
if (mem == MAP_FAILED) if (mem == MAP_FAILED)
throw std::runtime_error("allocMemoryPages - mmap failed"); throw std::runtime_error("allocMemoryPages - mmap failed");
#endif #endif
@ -141,7 +146,7 @@ void* allocLargePagesMemory(std::size_t bytes) {
mem = mmap(nullptr, bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, VM_FLAGS_SUPERPAGE_SIZE_2MB, 0); mem = mmap(nullptr, bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, VM_FLAGS_SUPERPAGE_SIZE_2MB, 0);
#elif defined(__FreeBSD__) #elif defined(__FreeBSD__)
mem = mmap(nullptr, bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_ALIGNED_SUPER, -1, 0); mem = mmap(nullptr, bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_ALIGNED_SUPER, -1, 0);
#elif defined(__OpenBSD__) #elif defined(__OpenBSD__) || defined(__NetBSD__)
mem = MAP_FAILED; // OpenBSD does not support huge pages mem = MAP_FAILED; // OpenBSD does not support huge pages
#else #else
mem = mmap(nullptr, bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_POPULATE, -1, 0); mem = mmap(nullptr, bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_POPULATE, -1, 0);