Commit Graph

1109 Commits

Author SHA1 Message Date
Ishant Goyal
addf751420 Added support to configure lower bound on per-thread cache size
[alkondratenko@gmail.com: removed spurious new line at thread_cache.h]
Signed-off-by: Aliaksey Kandratsenka <alkondratenko@gmail.com>
2024-06-04 12:11:01 -04:00
Aliaksey Kandratsenka
9fb05f3467 Reapply "[osx] implement native c++ allocation operators on osx"
This reverts commit a0880d78f3.

We had to revert it, because some most recent OSX versions had broken
exceptions for unwinding through functions with ATTRIBUTE_SECTION. But
now that ATTRIBUTE_SECTION is dropped on OSX, we can recover it.

Same as before, operator new/delete integration is much faster on
OSX. Unlike malloc/free and similar APIs which are ~fundamentally
broken performance-wise on OSX due to expensive arenas integration, we
don't need to support this overhead for C++ primitives. So lets get
this win at last.
2024-05-29 19:22:09 -04:00
Aliaksey Kandratsenka
5472781f4a drop broken and deprecated ATTRIBUTE_SECTION support for OSX
This support is only strictly necessary for
MallocHook_GetCallerStackTrace which is only needed for heap
checker. And since heap checker is Linux-only, lets just amputate this
code.
2024-05-29 19:20:21 -04:00
Aliaksey Kandratsenka
2b0b119b6d always use full 64 bits of address on Solaris
Solaris has somewhat nice behavior of mmaps where high bits of address
are set. It still uses only 48 bits of address on x86 (and,
presumably, 64-bit arm), just with somewhat non-standard way. But this
behavior causes some inconveniences to us. In particular, we had to
disable mmap sys allocator and had failing emergency malloc tests (due
to assertion in TryGetSizeClass in CheckCachedSizeClass). We could
consider more comprehensive fix, but lets just do "honest" 64-bit
addresses at least for now.
2024-05-29 18:30:09 -04:00
Aliaksey Kandratsenka
8be31af0fc unbreak debugallocation_test on Solaris
Allow debugallocation death test to accept RUN_ALL_TESTS in backtrace
instead of main. For some reason solaris ends up either optimizing
tail-calling of RUN_ALL_TESTS or something else happens in gtest
platform integration, but failure backtraces don't have main
there. But the intention of the test is simply to ensure that we got
failure with backtrace. So lets accept RUN_ALL_TESTS as well.
2024-05-29 18:30:09 -04:00
Aliaksey Kandratsenka
c61f35f04c simplify tcmalloc/sbrk/sbrk-hooks integration
Instead of relying on __sbrk (on subset of Linux systems) or invoking
sbrk syscall directly (on subset of FreeBSD systems), we have malloc
invoke special tcmalloc_hooked_sbrk function. Which handles hooking
and then invokes regular system's sbrk. Yes, we loose theoretical
ability to hook into non-tcmalloc uses of sbrk, but we gain portable
simplicity.
2024-05-29 18:30:09 -04:00
Aliaksey Kandratsenka
29f394339b refactor and simplify capturing backtraces from mmap hooks
The case of mmap and sbrk hooks is simple enough that we can do
simpler "skip right number of frames" approach. Instead of relying on
less portable and brittle attribute section trick.
2024-05-29 18:30:09 -04:00
Aliaksey Kandratsenka
29b6eff4c7 replace heap-checker "bcad" stuff
The comments in this file stated that it has to be linked in specific
order to get initialized early enough. But our initialization is
de-facto via initial malloc hook. And to deal with latest-possible
destruction, we use more convenient destructor function
attribute, and make things simpler.
2024-05-29 18:30:09 -04:00
Aliaksey Kandratsenka
33cda2c9b3 unbreak grab-backtrace frame skipping logic
When building with -O0 -fno-inlines, +[] () {} (lambda) syntax for
function pointers actually creates "wrapper function" so we see extra
frame (due to disabled inlinings). Fix is to create explicit function
and pass it, instead of lambda thingy.
2024-05-29 18:30:09 -04:00
Yikai Zhao
38b19664d3 generic_fp stacktrace: aarch64 frame pointer may be 8 byte aligned 2024-05-25 12:32:44 -04:00
Aliaksey Kandratsenka
7b9dc8e1fc unbreak skip_count adjustment in tcmalloc::GrabBacktrace
I broke it with "simple" off by one error in big emergency malloc
refactoring change.

It is somewhat shocking that no test caught this, but we'll soon be
adding such test.
2024-05-23 15:08:02 -04:00
Aliaksey Kandratsenka
d9263eea08 unbreak pagemap unittest compilation on msvc 2024-05-23 13:31:42 -04:00
Aliaksey Kandratsenka
d9a99c290a expand emergency malloc integration to !kHaveGoodTLS systems
References github issue #1503.

This significantly reworks both early thread cache access and related
emergency malloc mode checking integration. As a result, we're able to
to rely on emergency malloc even on systems without "good"
TLS (e.g. QNX which does emutls).

