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

Support for forge coverage Command #673

Open
Karrq opened this issue Oct 29, 2024 · 0 comments
Open

Support for forge coverage Command #673

Karrq opened this issue Oct 29, 2024 · 0 comments
Assignees
Labels
challenging 🏴‍☠️ Indicates a difficult item p1 🟠 Indicates high priority item upstream-parity 🟰 Needed for upstream feature parity

Comments

@Karrq
Copy link
Contributor

Karrq commented Oct 29, 2024

Currently Blocked by: matter-labs/era-compiler-solidity#160

How it works

2 steps, prepare and collect

prepare:

  1. map source ids to paths, skip libraries if opt is set
  2. Get artifact data/ id + bytecodes
  3. Analyze sources and asts using the SourceAnalyzer, this walks the ast and creates CoverageItems which are elements that need to be analyzed in coverage paired with a SourceLocation, which indicates where in the source code they are.
    4 Look at source maps and create anchors which map bytecode instructions to CoverageItems

collect:

  1. Create a contract runner with some options and coverage set to "true"
  2. Run tests
  3. Collect hit data (in TestResult.coverage, collected by the CoverageCollector), with artifact id and wether it comes from creation code or runtime code
  4. Do the corresponding analysis depending on the options

How hit map is built:

There's a CoverageCollector inspector which collects hit maps
This is a mapping of bytecode hash -> HitMap

pub struct HitMap {
	pub bytecode: Bytes,
	pub hits: BTreeMap<usize, u64>, // usize is program counter, u64 is number of hits
}

On each step it will add a hit to the program counter of the instruction

What we would need:

  • Build a hitmap. This can be done with a DynTracer, looking at non system contracts. Pc is offset from bytecode in number of instructions (not bytes), each instruction is 16bits and it will have the u16 as the raw_opcode_unmasked. We could do the same that on evm, map pc for a given hash/bytecode with hits.
  • Once zksolc adds a source map, use it to create the anchors.

🔗 Useful Links

https://book.getfoundry.sh/reference/forge/forge-coverage?highlight=cforge coverage#examples

@Karrq Karrq added challenging 🏴‍☠️ Indicates a difficult item p0 ⚪ Indicates blocker priority item upstream-parity 🟰 Needed for upstream feature parity labels Oct 29, 2024
@Karrq Karrq added p1 🟠 Indicates high priority item and removed p0 ⚪ Indicates blocker priority item labels Nov 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
challenging 🏴‍☠️ Indicates a difficult item p1 🟠 Indicates high priority item upstream-parity 🟰 Needed for upstream feature parity
Projects
None yet
Development

No branches or pull requests

2 participants