Skip to content

[compiler-rt][sanitizer] add Haiku support #134772

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions compiler-rt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,10 @@ append_list_if(COMPILER_RT_HAS_LIBC c SANITIZER_COMMON_LINK_LIBS)
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Fuchsia")
list(APPEND SANITIZER_COMMON_LINK_LIBS zircon)
endif()
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Haiku")
list(APPEND SANITIZER_COMMON_LINK_LIBS root)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can it link without ? surprised we need it.

Copy link
Contributor Author

@brad0 brad0 Apr 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

libroot is the libc equivalent; as in there is no libc.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes I know but I was assuming it would be implictly linked to.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without libroot it fails to build...

ld.lld: error: undefined symbol: environ
>>> referenced by sanitizer_linux.cpp:692 (/boot/home/llvm-brad/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp:692)
>>>               compiler-rt/lib/sanitizer_common/CMakeFiles/RTSanitizerCommon.x86_64.dir/sanitizer_linux.cpp.o:(__sanitizer::GetEnv(char const*))
>>> referenced by sanitizer_linux.cpp:765 (/boot/home/llvm-brad/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp:765)
>>>               compiler-rt/lib/sanitizer_common/CMakeFiles/RTSanitizerCommon.x86_64.dir/sanitizer_linux.cpp.o:(__sanitizer::GetEnviron())

ld.lld: error: undefined symbol: __libc_argv
>>> referenced by sanitizer_linux.cpp:764 (/boot/home/llvm-brad/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp:764)
>>>               compiler-rt/lib/sanitizer_common/CMakeFiles/RTSanitizerCommon.x86_64.dir/sanitizer_linux.cpp.o:(__sanitizer::GetArgv())

ld.lld: error: undefined symbol: sched_yield
>>> referenced by sanitizer_linux.cpp:826 (/boot/home/llvm-brad/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp:826)
>>>               compiler-rt/lib/sanitizer_common/CMakeFiles/RTSanitizerCommon.x86_64.dir/sanitizer_linux.cpp.o:(__sanitizer::FutexWait(__sanitizer::atomic_uint32_t*, unsigned int))

ld.lld: error: undefined symbol: sysconf
>>> referenced by sanitizer_linux.cpp:1279 (/boot/home/llvm-brad/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp:1279)
>>>               compiler-rt/lib/sanitizer_common/CMakeFiles/RTSanitizerCommon.x86_64.dir/sanitizer_linux.cpp.o:(__sanitizer::GetPageSize())
>>> referenced by sanitizer_posix_libcdep.cpp:549 (/boot/home/llvm-brad/compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp:549)
>>>               compiler-rt/lib/sanitizer_common/CMakeFiles/RTSanitizerCommonLibc.x86_64.dir/sanitizer_posix_libcdep.cpp.o:(__sanitizer::StartSubprocess(char const*, char const* const*, char const* const*, int, int, int))

ld.lld: error: undefined symbol: _get_next_image_info
>>> referenced by sanitizer_linux.cpp:1289 (/boot/home/llvm-brad/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp:1289)
>>>               compiler-rt/lib/sanitizer_common/CMakeFiles/RTSanitizerCommon.x86_64.dir/sanitizer_linux.cpp.o:(__sanitizer::ReadBinaryName(char*, unsigned long))
>>> referenced by sanitizer_linux.cpp:1289 (/boot/home/llvm-brad/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp:1289)
>>>               compiler-rt/lib/sanitizer_common/CMakeFiles/RTSanitizerCommon.x86_64.dir/sanitizer_linux.cpp.o:(__sanitizer::ReadLongProcessName(char*, unsigned long))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see good to know thx.

list(APPEND SANITIZER_COMMON_LINK_LIBS bsd)
endif()

