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

Fix for #244 #245

Merged
merged 3 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 37 additions & 31 deletions src/instrumenter/interpose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
PointerType,
specializeType,
StateVariableVisibility,
SuperType,
TypeName,
TypeNode,
UserDefinedType,
Expand Down Expand Up @@ -403,37 +404,42 @@ export function interposeCall(
const params: VariableDeclaration[] = [];
const returns: VariableDeclaration[] = [];

let receiver: Expression;
let receiver: Expression | undefined;
let callOriginalExp: Expression;

const baseT = ctx.typeEnv.inference.typeOf(callee.vExpression);

if (call.vFunctionCallType === ExternalReferenceType.UserDefined) {
assert(
baseT instanceof UserDefinedType && baseT.definition instanceof ContractDefinition,
"Expected base to be a reference to a contract, not {0}",
baseT
);

params.push(
factory.makeVariableDeclaration(
false,
false,
"receiver",
wrapper.id,
false,
DataLocation.Default,
StateVariableVisibility.Default,
Mutability.Mutable,
baseT.pp(),
undefined,
factory.makeUserDefinedTypeName(
"<missing>",
getFQName(baseT.definition, call),
baseT.definition.id
if (baseT instanceof UserDefinedType && baseT.definition instanceof ContractDefinition) {
params.push(
factory.makeVariableDeclaration(
false,
false,
"receiver",
wrapper.id,
false,
DataLocation.Default,
StateVariableVisibility.Default,
Mutability.Mutable,
baseT.pp(),
undefined,
factory.makeUserDefinedTypeName(
"<missing>",
getFQName(baseT.definition, call),
baseT.definition.id
)
)
)
);
);

receiver = factory.copy(callee.vExpression);
copySrc(callee.vExpression, receiver);
} else {
assert(
baseT instanceof SuperType,
"Expected base to be a reference to a contract or super, not {0}",
baseT
);
}

params.push(
...calleeT.parameters.map((paramT, idx) => {
Expand All @@ -459,12 +465,11 @@ export function interposeCall(
)
);

receiver = factory.copy(callee.vExpression);
copySrc(callee.vExpression, receiver);

callOriginalExp = factory.makeMemberAccess(
call.vExpression.typeString,
factory.makeIdentifierFor(params[0]),
baseT instanceof SuperType
? factory.makeIdentifier("<missing>", "super", -1)
: factory.makeIdentifierFor(params[0]),
callee.memberName,
callee.referencedDeclaration
);
Expand Down Expand Up @@ -554,7 +559,7 @@ export function interposeCall(
);
}

let nImplicitArgs = 1;
let nImplicitArgs = baseT instanceof SuperType ? 0 : 1;

/**
* If the original call had gas/value function call options, we need
Expand Down Expand Up @@ -644,7 +649,8 @@ export function interposeCall(

contract.appendChild(wrapper);
call.vExpression = newCallee;
call.vArguments.unshift(receiver);

if (receiver) call.vArguments.unshift(receiver);

// If the call is in a pure/view function change its mutability
const containingFun = getScopeFun(call);
Expand Down
6 changes: 3 additions & 3 deletions test/samples/hardhat_test.instrumented.sol
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ contract HardHatTest {
}
{
console.logString("HIT: 007987:0040:000 6: ");
if (!(__ScribbleUtilsLib__18.eq_encoded(abi.encode(str), "abc"))) {
if (!(__ScribbleUtilsLib__19.eq_encoded(abi.encode(str), "abc"))) {
console.logString("str");
console.logString(str);
console.logString("007987:0040:000 6: ");
Expand All @@ -227,7 +227,7 @@ contract HardHatTest {
}
{
console.logString("HIT: 008360:0040:000 7: ");
if (!(__ScribbleUtilsLib__18.eq_encoded(bts, hex"010203"))) {
if (!(__ScribbleUtilsLib__19.eq_encoded(bts, hex"010203"))) {
console.logString("bts");
console.logBytes(bts);
console.logString("008360:0040:000 7: ");
Expand Down Expand Up @@ -539,7 +539,7 @@ contract HardHatTest {
function _original_HardHatTest_main(string memory str, bytes memory bts) private {}
}

library __ScribbleUtilsLib__18 {
library __ScribbleUtilsLib__19 {
function isInContract() internal returns (bool res) {
assembly {
res := sload(0x5f0b92cf9616afdee4f4136f66393f1343b027f01be893fa569eb2e2b667a40c)
Expand Down
1 change: 1 addition & 0 deletions test/samples/hardhat_test.sol
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pragma solidity 0.8.21;
contract HardHatTest {
uint8[] b = [1, 2, 3];

Expand Down
8 changes: 4 additions & 4 deletions test/samples/immutables.instrumented.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// This file is auto-generated by Scribble and shouldn't be edited directly.
/// Use --disarm prior to make any changes.
pragma solidity 0.8.21;
pragma solidity 0.8.20;

/// #if_succeeds x == 5;
contract Example01 {
Expand All @@ -10,7 +10,7 @@ contract Example01 {
x = 5;
unchecked {
if (!(x == 5)) {
emit __ScribbleUtilsLib__55.AssertionFailed("000324:0066:000 0: ");
emit __ScribbleUtilsLib__56.AssertionFailed("000324:0066:000 0: ");
assert(false);
}
}
Expand All @@ -25,7 +25,7 @@ contract Example02 {
x = 5;
unchecked {
if (!(x == 5)) {
emit __ScribbleUtilsLib__55.AssertionFailed("000637:0066:000 1: ");
emit __ScribbleUtilsLib__56.AssertionFailed("000637:0066:000 1: ");
assert(false);
}
}
Expand Down Expand Up @@ -58,7 +58,7 @@ contract Example04 {
}
}

library __ScribbleUtilsLib__55 {
library __ScribbleUtilsLib__56 {
event AssertionFailed(string message);

event AssertionFailedData(int eventId, bytes encodingData);
Expand Down
1 change: 1 addition & 0 deletions test/samples/immutables.sol
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pragma solidity 0.8.20;
/// #if_succeeds x == 5;
contract Example01 {
uint private immutable x;
Expand Down
2 changes: 1 addition & 1 deletion test/samples/immutables.sol.json

Large diffs are not rendered by default.

Loading