From e12fa127f4f6bf3293c65761cf5825e8d12b32eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Fri, 2 Jan 2015 16:05:59 +0100 Subject: [PATCH] Suicide rework --- libevmjit-cpp/Env.cpp | 5 ----- libevmjit-cpp/JitVM.cpp | 4 ++++ libevmjit/Compiler.cpp | 13 +++++++------ libevmjit/Ext.cpp | 8 -------- libevmjit/Ext.h | 2 -- libevmjit/RuntimeData.h | 4 +++- libevmjit/RuntimeManager.cpp | 5 +++++ libevmjit/RuntimeManager.h | 1 + 8 files changed, 20 insertions(+), 22 deletions(-) diff --git a/libevmjit-cpp/Env.cpp b/libevmjit-cpp/Env.cpp index 50945583b..c0d8db75a 100644 --- a/libevmjit-cpp/Env.cpp +++ b/libevmjit-cpp/Env.cpp @@ -42,11 +42,6 @@ extern "C" *o_value = eth2llvm(u); } - EXPORT void ext_suicide(ExtVMFace* _env, h256 const* _address) - { - _env->suicide(right160(*_address)); - } - EXPORT void env_create(ExtVMFace* _env, i256* io_gas, i256* _endowment, byte* _initBeg, uint64_t _initSize, h256* o_address) { if (_env->depth == 1024) diff --git a/libevmjit-cpp/JitVM.cpp b/libevmjit-cpp/JitVM.cpp index 46dabe275..815aa6332 100644 --- a/libevmjit-cpp/JitVM.cpp +++ b/libevmjit-cpp/JitVM.cpp @@ -34,6 +34,10 @@ bytesConstRef JitVM::go(ExtVMFace& _ext, OnOpFunc const&, uint64_t) auto exitCode = m_engine.run(_ext.code, &m_data, env); switch (exitCode) { + case ReturnCode::Suicide: + _ext.suicide(right160(m_data.get(RuntimeData::SuicideDestAddress))); + break; + case ReturnCode::BadJumpDestination: BOOST_THROW_EXCEPTION(BadJumpDestination()); case ReturnCode::OutOfGas: diff --git a/libevmjit/Compiler.cpp b/libevmjit/Compiler.cpp index 73c96b60b..48dc50d60 100644 --- a/libevmjit/Compiler.cpp +++ b/libevmjit/Compiler.cpp @@ -798,14 +798,15 @@ void Compiler::compileBasicBlock(BasicBlock& _basicBlock, bytes const& _bytecode } case Instruction::SUICIDE: - case Instruction::STOP: { - if (inst == Instruction::SUICIDE) - { - auto address = stack.pop(); - _ext.suicide(address); - } + _runtimeManager.registerSuicide(stack.pop()); + m_builder.CreateRet(Constant::get(ReturnCode::Suicide)); + break; + } + + case Instruction::STOP: + { m_builder.CreateRet(Constant::get(ReturnCode::Stop)); break; } diff --git a/libevmjit/Ext.cpp b/libevmjit/Ext.cpp index 2b9802bb0..98063b538 100644 --- a/libevmjit/Ext.cpp +++ b/libevmjit/Ext.cpp @@ -44,7 +44,6 @@ Ext::Ext(RuntimeManager& _runtimeManager, Memory& _memoryMan): m_sload = llvm::Function::Create(llvm::FunctionType::get(Type::Void, {argsTypes, 3}, false), Linkage::ExternalLinkage, "env_sload", module); m_sstore = llvm::Function::Create(llvm::FunctionType::get(Type::Void, {argsTypes, 3}, false), Linkage::ExternalLinkage, "env_sstore", module); m_balance = llvm::Function::Create(llvm::FunctionType::get(Type::Void, {argsTypes, 3}, false), Linkage::ExternalLinkage, "ext_balance", module); - m_suicide = llvm::Function::Create(llvm::FunctionType::get(Type::Void, {argsTypes, 2}, false), Linkage::ExternalLinkage, "ext_suicide", module); llvm::Type* sha3ArgsTypes[] = {Type::BytePtr, Type::Size, Type::WordPtr}; m_sha3 = llvm::Function::Create(llvm::FunctionType::get(Type::Void, sha3ArgsTypes, false), Linkage::ExternalLinkage, "env_sha3", module); @@ -96,13 +95,6 @@ llvm::Value* Ext::balance(llvm::Value* _address) return m_builder.CreateLoad(m_args[1]); } -void Ext::suicide(llvm::Value* _address) -{ - auto address = Endianness::toBE(m_builder, _address); - m_builder.CreateStore(address, m_args[0]); - m_builder.CreateCall2(m_suicide, getRuntimeManager().getEnvPtr(), m_args[0]); -} - llvm::Value* Ext::create(llvm::Value*& _gas, llvm::Value* _endowment, llvm::Value* _initOff, llvm::Value* _initSize) { m_builder.CreateStore(_gas, m_args[0]); diff --git a/libevmjit/Ext.h b/libevmjit/Ext.h index 0227d806d..9c79b93d5 100644 --- a/libevmjit/Ext.h +++ b/libevmjit/Ext.h @@ -27,7 +27,6 @@ public: void sstore(llvm::Value* _index, llvm::Value* _value); llvm::Value* balance(llvm::Value* _address); - void suicide(llvm::Value* _address); llvm::Value* calldataload(llvm::Value* _index); llvm::Value* create(llvm::Value*& _gas, llvm::Value* _endowment, llvm::Value* _initOff, llvm::Value* _initSize); llvm::Value* call(llvm::Value*& _gas, llvm::Value* _receiveAddress, llvm::Value* _value, llvm::Value* _inOff, llvm::Value* _inSize, llvm::Value* _outOff, llvm::Value* _outSize, llvm::Value* _codeAddress); @@ -54,7 +53,6 @@ private: llvm::Function* m_sstore; llvm::Function* m_calldataload; llvm::Function* m_balance; - llvm::Function* m_suicide; llvm::Function* m_create; llvm::Function* m_call; llvm::Function* m_sha3; diff --git a/libevmjit/RuntimeData.h b/libevmjit/RuntimeData.h index b17f52620..89987bdeb 100644 --- a/libevmjit/RuntimeData.h +++ b/libevmjit/RuntimeData.h @@ -32,7 +32,8 @@ struct RuntimeData _size, ReturnDataOffset = CallValue, // Reuse 2 fields for return data reference - ReturnDataSize = CallDataSize + ReturnDataSize = CallDataSize, + SuicideDestAddress = Address, ///< Suicide balance destination address }; i256 elems[_size] = {}; @@ -40,6 +41,7 @@ struct RuntimeData byte const* code = nullptr; void set(Index _index, u256 _value) { elems[_index] = eth2llvm(_value); } + u256 get(Index _index) { return llvm2eth(elems[_index]); } }; /// VM Environment (ExtVM) opaque type diff --git a/libevmjit/RuntimeManager.cpp b/libevmjit/RuntimeManager.cpp index e07adbe65..14280f80f 100644 --- a/libevmjit/RuntimeManager.cpp +++ b/libevmjit/RuntimeManager.cpp @@ -132,6 +132,11 @@ void RuntimeManager::registerReturnData(llvm::Value* _offset, llvm::Value* _size set(RuntimeData::ReturnDataSize, _size); } +void RuntimeManager::registerSuicide(llvm::Value* _balanceAddress) +{ + set(RuntimeData::SuicideDestAddress, _balanceAddress); +} + void RuntimeManager::raiseException(ReturnCode _returnCode) { m_builder.CreateCall2(m_longjmp, getJmpBuf(), Constant::get(_returnCode)); diff --git a/libevmjit/RuntimeManager.h b/libevmjit/RuntimeManager.h index 1cff606e3..ce60424ac 100644 --- a/libevmjit/RuntimeManager.h +++ b/libevmjit/RuntimeManager.h @@ -29,6 +29,7 @@ public: void setGas(llvm::Value* _gas); void registerReturnData(llvm::Value* _index, llvm::Value* _size); + void registerSuicide(llvm::Value* _balanceAddress); void raiseException(ReturnCode _returnCode);