<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/63179>63179</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
CLang source-based code coverage creates broken profraw on some environments
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
iskiselev
</td>
</tr>
</table>
<pre>
I use clang source-based code coverage. When I run produced binaries on one of my environments, it produce invalid profraw.
So, when I run `llvm-profdata merge -sparse bad.profraw -o combined.profdata`, I see next output:
```
warning: bad.profraw: invalid instrumentation profile data (file header is corrupt)
error: no profile can be merged
```
The instrumented (windows) binaries are executed in windows container. When it is executed on my build server in particular windows docker container, I have stable reproduction for the issue. If I use different docker container image, it produce correct profraw. Also when I run same binary in the same container (that produced bad result) on another computer, it produce correct profraw file. It also produce good profraw. when another binary is tested on both computers and containers.
To produce binaries, I used clang provided in Microsoft Visual Studio Build Tools 17.6.2. It reports `clang version 15.0.1`. To produce code coverage `clang++` driver is used with flags `-fprofile-instr-generate -fcoverage-mapping`.
If it is matter, the instrumented binary is dll, loaded in non-instrumented executable. Next code is used to facilitate coverage:
```
#include "code_coverage.h"
#include <chrono>
#include <mutex>
#include <thread>
#ifdef MANUAL_COVERAGE_BUILD
extern "C" {
__attribute__((visibility("default"))) int __llvm_profile_runtime;
int __llvm_profile_write_file(void);
void __llvm_profile_initialize_file(void);
}
#endif
void CodeCoverage::Init()
{
}
void CodeCoverage::Write()
{
#ifdef MANUAL_COVERAGE_BUILD
static std::once_flag flag;
std::call_once(flag, []() {
for (int i = 0; i < 100; i++) {
__llvm_profile_initialize_file();
if (__llvm_profile_write_file() == 0) {
return;
}
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}
});
#endif
}
```
`CodeCoverage::Write()` method is called once after application signalize to library that it will be unloaded.
I've tried to debug it a little bit, and error generated from https://github.com/llvm/llvm-project/blob/556c9e66ad244ae12d1fc80e5e3ea5cd438f8e69/llvm/lib/ProfileData/InstrProfReader.cpp#L572
I'm willing to collect any additional information required to investigate the issue.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVltv47oR_jX0y8CCTMWy_eCHxNkUBvacFue2jwZFjqzpUqRLUvamv74Yyrdks9sWMBJRmss331w4KkbaO8S1mD-J-fNEDanzYU3xK0W0eJw03ryutzBEBG2V20P0Q9A4bVREA9obBO2PGNQeC_jSoYMthMHBIXgzaDTQkFOBMIJ34B2Cb6F_BXRHCt716FIUcgOULhpA7qgsGT63QZ0KUT6L8vF3z2KnmwNRl9Ye-ymLGZUU9Bj2CNN4UCEiNMoUZwsw9aB935DD8R2Li7pkg1uIiODwWwI_pMOQRPU4OmSB8ZePJxUcub2oHu9N8_GCl1xMYeCIVCKfGWjJImRwQi7zoUNlMABF0D6E4ZCEXI0OMAQf2J7zV1WtHDQ4RmY-hDX-_aPDO_do2N2JnPGnKOTqlgIVEPAb6oFlyMFZBrR3SZHDcE4hJUZ4lfSOU9YMZA1EDEcOwMFBhUR6sCpc7Rivv2K4mRsZ7tQRISbVWISAY5ozQ60PkBh6jAMWsG1hLDRDbYsBXfrOIFCv9viuYJhJ1OlaMPBoo78vlah6HEl4ZeDsMr-6mRVymTqV7qpWGQgYB8sJYgKU86nLUPrDkMbQfowBOH0FbBMoxnKR2nt_q-sR4cXuBV6EhPFMeuNTd3UYQTlzgxyLNwVw83FJ9sj9kLs0N-4h-COZMfG_kA4--jbBXxQHZeH3NBjy8JRz_If3NsJsUdSFzEEEPPiQIvfcaOuIIXIGZ_OiLGaiLgu4g_BmKlyVhHziX12CCXQcmyDDO1HqoLVqnx1M23P1T3NFT_foMKiEMG0vJqe9Ohy4GevyzMK2Pddsr9I5Oel9T9wYNtayhPXqTIfzbvpGdix9rtgCfuXhkEO6AE4eWqXJUmJcF1Q_mhxCVuS0HQyCkJIN7a4TsxNS3ufxXrba6C5450X16aOP_ZDw2w--pS6gMrePV5H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