<commit_before>#include "Memory.h"

#include "preprocessor/llvm_includes_start.h"
#include <llvm/IR/IntrinsicInst.h>
#include "preprocessor/llvm_includes_end.h"

#include "Type.h"
#include "Runtime.h"
#include "GasMeter.h"
#include "Endianness.h"
#include "RuntimeManager.h"

namespace dev
{
namespace eth
{
namespace jit
{

Memory::Memory(RuntimeManager& _runtimeManager, GasMeter& _gasMeter):
	RuntimeHelper(_runtimeManager),  // TODO: RuntimeHelper not needed
	m_memory{getBuilder(), _runtimeManager.getMem()},
	m_gasMeter(_gasMeter)
{}

llvm::Function* Memory::getRequireFunc()
{
	auto& func = m_require;
	if (!func)
	{
		llvm::Type* argTypes[] = {Array::getType()->getPointerTo(), Type::Word, Type::Word, Type::BytePtr, Type::GasPtr};
		func = llvm::Function::Create(llvm::FunctionType::get(Type::Void, argTypes, false), llvm::Function::PrivateLinkage, "mem.require", getModule());
		func->setDoesNotThrow();

		auto mem = &func->getArgumentList().front();
		mem->setName("mem");
		auto blkOffset = mem->getNextNode();
		blkOffset->setName("blkOffset");
		auto blkSize = blkOffset->getNextNode();
		blkSize->setName("blkSize");
		auto jmpBuf = blkSize->getNextNode();
		jmpBuf->setName("jmpBuf");
		auto gas = jmpBuf->getNextNode();
		gas->setName("gas");

		auto preBB = llvm::BasicBlock::Create(func->getContext(), "Pre", func);
		auto checkBB = llvm::BasicBlock::Create(func->getContext(), "Check", func);
		auto resizeBB = llvm::BasicBlock::Create(func->getContext(), "Resize", func);
		auto returnBB = llvm::BasicBlock::Create(func->getContext(), "Return", func);

		InsertPointGuard guard(m_builder); // Restores insert point at function exit

		// BB "Pre": Ignore checks with size 0
		m_builder.SetInsertPoint(preBB);
		m_builder.CreateCondBr(m_builder.CreateICmpNE(blkSize, Constant::get(0)), checkBB, returnBB, Type::expectTrue);

		// BB "Check"
		m_builder.SetInsertPoint(checkBB);
		static const auto c_inputMax = uint64_t(1) << 32; // max value of blkSize and blkOffset that will not result in integer overflow in calculations below
		auto blkOffsetOk = m_builder.CreateICmpULE(blkOffset, Constant::get(c_inputMax), "blkOffsetOk");
		auto blkO = m_builder.CreateSelect(blkOffsetOk, m_builder.CreateTrunc(blkOffset, Type::Size), m_builder.getInt64(c_inputMax), "bklO");
		auto blkSizeOk = m_builder.CreateICmpULE(blkSize, Constant::get(c_inputMax), "blkSizeOk");
		auto blkS = m_builder.CreateSelect(blkSizeOk, m_builder.CreateTrunc(blkSize, Type::Size), m_builder.getInt64(c_inputMax), "bklS");

		auto sizeReq0 = m_builder.CreateNUWAdd(blkO, blkS, "sizeReq0");
		auto sizeReq = m_builder.CreateAnd(m_builder.CreateNUWAdd(sizeReq0, m_builder.getInt64(31)), uint64_t(-1) << 5, "sizeReq"); // s' = ((s0 + 31) / 32) * 32
		auto sizeCur = m_memory.size(mem);
		auto sizeOk = m_builder.CreateICmpULE(sizeReq, sizeCur, "sizeOk");

		m_builder.CreateCondBr(sizeOk, returnBB, resizeBB, Type::expectTrue);

		// BB "Resize"
		m_builder.SetInsertPoint(resizeBB);
		// Check gas first
		auto w1 = m_builder.CreateLShr(sizeReq, 5);
		auto w1s = m_builder.CreateNUWMul(w1, w1);
		auto c1 = m_builder.CreateAdd(w1, m_builder.CreateLShr(w1s, 10));
		auto w0 = m_builder.CreateLShr(sizeCur, 5);<commit_msg>Increase memory limit to 2^33 (8GB)<commit_after>#include "Memory.h"

#include "preprocessor/llvm_includes_start.h"
#include <llvm/IR/IntrinsicInst.h>
#include "preprocessor/llvm_includes_end.h"

#include "Type.h"
#include "Runtime.h"
#include "GasMeter.h"
#include "Endianness.h"
#include "RuntimeManager.h"

namespace dev
{
namespace eth
{
namespace jit
{

Memory::Memory(RuntimeManager& _runtimeManager, GasMeter& _gasMeter):
	RuntimeHelper(_runtimeManager),  // TODO: RuntimeHelper not needed
	m_memory{getBuilder(), _runtimeManager.getMem()},
	m_gasMeter(_gasMeter)
{}

llvm::Function* Memory::getRequireFunc()
{
	auto& func = m_require;
	if (!func)
	{
		llvm::Type* argTypes[] = {Array::getType()->getPointerTo(), Type::Word, Type::Word, Type::BytePtr, Type::GasPtr};
		func = llvm::Function::Create(llvm::FunctionType::get(Type::Void, argTypes, false), llvm::Function::PrivateLinkage, "mem.require", getModule());
		func->setDoesNotThrow();

		auto mem = &func->getArgumentList().front();
		mem->setName("mem");
		auto blkOffset = mem->getNextNode();
		blkOffset->setName("blkOffset");
		auto blkSize = blkOffset->getNextNode();
		blkSize->setName("blkSize");
		auto jmpBuf = blkSize->getNextNode();
		jmpBuf->setName("jmpBuf");
		auto gas = jmpBuf->getNextNode();
		gas->setName("gas");

		auto preBB = llvm::BasicBlock::Create(func->getContext(), "Pre", func);
		auto checkBB = llvm::BasicBlock::Create(func->getContext(), "Check", func);
		auto resizeBB = llvm::BasicBlock::Create(func->getContext(), "Resize", func);
		auto returnBB = llvm::BasicBlock::Create(func->getContext(), "Return", func);

		InsertPointGuard guard(m_builder); // Restores insert point at function exit

		// BB "Pre": Ignore checks with size 0
		m_builder.SetInsertPoint(preBB);
		m_builder.CreateCondBr(m_builder.CreateICmpNE(blkSize, Constant::get(0)), checkBB, returnBB, Type::expectTrue);

		// BB "Check"
		m_builder.SetInsertPoint(checkBB);
		static const auto c_inputMax = uint64_t(1) << 33; // max value of blkSize and blkOffset that will not result in integer overflow in calculations below
		auto blkOffsetOk = m_builder.CreateICmpULE(blkOffset, Constant::get(c_inputMax), "blkOffsetOk");
		auto blkO = m_builder.CreateSelect(blkOffsetOk, m_builder.CreateTrunc(blkOffset, Type::Size), m_builder.getInt64(c_inputMax), "bklO");
		auto blkSizeOk = m_builder.CreateICmpULE(blkSize, Constant::get(c_inputMax), "blkSizeOk");
		auto blkS = m_builder.CreateSelect(blkSizeOk, m_builder.CreateTrunc(blkSize, Type::Size), m_builder.getInt64(c_inputMax), "bklS");

		auto sizeReq0 = m_builder.CreateNUWAdd(blkO, blkS, "sizeReq0");
		auto sizeReq = m_builder.CreateAnd(m_builder.CreateNUWAdd(sizeReq0, m_builder.getInt64(31)), uint64_t(-1) << 5, "sizeReq"); // s' = ((s0 + 31) / 32) * 32
		auto sizeCur = m_memory.size(mem);
		auto sizeOk = m_builder.CreateICmpULE(sizeReq, sizeCur, "sizeOk");

		m_builder.CreateCondBr(sizeOk, returnBB, resizeBB, Type::expectTrue);

		// BB "Resize"
		m_builder.SetInsertPoint(resizeBB);
		// Check gas first
		auto w1 = m_builder.CreateLShr(sizeReq, 5);
		auto w1s = m_builder.CreateNUWMul(w1, w1);
		auto c1 = m_builder.CreateAdd(w1, m_builder.CreateLShr(w1s, 10));
		auto w0 = m_builder.CreateLShr(sizeCur, 5);
{'fraction_non_alphanumeric': 0.15512422360248448, 'fraction_numerical': 0.013354037267080746, 'mean_word_length': 6.286199095022624, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5732110', 'n_tokens_mistral': 2383, 'n_tokens_neox': 2272, 'n_words': 444}