rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
99.12k stars 12.8k forks source link

[BUG] `llvm-cov` warning `mismatched data` when double slash comment above `use` #130065

Open loynoir opened 2 months ago

loynoir commented 2 months ago

bug

llvm-cov warning: 1 functions have mismatched data caused by comment line

background

Found llvm-cov warning: N functions have mismatched data.

After debug, very suprisingly find out this may all caused by comment line.

actual

llvm-cov warning: 1 functions have mismatched data caused by comment line // foo

crate A

#![feature(str_from_raw_parts)]

// foo
use core::str::from_raw_parts;

/// # Safety
///
/// TODO
#[inline]
#[must_use]
pub const unsafe fn str_from_raw_parts<'a>(ptr: *const u8, len: usize) -> &'a str {
    from_raw_parts(ptr, len)
}

crate B

    use feature_str_from_raw_parts_util::str_from_raw_parts;

    #[test]
    fn should_ok() {
        let x = unsafe { str_from_raw_parts("foobar".as_ptr(), 3) };
        let _ = x;
$ yarn cleanup:everything && yarn test:coverage
...
+ /path/to/llvm-cov report ...
warning: 1 functions have mismatched data
...

expected

comment line // foo should not lead to llvm-cov warning: 1 functions have mismatched data

crate A remove comment line

#![feature(str_from_raw_parts)]

use core::str::from_raw_parts;

/// # Safety
///
/// TODO
#[inline]
#[must_use]
pub const unsafe fn str_from_raw_parts<'a>(ptr: *const u8, len: usize) -> &'a str {
    from_raw_parts(ptr, len)
}
$ yarn cleanup:everything && yarn test:coverage
...
(llvm-cov no warning)
...

version

rustc 1.82.0-nightly (1f12b9b0f 2024-08-27)
binary: rustc
commit-hash: 1f12b9b0fdbe735968ac002792a720f0ba4faca6
commit-date: 2024-08-27
host: x86_64-unknown-linux-gnu
release: 1.82.0-nightly
LLVM version: 19.1.0
ranger-ross commented 2 months ago

Hey there @loynoir

Can you also provide the command and arguments that are run inside of yarn test:coverage? Also any RUSTFLAGS you might have set?

I can't reproduce it when running when running cargo llvm-cov with nightly-2024-08-27

loynoir commented 2 months ago

@ranger-ross

I follow coverage document with doctest support

https://doc.rust-lang.org/rustc/instrument-coverage.html

Something like below

env \
    RUSTFLAGS='-C instrument-coverage' \
    RUSTDOCFLAGS='-C instrument-coverage -Z unstable-options --persist-doctests /path/to/doctestbins' \
    LLVM_PROFILE_FILE="/path/to/profraw/cargo-test-%p-%9m.profraw" \
    cargo test --verbose --profile release

# TODO
OBJS=(
    # -object /path/to/target/xxx/deps/xxx
    # -object /path/to/target/xxx/deps/xxx
    # -object /path/to/target/xxx/deps/xxx
    # -object /path/to/doctestbins/*/rust_out
)

D="$(rustc --print sysroot)"/lib/rustlib/x86_64-unknown-linux-gnu/bin

"$D"/llvm-profdata merge -sparse -o /path/to/profdata /path/to/profraw/

"$D"/llvm-cov show --format html \
    --output-dir /path/to/html \
    --ignore-filename-regex=/.cargo/registry \
    --ignore-filename-regex=/rustc/ \
    --instr-profile /path/to/profdata \
    "${OBJS[@]}" \
    --show-instantiations \
    --show-line-counts-or-regions \
    --Xdemangler="${HOME}"/.cargo/bin/rustfilt
ranger-ross commented 2 months ago

@loynoir I spent some more time trying to reproduce the issue, but unfortunately wasn't able to. I tried with a few different version of nightly before and after nightly-2024-08-27.

I pushed up my testing code to https://github.com/ranger-ross/rust-130065

When I run ./crate-b/test.sh the report the HTML report is generated properly.

loynoir commented 2 months ago

I also find out I cannot reproduce this from scratch.

I will find a time to extract the reproduce logic out from workspace.

Maybe there is something within workspace but not mentioned above.

loynoir commented 2 months ago

Hi @ranger-ross , I can reproduce using https://github.com/loynoir/reproduce-rust-130065

loynoir commented 2 months ago

Hi @ranger-ross , I tested able to reproduce from scratch using docker with ubuntu image.

ranger-ross commented 2 months ago

I spent some time debugging this and this issue appears to have been introduced in nightly-2024-02-25 and is not an issue on nightly-2024-02-24 and below. (commits between them)

Unfortunately, finding the issue is a bit beyond my abilities/knowledge of how the compiler instruments the LLVM IR :sweat_smile:

@rustbot label +A-code-coverage +A-codegen +T-compiler +S-has-mcve +requires-nightly -needs-triage

ranger-ross commented 2 months ago

Also I have noticed that, its not just comments. Replacing the comment with another import like use std::sync::Arc; also causes the issue for me.

loynoir commented 2 months ago

When I want to suppress this warning as workaround, another thing I found is, seems like llvm-cov report suppress warning when stderr is not terminal.

{
  llvm-cov export ...
} 2> <(cat)
ranger-ross commented 2 months ago

I did some more debugging this weekend and the LLVM IR seemingly appears to be correct. I haven't looked into the llvm-cov implementation but I think it could possibly be related to https://github.com/llvm/llvm-project/issues/72786