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

    <tr>
        <th>Summary</th>
        <td>
            libFuzzer does not build on mingw with pthreads
        </td>
    </tr>

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

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

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

<pre>
    This code, https://github.com/Zentrik/llvm-project/blob/25afb7768f8278bfbc1839c914caf2f4883d5e43/compiler-rt/lib/fuzzer/FuzzerUtilWindows.cpp#L241-L256 introduced in https://github.com/Zentrik/llvm-project/commit/8bf8d36f8e82a1e2d32f33dbe7369d9cecd57f46 throws the following error
```
ninja: job failed: /opt/bin/x86_64-w64-mingw32-libgfortran5-cxx11/x86_64-w64-mingw32-clang++ --target=x86_64-w64-mingw32 --sysroot=/opt/x86_64-w64-mingw32/x86_64-w64-mingw32/sys-root/ -D_FILE_OFFSET_BITS=64 -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I/workspace/build/projects/compiler-rt/lib/fuzzer -I/workspace/srcdir/llvm-project/compiler-rt/lib/fuzzer -I/workspace/build/include -I/workspace/srcdir/llvm-project/llvm/include -I/workspace/srcdir/llvm-project/compiler-rt/lib/fuzzer/../../include -remap -D__USING_SJLJ_EXCEPTIONS__ -D__CRT__NO_INLINE -pthread -DMLIR_CAPI_ENABLE_WINDOWS_DLL_DECLSPEC -fvisibility-inlines-hidden -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -pedantic -Wno-long-long -Wimplicit-fallthrough -Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wsuggest-override -Wstring-conversion -ffunction-sections -fdata-sections -Wall -O3 -DNDEBUG -m64 -fno-builtin -fno-exceptions -fomit-frame-pointer -funwind-tables -fno-stack-protector -fno-sanitize=safe-stack -fvisibility=hidden -fno-lto -O3 -gline-tables-only -fms-extensions -fno-omit-frame-pointer -std=c++17 -MD -MT projects/compiler-rt/lib/fuzzer/CMakeFiles/RTfuzzer.x86_64.dir/FuzzerUtilWindows.cpp.obj -MF projects/compiler-rt/lib/fuzzer/CMakeFiles/RTfuzzer.x86_64.dir/FuzzerUtilWindows.cpp.obj.d -o projects/compiler-rt/lib/fuzzer/CMakeFiles/RTfuzzer.x86_64.dir/FuzzerUtilWindows.cpp.obj -c /workspace/srcdir/llvm-project/compiler-rt/lib/fuzzer/FuzzerUtilWindows.cpp
clang-16: warning: argument unused during compilation: '-remap' [-Wunused-command-line-argument]
/workspace/srcdir/llvm-project/compiler-rt/lib/fuzzer/FuzzerUtilWindows.cpp:252:30: error: cannot initialize a parameter of type 'HANDLE' (aka 'void *') with an rvalue of type 'native_handle_type' (aka 'unsigned long long')
 (void)ThreadNameProc(thread.native_handle(), buf.c_str());
 ^~~~~~~~~~~~~~~~~~~~~~
1 error generated.
```

Using the pthreads implementation used for linux fixes this issue, i.e.
```c++
void SetThreadName(std::thread &thread, const std::string &name) {
  (void)pthread_setname_np(thread.native_handle(), name.c_str());
}
```

I'm not sure what exactly we should test to know which version of the function to use. The commit https://github.com/llvm/llvm-project/commit/a8d15a926689c126c4d316788786e0160cfc1d5d has a message discussing some different ways to detect pthreads but doesn't seem to have a solution.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8V9Fu2zrSfhrmhqBhUbYkX_jCsazz-8BxitqFf-yNQJEjiQ1FeknKTnqxz76g5Jy027TbPdg9QSCLw5nh8OPwmxFzTjYaYInm92ie37Het8Yu_wbaW_l0Vxnxsjy20mFuBCC6xq33Z4fiFaIFokUjfdtXE246RIubEaKFUpeOnK35DNwjWlTKVIgWdM7qKk2TrM5omlV1xaMsXvBFNOOspvUsy2Ixh1mMaMFNd5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