• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

xd009642 / llvm-profparser / #206

07 Aug 2025 02:14PM UTC coverage: 69.151% (-0.3%) from 69.494%
#206

push

web-flow
support mc/dc mapping regions in profdata files (#52)

2 of 27 new or added lines in 2 files covered. (7.41%)

13 existing lines in 6 files now uncovered.

1206 of 1744 relevant lines covered (69.15%)

3.4 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

71.13
/src/coverage/coverage_mapping.rs
1
use crate::coverage::reporting::*;
2
use crate::coverage::*;
3
use crate::instrumentation_profile::types::*;
4
use crate::util::*;
5
use anyhow::{bail, Result};
6
use nom::error::Error as NomError;
7
use object::{Endian, Endianness, Object, ObjectSection, ReadCache, ReadRef, Section};
8
use std::convert::TryInto;
9
use std::error::Error;
10
use std::fmt;
11
use std::fs;
12
use std::io::BufReader;
13
use std::path::{Path, PathBuf};
14
use tracing::{debug, error, trace, warn};
15

16
/// Stores the instrumentation profile and information from the coverage mapping sections in the
17
/// object files in order to construct a coverage report. Inspired, from the LLVM implementation
18
/// with some differences/simplifications due to the fact this only hands instrumentation profiles
19
/// for coverage.
20
///
21
/// So what the LLVM one has that this one doesn't yet:
22
///
23
/// 1. DenseMap<size_t, DenseSet<size_t>> RecordProvenance
24
/// 2. std::vector<FunctionRecord> functions (this is probably taken straight from
25
/// InstrumentationProfile
26
/// 3. DenseMap<size_t, SmallVector<unsigned, 0>> FilenameHash2RecordIndices
27
/// 4. Vec<Pair<String, u64>> FuncHashMismatches
28
#[derive(Debug)]
29
pub struct CoverageMapping<'a> {
30
    profile: &'a InstrumentationProfile,
31
    pub mapping_info: Vec<CoverageMappingInfo>,
32
}
33

34
#[derive(Copy, Clone, Debug)]
35
pub enum LlvmSection {
36
    CoverageMap,
37
    ProfileNames,
38
    ProfileCounts,
39
    ProfileData,
40
    CoverageFunctions,
41
}
42

43
#[derive(Copy, Clone, Debug)]
44
pub enum SectionReadError {
45
    EmptySection(LlvmSection),
46
    MissingSection(LlvmSection),
47
    InvalidPathList,
48
}
49

50
impl fmt::Display for SectionReadError {
51
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
×
52
        match self {
×
53
            Self::EmptySection(s) => write!(f, "empty section: {:?}", s),
×
54
            Self::MissingSection(s) => write!(f, "missing section: {:?}", s),
×
55
            Self::InvalidPathList => write!(f, "unable to read path list"),
×
56
        }
57
    }
58
}
59

60
impl Error for SectionReadError {}
61

62
pub fn read_object_file(object: &Path, version: u64) -> Result<CoverageMappingInfo> {
1✔
63
    // I believe vnode sections added by llvm are unnecessary
64

65
    let binary_data = ReadCache::new(BufReader::new(fs::File::open(object)?));
1✔
66
    let object_file = object::File::parse(&binary_data)?;
2✔
67

68
    let prof_counts = object_file
69
        .section_by_name("__llvm_prf_cnts")
70
        .or(object_file.section_by_name(".lprfc"))
1✔
71
        .and_then(|x| parse_profile_counters(object_file.endianness(), &x).ok());
3✔
72

73
    debug!("Parsed prf_cnts: {:?}", prof_counts);
3✔
74

75
    let prof_data = object_file
76
        .section_by_name("__llvm_prf_data")
77
        .or(object_file.section_by_name(".lprfd"))
1✔
78
        .and_then(|x| parse_profile_data(object_file.endianness(), &x).ok());
3✔
79

80
    debug!("Parsed prf_data section: {:?}", prof_data);
3✔
81

82
    let cov_fun = object_file
1✔
83
        .section_by_name("__llvm_covfun")
84
        .or(object_file.section_by_name(".lcovfun"))
1✔
85
        .map(|x| parse_coverage_functions(object_file.endianness(), &x))
3✔
86
        .ok_or(SectionReadError::MissingSection(
1✔
87
            LlvmSection::CoverageFunctions,
1✔
88
        ))??;
89

90
    debug!("Parsed covfun section: {:?}", cov_fun);
3✔
91

92
    let cov_map = object_file
1✔
93
        .section_by_name("__llvm_covmap")
94
        .or(object_file.section_by_name(".lcovmap"))
1✔
95
        .map(|x| parse_coverage_mapping(object_file.endianness(), &x, version))
3✔
96
        .ok_or(SectionReadError::MissingSection(LlvmSection::CoverageMap))??;
1✔
97

98
    debug!("Parsed covmap section: {:?}", cov_map);
3✔
99

100
    Ok(CoverageMappingInfo {
1✔
101
        cov_map,
1✔
102
        cov_fun,
1✔
103
        prof_counts,
1✔
104
        prof_data,
1✔
105
    })
106
}
107

