Skip to content

libFuzzer does not build on mingw with pthreads #106871

Closed
@Zentrik

Description

@Zentrik

This code,

void SetThreadName(std::thread &thread, const std::string &name) {
typedef HRESULT(WINAPI * proc)(HANDLE, PCWSTR);
HMODULE kbase = GetModuleHandleA("KernelBase.dll");
proc ThreadNameProc =
reinterpret_cast<proc>(GetProcAddress(kbase, "SetThreadDescription"));
if (ThreadNameProc) {
std::wstring buf;
auto sz = MultiByteToWideChar(CP_UTF8, 0, name.data(), -1, nullptr, 0);
if (sz > 0) {
buf.resize(sz);
if (MultiByteToWideChar(CP_UTF8, 0, name.data(), -1, &buf[0], sz) > 0) {
(void)ThreadNameProc(thread.native_handle(), buf.c_str());
}
}
}
}
introduced in 8bf8d36 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.

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 a8d15a9 has a message discussing some different ways to detect pthreads but doesn't seem to have a solution.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions