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(traces): identify artifacts using both deployed and creation code #9050

Merged
merged 8 commits into from
Oct 14, 2024

Conversation

grandizzy
Copy link
Collaborator

@grandizzy grandizzy commented Oct 7, 2024

Motivation

Closes #8995
see #8998 (comment)

Solution

@grandizzy grandizzy marked this pull request as ready for review October 7, 2024 08:54
@zerosnacks
Copy link
Member

zerosnacks commented Oct 7, 2024

Could you expand on how the bug raised in #8995 is connected to the suggested change? As in, how does it solve it?

@grandizzy
Copy link
Collaborator Author

Could you expand on how the bug raised in #8995 is connected to the suggested change? As in, how does it solve it?

ah, yep, here's the explanation #8998 (comment)

Copy link
Member

@zerosnacks zerosnacks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see, makes sense!

LGTM

Comment on lines 49 to 55
let mut score = bytecode_diff_score(deployed_bytecode, runtime_code);

// Compute and use creation code score.
if let Some(bytecode) = contract.bytecode() {
score += bytecode_diff_score(bytecode, creation_code);
score /= 2.0;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we actually need to check both here? are there cases when comparing just creation code is not enough?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is done quite a lot, so don't think we should make it twice slower without a strong motivation

Copy link
Collaborator Author

@grandizzy grandizzy Oct 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there seems to be regression introduced if not checking both, for example for

abstract contract ParentProxy {
    Counter impl;
    bytes data;

    constructor(Counter _implementation, bytes memory _data) {
        impl = _implementation;
        data = _data;
    }
}

and inherited

contract Proxy is ParentProxy {
    constructor(Counter _implementation, bytes memory _data)
        ParentProxy(_implementation, _data)
    {}
}

following test

    function test_proxy_trace() public {
        Counter counter = new Counter();
        Proxy proxy = new Proxy(counter, "");
    }

results in Unknown id (for both with and without --no-metadata runs)

Traces:
  [130526] Issue8995Test::test_proxy_trace()
    ├─ [38693] → new Counter@0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f
    │   └─ ← [Return] 193 bytes of code
    ├─ [27175] → new <unknown>@0x2e234DAe75C793f67A35089C9d99245E1C58470b
    │   └─ ← [Return] 9 bytes of code
    └─ ← [Stop]

hence opted to compute for both

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks like an edge case we've discussed earlier. constructor args are of length 192 while bytecode length is 1320. on >10% diff we consider bytecodes completely different.

what we could do is to only fallback to deployed bytecode comparison if we haven't found a match through comparing creation codes and/or target for exact match when comparing creation codes (i.e if we have an artifact with creation code that exactly matches the beginning of some creation bytecode in traces, then this is a 100% match)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@klkvr sorry for the delay, fell out of the radar. Can you please recheck and see if that's inline (if creation bytecode score is > threshold of 0.85 then the deployed bytecode is checked and confirm is fixing scenario above). thanks!

Copy link
Member

@klkvr klkvr Oct 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice! yep, this is waht we want. could we refactor this a bit and still have a single check closure which would just accept a is_creation: bool? looks like current closures are identical

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated in 781b892 I also added a test for scenario mentioned above, ptal (unrelated tests failing due to avax rpc url) Thanks!

@grandizzy grandizzy requested a review from klkvr October 14, 2024 09:38
Copy link
Member

@klkvr klkvr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

@klkvr klkvr merged commit fdd321b into foundry-rs:master Oct 14, 2024
21 checks passed
rplusq pushed a commit to rplusq/foundry that referenced this pull request Nov 29, 2024
…foundry-rs#9050)

* Identify by creation code

* Compute score for both creation and runtime code

* Fallback to deployed bytecode only if min creation bytecode score is under threshold

* reuse check closure, add basic test
@grandizzy grandizzy added T-bug Type: bug C-forge Command: forge labels Dec 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-forge Command: forge T-bug Type: bug
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

bug(forge script): Failed to decode constructor arguments: buffer overrun while deserializing
3 participants