108
impl<'a> CoverageMapping<'a> {
109
    pub fn new(
1✔
110
        object_files: &[PathBuf],
111
        profile: &'a InstrumentationProfile,
112
        allow_parsing_failures: bool,
113
    ) -> Result<Self> {
114
        let mut mapping_info = vec![];
1✔
115
        let version = match profile.version() {
2✔
116
            Some(v) => v,
1✔
117
            None => bail!("Invalid profile instrumentation, no version number provided"),
×
118
        };
119
        for file in object_files {
2✔
120
            match read_object_file(file.as_path(), version) {
2✔
121
                Ok(info) => mapping_info.push(info),
1✔
122
                Err(e) => {
×
123
                    error!("{} couldn't be interpretted: {}", file.display(), e);
×
124
                    if !allow_parsing_failures {
×
125
                        return Err(e);
×
126
                    }
127
                }
128
            };
129
        }
130
        Ok(Self {
1✔
131
            profile,
×
132
            mapping_info,
1✔
133
        })
134
    }
135

136
    /// All counters of type `CounterKind::ProfileInstrumentation` can be used in function regions
137
    /// other than their own (particulary for functions which are only called from one location).
138
    /// This gathers them all to use as a base list of counters.
139
    pub(crate) fn get_simple_counters(&self, func: &FunctionRecordV3) -> FxHashMap<Counter, i64> {
1✔
140
        let mut result = FxHashMap::default();
1✔
141
        result.insert(Counter::default(), 0);
2✔
142
        let record = self.profile.find_record_by_hash(func.header.name_hash);
1✔
143
        if let Some(func_record) = record.as_ref() {
1✔
144
            result.reserve(func_record.record.counts.len());
2✔
145
            for (id, count) in func_record.record.counts.iter().enumerate() {
1✔
146
                result.insert(Counter::instrumentation(id as u64), *count as i64);
1✔
147
            }
148
        }
149
        result
1✔
150
    }
151

152
    pub fn generate_subreport<P>(&self, mut predicate: P) -> CoverageReport
1✔
153
    where
154
        P: FnMut(&[PathBuf]) -> bool,
155
    {
156
        let mut report = CoverageReport::default();
1✔
157
        //let base_region_ids = info.get_simple_counters(self.profile);
158
        for info in &self.mapping_info {
2✔
159
            for func in &info.cov_fun {
2✔
160
                let base_region_ids = self.get_simple_counters(func);
1✔
161
                let paths = info.get_files_from_id(func.header.filenames_ref);
1✔
162
                if paths.is_empty() || !predicate(&paths) {
3✔
163
                    continue;
×
164
                }
165

166
                let mut region_ids = base_region_ids.clone();
1✔
167

168
                for region in func.regions.iter().filter(|x| !x.count.is_expression()) {
4✔
169
                    let count = region_ids.get(&region.count).copied().unwrap_or_default();
2✔
170
                    let result = report
1✔
171
                        .files
×
172
                        .entry(paths[region.file_id].clone())
1✔
173
                        .or_default();
174
                    result.insert(region.loc.clone(), count as usize);
1✔
175
                }
176

177
                let mut pending_exprs = vec![];
1✔
178

179
                for (expr_index, expr) in func.expressions.iter().enumerate().rev() {
2✔
180
                    let lhs = region_ids.get(&expr.lhs);
2✔
181
                    let rhs = region_ids.get(&expr.rhs);
1✔
182
                    match (lhs, rhs) {
1✔
183
                        (Some(lhs), Some(rhs)) => {
1✔
184
                            let count: i64 = match expr.kind {
1✔
185
                                ExprKind::Subtract => {
×
186
                                    trace!("Subtracting counts: {} - {}", lhs, rhs);
3✔
187
                                    // TODO these saturating ops need investigation. Potentially
188
                                    // something is wrong.
189
                                    lhs.saturating_sub(*rhs)
2✔
190
                                }
191
                                ExprKind::Add => {
×
192
                                    trace!("Adding counts: {} + {}", lhs, rhs);
3✔
193
                                    lhs.saturating_add(*rhs)
2✔
194
                                }
195
                            };
196

197
                            let counter = Counter {
198
                                kind: CounterType::Expression(expr.kind),
1✔
199
                                id: expr_index as _,
1✔
200
                            };
201

202
                            region_ids.insert(counter, count);
2✔
203
                            if let Some(expr_region) = func.regions.iter().find(|x| {
2✔
204
                                x.count.is_expression() && x.count.id == expr_index as u64
1✔
205
                            }) {
206
                                let result = report
1✔
207
                                    .files
×
208
                                    .entry(paths[expr_region.file_id].clone())
1✔
209
                                    .or_default();
210
                                result.insert(expr_region.loc.clone(), count as _);
1✔
211
                            }
212
                        }
213
                        _ => {
×
214
                            let lhs_none = lhs.is_none();
2✔
215
                            let rhs_none = rhs.is_none();
1✔
216
                            // These counters have been optimised out, so just add then in as 0
217
                            if lhs_none && expr.lhs.is_instrumentation() {
2✔
218
                                region_ids.insert(expr.lhs, 0);
×
219
                            }
220
                            if rhs_none && expr.rhs.is_instrumentation() {
2✔
221
                                region_ids.insert(expr.rhs, 0);
×
222
                            }
223
                            pending_exprs.push(Some((expr_index, expr)));
2✔
224
                            continue;
×
225
                        }
226
                    }
227
                }
228
                let mut index = 0;
1✔
229
                let mut cleared_expressions = 0;
1✔
230
                let mut tries_left = pending_exprs.len() + 1;
1✔
231
                while pending_exprs.len() != cleared_expressions {
3✔
232
                    assert!(tries_left > 0);
1✔
233
                    if index >= pending_exprs.len() {
1✔
234
                        index = 0;
×
235
                        tries_left -= 1;
×
236
                    }
237
                    let (expr_index, expr) = match pending_exprs[index].as_ref() {
3✔
238
                        Some((idx, expr)) => (idx, expr),
1✔
239
                        None => {
×
240
                            index += 1;
×
241
                            continue;
×
242
                        }
243
                    };
244
                    let lhs = region_ids.get(&expr.lhs);
2✔
245
                    let rhs = region_ids.get(&expr.rhs);
1✔
246
                    match (lhs, rhs) {
1✔
247
                        (Some(lhs), Some(rhs)) => {
1✔
248
                            let count = match expr.kind {
1✔
249
                                ExprKind::Subtract => lhs - rhs,
×
250
                                ExprKind::Add => lhs + rhs,
2✔
251
                            };
252

253
                            let counter = Counter {
254
                                kind: CounterType::Expression(expr.kind),
1✔
255
                                id: *expr_index as _,
1✔
256
                            };
257

258
                            region_ids.insert(counter, count);
2✔
259
                            if let Some(expr_region) = func.regions.iter().find(|x| {
2✔
260
                                x.count.is_expression() && x.count.id == *expr_index as u64
1✔
261
                            }) {
262
                                let result = report
1✔
263
                                    .files
×
264
                                    .entry(paths[expr_region.file_id].clone())
2✔
265
                                    .or_default();
266
                                result.insert(expr_region.loc.clone(), count as _);
1✔
267
                            }
268
                            pending_exprs[index] = None;
2✔
269
                            cleared_expressions += 1;
1✔
270
                        }
271
                        _ => {
×
272
                            index += 1;
×
273
                            continue;
×
274
                        }
275
                    }
276
                }
277
            }
278
        }
279
        report
1✔
280
    }
281

282
    pub fn generate_report(&self) -> CoverageReport {
1✔
283
        self.generate_subreport(|_| true)
3✔
284
    }
285
}
286