if("${CMAKE_SYSTEM_NAME}" STREQUAL "Fuchsia")
set(SANITIZER_NO_UNDEFINED_SYMBOLS_DEFAULT ON)
Expand Down
4 changes: 2 additions & 2 deletions compiler-rt/cmake/config-ix.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ set(COMPILER_RT_SANITIZERS_TO_BUILD all CACHE STRING
list_replace(COMPILER_RT_SANITIZERS_TO_BUILD all "${ALL_SANITIZERS}")

if (SANITIZER_COMMON_SUPPORTED_ARCH AND NOT LLVM_USE_SANITIZER AND
(OS_NAME MATCHES "Android|Darwin|Linux|FreeBSD|NetBSD|Fuchsia|SunOS" OR
(OS_NAME MATCHES "Android|Darwin|Linux|FreeBSD|NetBSD|Fuchsia|SunOS|Haiku" OR
(OS_NAME MATCHES "Windows" AND NOT CYGWIN AND
(NOT MINGW OR CMAKE_CXX_COMPILER_ID MATCHES "Clang"))))
set(COMPILER_RT_HAS_SANITIZER_COMMON TRUE)
Expand Down Expand Up @@ -875,7 +875,7 @@ else()
endif()

if (COMPILER_RT_HAS_SANITIZER_COMMON AND UBSAN_SUPPORTED_ARCH AND
OS_NAME MATCHES "Darwin|Linux|FreeBSD|NetBSD|Windows|Android|Fuchsia|SunOS")
OS_NAME MATCHES "Darwin|Linux|FreeBSD|NetBSD|Windows|Android|Fuchsia|SunOS|Haiku")
set(COMPILER_RT_HAS_UBSAN TRUE)
else()
set(COMPILER_RT_HAS_UBSAN FALSE)
Expand Down
20 changes: 17 additions & 3 deletions compiler-rt/lib/asan/asan_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@

#include "sanitizer_common/sanitizer_platform.h"
#if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD || \
SANITIZER_SOLARIS
SANITIZER_SOLARIS || SANITIZER_HAIKU

# if SANITIZER_HAIKU
# define _DEFAULT_SOURCE
# endif

# include <dlfcn.h>
# include <fcntl.h>
Expand All @@ -22,7 +26,9 @@
# include <stdio.h>
# include <sys/mman.h>
# include <sys/resource.h>
# include <sys/syscall.h>
# if !SANITIZER_HAIKU
# include <sys/syscall.h>
# endif
# include <sys/time.h>
# include <sys/types.h>
# include <unistd.h>
Expand All @@ -37,7 +43,7 @@
# include "sanitizer_common/sanitizer_libc.h"
# include "sanitizer_common/sanitizer_procmaps.h"

# if SANITIZER_FREEBSD
# if SANITIZER_FREEBSD || SANITIZER_HAIKU
# include <sys/link_elf.h>
# endif

Expand All @@ -54,6 +60,8 @@
# elif SANITIZER_NETBSD
# include <link_elf.h>
# include <ucontext.h>
# elif SANITIZER_HAIKU
extern "C" void *_DYNAMIC;
# else
# include <link.h>
# include <sys/ucontext.h>
Expand Down Expand Up @@ -162,6 +170,12 @@ static int FindFirstDSOCallback(struct dl_phdr_info *info, size_t size,
return 0;
}

# if SANITIZER_HAIKU
if (!info->dlpi_name[0] ||
internal_strncmp(info->dlpi_name, "/boot/system/runtime_loader",
sizeof("/boot/system/runtime_loader") - 1) == 0)
return 0;
# endif
# if SANITIZER_LINUX
// Ignore vDSO. glibc versions earlier than 2.15 (and some patched
// by distributors) return an empty name for the vDSO entry, so
Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/lib/asan/asan_malloc_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

#include "sanitizer_common/sanitizer_platform.h"
#if SANITIZER_FREEBSD || SANITIZER_FUCHSIA || SANITIZER_LINUX || \
SANITIZER_NETBSD || SANITIZER_SOLARIS
SANITIZER_NETBSD || SANITIZER_SOLARIS || SANITIZER_HAIKU

# include "asan_allocator.h"
# include "asan_interceptors.h"
Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/lib/asan/asan_posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ static void AfterFork(bool fork_child) {

void InstallAtForkHandler() {
# if SANITIZER_SOLARIS || SANITIZER_NETBSD || SANITIZER_APPLE || \
(SANITIZER_LINUX && SANITIZER_SPARC)
(SANITIZER_LINUX && SANITIZER_SPARC) || SANITIZER_HAIKU
// While other Linux targets use clone in internal_fork which doesn't
// trigger pthread_atfork handlers, Linux/sparc64 uses __fork, causing a
// hang.
Expand Down
1 change: 1 addition & 0 deletions compiler-rt/lib/asan/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ if(NOT APPLE)
append_list_if(COMPILER_RT_HAS_LIBM -lm ASAN_UNITTEST_NOINST_LINK_FLAGS)
append_list_if(COMPILER_RT_HAS_LIBDL -ldl ASAN_UNITTEST_NOINST_LINK_FLAGS)
append_list_if(COMPILER_RT_HAS_LIBRT -lrt ASAN_UNITTEST_NOINST_LINK_FLAGS)
append_list_if(HAIKU -lbsd ASAN_UNITTEST_NOINST_LINK_FLAGS)
append_list_if(COMPILER_RT_HAS_LIBPTHREAD -pthread ASAN_UNITTEST_NOINST_LINK_FLAGS)
append_list_if(COMPILER_RT_HAS_LIBPTHREAD -pthread ASAN_DYNAMIC_UNITTEST_INSTRUMENTED_LINK_FLAGS)
endif()
Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/lib/asan/tests/asan_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1163,7 +1163,7 @@ TEST(AddressSanitizer, DISABLED_StressStackReuseAndExceptionsTest) {
}
#endif

#if !defined(_WIN32)
#if !defined(_WIN32) && !defined(__HAIKU__)
TEST(AddressSanitizer, MlockTest) {
EXPECT_EQ(0, mlockall(MCL_CURRENT));
EXPECT_EQ(0, mlock((void *)0x12345, 0x5678));
Expand Down
4 changes: 2 additions & 2 deletions compiler-rt/lib/interception/interception.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#if !SANITIZER_LINUX && !SANITIZER_FREEBSD && !SANITIZER_APPLE && \
!SANITIZER_NETBSD && !SANITIZER_WINDOWS && !SANITIZER_FUCHSIA && \
!SANITIZER_SOLARIS
!SANITIZER_SOLARIS && !SANITIZER_HAIKU
# error "Interception doesn't work on this operating system."
#endif

Expand Down Expand Up @@ -368,7 +368,7 @@ inline void DoesNotSupportStaticLinking() {}
#define INCLUDED_FROM_INTERCEPTION_LIB

#if SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD || \
SANITIZER_SOLARIS
SANITIZER_SOLARIS || SANITIZER_HAIKU

# include "interception_linux.h"
# define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_LINUX_OR_FREEBSD(func)
Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/lib/interception/interception_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "interception.h"

#if SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD || \
SANITIZER_SOLARIS
SANITIZER_SOLARIS || SANITIZER_HAIKU

#include <dlfcn.h> // for dlsym() and dlvsym()

Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/lib/interception/interception_linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//===----------------------------------------------------------------------===//

#if SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD || \
SANITIZER_SOLARIS
SANITIZER_SOLARIS || SANITIZER_HAIKU

#if !defined(INCLUDED_FROM_INTERCEPTION_LIB)
# error interception_linux.h should be included from interception library only
Expand Down
5 changes: 5 additions & 0 deletions compiler-rt/lib/sanitizer_common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ set(SANITIZER_SOURCES_NOTERMINATION
sanitizer_flags.cpp
sanitizer_flag_parser.cpp
sanitizer_fuchsia.cpp
sanitizer_haiku.cpp
sanitizer_libc.cpp
sanitizer_libignore.cpp
sanitizer_linux.cpp
Expand All @@ -28,6 +29,7 @@ set(SANITIZER_SOURCES_NOTERMINATION
sanitizer_procmaps_common.cpp
sanitizer_procmaps_bsd.cpp
sanitizer_procmaps_fuchsia.cpp
sanitizer_procmaps_haiku.cpp
sanitizer_procmaps_linux.cpp
sanitizer_procmaps_mac.cpp
sanitizer_procmaps_solaris.cpp
Expand Down Expand Up @@ -227,6 +229,9 @@ set(SANITIZER_COMMON_DEFINITIONS

# note: L not I, this is nodefaultlibs for msvc
append_list_if(MSVC /Zl SANITIZER_COMMON_CFLAGS)
if(HAIKU)
list(APPEND SANITIZER_COMMON_CFLAGS -I/system/develop/headers/private -I/system/develop/headers/private/system/arch/x86_64 -I/system/develop/headers/private/system)
endif()
set(SANITIZER_CFLAGS ${SANITIZER_COMMON_CFLAGS})

# Too many existing bugs, needs cleanup.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,35 +48,39 @@ static void ioctl_table_fill() {
++ioctl_table_size; \
}

_(FIONBIO, READ, sizeof(int));
#if !SANITIZER_HAIKU
_(FIOASYNC, READ, sizeof(int));
_(FIOCLEX, NONE, 0);
_(FIOGETOWN, WRITE, sizeof(int));
_(FIONBIO, READ, sizeof(int));
_(FIONCLEX, NONE, 0);
_(FIOSETOWN, READ, sizeof(int));
#endif
_(SIOCATMARK, WRITE, sizeof(int));
_(SIOCGIFCONF, CUSTOM, 0);
_(SIOCGPGRP, WRITE, sizeof(int));
_(SIOCSPGRP, READ, sizeof(int));
#if !SANITIZER_SOLARIS
#if !SANITIZER_SOLARIS && !SANITIZER_HAIKU
_(TIOCCONS, NONE, 0);
#endif
_(TIOCEXCL, NONE, 0);
#if !SANITIZER_HAIKU
_(TIOCGETD, WRITE, sizeof(int));
_(TIOCNOTTY, NONE, 0);
_(TIOCPKT, READ, sizeof(int));
_(TIOCSETD, READ, sizeof(int));
_(TIOCSTI, READ, sizeof(char));
#endif
_(TIOCEXCL, NONE, 0);
_(TIOCGPGRP, WRITE, pid_t_sz);
_(TIOCGWINSZ, WRITE, struct_winsize_sz);
_(TIOCMBIC, READ, sizeof(int));
_(TIOCMBIS, READ, sizeof(int));
_(TIOCMGET, WRITE, sizeof(int));
_(TIOCMSET, READ, sizeof(int));
_(TIOCNOTTY, NONE, 0);
_(TIOCNXCL, NONE, 0);
_(TIOCOUTQ, WRITE, sizeof(int));
_(TIOCPKT, READ, sizeof(int));
_(TIOCSCTTY, NONE, 0);
_(TIOCSETD, READ, sizeof(int));
_(TIOCSPGRP, READ, pid_t_sz);
_(TIOCSTI, READ, sizeof(char));
_(TIOCSWINSZ, READ, struct_winsize_sz);

#if !SANITIZER_IOS
Expand Down
2 changes: 2 additions & 0 deletions compiler-rt/lib/sanitizer_common/sanitizer_errno.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
# define __errno_location ___errno
#elif SANITIZER_WINDOWS
# define __errno_location _errno
#elif SANITIZER_HAIKU
# define __errno_location _errnop
#endif

extern "C" int *__errno_location();
Expand Down
21 changes: 15 additions & 6 deletions compiler-rt/lib/sanitizer_common/sanitizer_errno_codes.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,21 @@

namespace __sanitizer {

#define errno_ENOMEM 12
#define errno_EBUSY 16
#define errno_EINVAL 22
#define errno_ERANGE 34
#define errno_ENAMETOOLONG 36
#define errno_ENOSYS 38
#ifdef __HAIKU__
# define errno_ENOMEM (0x80000000)
# define errno_EBUSY (0x80000000 + 14)
# define errno_EINVAL (0x80000000 + 5)
# define errno_ERANGE (0x80007000 + 17)
# define errno_ENAMETOOLONG (0x80000000 + 0x6004)
# define errno_ENOSYS (0x80007009)
#else
# define errno_ENOMEM 12
# define errno_EBUSY 16
# define errno_EINVAL 22
# define errno_ERANGE 34
# define errno_ENAMETOOLONG 36
# define errno_ENOSYS 38
#endif

// Those might not present or their value differ on different platforms.
extern const int errno_EOWNERDEAD;
Expand Down
Loading
Loading