#include "Memory.h" #include #include #include #include #include #include #include #ifdef _MSC_VER #define EXPORT __declspec(dllexport) #else #define EXPORT #endif namespace evmcc { struct i256 { uint64_t a; uint64_t b; uint64_t c; uint64_t d; }; static_assert(sizeof(i256) == 32, "Wrong i256 size"); using MemoryImpl = dev::bytes; Memory::Memory(llvm::IRBuilder<>& _builder, llvm::Module* _module) : m_builder(_builder) { auto voidTy = m_builder.getVoidTy(); auto i64Ty = m_builder.getInt64Ty(); auto memoryCreate = llvm::Function::Create(llvm::FunctionType::get(voidTy, false), llvm::GlobalValue::LinkageTypes::ExternalLinkage, "evmccrt_memory_create", _module); m_builder.CreateCall(memoryCreate); auto memRequireTy = llvm::FunctionType::get(m_builder.getInt8PtrTy(), i64Ty, false); m_memRequire = llvm::Function::Create(memRequireTy, llvm::GlobalValue::LinkageTypes::ExternalLinkage, "evmccrt_memory_require", _module); auto memSizeTy = llvm::FunctionType::get(i64Ty, false); m_memSize = llvm::Function::Create(memSizeTy, llvm::GlobalValue::LinkageTypes::ExternalLinkage, "evmccrt_memory_size", _module); std::vector argTypes = {i64Ty, i64Ty}; auto dumpTy = llvm::FunctionType::get(m_builder.getVoidTy(), llvm::ArrayRef(argTypes), false); m_memDump = llvm::Function::Create(dumpTy, llvm::GlobalValue::LinkageTypes::ExternalLinkage, "evmccrt_memory_dump", _module); } llvm::Value* Memory::loadWord(llvm::Value* _addr) { // trunc _addr (an i256) to i64 index and use it to index the memory auto index = m_builder.CreateTrunc(_addr, m_builder.getInt64Ty(), "index"); // load from evmccrt_memory_require()[index] auto base = m_builder.CreateCall(m_memRequire, index, "base"); auto ptr = m_builder.CreateGEP(base, index, "ptr"); auto i256ptrTy = m_builder.getIntNTy(256)->getPointerTo(); auto wordPtr = m_builder.CreateBitCast(ptr, i256ptrTy, "wordptr"); auto byte = m_builder.CreateLoad(wordPtr, "word"); dump(0); return byte; } void Memory::storeWord(llvm::Value* _addr, llvm::Value* _word) { auto index = m_builder.CreateTrunc(_addr, m_builder.getInt64Ty(), "index"); auto index32 = m_builder.CreateAdd(index, llvm::ConstantInt::get(m_builder.getInt64Ty(), 32), "index32"); auto base = m_builder.CreateCall(m_memRequire, index32, "base"); auto ptr = m_builder.CreateGEP(base, index, "ptr"); auto i256ptrTy = m_builder.getIntNTy(256)->getPointerTo(); auto wordPtr = m_builder.CreateBitCast(ptr, i256ptrTy, "wordptr"); m_builder.CreateStore(_word, wordPtr); dump(0); } void Memory::storeByte(llvm::Value* _addr, llvm::Value* _word) { auto byte = m_builder.CreateTrunc(_word, m_builder.getInt8Ty(), "byte"); auto index = m_builder.CreateTrunc(_addr, m_builder.getInt64Ty(), "index"); auto base = m_builder.CreateCall(m_memRequire, index, "base"); auto ptr = m_builder.CreateGEP(base, index, "ptr"); m_builder.CreateStore(byte, ptr); dump(0); } llvm::Value* Memory::getSize() { auto size = m_builder.CreateCall(m_memSize, "mem.size"); auto word = m_builder.CreateZExt(size, m_builder.getIntNTy(256), "mem.wsize"); return word; } void Memory::dump(uint64_t _begin, uint64_t _end) { if (getenv("EVMCC_DEBUG_MEMORY") == nullptr) return; auto beginVal = llvm::ConstantInt::get(m_builder.getInt64Ty(), _begin); auto endVal = llvm::ConstantInt::get(m_builder.getInt64Ty(), _end); std::vector args = {beginVal, endVal}; m_builder.CreateCall(m_memDump, llvm::ArrayRef(args)); } } // namespace evmcc extern "C" { using namespace evmcc; EXPORT MemoryImpl* evmccrt_memory; EXPORT void evmccrt_memory_create(void) { evmccrt_memory = new MemoryImpl(); std::cerr << "MEMORY: create(), initial size = " << evmccrt_memory->size() << std::endl; } // Resizes memory to contain at least _index + 1 bytes and returns the base address. EXPORT uint8_t* evmccrt_memory_require(uint64_t _index) { uint64_t size = _index + 1; std::cerr << "MEMORY: require(), current size = " << evmccrt_memory->size() << ", required size = " << size << std::endl; if (evmccrt_memory->size() < size) evmccrt_memory->resize(size); return &(*evmccrt_memory)[0]; } EXPORT uint64_t evmccrt_memory_size() { return (evmccrt_memory->size() + 31) / 32; } EXPORT void evmccrt_memory_dump(uint64_t _begin, uint64_t _end) { if (_end == 0) _end = evmccrt_memory->size(); std::cerr << "Active memory size: " << evmccrt_memory_size() << " words\n"; std::cerr << "Memory dump from " << std::hex << _begin << " to " << std::hex << _end << ":"; if (_end <= _begin) return; _begin = _begin / 16 * 16; for (size_t i = _begin; i < _end; i++) { if ((i - _begin) % 16 == 0) std::cerr << '\n' << std::dec << i << ": "; uint8_t b = (*evmccrt_memory)[i]; std::cerr << std::hex << std::setw(2) << static_cast(b) << ' '; } std::cerr << std::endl; } } // extern "C"