287
fn parse_coverage_mapping<'data, R: ReadRef<'data>>(
1✔
288
    endian: Endianness,
289
    section: &Section<'data, '_, R>,
290
    version: u64,
291
) -> Result<FxHashMap<u64, Vec<PathBuf>>, SectionReadError> {
292
    if let Ok(mut data) = section.data() {
2✔
293
        let mut result = FxHashMap::default();
1✔
294
        while !data.is_empty() {
3✔
295
            let data_len = data.len();
1✔
296
            // Read the number of affixed function records (now just 0 as not in this header)
297
            debug_assert_eq!(endian.read_i32_bytes(data[0..4].try_into().unwrap()), 0);
2✔
298
            let filename_data_len = endian.read_i32_bytes(data[4..8].try_into().unwrap());
1✔
299
            // Read the length of the affixed string that contains encoded coverage mapping data (now 0
300
            // as not in this header)
301
            debug_assert_eq!(endian.read_i32_bytes(data[8..12].try_into().unwrap()), 0);
1✔
302
            let _format_version = endian.read_i32_bytes(data[12..16].try_into().unwrap());
1✔
303

304
            let hash = md5::compute(&data[16..(filename_data_len as usize + 16)]);
1✔
305
            let hash = endian.read_u64_bytes(hash.0[..8].try_into().unwrap());
1✔
306

307
            //let bytes = &data[16..(16 + filename_data_len as usize)];
308
            let bytes = &data[16..];
1✔
309
            let (bytes, file_strings) = parse_path_list(bytes, version)
2✔
310
                .map_err(|_: nom::Err<NomError<_>>| SectionReadError::InvalidPathList)?;
1✔
311
            result.insert(hash, file_strings);
2✔
312
            let read_len = data_len - bytes.len();
1✔
313
            let padding = if !bytes.is_empty() && (read_len & 0x07) != 0 {
3✔
314
                8 - (read_len & 0x07)
1✔
315
            } else {
316
                0
×
317
            };
318
            if padding > bytes.len() {
1✔
319
                break;
×
320
            }
321
            data = &bytes[padding..];
2✔
322
        }
323
        Ok(result)
1✔
324
    } else {
325
        Err(SectionReadError::EmptySection(LlvmSection::CoverageMap))
×
326
    }
327
}
328

