Issue 134226
Summary The issue of the runtime execution of the __llvm_defilew_rite_file() function affecting the collection of coverage
Labels new issue
Assignees
Reporter Kurtcobainzl
    I am implementing a demo to collect coverage during runtime, but I found that when I execute the `__llvm_profile_write_file();` function, it causes the coverage data to be inaccurate. Below is my demo. After starting the program without performing any actions, the line printf("Invalid input, please try again.\n"); is shown as covered. I suspect that the execution of the` __llvm_profile_write_file(); `function is affecting the coverage results.
Here are the version information for my relevant tools, machine details, source code, commands, and the phenomenon. I would really like to know where the issue lies. Thank you.

version:  Mac OS  & Apple M4 Pro chip.

`oker@okerdeMacBook-Pro ~ % clang++ --version
Homebrew clang version 20.1.1
Target: arm64-apple-darwin24.2.0
Thread model: posix
InstalledDir: /opt/homebrew/Cellar/llvm/20.1.1/bin
Configuration file: /opt/homebrew/etc/clang/arm64-apple-darwin24.cfg
oker@okerdeMacBook-Pro ~ % llvm-profdata --version
Homebrew LLVM version 20.1.1
  Optimized build.
oker@okerdeMacBook-Pro ~ % llvm-cov --version
Homebrew LLVM version 20.1.1
  Optimized build.
`


and here is code ,It will execute __llvm_profile_write_file(); every 5 seconds to collect coverage information

`#include <stdio.h>
#include <stdint.h>
#include <execinfo.h>
#include <string.h>
#include <signal.h>    
#include <thread>
#include <chrono>
#include <atomic>

// Pre-declaration for Clang coverage interface
extern "C" int __llvm_profile_write_file(void);
std::atomic<bool> stop_thread(false);

// Periodic coverage data writing thread
void timerThread() {
    while (!stop_thread) {
        std::this_thread::sleep_for(std::chrono::seconds(5)); // run every 5 seconds
        __llvm_profile_write_file();
    }
}

void foo() {
    printf("foo()\n");
}

void bar() {
    printf("bar()\n");
}

void runUserLoop() {
    while (true) {
        printf("Please enter 1 to call foo(), 2 to call bar(), 3 to exit: ");
        int choice = 0;
        scanf("%d", &choice);
        if (choice == 1) {
            foo();
        } else if (choice == 2) {
            bar();
        } else if (choice == 3) {
            break;
        } else {
            printf("Invalid input, please try again.\n");
        }
    }
}

int main() {
    std::thread timer(timerThread); // start timer thread
    runUserLoop();
    return 0;
}`

Below are the commands I used to execute it, and after it runs, I did not perform any actions, waiting for 10 seconds before executing the command to collect coverage data 

`clang++ -fsanitize=address -fsanitize-coverage=trace-pc-guard \
        -fprofile-instr-generate -fcoverage-mapping \
        demo.cpp -o demo
export ASAN_OPTIONS=coverage=1:coverage_dir=./covdata
export LLVM_PROFILE_MERGE=1
mkdir -p covdata
./demo`
`llvm-profdata merge -sparse covdata/*.profraw -o demo.profdata
llvm-cov show ./demo -instr-profile=""
`

![Image](https://github.com/user-attachments/assets/cc75ee11-4032-42ab-8779-09135ee0e4d0)

_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to