Prevent libraries from being called.

This commit is contained in:
chriseth
2018-01-19 16:27:44 +01:00
parent 33723c457a
commit 6807010dc7
12 changed files with 143 additions and 7 deletions
+33
View File
@@ -3543,6 +3543,39 @@ BOOST_AUTO_TEST_CASE(library_call_in_homestead)
ABI_CHECK(callContractFunction("sender()"), encodeArgs(u160(m_sender)));
}
BOOST_AUTO_TEST_CASE(library_call_protection)
{
// This tests code that reverts a call if it is a direct call to a library
// as opposed to a delegatecall.
char const* sourceCode = R"(
library Lib {
struct S { uint x; }
// a direct call to this should revert
function np(S storage s) public returns (address) { s.x = 3; return msg.sender; }
// a direct call to this is fine
function v(S storage) public view returns (address) { return msg.sender; }
// a direct call to this is fine
function pu() public pure returns (uint) { return 2; }
}
contract Test {
Lib.S public s;
function np() public returns (address) { return Lib.np(s); }
function v() public view returns (address) { return Lib.v(s); }
function pu() public pure returns (uint) { return Lib.pu(); }
}
)";
compileAndRun(sourceCode, 0, "Lib");
ABI_CHECK(callContractFunction("np(Lib.S storage)"), encodeArgs());
ABI_CHECK(callContractFunction("v(Lib.S storage)"), encodeArgs(u160(m_sender)));
ABI_CHECK(callContractFunction("pu()"), encodeArgs(2));
compileAndRun(sourceCode, 0, "Test", bytes(), map<string, Address>{{"Lib", m_contractAddress}});
ABI_CHECK(callContractFunction("s()"), encodeArgs(0));
ABI_CHECK(callContractFunction("np()"), encodeArgs(u160(m_sender)));
ABI_CHECK(callContractFunction("s()"), encodeArgs(3));
ABI_CHECK(callContractFunction("v()"), encodeArgs(u160(m_sender)));
ABI_CHECK(callContractFunction("pu()"), encodeArgs(2));
}
BOOST_AUTO_TEST_CASE(store_bytes)
{
// this test just checks that the copy loop does not mess up the stack