From b8fb3a138c1c81f5823911e62d2dbe7b9c8e3ee9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Fri, 15 May 2015 17:31:08 +0200 Subject: [PATCH] Better style of ExecutionEngine initialization. Some eth::jit -> evmjit namespace transfers. --- include/evmjit/JIT.h | 10 +--------- libevmjit-cpp/JitVM.cpp | 18 ++++++++---------- libevmjit-cpp/JitVM.h | 2 +- libevmjit/Cache.cpp | 6 ++---- libevmjit/Cache.h | 5 +---- libevmjit/ExecStats.cpp | 5 +---- libevmjit/ExecStats.h | 6 ++---- libevmjit/ExecutionEngine.cpp | 35 +++++++++++------------------------ libevmjit/ExecutionEngine.h | 20 +++----------------- libevmjit/interface.cpp | 2 +- 10 files changed, 31 insertions(+), 78 deletions(-) diff --git a/include/evmjit/JIT.h b/include/evmjit/JIT.h index 446dd9e56..ba7a0dbc0 100644 --- a/include/evmjit/JIT.h +++ b/include/evmjit/JIT.h @@ -4,14 +4,6 @@ namespace dev { -namespace eth -{ -namespace jit -{ - class ExecutionEngine; -} -} - namespace evmjit { @@ -26,7 +18,7 @@ public: static bool isCodeReady(h256 _codeHash); private: - friend class dev::eth::jit::ExecutionEngine; + friend class ExecutionEngine; static void* getCode(h256 _codeHash); static void mapCode(h256 _codeHash, void* _funcAddr); diff --git a/libevmjit-cpp/JitVM.cpp b/libevmjit-cpp/JitVM.cpp index e44628c67..a4eed5793 100644 --- a/libevmjit-cpp/JitVM.cpp +++ b/libevmjit-cpp/JitVM.cpp @@ -19,8 +19,6 @@ extern "C" void env_sload(); // fake declaration for linker symbol stripping wor bytesConstRef JitVM::go(ExtVMFace& _ext, OnOpFunc const& _onOp, uint64_t _step) { - using namespace jit; - auto rejected = false; // TODO: Rejecting transactions with gas limit > 2^63 can be used by attacker to take JIT out of scope rejected |= m_gas > std::numeric_limits::max(); // Do not accept requests with gas > 2^63 (int64 max) @@ -54,23 +52,23 @@ bytesConstRef JitVM::go(ExtVMFace& _ext, OnOpFunc const& _onOp, uint64_t _step) m_data.codeSize = _ext.code.size(); m_data.codeHash = eth2llvm(sha3(_ext.code)); - m_context.init(m_data, reinterpret_cast(&_ext)); - auto exitCode = jit::ExecutionEngine::run(m_context); + m_context.init(m_data, reinterpret_cast(&_ext)); + auto exitCode = evmjit::ExecutionEngine::run(m_context); switch (exitCode) { - case ReturnCode::Suicide: + case evmjit::ReturnCode::Suicide: _ext.suicide(right160(llvm2eth(m_data.address))); break; - case ReturnCode::BadJumpDestination: + case evmjit::ReturnCode::BadJumpDestination: BOOST_THROW_EXCEPTION(BadJumpDestination()); - case ReturnCode::OutOfGas: + case evmjit::ReturnCode::OutOfGas: BOOST_THROW_EXCEPTION(OutOfGas()); - case ReturnCode::StackUnderflow: + case evmjit::ReturnCode::StackUnderflow: // FIXME: Remove support for detail errors BOOST_THROW_EXCEPTION(StackUnderflow()); - case ReturnCode::BadInstruction: + case evmjit::ReturnCode::BadInstruction: BOOST_THROW_EXCEPTION(BadInstruction()); - case ReturnCode::LinkerWorkaround: // never happens + case evmjit::ReturnCode::LinkerWorkaround: // never happens env_sload(); // but forces linker to include env_* JIT callback functions break; default: diff --git a/libevmjit-cpp/JitVM.h b/libevmjit-cpp/JitVM.h index aad379a75..a6f6f1d52 100644 --- a/libevmjit-cpp/JitVM.h +++ b/libevmjit-cpp/JitVM.h @@ -18,7 +18,7 @@ private: explicit JitVM(u256 _gas = 0) : VMFace(_gas) {} jit::RuntimeData m_data; - jit::ExecutionContext m_context; + evmjit::ExecutionContext m_context; std::unique_ptr m_fallbackVM; ///< VM used in case of input data rejected by JIT }; diff --git a/libevmjit/Cache.cpp b/libevmjit/Cache.cpp index 464912104..21be82d11 100644 --- a/libevmjit/Cache.cpp +++ b/libevmjit/Cache.cpp @@ -16,10 +16,9 @@ namespace dev { -namespace eth -{ -namespace jit +namespace evmjit { + using namespace eth::jit; namespace { @@ -177,4 +176,3 @@ std::unique_ptr ObjectCache::getObject(llvm::Module const* _ } } -} diff --git a/libevmjit/Cache.h b/libevmjit/Cache.h index ef4a0bac7..61635b8a1 100644 --- a/libevmjit/Cache.h +++ b/libevmjit/Cache.h @@ -12,9 +12,7 @@ namespace llvm namespace dev { -namespace eth -{ -namespace jit +namespace evmjit { class ExecutionEngineListener; @@ -57,4 +55,3 @@ public: } } -} diff --git a/libevmjit/ExecStats.cpp b/libevmjit/ExecStats.cpp index ff8c05307..c7f6ef0cd 100644 --- a/libevmjit/ExecStats.cpp +++ b/libevmjit/ExecStats.cpp @@ -8,9 +8,7 @@ namespace dev { -namespace eth -{ -namespace jit +namespace evmjit { void ExecStats::stateChanged(ExecState _state) @@ -95,4 +93,3 @@ StatsCollector::~StatsCollector() } } -} diff --git a/libevmjit/ExecStats.h b/libevmjit/ExecStats.h index 0451ccb05..3ea77733b 100644 --- a/libevmjit/ExecStats.h +++ b/libevmjit/ExecStats.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -8,9 +9,7 @@ namespace dev { -namespace eth -{ -namespace jit +namespace evmjit { class ExecStats : public ExecutionEngineListener @@ -42,4 +41,3 @@ public: } } -} diff --git a/libevmjit/ExecutionEngine.cpp b/libevmjit/ExecutionEngine.cpp index 736946079..e424d9d3a 100644 --- a/libevmjit/ExecutionEngine.cpp +++ b/libevmjit/ExecutionEngine.cpp @@ -30,11 +30,8 @@ namespace dev { -namespace eth +namespace evmjit { -namespace jit -{ -using evmjit::JIT; namespace { @@ -56,7 +53,7 @@ std::string hash2str(i256 const& _hash) return str; } -void printVersion() +void printVersion() // FIXME: Fix LLVM version parsing { std::cout << "Ethereum EVM JIT Compiler (http://github.com/ethereum/evmjit):\n" << " EVMJIT version " << EVMJIT_VERSION << "\n" @@ -90,19 +87,7 @@ void parseOptions() cl::ParseEnvironmentOptions("evmjit", "EVMJIT", "Ethereum EVM JIT Compiler"); } -// FIXME: It is temporary, becaue ExecutionEngine.h is currently our public header -// and including llvm::ExecutionEngine there is not a good idea. -llvm::ExecutionEngine* g_ee = nullptr; - -} - -ExecutionEngine& ExecutionEngine::get() -{ - static ExecutionEngine instance; - return instance; -} - -ExecutionEngine::ExecutionEngine() +std::unique_ptr init() { /// ExecutionEngine is created only once @@ -127,20 +112,23 @@ ExecutionEngine::ExecutionEngine() builder.setEngineKind(llvm::EngineKind::JIT); builder.setOptLevel(g_optimize ? llvm::CodeGenOpt::Default : llvm::CodeGenOpt::None); - g_ee = (builder.create()); + auto ee = std::unique_ptr{builder.create()}; // TODO: Update cache listener - g_ee->setObjectCache(Cache::init(g_cache, nullptr)); + ee->setObjectCache(Cache::init(g_cache, nullptr)); // FIXME: Disabled during API changes //if (preloadCache) // Cache::preload(*ee, funcCache); + + return ee; } +} ReturnCode ExecutionEngine::run(ExecutionContext& _context) { - ExecutionEngine::get(); // FIXME + static auto s_ee = init(); std::unique_ptr listener{new ExecStats}; listener->stateChanged(ExecState::Started); @@ -175,9 +163,9 @@ ReturnCode ExecutionEngine::run(ExecutionContext& _context) if (g_dump) module->dump(); - g_ee->addModule(std::move(module)); + s_ee->addModule(std::move(module)); listener->stateChanged(ExecState::CodeGen); - entryFuncPtr = (EntryFuncPtr)g_ee->getFunctionAddress(mainFuncName); + entryFuncPtr = (EntryFuncPtr)s_ee->getFunctionAddress(mainFuncName); if (!CHECK(entryFuncPtr)) return ReturnCode::LLVMLinkError; JIT::mapCode(codeHash, (void*)entryFuncPtr); // FIXME: Remove cast @@ -200,4 +188,3 @@ ReturnCode ExecutionEngine::run(ExecutionContext& _context) } } -} diff --git a/libevmjit/ExecutionEngine.h b/libevmjit/ExecutionEngine.h index 5c689403c..d14e50702 100644 --- a/libevmjit/ExecutionEngine.h +++ b/libevmjit/ExecutionEngine.h @@ -1,20 +1,14 @@ #pragma once -#include #include "Common.h" - namespace dev { namespace evmjit { - class ExecutionContext; -} -namespace eth -{ -namespace jit -{ - using namespace evmjit; // FIXME +class ExecutionContext; + +using namespace eth::jit; enum class ExecState { @@ -46,16 +40,8 @@ public: class ExecutionEngine { public: - ExecutionEngine(ExecutionEngine const&) = delete; - ExecutionEngine& operator=(ExecutionEngine const&) = delete; - EXPORT static ReturnCode run(ExecutionContext& _context); - -private: - ExecutionEngine(); - static ExecutionEngine& get(); }; } } -} diff --git a/libevmjit/interface.cpp b/libevmjit/interface.cpp index 18b36cd7f..3b4145cb7 100644 --- a/libevmjit/interface.cpp +++ b/libevmjit/interface.cpp @@ -3,8 +3,8 @@ extern "C" { - using namespace dev::eth::jit; +using namespace dev::evmjit; EXPORT void* evmjit_create(RuntimeData* _data, Env* _env) noexcept {