329
// Strictly speaking, parsing __llvm_covfun ought to depend on the version number
330
// observed in the __llvm_covmap headers.
331
//
332
// But in practice there haven’t been any non-additive changes to the covfun format
333
// in a long time, which is why you can get away with parsing them independently,
334
// at least for now.
335
fn parse_coverage_functions<'data, R: ReadRef<'data>>(
1✔
336
    endian: Endianness,
337
    section: &Section<'data, '_, R>,
338
) -> Result<Vec<FunctionRecordV3>, SectionReadError> {
339
    debug!("Parsing coverage functions");
4✔
340
    if let Ok(original_data) = section.data() {
3✔
341
        let mut bytes = original_data;
1✔
342
        let mut res = vec![];
1✔
343
        let section_len = bytes.len();
1✔
344
        while !bytes.is_empty() {
2✔
345
            let name_hash = endian.read_u64_bytes(bytes[0..8].try_into().unwrap());
2✔
346
            let data_len = endian.read_u32_bytes(bytes[8..12].try_into().unwrap());
1✔
347
            let fn_hash = endian.read_u64_bytes(bytes[12..20].try_into().unwrap());
1✔
348
            let filenames_ref = endian.read_u64_bytes(bytes[20..28].try_into().unwrap());
1✔
349
            let header = FunctionRecordHeader {
350
                name_hash,
351
                data_len,
352
                fn_hash,
353
                filenames_ref,
354
            };
355
            let _start_len = bytes[28..].len();
1✔
356
            bytes = &bytes[28..];
1✔
357

358
            let (data, id_len) = parse_leb128::<NomError<_>>(bytes).unwrap();
1✔
359
            bytes = data;
1✔
360
            let mut filename_indices = vec![];
1✔
361
            for _ in 0..id_len {
3✔
362
                let (data, id) = parse_leb128::<NomError<_>>(bytes).unwrap(); // Issue
2✔
363
                filename_indices.push(id);
1✔
364
                bytes = data;
1✔
365
            }
366

367
            let (data, expr_len) = parse_leb128::<NomError<_>>(bytes).unwrap();
1✔
368
            let expr_len = expr_len as usize;
1✔
369
            bytes = data;
1✔
370
            let mut exprs = vec![Expression::default(); expr_len];
1✔
371
            for i in 0..expr_len {
3✔
372
                let (data, lhs) = parse_leb128::<NomError<_>>(bytes).unwrap();
2✔
373
                let (data, rhs) = parse_leb128::<NomError<_>>(data).unwrap();
1✔
374
                let lhs = parse_counter(lhs, &mut exprs);
1✔
375
                let rhs = parse_counter(rhs, &mut exprs);
1✔
376
                exprs[i].lhs = lhs;
1✔
377
                exprs[i].rhs = rhs;
1✔
378
                bytes = data;
1✔
379
            }
380

381
            let (data, regions) =
1✔
382
                parse_mapping_regions(bytes, &filename_indices, &mut exprs).unwrap();
×
383

384
            res.push(FunctionRecordV3 {
1✔
385
                header,
×
386
                regions,
1✔
387
                expressions: exprs,
1✔
388
            });
389

390
            // Todo set couners for expansion regions - counter of expansion region is the counter
391
            // of the first region from the expanded file. This requires multiple passes to
392
            // correctly propagate across all nested regions. N.B. I haven't seen any expansion
393
            // regions in use so may not be an issue!
394

395
            bytes = data;
1✔
396
            let function_len = section_len - bytes.len(); // this should match header
1✔
397

398
            let padding = if function_len < section_len && (function_len & 0x07) != 0 {
3✔
399
                8 - (function_len & 0x07)
2✔
400
            } else {
401
                0
1✔
402
            };
403

404
            if padding > bytes.len() {
1✔
405
                break;
×
406
            }
407
            // Now apply padding, and if hash is 0 move on as it's a dummy otherwise add to result
408
            // And decide what end type will be
409
            bytes = &bytes[padding..];
2✔
410
        }
411
        Ok(res)
1✔
412
    } else {
413
        error!("Can't read data for coverage function section");
×
414
        Err(SectionReadError::EmptySection(
×
415
            LlvmSection::CoverageFunctions,
×
416
        ))
417
    }
418
}
419

