From 6d1c156b3cbde5638ef970b2449bfb5aeb873da5 Mon Sep 17 00:00:00 2001 From: klzgrad Date: Wed, 9 Nov 2022 23:36:59 +0800 Subject: [PATCH] allocator: Fix __THROW and mallinfo for Musl --- src/base/allocator/partition_allocator/partition_root.cc | 7 ++++++- .../allocator_shim_default_dispatch_to_partition_alloc.cc | 2 +- .../partition_allocator/shim/allocator_shim_internals.h | 3 ++- .../shim/allocator_shim_override_libc_symbols.h | 6 ++++++ 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/base/allocator/partition_allocator/partition_root.cc b/src/base/allocator/partition_allocator/partition_root.cc index 7e8125a7ef..3097f994a7 100644 --- a/src/base/allocator/partition_allocator/partition_root.cc +++ b/src/base/allocator/partition_allocator/partition_root.cc @@ -239,7 +239,12 @@ void PartitionAllocMallocInitOnce() { if (!g_global_init_called.compare_exchange_strong(expected, true)) return; -#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) +#if defined(__MUSL__) + // Musl calls malloc() in pthread_atfork(), resulting in a deadlock. + static_cast(BeforeForkInParent); + static_cast(AfterForkInParent); + static_cast(AfterForkInChild); +#elif BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) // When fork() is called, only the current thread continues to execute in the // child process. If the lock is held, but *not* by this thread when fork() is // called, we have a deadlock. diff --git a/src/base/allocator/partition_allocator/shim/allocator_shim_default_dispatch_to_partition_alloc.cc b/src/base/allocator/partition_allocator/shim/allocator_shim_default_dispatch_to_partition_alloc.cc index a6bfe8975e..56d14e12a7 100644 --- a/src/base/allocator/partition_allocator/shim/allocator_shim_default_dispatch_to_partition_alloc.cc +++ b/src/base/allocator/partition_allocator/shim/allocator_shim_default_dispatch_to_partition_alloc.cc @@ -798,7 +798,7 @@ SHIM_ALWAYS_EXPORT int mallopt(int cmd, int value) __THROW { #endif // !BUILDFLAG(IS_APPLE) && !BUILDFLAG(IS_ANDROID) -#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) +#if BUILDFLAG(IS_LINUX) && !defined(__MUSL__) || BUILDFLAG(IS_CHROMEOS) SHIM_ALWAYS_EXPORT struct mallinfo mallinfo(void) __THROW { partition_alloc::SimplePartitionStatsDumper allocator_dumper; Allocator()->DumpStats("malloc", true, &allocator_dumper); diff --git a/src/base/allocator/partition_allocator/shim/allocator_shim_internals.h b/src/base/allocator/partition_allocator/shim/allocator_shim_internals.h index 8bddea78c3..d35e7a980d 100644 --- a/src/base/allocator/partition_allocator/shim/allocator_shim_internals.h +++ b/src/base/allocator/partition_allocator/shim/allocator_shim_internals.h @@ -9,7 +9,8 @@ #if defined(__GNUC__) -#if BUILDFLAG(IS_POSIX) +// Musl does not provide sys/cdefs.h +#if BUILDFLAG(IS_POSIX) && !defined(__MUSL__) #include // for __THROW #endif diff --git a/src/base/allocator/partition_allocator/shim/allocator_shim_override_libc_symbols.h b/src/base/allocator/partition_allocator/shim/allocator_shim_override_libc_symbols.h index bb07170019..49c305a9ac 100644 --- a/src/base/allocator/partition_allocator/shim/allocator_shim_override_libc_symbols.h +++ b/src/base/allocator/partition_allocator/shim/allocator_shim_override_libc_symbols.h @@ -22,6 +22,12 @@ #include "base/allocator/partition_allocator/shim/allocator_shim_internals.h" +// Musl does not specify anything for malloc() etc. +#if defined(__MUSL__) +#undef __THROW +#define __THROW +#endif + extern "C" { // WARNING: Whenever a new function is added there (which, surprisingly enough,