One big change here is we're undoing early change to have single
"global" thread cache early during process lifetime. It was nice and
somewhat simpler approach. But because of inherent locking during
early process lifetime, we couldn't be sure of certain lock ordering
aspects w.r.t. backtracing/exception-stack-unwinding. So I choose to
keep it safe. So the new idea is we use SlowTLS facility to find
threads' caches when normal tls isn't ready yet. It avoids holding
locks around potentially recursion-ful things (like
ThreadCache::ModuleInit or growing heap). But we then have to be
careful to "re-attach" those early thread cache instances to regular
TLS. There will nearly always be just one of such thread caches. For
initial thread. But we cannot entirely rule out more general case
where someone creates threads before process initializers ran and
main() is reached. Another notable thing is free-ing memory in this
early mode will always using slow-path deletion directly into central
free list.

SlowTLS facility is "simply" a generalization of previous
CreateBadTLSCache code. I.e. we have a small fixed-size cache that
holds "exceptional" instances of thread-identitity to
thread-cache+emergency-mode-flag mappings.

We also take advantage of tcmalloc::kInvalidTLSKey we introduced
earlier and remove potentially raceful memory ordering between reading
tls_ready_ and tls_key_.

For emergency malloc detection we previously used thread_local
flag. Which we cannot use on !kHaveGoodTLS systems. So we instead
_remove_ thread's cache from it's normal tls storage and place it
"into" SlowTLS instead for the duration of WithStacktraceScope
call (which is how emergency mode is enabled now).
2024-05-23 13:31:42 -04:00
Aliaksey Kandratsenka
46d9a6293a introduce tcmalloc::kInvalidTLSKey
The intention is to initialize tls-key variable with this invalid
value. This will help us avoid separate "tls ready" flag and possible
memory ordering issues around distinct tls key and tls-ready
variables.

On windows we use TLS_OUT_OF_INDEXES values which is properly
"impossible" tls index value. On POSIX systems we add theoretically
unportable, but practically portable assumption that tls keys are
integers. And we make value of -1 be that invalid key.
2024-05-18 13:53:34 -04:00
Aliaksey Kandratsenka
65ce9e899e better abort in internal_logging.cc:Log
We use __builtin_trap (which compiles to explicitly undefined
instruction or "int 3" on x64-en), when available, to make those
crashing Log invokations a little nicer to debug.
2024-05-18 13:53:34 -04:00
Aliaksey Kandratsenka
a6864ae233 clean up FLAGS_tcmalloc_heap_limit_mb value in page heap tests
This will not only make things right w.r.t. possible order of test
runs, but it also unbreaks test failures on windows (where gtest ends
up doing some malloc after test completion, hits the limit and dies).
2024-05-18 13:51:54 -04:00
Aliaksey Kandratsenka
de12f89d2a expand emergency malloc test coverage
We add coverage of calloc and we also cover no-hooks case.
2024-05-01 17:43:49 -04:00
Aliaksey Kandratsenka
77ba2cf133 don't include malloc_extension.h in page_heap.h 2024-05-01 17:43:49 -04:00
Aliaksey Kandratsenka
d63b2fad17 introduce thread::SelfThreadId
Sadly, certain/many implementations of std::this_thread::id invoke
malloc. So we need something more robust. On Unix systems we use
address of errno as thread identifier. Sadly, this doesn't cover
windows where MS's C runtime facility will occasionally malloc when
errno location is grabbed (with some special trickery for when malloc
itself needs to set errno to ENOMEM!). So on windows, we do
GetCurrentThreadId which appears to be roughly as efficient as
"normal" system's __errno_location implementation.
2024-05-01 17:43:49 -04:00
Aliaksey Kandratsenka
13aecbe197 make debugallocation calloc hook invocation order consistent
I.e. we normally call new hook just before returning. In calloc's case
this means after zeroing allocated memory.
2024-05-01 16:54:32 -04:00
Aliaksey Kandratsenka
bc2aac871a re-introduce missing initial-exec attribute for per-thread data 2024-05-01 16:54:10 -04:00
Aliaksey Kandratsenka
786ecdfbc8 handle re-entrancy in check-address facility
We use mmap when we initialize it, which could via heap checker
recurse back into backtracing and check-address. So before we do mmap
and rest of initialization, we now set check-address implementation
to conservative two-syscalls version.
2024-05-01 16:51:29 -04:00
Aliaksey Kandratsenka
a038c8c23f unbreak cmake build around HAVE_SBRK check 2024-04-30 23:25:17 -04:00
leap
1bdabc0e37 Unbreak proc_maps_iterator.cc compilation on QNX
We had duplicate definition of flags_tmp variable.

Signed-off-by: Aliaksey Kandratsenka <alkondratenko@gmail.com>
[alkondratenko@gmail.com] updated commit message
2024-04-10 13:49:48 -04:00
Aliaksey Kandratsenka
7c0cbb3a0c avoid depending on {s,}brk on FreeBSD systems without it
Apparently some recent FreeBSDs occasionally lack brk. So our code
which previously hard-coded that this OS has brk (which we use to
implement hooked sbrk) fails to compile.

Our configure scripts already detects sbrk, so we simply need to pay
attention. Fixes github issue #1499
2024-04-03 15:46:06 -04:00
Aliaksey Kandratsenka
8dde01b4de unbreak emergency malloc tests on systems without "good" TLS
Our implementation of emergency malloc slow-path actually depends on
good TLS implementation. So on systems without good TLS (i.e. OSX), we
lied to ourselves that emergency malloc is available, but then failed
tests.
2024-03-27 18:36:29 -04:00
Aliaksey Kandratsenka
4cbd8ad245 refactor and simplify LowLevelAlloc
We don't expose DefaultArena anymore. Simply passing nullptr implies
default arena.

We also streamline default arena initialization (we
previously relied on combination of lazy initialazation in ArenaInit
and constexpr construction).

There is also no need to have elaborate ArenaLock thingy. We use plain
SpinLockHolder instead.
2024-03-27 18:18:20 -04:00
Aliaksey Kandratsenka
2cd876cdfd amputate unused LowLevelAlloc flags 2024-03-27 18:17:29 -04:00
Aliaksey Kandratsenka
6babeb1a12 apply StaticStorage to windows/system-alloc.cc 2024-03-27 15:48:30 -04:00
Aliaksey Kandratsenka
fa3753dc86 apply StaticStorage across the source code 2024-03-27 15:48:30 -04:00
Aliaksey Kandratsenka
ee123a1a0d apply StaticStorage to TrivialOnce 2024-03-27 15:48:30 -04:00
Aliaksey Kandratsenka
86606eef40 introduce tcmalloc::StaticStorage
We have a number of things that are explicitly constructed in some
statically allocated space, so lets finally have a helper for that.
2024-03-27 15:48:30 -04:00
Aliaksey Kandratsenka
7cbbbdd099 use DirectAnonMMap in emergency allocator
It is just more straightforward and cleaner than going via
LowLevelAllocators "default" pages allocator, which does direct mmap
thingy anyways.
2024-03-27 15:48:30 -04:00
Aliaksey Kandratsenka
6785aeb027 add test coverage of emergency malloc 2024-03-27 15:48:30 -04:00
Aliaksey Kandratsenka
703dc970fc add missing override declarations to TestingPortalImpl 2024-03-27 15:48:30 -04:00
hmihaylov-sc
e80e863e37 remove deprecated register storage class 2024-03-27 12:11:30 -04:00
Aliaksey Kandratsenka
c71e512664 implement support for hidden visibility
This is off by default for now. And autotools-only. But after
significant preparatory work, we're now able to do it cleanly. Stuff
that was previously exported just for tests (like page heap stuff or
flags) is now unexported.

Just like on windows all symbols explicitly exported by
PERFTOOLS_DLL_DECL are visible and exported and rest are hidden. Those
include all the malloc/new APIs of course, and all the other symbols
we advertise in our headers (e.g. MallocExtension, MallocHook).

Updates issue #600
2024-03-24 19:56:57 -04:00
Aliaksey Kandratsenka
38d741710b drop unused pwd.h/grp.h checks 2024-03-24 16:59:58 -04:00
Aliaksey Kandratsenka
02f10c15a5 update heap-checker_unittest to reach into malloc guts via TestingPortal 2024-03-24 16:59:58 -04:00
Aliaksey Kandratsenka
d5bd0087c1 avoid VLOG dependency in heap-profiler test
This makes the test compatible with -fvisibility=hidden
2024-03-24 16:59:58 -04:00
Aliaksey Kandratsenka
c23bbdb4c7 don't dllexport Log and HookList 2024-03-24 16:59:58 -04:00
Aliaksey Kandratsenka
ea4fceb796 don't dllexport flags 2024-03-24 16:59:58 -04:00
Aliaksey Kandratsenka
274038486b ensure dllexport/public visibility of all aliases we generate 2024-03-24 16:59:58 -04:00
Aliaksey Kandratsenka
32796c754e export all mmap and sbrk replacement functions 2024-03-24 16:59:58 -04:00
Aliaksey Kandratsenka
5cba04ead2 export IsHeapProfilerRunning symbol
It is part of our API and ABI, so it needs to public visibility
declaration, like rest of functions declared in the same public header.
2024-03-24 16:59:57 -04:00
Aliaksey Kandratsenka
2436c80d6c ensure that malloc_extension_c_test includes config.h first
Like we do everywhere.
2024-03-24 16:59:57 -04:00
Aliaksey Kandratsenka
701777bbee dont define unused HAVE_MEMORY_H in windows config.h 2024-03-24 16:59:57 -04:00
Aliaksey Kandratsenka
6038a09c76 simplify vdso_support.h/elf_mem_image.h integration
This is only used by Linux/PPC, so lets make it simpler and
clearer (e.g. no need to do __GLIBC__ test).
2024-03-24 16:59:57 -04:00
Aliaksey Kandratsenka
ae31e6afa3 drop unused LT_OBJDIR from cmake config.h template 2024-03-24 16:59:57 -04:00
Aliaksey Kandratsenka
e867bb7818 amputate unused Elf32_Versym check 2024-03-24 16:59:57 -04:00