From 9359223e694f28c1aa30717694a243d8fb057f07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Wed, 10 Jun 2015 13:04:19 +0200 Subject: [PATCH 1/2] Remove memory leak detector. Is it not thread-safe. --- libevmjit/Array.cpp | 43 ++----------------------------------------- 1 file changed, 2 insertions(+), 41 deletions(-) diff --git a/libevmjit/Array.cpp b/libevmjit/Array.cpp index 3266038db..0b511a058 100644 --- a/libevmjit/Array.cpp +++ b/libevmjit/Array.cpp @@ -9,8 +9,6 @@ #include "Runtime.h" #include "Utils.h" -#include // DEBUG only - namespace dev { namespace eth @@ -269,52 +267,15 @@ void Array::extend(llvm::Value* _arrayPtr, llvm::Value* _size) } } -namespace -{ - struct AllocatedMemoryWatchdog - { - std::set allocatedMemory; - - ~AllocatedMemoryWatchdog() - { - if (!allocatedMemory.empty()) - { - DLOG(mem) << allocatedMemory.size() << " MEM LEAKS!\n"; - for (auto&& leak : allocatedMemory) - DLOG(mem) << "\t" << leak << "\n"; - } - } - }; - - AllocatedMemoryWatchdog watchdog; -} - extern "C" { - using namespace dev::eth::jit; - EXPORT void* ext_realloc(void* _data, size_t _size) noexcept { - //std::cerr << "REALLOC: " << _data << " [" << _size << "]" << std::endl; - auto newData = std::realloc(_data, _size); - if (_data != newData) - { - DLOG(mem) << "REALLOC: " << newData << " <- " << _data << " [" << _size << "]\n"; - watchdog.allocatedMemory.erase(_data); - watchdog.allocatedMemory.insert(newData); - } - return newData; + return std::realloc(_data, _size); } EXPORT void ext_free(void* _data) noexcept { std::free(_data); - if (_data) - { - DLOG(mem) << "FREE : " << _data << "\n"; - watchdog.allocatedMemory.erase(_data); - } } - -} // extern "C" - +} From 787df33341d084c2d69e1e2dabe9a009744f8dcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Wed, 10 Jun 2015 14:22:44 +0200 Subject: [PATCH 2/2] Protect EVM JIT cache with mutex. Fixes ethereum/cpp-ethereum#2086. --- libevmjit/Cache.cpp | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/libevmjit/Cache.cpp b/libevmjit/Cache.cpp index 47a6386e9..42ccf44ac 100644 --- a/libevmjit/Cache.cpp +++ b/libevmjit/Cache.cpp @@ -1,5 +1,7 @@ #include "Cache.h" +#include + #include "preprocessor/llvm_includes_start.h" #include #include @@ -23,6 +25,8 @@ namespace jit namespace { + using Guard = std::lock_guard; + std::mutex x_cacheMutex; CacheMode g_mode; llvm::MemoryBuffer* g_lastObject; ExecutionEngineListener* g_listener; @@ -43,6 +47,9 @@ namespace ObjectCache* Cache::getObjectCache(CacheMode _mode, ExecutionEngineListener* _listener) { static ObjectCache objectCache; + + Guard g{x_cacheMutex}; + g_mode = _mode; g_listener = _listener; return &objectCache; @@ -50,6 +57,8 @@ ObjectCache* Cache::getObjectCache(CacheMode _mode, ExecutionEngineListener* _li void Cache::clear() { + Guard g{x_cacheMutex}; + using namespace llvm::sys; llvm::SmallString<256> cachePath; path::system_temp_directory(false, cachePath); @@ -62,6 +71,8 @@ void Cache::clear() void Cache::preload(llvm::ExecutionEngine& _ee, std::unordered_map& _funcCache) { + Guard g{x_cacheMutex}; + // TODO: Cache dir should be in one place using namespace llvm::sys; llvm::SmallString<256> cachePath; @@ -92,11 +103,14 @@ void Cache::preload(llvm::ExecutionEngine& _ee, std::unordered_map Cache::getObject(std::string const& id) { + Guard g{x_cacheMutex}; + if (g_mode != CacheMode::on && g_mode != CacheMode::read) return nullptr; - if (g_listener) - g_listener->stateChanged(ExecState::CacheLoad); + // TODO: Disabled because is not thread-safe. + //if (g_listener) + // g_listener->stateChanged(ExecState::CacheLoad); DLOG(cache) << id << ": search\n"; if (!CHECK(!g_lastObject)) @@ -136,12 +150,15 @@ std::unique_ptr Cache::getObject(std::string const& id) void ObjectCache::notifyObjectCompiled(llvm::Module const* _module, llvm::MemoryBuffer const* _object) { + Guard g{x_cacheMutex}; + // Only in "on" and "write" mode if (g_mode != CacheMode::on && g_mode != CacheMode::write) return; - if (g_listener) - g_listener->stateChanged(ExecState::CacheWrite); + // TODO: Disabled because is not thread-safe. + // if (g_listener) + // g_listener->stateChanged(ExecState::CacheWrite); auto&& id = _module->getModuleIdentifier(); llvm::SmallString<256> cachePath; @@ -161,6 +178,8 @@ void ObjectCache::notifyObjectCompiled(llvm::Module const* _module, llvm::Memory llvm::MemoryBuffer* ObjectCache::getObject(llvm::Module const* _module) { + Guard g{x_cacheMutex}; + DLOG(cache) << _module->getModuleIdentifier() << ": use\n"; auto o = g_lastObject; g_lastObject = nullptr;