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

Reimplement ternary instrumentation #219

Closed
area opened this issue Apr 16, 2018 · 2 comments
Closed

Reimplement ternary instrumentation #219

area opened this issue Apr 16, 2018 · 2 comments
Labels

Comments

@area
Copy link
Contributor

area commented Apr 16, 2018

So it seems our implementation for the instrumentation of ternary statements was unsupported all along, so that's not going to be supported again like I hoped it was going to...

I think, unfortunately, this leaves us with replacing ternary statements with 'full' if/then/else statements as the only option if we want this branch coverage back...

@cgewecke
Copy link
Member

This could be possible in the new design using a strategy similar to the one proposed for #175 here.

0.7.0 instruments as shown below. Line/branch hits are tracked by inspecting the vm stack at each opcode step and picking out hashes we've passed to an injected "coverage" function:

contract Test {
  function coverage_0xab123(bytes32 c__0xab123) public pure {}

  function a(uint x) public {

    coverage_0xab123(0xa9065...549e2); /* line */         
  
    if (x == 1) {
  
      coverage_0xab123(0x85e7e...afacf); /* branch */ 
      coverage_0xab123(0xfa262...4fb28); /* line */  
 
      x = 3;
  
    } else { 
      coverage_0xab123(0xb54c1...3ef6b); /* branch */ 
    }
}

If we modify this approach and define two coverage functions which return true/false bool respectively...

function covFn_True(bytes32 hash) public pure returns (bool) {
  return true; 
}

function covFn_False(bytes32 hash) public pure returns (bool) {}

We should be able to detect ternary branches by injecting like this:

a  ?  b : c;

// Transformed to:
( covFn_True(h) && a || covFn_False(h) ) ? b : c;

If a == true, the true branch gets marked (and executes).
if a == false, the false branch gets marked (but does not execute).

I think this is correct but the injection logic itself might be tricky to get right with complex conditions.

@cgewecke
Copy link
Member

cgewecke commented Feb 3, 2024

This was done in v0.8.x. Closing...

@cgewecke cgewecke closed this as completed Feb 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants