Skip to content

Commit

Permalink
Merge pull request #221 from libriscv/binary_translation_dlls
Browse files Browse the repository at this point in the history
Add support for loading binary translation DLLs on Windows
  • Loading branch information
fwsGonzo authored Nov 20, 2024
2 parents bda185d + c4686b0 commit 15e41e9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
5 changes: 3 additions & 2 deletions src/sandbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ Sandbox::Sandbox() {
}
this->constructor_initialize();
this->m_tree_base = this;
this->m_global_instance_count += 1;
this->m_global_instances_current += 1;
this->m_global_instances_seen += 1;
// In order to reduce checks we guarantee that this
// class is well-formed at all times.
this->reset_machine();
Expand All @@ -237,7 +238,7 @@ Sandbox::~Sandbox() {
if (this->is_in_vmcall()) {
ERR_PRINT("Sandbox instance destroyed while a VM call is in progress.");
}
this->m_global_instance_count -= 1;
this->m_global_instances_current -= 1;
this->set_program_data_internal(nullptr);
try {
if (this->m_machine != dummy_machine)
Expand Down
5 changes: 3 additions & 2 deletions src/sandbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ class Sandbox : public Node {

/// @brief Get the global instance count of all sandbox instances.
/// @return The global instance count.
static uint64_t get_global_instance_count() { return m_global_instance_count; }
static uint64_t get_global_instance_count() { return m_global_instances_current; }

/// @brief Get the globally accumulated startup time of all sandbox instantiations.
/// @return The accumulated startup time.
Expand Down Expand Up @@ -563,7 +563,8 @@ class Sandbox : public Node {
static inline uint64_t m_global_timeouts = 0;
static inline uint64_t m_global_exceptions = 0;
static inline uint64_t m_global_calls_made = 0;
static inline uint32_t m_global_instance_count = 0;
static inline uint32_t m_global_instances_current = 0; // Counts the number of current instances
static inline uint32_t m_global_instances_seen = 0; // Incremented for each instance created
static inline double m_accumulated_startup_time = 0.0;
};

Expand Down
31 changes: 27 additions & 4 deletions src/sandbox_bintr.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
#include "sandbox.h"

#ifdef __linux__
#include <dlfcn.h>
#if defined(__linux__)
# include <dlfcn.h>
#elif defined(__MINGW32__) || defined(__MINGW64__) || defined(_MSC_VER)
# define YEP_IS_WINDOWS 1
# include <libriscv/win32/dlfcn.h>
# ifdef _MSC_VER
# define access _access
# define unlink _unlink
extern "C" int access(const char* path, int mode);
extern "C" int unlink(const char* path);
# define R_OK 4 /* Test for read permission. */
# else // _MSC_VER
# include <unistd.h>
# endif
#elif defined(__APPLE__) && defined(__MACH__) // macOS OSX
# include <TargetConditionals.h>
# if TARGET_OS_MAC
# include <dlfcn.h>
# define YEP_IS_OSX 1
# endif
#endif
extern "C" void libriscv_register_translation8(...);

Expand Down Expand Up @@ -44,10 +62,15 @@ String Sandbox::emit_binary_translation(bool ignore_instruction_limit, bool auto
}

bool Sandbox::load_binary_translation(const String &shared_library_path) {
if (m_global_instances_seen > 0) {
ERR_PRINT("Sandbox: Loading shared libraries after Sandbox instances have been created is a security risk."
"Please load shared libraries before creating any Sandbox instances.");
return false;
}
#ifdef RISCV_BINARY_TRANSLATION
// Load the shared library on platforms that support it
# ifdef __linux__
void *handle = dlopen(shared_library_path.utf8().ptr(), RTLD_LAZY|RTLD_GLOBAL);
# if defined(__linux__) || defined(YEP_IS_WINDOWS) || defined(YEP_IS_OSX)
void *handle = dlopen(shared_library_path.utf8().ptr(), RTLD_LAZY);
if (handle == nullptr) {
ERR_PRINT("Sandbox: Failed to load shared library: " + shared_library_path);
return false;
Expand Down

0 comments on commit 15e41e9

Please sign in to comment.