420
/// This code is ported from `RawCoverageMappingReader::readMappingRegionsSubArray`
421
fn parse_mapping_regions<'a>(
1✔
422
    mut bytes: &'a [u8],
423
    file_indices: &[u64],
424
    expressions: &mut Vec<Expression>,
425
) -> IResult<&'a [u8], Vec<CounterMappingRegion>> {
426
    let mut mapping = vec![];
1✔
427
    for i in file_indices {
2✔
428
        let (data, regions_len) = parse_leb128(bytes)?;
2✔
429
        bytes = data;
1✔
430
        let mut last_line = 0;
1✔
431
        for _ in 0..regions_len {
1✔
432
            let mut mcdc_params = None;
1✔
433

434
            let mut false_count = Counter::default();
1✔
435
            let mut kind = RegionKind::Code;
1✔
436
            let (data, raw_header) = parse_leb128(bytes)?;
1✔
437
            bytes = data;
1✔
438
            let mut expanded_file_id = 0;
1✔
439
            let mut counter = parse_counter(raw_header, expressions);
1✔
440
            if counter.is_zero() {
1✔
441
                if raw_header & Counter::ENCODING_EXPANSION_REGION_BIT > 0 {
×
442
                    kind = RegionKind::Expansion;
×
443
                    expanded_file_id = raw_header >> Counter::ENCODING_TAG_AND_EXP_REGION_BITS;
×
444
                    if expanded_file_id >= file_indices.len() as u64 {
×
NEW
445
                        panic!("expanded_file_id is invalid");
×
446
                    }
447
                } else {
448
                    let shifted_counter = raw_header >> Counter::ENCODING_TAG_AND_EXP_REGION_BITS;
×
449
                    match shifted_counter.try_into() {
×
450
                        Ok(RegionKind::Code) | Ok(RegionKind::Skipped) => {}
×
451
                        Ok(RegionKind::Branch) => {
×
452
                            kind = RegionKind::Branch;
×
453
                            let (data, c1) = parse_leb128(bytes)?;
×
454
                            let (data, c2) = parse_leb128(data)?;
×
455

456
                            counter = parse_counter(c1, expressions);
×
457
                            false_count = parse_counter(c2, expressions);
×
458
                            bytes = data;
×
459
                        }
NEW
460
                        Ok(RegionKind::MCDCBranch) => {
×
NEW
461
                            kind = RegionKind::MCDCBranch;
×
NEW
462
                            let (data, c1) = parse_leb128(bytes)?;
×
NEW
463
                            let (data, c2) = parse_leb128(data)?;
×
464

NEW
465
                            counter = parse_counter(c1, expressions);
×
NEW
466
                            false_count = parse_counter(c2, expressions);
×
467

NEW
468
                            let (data, id1) = parse_leb128(data)?;
×
NEW
469
                            let (data, tid1) = parse_leb128(data)?;
×
NEW
470
                            let (data, fid1) = parse_leb128(data)?;
×
NEW
471
                            bytes = data;
×
472

NEW
473
                            mcdc_params = Some(MCDCParams::Branch(BranchParameters {
×
NEW
474
                                id: id1.try_into().unwrap(),
×
NEW
475
                                false_cond: fid1.try_into().unwrap(),
×
NEW
476
                                true_cond: tid1.try_into().unwrap(),
×
477
                            }));
478
                        }
NEW
479
                        Ok(RegionKind::MCDCDecision) => {
×
NEW
480
                            kind = RegionKind::MCDCDecision;
×
NEW
481
                            let (data, bidx) = parse_leb128(data)?;
×
NEW
482
                            let (data, nc) = parse_leb128(data)?;
×
NEW
483
                            bytes = data;
×
484

NEW
485
                            mcdc_params = Some(MCDCParams::Decision(DecisionParameters {
×
NEW
486
                                bitmap_idx: bidx.try_into().unwrap(),
×
NEW
487
                                num_conditions: nc.try_into().unwrap(),
×
488
                            }));
489
                        }
UNCOV
490
                        e => panic!("Malformed: {:?}", e),
×
491
                    }
492
                }
493
            }
494

495
            let (data, delta_line) = parse_leb128(bytes)?;
2✔
496
            let (data, column_start) = parse_leb128(data)?;
1✔
497
            let (data, lines_len) = parse_leb128(data)?;
1✔
498
            let (data, column_end) = parse_leb128(data)?;
1✔
499
            bytes = data;
1✔
500

501
            let (column_start, column_end) = if column_start == 0 && column_end == 0 {
3✔
502
                (1usize, usize::MAX)
×
503
            } else {
504
                (column_start as usize, column_end as usize)
1✔
505
            };
506

507
            let line_start = last_line + delta_line as usize;
1✔
508
            let line_end = line_start + lines_len as usize;
2✔
509
            last_line = line_start;
1✔
510

511
            // Add region working-out-stuff
512
            mapping.push(CounterMappingRegion {
2✔
513
                kind,
1✔
514
                count: counter,
1✔
515
                false_count,
1✔
516
                file_id: *i as usize,
1✔
517
                expanded_file_id: expanded_file_id as _,
1✔
518
                loc: SourceLocation {
1✔
519
                    line_start,
×
520
                    line_end,
×
521
                    column_start,
×
522
                    column_end,
×
523
                },
524
                mcdc_params,
1✔
525
            });
526
        }
527
    }
