Adjust data section size during creation.

This commit is contained in:
Daniel Kirchner
2023-01-05 13:06:14 +01:00
parent b8f74c968e
commit b73d082809
5 changed files with 48 additions and 4 deletions
@@ -5,14 +5,31 @@ contract D {
}
}
// TODO: this is horrible and hopefully avoided at the spec level
function adjustContractCodeForArgSize(bytes memory x, uint16 argSize)
{
assembly {
let memPos := add(x, 32)
let numCodeSections := shr(240, mload(add(memPos, 7)))
let dataSectionSizeOffset := add(memPos, add(10, mul(numCodeSections, 2)))
let tmp := mload(dataSectionSizeOffset)
let dataSectionSize := shr(240, tmp)
dataSectionSize := add(dataSectionSize, argSize)
if gt(dataSectionSize, 0xFFFF) { revert(0,0) }
mstore(dataSectionSizeOffset, or(shr(16, shl(16, tmp)), shl(240, dataSectionSize)))
}
}
contract C {
function createDSalted(bytes32 salt, uint arg) public {
bytes memory creationCode = type(D).creationCode;
adjustContractCodeForArgSize(creationCode, 32);
address predictedAddress = address(uint160(uint(keccak256(abi.encodePacked(
bytes1(0xff),
address(this),
salt,
keccak256(abi.encodePacked(
type(D).creationCode,
creationCode,
arg
))
)))));