Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

Commit

Permalink
Generate source-line coverage in task
Browse files Browse the repository at this point in the history
  • Loading branch information
ranweiler committed Dec 17, 2021
1 parent 94bfd89 commit bb45b0c
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/agent/onefuzz-agent/src/tasks/coverage/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use async_trait::async_trait;
use coverage::block::CommandBlockCov;
use coverage::cache::ModuleCache;
use coverage::code::{CmdFilter, CmdFilterDef};
use coverage::debuginfo::DebugInfo;
use onefuzz::expand::{Expand, PlaceHolder};
use onefuzz::syncdir::SyncedDir;
use onefuzz_telemetry::{warn, Event::coverage_data, EventData};
Expand All @@ -28,6 +29,7 @@ use crate::tasks::heartbeat::{HeartbeatSender, TaskHeartbeatClient};

const MAX_COVERAGE_RECORDING_ATTEMPTS: usize = 2;
const COVERAGE_FILE: &str = "coverage.json";
const SOURCE_COVERAGE_FILE: &str = "source-coverage.json";
const MODULE_CACHE_FILE: &str = "module-cache.json";

const DEFAULT_TARGET_TIMEOUT: Duration = Duration::from_secs(5);
Expand Down Expand Up @@ -168,6 +170,7 @@ struct TaskContext<'a> {
cache: Arc<Mutex<ModuleCache>>,
config: &'a Config,
coverage: CommandBlockCov,
debuginfo: Mutex<DebugInfo>,
filter: CmdFilter,
heartbeat: Option<TaskHeartbeatClient>,
}
Expand All @@ -181,11 +184,13 @@ impl<'a> TaskContext<'a> {
heartbeat: Option<TaskHeartbeatClient>,
) -> Self {
let cache = Arc::new(Mutex::new(cache));
let debuginfo = Mutex::new(DebugInfo::default());

Self {
cache,
config,
coverage,
debuginfo,
filter,
heartbeat,
}
Expand Down Expand Up @@ -339,11 +344,24 @@ impl<'a> TaskContext<'a> {

pub async fn save_and_sync_coverage(&self) -> Result<()> {
let path = self.config.coverage.local_path.join(COVERAGE_FILE);
let text = serde_json::to_string(&self.coverage).context("serializing coverage to JSON")?;

let text = serde_json::to_string(&self.coverage).context("serializing block coverage")?;
fs::write(&path, &text)
.await
.with_context(|| format!("writing coverage to {}", path.display()))?;

let path = self.config.coverage.local_path.join(SOURCE_COVERAGE_FILE);
let src_coverage = {
let mut debuginfo = self
.debuginfo
.lock()
.map_err(|e| anyhow::format_err!("{}", e))?;
self.coverage.source_coverage(&mut *debuginfo)?
};
let text = serde_json::to_string(&src_coverage).context("serializing source coverage")?;
fs::write(&path, &text)
.await
.with_context(|| format!("writing source coverage to {}", path.display()))?;

self.config.coverage.sync_push().await?;

Ok(())
Expand Down

0 comments on commit bb45b0c

Please sign in to comment.