528
    Ok((bytes, mapping))
1✔
529
}
530

531
fn parse_profile_data<'data, R: ReadRef<'data>>(
1✔
532
    endian: Endianness,
533
    section: &Section<'data, '_, R>,
534
) -> Result<Vec<ProfileData>, SectionReadError> {
535
    if let Ok(data) = section.data() {
2✔
536
        let mut bytes = data;
1✔
537
        let mut res = vec![];
1✔
538
        while !bytes.is_empty() {
2✔
539
            // bytes.len() >= 24 {
540
            let name_md5 = endian.read_u64_bytes(bytes[..8].try_into().unwrap());
2✔
541
            let structural_hash = endian.read_u64_bytes(bytes[8..16].try_into().unwrap());
1✔
542

543
            let _counter_ptr = endian.read_u64_bytes(bytes[16..24].try_into().unwrap());
1✔
544
            let counters_location = 24 + 16;
1✔
545
            if bytes.len() <= counters_location {
2✔
546
                bytes = &bytes[counters_location..];
×
547
                let counters_len = endian.read_u32_bytes(bytes[..4].try_into().unwrap());
×
548
                // TODO Might need to get the counter offset and get the list of counters from this?
549
                // And potentially check against the maximum number of counters just to make sure that
550
                // it's not being exceeded?
551
                //
552
                // Also counters_len >= 1 so this should be checked to make sure it's not malformed
553

554
                bytes = &bytes[8..];
×
555

556
                res.push(ProfileData {
×
557
                    name_md5,
×
558
                    structural_hash,
×
559
                    counters_len,
×
560
                });
561
            } else {
562
                bytes = &[];
1✔
563
            }
564
        }
565
        if !bytes.is_empty() {
2✔
566
            warn!("{} bytes left in profile data", bytes.len());
×
567
        }
568
        Ok(res)
1✔
569
    } else {
570
        Err(SectionReadError::EmptySection(LlvmSection::ProfileData))
×
571
    }
572
}
573

