From 70279f86796ae883a1dfb9b84ebd293cc6e0aff1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Tue, 16 Dec 2014 14:46:36 +0100 Subject: [PATCH] Changes in setjmps --- libevmjit-cpp/Env.cpp | 3 +++ libevmjit/ExecutionEngine.cpp | 6 +++--- libevmjit/Runtime.cpp | 27 +++++++++++++++++++++++---- libevmjit/Runtime.h | 16 ++++++++++------ libevmjit/Utils.cpp | 8 ++++++++ libevmjit/Utils.h | 2 ++ 6 files changed, 49 insertions(+), 13 deletions(-) diff --git a/libevmjit-cpp/Env.cpp b/libevmjit-cpp/Env.cpp index 11761faf9..72dc062b4 100644 --- a/libevmjit-cpp/Env.cpp +++ b/libevmjit-cpp/Env.cpp @@ -67,6 +67,9 @@ extern "C" EXPORT bool env_call(ExtVMFace* _env, i256* io_gas, h256* _receiveAddress, i256* _value, byte* _inBeg, uint64_t _inSize, byte* _outBeg, uint64_t _outSize, h256* _codeAddress) { + if (_env->depth == 1024) + jit::terminate(jit::ReturnCode::OutOfGas); + assert(_env->depth < 1024); // TODO: Handle call depth auto value = llvm2eth(*_value); diff --git a/libevmjit/ExecutionEngine.cpp b/libevmjit/ExecutionEngine.cpp index 3fbc1e413..6e625388d 100644 --- a/libevmjit/ExecutionEngine.cpp +++ b/libevmjit/ExecutionEngine.cpp @@ -110,9 +110,9 @@ ReturnCode ExecutionEngine::run(std::unique_ptr _module, RuntimeDa ReturnCode ExecutionEngine::run(ExecBundle const& _exec, RuntimeData* _data, Env* _env) { ReturnCode returnCode; - std::jmp_buf buf; - Runtime runtime(_data, _env, buf); - auto r = setjmp(buf); + Runtime runtime(_data, _env); + + auto r = setjmp(runtime.getJmpBuf()); if (r == 0) { auto result = _exec.engine->runFunction(_exec.entryFunc, {{}, llvm::GenericValue(&runtime)}); diff --git a/libevmjit/Runtime.cpp b/libevmjit/Runtime.cpp index e725334db..c0ba5ebee 100644 --- a/libevmjit/Runtime.cpp +++ b/libevmjit/Runtime.cpp @@ -5,7 +5,7 @@ #include #include -//#include +#include namespace dev { @@ -13,12 +13,31 @@ namespace eth { namespace jit { +namespace +{ + jmp_buf_ref g_currJmpBuf; +} + +jmp_buf_ref Runtime::getCurrJmpBuf() +{ + return g_currJmpBuf; +} -Runtime::Runtime(RuntimeData* _data, Env* _env, JmpBufRef _jmpBuf): +Runtime::Runtime(RuntimeData* _data, Env* _env): m_data(*_data), m_env(*_env), - m_jmpBuf(_jmpBuf) -{} + m_currJmpBuf(m_jmpBuf), + m_prevJmpBuf(g_currJmpBuf) +{ + g_currJmpBuf = m_jmpBuf; + std::cerr << "JB push " << g_currJmpBuf << "\n"; +} + +Runtime::~Runtime() +{ + std::cerr << "JB pop " << g_currJmpBuf << "\n"; + g_currJmpBuf = m_prevJmpBuf; +} u256 Runtime::getGas() const { diff --git a/libevmjit/Runtime.h b/libevmjit/Runtime.h index d86acbfc4..adb736ecc 100644 --- a/libevmjit/Runtime.h +++ b/libevmjit/Runtime.h @@ -26,12 +26,13 @@ namespace jit using StackImpl = std::vector; using MemoryImpl = bytes; -using JmpBufRef = decltype(&jmp_buf{}[0]); +using jmp_buf_ref = decltype(&std::jmp_buf{}[0]); class Runtime { public: - Runtime(RuntimeData* _data, Env* _env, JmpBufRef _jmpBuf); + Runtime(RuntimeData* _data, Env* _env); + ~Runtime(); Runtime(const Runtime&) = delete; void operator=(const Runtime&) = delete; @@ -42,12 +43,15 @@ public: u256 getGas() const; bytes getReturnData() const; - JmpBufRef getJmpBuf() { return m_jmpBuf; } + jmp_buf_ref getJmpBuf() { return m_jmpBuf; } + static jmp_buf_ref getCurrJmpBuf(); private: - RuntimeData& m_data; - Env& m_env; - JmpBufRef m_jmpBuf; + RuntimeData& m_data; ///< Pointer to data. Expected by compiled contract. + Env& m_env; ///< Pointer to environment proxy. Expected by compiled contract. + jmp_buf_ref m_currJmpBuf; ///< Pointer to jump buffer. Expected by compiled contract. + jmp_buf_ref m_prevJmpBuf; + std::jmp_buf m_jmpBuf; StackImpl m_stack; MemoryImpl m_memory; }; diff --git a/libevmjit/Utils.cpp b/libevmjit/Utils.cpp index 0dad56548..6c2bd81ba 100644 --- a/libevmjit/Utils.cpp +++ b/libevmjit/Utils.cpp @@ -1,6 +1,8 @@ +#include #include "Utils.h" #include "Instruction.h" +#include "Runtime.h" namespace dev { @@ -53,6 +55,12 @@ u256 readPushData(bytes::const_iterator& _curr, bytes::const_iterator _end) return value; } +void terminate(ReturnCode _returnCode) +{ + auto jmpBuf = Runtime::getCurrJmpBuf(); + std::longjmp(jmpBuf, static_cast(_returnCode)); +} + } } } diff --git a/libevmjit/Utils.h b/libevmjit/Utils.h index f672365c6..db0647fdf 100644 --- a/libevmjit/Utils.h +++ b/libevmjit/Utils.h @@ -17,6 +17,8 @@ struct JIT: public NoteChannel { static const char* name() { return "JIT"; } }; u256 llvm2eth(i256); i256 eth2llvm(u256); +void terminate(ReturnCode _returnCode); + } } }