Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide access to a contract's creation and runtime code #5647

Closed
chriseth opened this issue Dec 13, 2018 · 12 comments
Closed

Provide access to a contract's creation and runtime code #5647

chriseth opened this issue Dec 13, 2018 · 12 comments
Assignees

Comments

@chriseth
Copy link
Contributor

Contract types should get new members:

  • creationCode
  • runtimeCode

which are immutable members of bytes memory type that provide access to the contract's creation and runtime code. This is useful for using create2 in inline assembly.

If the optimizer does not compute keccak256(C.creationCode), we should also provide an accessor for that.

Note that we have to be careful with libraries, because they modifier their code at creation time.

@chriseth chriseth changed the title Provide access to a contract's creation and runtime code [Breaking] Provide access to a contract's creation and runtime code Dec 20, 2018
@chriseth chriseth changed the title [Breaking] Provide access to a contract's creation and runtime code Provide access to a contract's creation and runtime code Dec 20, 2018
@chriseth
Copy link
Contributor Author

@axic noted that it should not be breaking and I think I agree :)

@jochem-brouwer
Copy link
Member

This is dangerous!

Example:

contract Test{

function a() public view returns (uint) { return 3; }

    constructor() public { 
        assembly{ 

            return(0,0)
        }
    }
}


This does not dump any code at an address while solidity assumes that the function a() can be reached. Hence runtime code is empty, but solidity thinks runtime code is not empty.

@chriseth
Copy link
Contributor Author

chriseth commented Jan 7, 2019

I agree that this needs to be properly documented, but I would argue that the runtime code of the contract type Test is still well-defined and non-empty. Using C.runtimeCode has the same pitfalls as calling a on a contract of type C has. The danger does not lie in C.runtimeCode, it lies in not properly understanding the code of a contract that uses inline assembly.

@axic
Copy link
Member

axic commented Jan 10, 2019

@chriseth mentioned contract types already have structs and enums as properties and runtimeCode/creationCode would conflict with that.

A potential solutions is to consider #1447: meta(C).runtimeCode.

meta is not a keyword currently and this would mean making this a breaking change. @chriseth suggested to use type(C), I've suggested to use contract(C) as an alternative solution.

@ekpyron
Copy link
Member

ekpyron commented Jan 10, 2019

At the moment I'd actually prefer type over meta and contract...

@ekpyron
Copy link
Member

ekpyron commented Jan 10, 2019

Although I'd make it a method then type(C).creationCode().

@chriseth
Copy link
Contributor Author

Implementing as type(C).creationCode and type(C).runtimeCode. The same restrictions about cyclicity apply as for the new operator (cannot access the code of the current contract, etc).

@axic
Copy link
Member

axic commented Jan 15, 2019

The same restrictions about cyclicity apply as for the new operator (cannot access the code of the current contract, etc).

That shouldn't be problem if it uses codecopy.

@chriseth
Copy link
Contributor Author

@axic I'm going with the same semantics for new for now for ease of implementation. If people request this, we can relax it. For now, I have only seen factory contracts and not contracts that can clone themselves.

@chriseth
Copy link
Contributor Author

It turns out the optimizer cannot optimize keccak256(type(C).creationCode). Furthermore, it might also be difficult to provide direct access to it, because the hash will only be fixed after assembly.

@axic
Copy link
Member

axic commented Jan 15, 2019

Right, perhaps if we assume the main user going to be factory contracts, then for the single reason of being able to optimise out the has it makes sense to make that restriction right now.

@chriseth
Copy link
Contributor Author

chriseth commented Feb 9, 2021

Why did we call this runtimeCode and not deployedCode? :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants