<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/134226>134226</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            The issue of the runtime execution of the __llvm_defilew_rite_file() function affecting the collection of coverage
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          Kurtcobainzl
      </td>
    </tr>
</table>

<pre>
    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=demo.profdata
`

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

</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJykV02T2rrS_jWaTZcpIwMzLFh4ILyHepOT1Enu3VJCbmPdkSWXJEMmi_vbb7X8AROYnFQdFgMjtfrj0aP-EN6ro0Fcsfkzm28eRBsq61b_37og7UEo80M_HGzxutqBqEHVjcYaTVDmCAIKrC0EC9JqjTKAtCd04ohQtI4kXGuCqpHxNRzaADsobWsKCJUIcK7QwA7wO8o2IIQKgS3S_V7rU71vnC2Vxv3ZqYB7-sn4E-NLlj2zRQpla2RQ1pBiFUCK1qOPKi4eiCDItQOCMkLK1omAE3hGbc-gPNSv0fsJ5GVABz4IF4MiJY2zRydqOKtQ2TZAg660ro4xm1cQ0bYn4yStlaEjyoQyOsl35iS0KkCZpg0k1WgUHiG4VxBHocyEzdeGcd4FRN74yp4NCN_5j8UEduBb3xCoES0y1EGlrAFb0gIh8fd4EaoDXmRKlCXKMdQRL4e-1cFPWJqzNP8DHYJw3bWc0Pl42BAKIioq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