Skip to content

Commit 03651b7

Browse files
Merge pull request #150 from microsoft/main
Fork Sync: Update from parent repository
2 parents 3c59403 + f7bd508 commit 03651b7

File tree

5 files changed

+112
-12
lines changed

5 files changed

+112
-12
lines changed

src/agent/coverage/src/binary.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@
44
use std::collections::{BTreeMap, BTreeSet};
55

66
use anyhow::{bail, Result};
7-
use debuggable_module::{block, path::FilePath, Module, Offset};
7+
use debuggable_module::Module;
8+
pub use debuggable_module::{block, path::FilePath, Offset};
89
use symbolic::debuginfo::Object;
910
use symbolic::symcache::{SymCache, SymCacheConverter};
1011

1112
use crate::allowlist::TargetAllowList;
1213

13-
#[derive(Clone, Debug, Default)]
14+
#[derive(Clone, Debug, Default, Eq, PartialEq)]
1415
pub struct BinaryCoverage {
1516
pub modules: BTreeMap<FilePath, ModuleBinaryCoverage>,
1617
}
1718

18-
#[derive(Clone, Debug, Default)]
19+
#[derive(Clone, Debug, Default, Eq, PartialEq)]
1920
pub struct ModuleBinaryCoverage {
2021
pub offsets: BTreeMap<Offset, Count>,
2122
}

src/agent/onefuzz-file-format/src/coverage/binary/v1.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,29 @@ use crate::hex::Hex;
1313
#[derive(Deserialize, Serialize)]
1414
pub struct BinaryCoverageJson {
1515
#[serde(flatten)]
16-
pub modules: BTreeMap<String, BTreeMap<Hex, u32>>,
16+
pub modules: BTreeMap<String, ModuleCoverageJson>,
17+
}
18+
19+
#[derive(Deserialize, Serialize)]
20+
pub struct ModuleCoverageJson {
21+
pub blocks: BTreeMap<Hex, u32>,
1722
}
1823

1924
impl From<BinaryCoverage> for BinaryCoverageJson {
2025
fn from(binary: BinaryCoverage) -> Self {
2126
let mut modules = BTreeMap::new();
2227

23-
for (module, offsets) in &binary.modules {
24-
let mut map: BTreeMap<Hex, u32> = BTreeMap::new();
28+
for (path, offsets) in &binary.modules {
29+
let mut blocks: BTreeMap<Hex, u32> = BTreeMap::new();
2530

2631
for (offset, count) in offsets.as_ref() {
27-
map.insert(Hex(offset.0), count.0);
32+
blocks.insert(Hex(offset.0), count.0);
2833
}
2934

30-
let path = module.as_str().to_owned();
31-
modules.insert(path, map);
35+
let path = path.as_str().to_owned();
36+
let module = ModuleCoverageJson { blocks };
37+
38+
modules.insert(path, module);
3239
}
3340

3441
Self { modules }
@@ -41,15 +48,15 @@ impl TryFrom<BinaryCoverageJson> for BinaryCoverage {
4148
fn try_from(json: BinaryCoverageJson) -> Result<Self> {
4249
let mut process = BinaryCoverage::default();
4350

44-
for (module, offsets) in json.modules {
51+
for (path, module) in json.modules {
4552
let mut coverage = ModuleBinaryCoverage::default();
4653

47-
for (hex, count) in offsets {
54+
for (hex, count) in module.blocks {
4855
let offset = Offset(hex.0);
4956
coverage.offsets.insert(offset, Count(count));
5057
}
5158

52-
let path = FilePath::new(module)?;
59+
let path = FilePath::new(path)?;
5360
process.modules.insert(path, coverage);
5461
}
5562

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
[
2+
{
3+
"module": "/setup/main.exe",
4+
"blocks": [
5+
{
6+
"offset": 1,
7+
"count": 0
8+
},
9+
{
10+
"offset": 300,
11+
"count": 1
12+
},
13+
{
14+
"offset": 5000,
15+
"count": 0
16+
}
17+
]
18+
},
19+
{
20+
"module": "/setup/lib/some.dll",
21+
"blocks": [
22+
{
23+
"offset": 123,
24+
"count": 0
25+
},
26+
{
27+
"offset": 456,
28+
"count": 10
29+
}
30+
]
31+
}
32+
]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"/setup/main.exe": {
3+
"blocks": {
4+
"1": 0,
5+
"12c": 1,
6+
"1388": 0
7+
}
8+
},
9+
"/setup/lib/some.dll": {
10+
"blocks": {
11+
"7b": 0,
12+
"1c8": 10
13+
}
14+
}
15+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
use pretty_assertions::assert_eq;
5+
6+
use anyhow::Result;
7+
use coverage::binary::{BinaryCoverage, Count, FilePath, ModuleBinaryCoverage, Offset};
8+
use onefuzz_file_format::coverage::binary::{v0, v1};
9+
10+
fn expected_binary_coverage() -> Result<BinaryCoverage> {
11+
let main_exe_path = FilePath::new("/setup/main.exe")?;
12+
let some_dll_path = FilePath::new("/setup/lib/some.dll")?;
13+
14+
let mut main_exe = ModuleBinaryCoverage::default();
15+
main_exe.offsets.insert(Offset(1), Count(0));
16+
main_exe.offsets.insert(Offset(300), Count(1));
17+
main_exe.offsets.insert(Offset(5000), Count(0));
18+
19+
let mut some_dll = ModuleBinaryCoverage::default();
20+
some_dll.offsets.insert(Offset(123), Count(0));
21+
some_dll.offsets.insert(Offset(456), Count(10));
22+
23+
let mut binary = BinaryCoverage::default();
24+
binary.modules.insert(some_dll_path, some_dll);
25+
binary.modules.insert(main_exe_path, main_exe);
26+
27+
Ok(binary)
28+
}
29+
30+
#[test]
31+
fn test_binary_coverage_formats() -> Result<()> {
32+
let expected = expected_binary_coverage()?;
33+
34+
let v0_text = include_str!("files/binary-coverage.v0.json");
35+
let v0_json: v0::BinaryCoverageJson = serde_json::from_str(v0_text)?;
36+
let from_v0 = BinaryCoverage::try_from(v0_json)?;
37+
assert_eq!(from_v0, expected);
38+
39+
let v1_text = include_str!("files/binary-coverage.v1.json");
40+
let v1_json: v1::BinaryCoverageJson = serde_json::from_str(v1_text)?;
41+
let from_v1 = BinaryCoverage::try_from(v1_json)?;
42+
assert_eq!(from_v1, expected);
43+
44+
Ok(())
45+
}

0 commit comments

Comments
 (0)