aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorchriseth <chris@ethereum.org>2019-01-18 07:16:06 +0800
committerGitHub <noreply@github.com>2019-01-18 07:16:06 +0800
commit2ec997e697e306dd54165aad365406ee88c534cb (patch)
tree5e943e23d38e332de3eedd33be11f2cf9df5fd69 /test
parent0711873a2f13d7b0f27e268fcd0a7683665f339d (diff)
parent2a92403690a4998ab097503231ac39f854b9c76c (diff)
downloaddexon-solidity-2ec997e697e306dd54165aad365406ee88c534cb.tar
dexon-solidity-2ec997e697e306dd54165aad365406ee88c534cb.tar.gz
dexon-solidity-2ec997e697e306dd54165aad365406ee88c534cb.tar.bz2
dexon-solidity-2ec997e697e306dd54165aad365406ee88c534cb.tar.lz
dexon-solidity-2ec997e697e306dd54165aad365406ee88c534cb.tar.xz
dexon-solidity-2ec997e697e306dd54165aad365406ee88c534cb.tar.zst
dexon-solidity-2ec997e697e306dd54165aad365406ee88c534cb.zip
Merge pull request #5775 from ethereum/codeAccess
Provide access to code of contract types.
Diffstat (limited to 'test')
-rw-r--r--test/libsolidity/ASTJSON/address_payable.json2
-rw-r--r--test/libsolidity/ASTJSON/address_payable_legacy.json2
-rw-r--r--test/libsolidity/Assembly.cpp3
-rw-r--r--test/libsolidity/SolidityEndToEndTest.cpp107
-rw-r--r--test/libsolidity/SolidityParser.cpp1
-rw-r--r--test/libsolidity/syntaxTests/metaTypes/codeAccess.sol12
-rw-r--r--test/libsolidity/syntaxTests/metaTypes/codeAccessAbstractCreation.sol10
-rw-r--r--test/libsolidity/syntaxTests/metaTypes/codeAccessAbstractRuntime.sol10
-rw-r--r--test/libsolidity/syntaxTests/metaTypes/codeAccessBase.sol22
-rw-r--r--test/libsolidity/syntaxTests/metaTypes/codeAccessCyclic.sol12
-rw-r--r--test/libsolidity/syntaxTests/metaTypes/codeAccessIsConstant.sol7
-rw-r--r--test/libsolidity/syntaxTests/metaTypes/codeAccessLibrary.sol12
-rw-r--r--test/libsolidity/syntaxTests/metaTypes/codeIsNoLValue.sol10
-rw-r--r--test/libsolidity/syntaxTests/metaTypes/noArgForType.sol7
-rw-r--r--test/libsolidity/syntaxTests/metaTypes/runtimeCodeWarningAssembly.sol17
-rw-r--r--test/libsolidity/syntaxTests/metaTypes/tooManyArgsForType.sol7
-rw-r--r--test/libsolidity/syntaxTests/metaTypes/typeNotRegularIdentifierContractName.sol3
-rw-r--r--test/libsolidity/syntaxTests/metaTypes/typeNotRegularIdentifierFunction.sol6
-rw-r--r--test/libsolidity/syntaxTests/metaTypes/typeNotRegularIdentifierParameter.sol6
-rw-r--r--test/libsolidity/syntaxTests/metaTypes/typeNotRegularIdentifierStateVariable.sol5
-rw-r--r--test/libsolidity/syntaxTests/metaTypes/typeNotRegularIdentifierVariable.sol7
-rw-r--r--test/libsolidity/syntaxTests/metaTypes/typeOfContract.sol6
-rw-r--r--test/libsolidity/syntaxTests/metaTypes/typeRecursive.sol8
-rw-r--r--test/libsolidity/syntaxTests/metaTypes/unsupportedArgForType.sol9
24 files changed, 287 insertions, 4 deletions
diff --git a/test/libsolidity/ASTJSON/address_payable.json b/test/libsolidity/ASTJSON/address_payable.json
index 0f30e8e8..c8b71628 100644
--- a/test/libsolidity/ASTJSON/address_payable.json
+++ b/test/libsolidity/ASTJSON/address_payable.json
@@ -277,7 +277,7 @@
"name" : "this",
"nodeType" : "Identifier",
"overloadedDeclarations" : [],
- "referencedDeclaration" : 65,
+ "referencedDeclaration" : 66,
"src" : "217:4:1",
"typeDescriptions" :
{
diff --git a/test/libsolidity/ASTJSON/address_payable_legacy.json b/test/libsolidity/ASTJSON/address_payable_legacy.json
index dd8a5582..8abebce0 100644
--- a/test/libsolidity/ASTJSON/address_payable_legacy.json
+++ b/test/libsolidity/ASTJSON/address_payable_legacy.json
@@ -424,7 +424,7 @@
[
null
],
- "referencedDeclaration" : 65,
+ "referencedDeclaration" : 66,
"type" : "contract C",
"value" : "this"
},
diff --git a/test/libsolidity/Assembly.cpp b/test/libsolidity/Assembly.cpp
index baa9bff1..b5a1797b 100644
--- a/test/libsolidity/Assembly.cpp
+++ b/test/libsolidity/Assembly.cpp
@@ -46,6 +46,7 @@ namespace dev
{
namespace solidity
{
+class Contract;
namespace test
{
@@ -84,7 +85,7 @@ eth::AssemblyItems compileContract(std::shared_ptr<CharStream> _sourceCode)
if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
{
Compiler compiler(dev::test::Options::get().evmVersion());
- compiler.compileContract(*contract, map<ContractDefinition const*, Assembly const*>{}, bytes());
+ compiler.compileContract(*contract, map<ContractDefinition const*, shared_ptr<Compiler const>>{}, bytes());
return compiler.runtimeAssemblyItems();
}
diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp
index 8d219d16..38be5ae7 100644
--- a/test/libsolidity/SolidityEndToEndTest.cpp
+++ b/test/libsolidity/SolidityEndToEndTest.cpp
@@ -14234,6 +14234,113 @@ BOOST_AUTO_TEST_CASE(base_access_to_function_type_variables)
ABI_CHECK(callContractFunction("h()"), encodeArgs(2));
}
+BOOST_AUTO_TEST_CASE(code_access)
+{
+ char const* sourceCode = R"(
+ contract C {
+ function lengths() public pure returns (bool) {
+ uint crLen = type(D).creationCode.length;
+ uint runLen = type(D).runtimeCode.length;
+ require(runLen < crLen);
+ require(crLen >= 0x20);
+ require(runLen >= 0x20);
+ return true;
+ }
+ function creation() public pure returns (bytes memory) {
+ return type(D).creationCode;
+ }
+ function runtime() public pure returns (bytes memory) {
+ return type(D).runtimeCode;
+ }
+ function runtimeAllocCheck() public pure returns (bytes memory) {
+ uint[] memory a = new uint[](2);
+ bytes memory c = type(D).runtimeCode;
+ uint[] memory b = new uint[](2);
+ a[0] = 0x1111;
+ a[1] = 0x2222;
+ b[0] = 0x3333;
+ b[1] = 0x4444;
+ return c;
+ }
+ }
+ contract D {
+ function f() public pure returns (uint) { return 7; }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "C");
+ ABI_CHECK(callContractFunction("lengths()"), encodeArgs(true));
+ bytes codeCreation = callContractFunction("creation()");
+ bytes codeRuntime1 = callContractFunction("runtime()");
+ bytes codeRuntime2 = callContractFunction("runtimeAllocCheck()");
+ ABI_CHECK(codeRuntime1, codeRuntime2);
+}
+
+BOOST_AUTO_TEST_CASE(code_access_create)
+{
+ char const* sourceCode = R"(
+ contract C {
+ function test() public returns (uint) {
+ bytes memory c = type(D).creationCode;
+ D d;
+ assembly {
+ d := create(0, add(c, 0x20), mload(c))
+ }
+ return d.f();
+ }
+ }
+ contract D {
+ uint x;
+ constructor() public { x = 7; }
+ function f() public view returns (uint) { return x; }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "C");
+ ABI_CHECK(callContractFunction("test()"), encodeArgs(7));
+}
+
+BOOST_AUTO_TEST_CASE(code_access_content)
+{
+ char const* sourceCode = R"(
+ contract C {
+ function testRuntime() public returns (bool) {
+ D d = new D();
+ bytes32 runtimeHash = keccak256(type(D).runtimeCode);
+ bytes32 otherHash;
+ uint size;
+ assembly {
+ size := extcodesize(d)
+ extcodecopy(d, mload(0x40), 0, size)
+ otherHash := keccak256(mload(0x40), size)
+ }
+ require(size == type(D).runtimeCode.length);
+ require(runtimeHash == otherHash);
+ return true;
+ }
+ function testCreation() public returns (bool) {
+ D d = new D();
+ bytes32 creationHash = keccak256(type(D).creationCode);
+ require(creationHash == d.x());
+ return true;
+ }
+ }
+ contract D {
+ bytes32 public x;
+ constructor() public {
+ bytes32 codeHash;
+ assembly {
+ let size := codesize()
+ codecopy(mload(0x40), 0, size)
+ codeHash := keccak256(mload(0x40), size)
+ }
+ x = codeHash;
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "C");
+ ABI_CHECK(callContractFunction("testRuntime()"), encodeArgs(true));
+ ABI_CHECK(callContractFunction("testCreation()"), encodeArgs(true));
+}
+
BOOST_AUTO_TEST_SUITE_END()
}
diff --git a/test/libsolidity/SolidityParser.cpp b/test/libsolidity/SolidityParser.cpp
index a33c6134..413a8c9c 100644
--- a/test/libsolidity/SolidityParser.cpp
+++ b/test/libsolidity/SolidityParser.cpp
@@ -539,7 +539,6 @@ BOOST_AUTO_TEST_CASE(keyword_is_reserved)
"supports",
"switch",
"try",
- "type",
"typedef",
"typeof",
"unchecked"
diff --git a/test/libsolidity/syntaxTests/metaTypes/codeAccess.sol b/test/libsolidity/syntaxTests/metaTypes/codeAccess.sol
new file mode 100644
index 00000000..e90443e1
--- /dev/null
+++ b/test/libsolidity/syntaxTests/metaTypes/codeAccess.sol
@@ -0,0 +1,12 @@
+contract Test {
+ function creationOther() public pure returns (bytes memory) {
+ return type(Other).creationCode;
+ }
+ function runtimeOther() public pure returns (bytes memory) {
+ return type(Other).runtimeCode;
+ }
+}
+contract Other {
+ function f(uint) public pure returns (uint) {}
+}
+// ----
diff --git a/test/libsolidity/syntaxTests/metaTypes/codeAccessAbstractCreation.sol b/test/libsolidity/syntaxTests/metaTypes/codeAccessAbstractCreation.sol
new file mode 100644
index 00000000..bae137c5
--- /dev/null
+++ b/test/libsolidity/syntaxTests/metaTypes/codeAccessAbstractCreation.sol
@@ -0,0 +1,10 @@
+contract Test {
+ function creationOther() public pure returns (bytes memory) {
+ return type(Other).creationCode;
+ }
+}
+contract Other {
+ function f(uint) public returns (uint);
+}
+// ----
+// TypeError: (97-121): Member "creationCode" not found or not visible after argument-dependent lookup in type(contract Other).
diff --git a/test/libsolidity/syntaxTests/metaTypes/codeAccessAbstractRuntime.sol b/test/libsolidity/syntaxTests/metaTypes/codeAccessAbstractRuntime.sol
new file mode 100644
index 00000000..186d2714
--- /dev/null
+++ b/test/libsolidity/syntaxTests/metaTypes/codeAccessAbstractRuntime.sol
@@ -0,0 +1,10 @@
+contract Test {
+ function runtime() public pure returns (bytes memory) {
+ return type(Other).runtimeCode;
+ }
+}
+contract Other {
+ function f(uint) public returns (uint);
+}
+// ----
+// TypeError: (91-114): Member "runtimeCode" not found or not visible after argument-dependent lookup in type(contract Other).
diff --git a/test/libsolidity/syntaxTests/metaTypes/codeAccessBase.sol b/test/libsolidity/syntaxTests/metaTypes/codeAccessBase.sol
new file mode 100644
index 00000000..33dbfd7c
--- /dev/null
+++ b/test/libsolidity/syntaxTests/metaTypes/codeAccessBase.sol
@@ -0,0 +1,22 @@
+contract Base {
+ function f() public pure returns (uint) {}
+}
+contract Test is Base {
+ function creation() public pure returns (bytes memory) {
+ return type(Test).creationCode;
+ }
+ function runtime() public pure returns (bytes memory) {
+ return type(Test).runtimeCode;
+ }
+ function creationBase() public pure returns (bytes memory) {
+ return type(Base).creationCode;
+ }
+ function runtimeBase() public pure returns (bytes memory) {
+ return type(Base).runtimeCode;
+ }
+}
+// ----
+// TypeError: (165-188): Circular reference for contract code access.
+// TypeError: (271-293): Circular reference for contract code access.
+// TypeError: (381-404): Circular reference for contract code access.
+// TypeError: (491-513): Circular reference for contract code access.
diff --git a/test/libsolidity/syntaxTests/metaTypes/codeAccessCyclic.sol b/test/libsolidity/syntaxTests/metaTypes/codeAccessCyclic.sol
new file mode 100644
index 00000000..d5723df6
--- /dev/null
+++ b/test/libsolidity/syntaxTests/metaTypes/codeAccessCyclic.sol
@@ -0,0 +1,12 @@
+contract A {
+ function f() public pure {
+ type(B).runtimeCode;
+ }
+}
+contract B {
+ function f() public pure {
+ type(A).runtimeCode;
+ }
+}
+// ----
+// TypeError: (133-152): Circular reference for contract code access.
diff --git a/test/libsolidity/syntaxTests/metaTypes/codeAccessIsConstant.sol b/test/libsolidity/syntaxTests/metaTypes/codeAccessIsConstant.sol
new file mode 100644
index 00000000..cda5d5c3
--- /dev/null
+++ b/test/libsolidity/syntaxTests/metaTypes/codeAccessIsConstant.sol
@@ -0,0 +1,7 @@
+contract Test {
+ bytes constant c = type(B).creationCode;
+ bytes constant r = type(B).runtimeCode;
+
+}
+contract B { function f() public pure {} }
+// ----
diff --git a/test/libsolidity/syntaxTests/metaTypes/codeAccessLibrary.sol b/test/libsolidity/syntaxTests/metaTypes/codeAccessLibrary.sol
new file mode 100644
index 00000000..f746dc35
--- /dev/null
+++ b/test/libsolidity/syntaxTests/metaTypes/codeAccessLibrary.sol
@@ -0,0 +1,12 @@
+contract Test {
+ function creationOther() public pure returns (bytes memory) {
+ return type(Library).creationCode;
+ }
+ function runtime() public pure returns (bytes memory) {
+ return type(Library).runtimeCode;
+ }
+}
+contract Library {
+ function f(uint) public pure returns (uint) {}
+}
+// ----
diff --git a/test/libsolidity/syntaxTests/metaTypes/codeIsNoLValue.sol b/test/libsolidity/syntaxTests/metaTypes/codeIsNoLValue.sol
new file mode 100644
index 00000000..022e4d6f
--- /dev/null
+++ b/test/libsolidity/syntaxTests/metaTypes/codeIsNoLValue.sol
@@ -0,0 +1,10 @@
+contract Test {
+ function f() public pure {
+ type(C).creationCode = new bytes(6);
+ type(C).runtimeCode = new bytes(6);
+ }
+}
+contract C {}
+// ----
+// TypeError: (55-75): Expression has to be an lvalue.
+// TypeError: (100-119): Expression has to be an lvalue.
diff --git a/test/libsolidity/syntaxTests/metaTypes/noArgForType.sol b/test/libsolidity/syntaxTests/metaTypes/noArgForType.sol
new file mode 100644
index 00000000..542aaf53
--- /dev/null
+++ b/test/libsolidity/syntaxTests/metaTypes/noArgForType.sol
@@ -0,0 +1,7 @@
+contract Test {
+ function creation() public pure returns (bytes memory) {
+ type();
+ }
+}
+// ----
+// TypeError: (85-91): This function takes one argument, but 0 were provided.
diff --git a/test/libsolidity/syntaxTests/metaTypes/runtimeCodeWarningAssembly.sol b/test/libsolidity/syntaxTests/metaTypes/runtimeCodeWarningAssembly.sol
new file mode 100644
index 00000000..ec8d9784
--- /dev/null
+++ b/test/libsolidity/syntaxTests/metaTypes/runtimeCodeWarningAssembly.sol
@@ -0,0 +1,17 @@
+contract Test {
+ function f() public pure returns (uint) {
+ return type(C).runtimeCode.length +
+ type(D).runtimeCode.length +
+ type(C).creationCode.length +
+ type(D).creationCode.length;
+ }
+}
+contract C {
+ constructor() public { assembly {} }
+}
+contract D is C {
+ constructor() public {}
+}
+// ----
+// Warning: (77-96): The constructor of the contract (or its base) uses inline assembly. Because of that, it might be that the deployed bytecode is different from type(...).runtimeCode.
+// Warning: (118-137): The constructor of the contract (or its base) uses inline assembly. Because of that, it might be that the deployed bytecode is different from type(...).runtimeCode.
diff --git a/test/libsolidity/syntaxTests/metaTypes/tooManyArgsForType.sol b/test/libsolidity/syntaxTests/metaTypes/tooManyArgsForType.sol
new file mode 100644
index 00000000..61c2b779
--- /dev/null
+++ b/test/libsolidity/syntaxTests/metaTypes/tooManyArgsForType.sol
@@ -0,0 +1,7 @@
+contract Test {
+ function creation() public pure returns (bytes memory) {
+ type(1, 2);
+ }
+}
+// ----
+// TypeError: (85-95): This function takes one argument, but 2 were provided.
diff --git a/test/libsolidity/syntaxTests/metaTypes/typeNotRegularIdentifierContractName.sol b/test/libsolidity/syntaxTests/metaTypes/typeNotRegularIdentifierContractName.sol
new file mode 100644
index 00000000..144ca1c3
--- /dev/null
+++ b/test/libsolidity/syntaxTests/metaTypes/typeNotRegularIdentifierContractName.sol
@@ -0,0 +1,3 @@
+contract type { }
+// ----
+// ParserError: (9-13): Expected identifier but got 'type'
diff --git a/test/libsolidity/syntaxTests/metaTypes/typeNotRegularIdentifierFunction.sol b/test/libsolidity/syntaxTests/metaTypes/typeNotRegularIdentifierFunction.sol
new file mode 100644
index 00000000..b7881f15
--- /dev/null
+++ b/test/libsolidity/syntaxTests/metaTypes/typeNotRegularIdentifierFunction.sol
@@ -0,0 +1,6 @@
+contract Test {
+ function type() public pure {
+ }
+}
+// ----
+// ParserError: (29-33): Expected identifier but got 'type'
diff --git a/test/libsolidity/syntaxTests/metaTypes/typeNotRegularIdentifierParameter.sol b/test/libsolidity/syntaxTests/metaTypes/typeNotRegularIdentifierParameter.sol
new file mode 100644
index 00000000..001ba840
--- /dev/null
+++ b/test/libsolidity/syntaxTests/metaTypes/typeNotRegularIdentifierParameter.sol
@@ -0,0 +1,6 @@
+contract Test {
+ function f(uint type) public pure {
+ }
+}
+// ----
+// ParserError: (36-40): Expected ',' but got 'type'
diff --git a/test/libsolidity/syntaxTests/metaTypes/typeNotRegularIdentifierStateVariable.sol b/test/libsolidity/syntaxTests/metaTypes/typeNotRegularIdentifierStateVariable.sol
new file mode 100644
index 00000000..fa827a33
--- /dev/null
+++ b/test/libsolidity/syntaxTests/metaTypes/typeNotRegularIdentifierStateVariable.sol
@@ -0,0 +1,5 @@
+contract Test {
+ uint type;
+}
+// ----
+// ParserError: (25-29): Expected identifier but got 'type'
diff --git a/test/libsolidity/syntaxTests/metaTypes/typeNotRegularIdentifierVariable.sol b/test/libsolidity/syntaxTests/metaTypes/typeNotRegularIdentifierVariable.sol
new file mode 100644
index 00000000..fa57698d
--- /dev/null
+++ b/test/libsolidity/syntaxTests/metaTypes/typeNotRegularIdentifierVariable.sol
@@ -0,0 +1,7 @@
+contract Test {
+ function f() public pure {
+ uint type;
+ }
+}
+// ----
+// ParserError: (60-64): Expected ';' but got 'type'
diff --git a/test/libsolidity/syntaxTests/metaTypes/typeOfContract.sol b/test/libsolidity/syntaxTests/metaTypes/typeOfContract.sol
new file mode 100644
index 00000000..036c36e8
--- /dev/null
+++ b/test/libsolidity/syntaxTests/metaTypes/typeOfContract.sol
@@ -0,0 +1,6 @@
+contract Test {
+ function f() public pure returns (bytes memory) {
+ type(Test);
+ }
+}
+// ----
diff --git a/test/libsolidity/syntaxTests/metaTypes/typeRecursive.sol b/test/libsolidity/syntaxTests/metaTypes/typeRecursive.sol
new file mode 100644
index 00000000..0ce06786
--- /dev/null
+++ b/test/libsolidity/syntaxTests/metaTypes/typeRecursive.sol
@@ -0,0 +1,8 @@
+contract Test {
+ function f() public pure {
+ type(type(type(Test)));
+ }
+}
+// ----
+// TypeError: (65-75): Invalid type for argument in function call. Contract type required, but type(contract Test) provided.
+// TypeError: (60-76): Invalid type for argument in function call. Contract type required, but tuple() provided.
diff --git a/test/libsolidity/syntaxTests/metaTypes/unsupportedArgForType.sol b/test/libsolidity/syntaxTests/metaTypes/unsupportedArgForType.sol
new file mode 100644
index 00000000..5c27d42f
--- /dev/null
+++ b/test/libsolidity/syntaxTests/metaTypes/unsupportedArgForType.sol
@@ -0,0 +1,9 @@
+contract Test {
+ struct S { uint x; }
+ function f() public pure {
+ // Unsupported for now, but might be supported in the future
+ type(S);
+ }
+}
+// ----
+// TypeError: (154-155): Invalid type for argument in function call. Contract type required, but type(struct Test.S) provided.