574
fn parse_profile_counters<'data, R: ReadRef<'data>>(
1✔
575
    endian: Endianness,
576
    section: &Section<'data, '_, R>,
577
) -> Result<Vec<u64>, SectionReadError> {
578
    if let Ok(data) = section.data() {
2✔
579
        let mut result = vec![];
1✔
580
        for i in (0..data.len()).step_by(8) {
2✔
581
            if data.len() < (i + 8) {
2✔
582
                break;
×
583
            }
584
            result.push(endian.read_u64_bytes(data[i..(i + 8)].try_into().unwrap()));
1✔
585
        }
586
        Ok(result)
1✔
587
    } else {
588
        Err(SectionReadError::EmptySection(LlvmSection::ProfileCounts))
×
589
    }
590
}
591

592
/// The equivalent llvm function is `RawCoverageMappingReader::decodeCounter`. This makes it
593
/// stateless as I don't want to be maintaining an expression vector and clearing it and
594
/// repopulating for every function record.
595
fn parse_counter(input: u64, exprs: &mut Vec<Expression>) -> Counter {
1✔
596
    let ty = (Counter::ENCODING_TAG_MASK & input) as u8;
2✔
597
    let id = input >> 2; // For zero we don't actually care about this but we'll still do it
2✔
598
    let kind = match ty {
2✔
599
        0 => CounterType::Zero,
1✔
600
        1 => CounterType::ProfileInstrumentation,
1✔
601
        2 | 3 => {
602
            let expr_kind = if ty == 2 {
2✔
603
                ExprKind::Subtract
1✔
604
            } else {
605
                ExprKind::Add
1✔
606
            };
607
            let id = id as usize;
1✔
608
            if exprs.len() <= id {
1✔
609
                debug!(
×
610
                    "Not enough expressions resizing {}->{}",
611
                    exprs.len(),
×
612
                    id + 1
×
613
                );
614
                exprs.resize(id + 1, Expression::default());
×
615
            }
616
            exprs[id].set_kind(expr_kind);
1✔
617
            CounterType::Expression(expr_kind)
1✔
618
        }
619
        _ => unreachable!(),
620
    };
621
    Counter { kind, id }
622
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc