Commit graph

1767 commits

Author SHA1 Message Date
Jiyong Park
3ff116a68c Load libc_malloc_* libraries from the runtime APEX
/system/lib/libc.so is a symlink to libc.so in the runtime APEX.
libc_malloc_* libraries are bundled with libc.so because they share
implementation details.

However, since libc.so is loaded in the default namespace where the
runtime APEX path (/apex/com.android.runtime/lib) is not accessible,
libc.so has been using libc_malloc_* from /system/lib. This is
wrong because libc.so (from the runtime APEX) and libc_malloc_* (from
the platform) may not be in-sync.

libc.so now uses android_dlopen_ext to load libc_malloc_* libraries
correctly from the "runtime" linker namespace.

Bug: 122566199
Test: bionic-unit-tests

Merged-In: I46980fbe89e93ea79a7760c9b8eb007af0ada8d8
Change-Id: I46980fbe89e93ea79a7760c9b8eb007af0ada8d8
(cherry picked from commit 4e46ac69c2)
2019-04-11 21:36:16 +09:00
Ryan Prichard
93ea856949 Merge changes I59a8bc4a,Ic437d352
* changes:
  Fix dlsym and dladdr for TLS symbols
  Fix BionicAllocator comment
2019-04-04 20:38:47 +00:00
Christopher Ferris
e6fd53b751 Merge "Disable info messages by default for malloc debug." 2019-04-02 19:46:29 +00:00
Christopher Ferris
c328e4465d Disable info messages by default for malloc debug.
Add a new option verbose for malloc debug that is not enabled by default.
This disables all of the info log messages. It turns out these log
messages can add a measurable amount of time and can change the boot up.

Bug: 129239269

Test: Adjusted unit tests pass.
Test: Verified no messages unless verbose option used.
Change-Id: I805cb7c8ecb44de88119574e59d784877cacc383
2019-04-02 10:55:21 -07:00
Ryan Prichard
db6edcca36 Fix BionicAllocator comment
Test: n/a
Bug: none
Change-Id: Ic437d35231b47553add49e20d7ee451d42db710c
2019-04-01 16:17:59 -07:00
Christopher Ferris
fc26d71af1 Create a lib that uses scudo instead of jemalloc.
The media processes already use scudo as their allocator. However, it
doesn't really correctly replace the normal allocation functions, so create
a set of wrappers that allow us to use scudo closer to how jemalloc is used.

This is only a temporary change, and should be removed for the next
release of Android. In that version, we will be using standalone
scudo which won't require this wrapper code.

Bug: 123689570

Test: Ran new bionic unit tests. There are failures, but only with
Test: extensions that scudo does not support.

Change-Id: I0516c23d654a9b6c69b157c5501245d2e0b3d264
2019-04-01 13:02:49 -07:00
Christopher Ferris
1e3758e7a2 Fix double initialization call.
The previous refactor left a double call to the initialization of
the loaded hooks. Remove the unnecessary call.

Bug: 129239269

Test: All unit tests pass. No double printing of init messages.
Change-Id: Ie980f2383c75d69f8b06bf9a431bb59caef21188
2019-03-28 17:12:26 +00:00
Peter Collingbourne
feb5ed1b54 Use PR_SET_VMA_ANON_NAME to name the abort message mapping.
This makes it easier for tools to find the mapping. I am planning
to use this in crashpad to add HWASAN reports to the minidump.

Bug: http://crbug.com/crashpad/287
Change-Id: I600e551ef26d6ff62849319365d77912afa82fde
2019-03-27 16:50:27 -07:00
Christopher Ferris
ce491abe24 Increase the allocation limit again.
Still getting a few flakes, so double the allowed tries.

Bug: 128872105

Test: Test passes.
Change-Id: I0fb3d74655eaece6660dec26a2a1d01430ef8fbc
2019-03-26 15:47:07 -07:00
Elliott Hughes
886370c240 Fix internal uses of _PATH_BSHELL.
We regressed on this recently: code under the upstream-* directories has
_PATH_BSHELL defined as a call to __bionic_get_shell_path(). In our own
code, we may as well just call it directly.

Bug: https://issuetracker.google.com/129030706
Test: ran tests
Change-Id: Ic2423f521272be95e67f94771772fe8072636ef0
2019-03-25 17:28:22 -07:00
Peter Collingbourne
d75e308e61 Call __hwasan_init_static() during libc startup in statically linked executables.
__hwasan_init() was segfaulting when called from here because it
was calling into libc functions which required more of libc to be
initialized. Instead, call __hwasan_init_static(), which does a
minimal amount of initialization for statically linked executables,
just enough that we can run instrumented code. __hwasan_init() itself
will end up being called later (most likely from a global ctor)
after libc is fully initialized.

We'll need to wait for LLVM r352816+r352823 to land in our toolchain
before landing this.

Change-Id: I12ffc7e08f6dd161e4ff2088f8d56265af7baedf
2019-03-19 21:56:17 -07:00
Peter Collingbourne
b62888b709 Merge "Increase the size of the shadow call stack guard region to 16MB." 2019-03-19 23:29:05 +00:00
Christopher Ferris
9b78aa3529 Increase num tries before failing.
Bug: 128872105

Test: Ran the android_mallopt.set_allocation_limit_multiple_threads test
Test: a thousand times on taimen.
Change-Id: I67a474c53cd6eda8106feac99aee8e7b0bee1254
2019-03-18 21:45:36 -07:00
Peter Collingbourne
149ce93056 Add missing #include.
Fixes sanitizer build.

Test: walleye_hwasan-userdebug builds
Change-Id: If7890dbf2e715ca89b68f5c39c6ffbd24f15f2c2
2019-03-15 22:45:06 -07:00
Christopher Ferris
1fc5ccfe76 Add a platform API for setting an allocation limit.
Introduce an M_SET_ALLOCATION_LIMIT enumerator for android_mallopt(),
which can be used to set an upper bound on the total size of all
allocations made using the memory allocation APIs.

This is useful for programs such as audioextractor and mediaserver
which need to set such a limit as a security mitigation. Currently
these programs are using setrlimit(RLIMIT_AS) which isn't exactly
what these programs want to control. RLIMIT_AS is also problematic
under sanitizers which allocate large amounts of address space as
shadow memory, and is especially problematic under shadow call stack,
which requires 16MB of address space per thread.

Add new unit tests for bionic.

Add new unit tests for malloc debug that verify that when the limit
is enabled, malloc debug still functions for nearly every allocation
function.

Bug: 118642754
Test: Ran bionic-unit-tests/bionic-unit-tests-static.
Test: Ran malloc debug tests and perfetto integration tests.
Change-Id: I735403c4d2c87f00fb2cdef81d00af0af446b2bb
2019-03-15 10:54:55 -07:00
Christopher Ferris
fa10a3aa9a Add malloc_info for sanitizer.
Test: hwasan builds.
Change-Id: I39267c642af75b1ebb99633f25959638cc39628c
2019-03-08 11:11:27 -08:00
Christopher Ferris
6c619a0da3 Refactor the malloc_info code.
malloc_info needs to be per native allocator, but the code treated it
like a global function that doesn't depend on the native memory allocator.

Update malloc debug to dump the actual pointers that it has been tracking.

Test: bionic-unit-tests pass.
Test: malloc debug tests pass.
Test: malloc hook tests pass.
Change-Id: I3b0d4d748489dd84c16d16933479dc8b8d79013e
Merged-In: I3b0d4d748489dd84c16d16933479dc8b8d79013e
(cherry picked from commit a3656a98b1)
2019-03-07 08:39:55 -08:00
Christopher Ferris
a22f5d5175 Make aligned_alloc match the standard.
Jemalloc does not verify that the size parameter is a multiple of
alignment. Fix this since it only went into P.

Fix the unit tests, and fix malloc debug/malloc hooks to handle this
new restrictive behavior.

Bug: 126944692

Test: Ran bionic unit tests.
Test: Ran bionic unit tests with malloc hooks enabled (no new tests fail).
Test: Ran bionic unit tests with malloc debug enabled (no new tests fail).
Test: Ran malloc debug unit tests.
Change-Id: I4d50785928815679c781ca729f998454d76b9192
2019-03-01 23:56:23 -08:00
Treehugger Robot
0771b752f1 Merge "Workaround string-plus-int warning" 2019-03-01 02:21:16 +00:00
Yi Kong
4ca9a6b576 Workaround string-plus-int warning
The upcoming compiler warns against adding string and int:
In file included from bionic/libc/bionic/strsignal.cpp:41:
  bionic/libc/private/bionic_sigdefs.h:58:1: error: adding 'int' to a string does not append to the string [-Werror,-Wstring-plus-int]
  __BIONIC_SIGDEF(SIGWINCH,  "Window size changed")
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  bionic/libc/bionic/strsignal.cpp:40:83: note: expanded from macro '__BIONIC_SIGDEF'
  #define __BIONIC_SIGDEF(signal_number, unused) [ signal_number ] = #signal_number + 3,
                                                                     ~~~~~~~~~~~~~~~^~~

Use array indexing index to avoid this warning.

Test: m checkbuild
Change-Id: Ib5e20edbf5bac76352df0484dd233d0621beb4e9
2019-02-28 15:54:58 -08:00
Nick Kralevich
7fa3b47813 Ensure STDIN/STDOUT/STDERR always exist
File descriptor confusion can result if a process is exec()d and
STDIN/STDOUT/STDERR do not exist. In those situations, the first,
second, and third files opened by the exec()d application will have FD
0, 1, and 2 respectively. Code which reads / writes to these STD* file
descriptors may end up reading / writing to unintended files.

To prevent this, guarantee that FDs 0, 1, and 2 always exist. Bionic
only currently guarantees this for AT_SECURE programs (eg, a setuid
binary, setgid binary, filesystem capabilities, or SELinux domain
transition).

Extending this to all exec()s adds robustness against this class of
bugs. Additionally, it allows a caller to do:

  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);

and know that the exec()d process will reopen these file descriptors on
its own. This has the potential to simplify other parts of Android, eg
https://android-review.googlesource.com/c/platform/system/apex/+/915694

Steps to reproduce:

  sleep 100 <&- >&- 2>&- & BGPID=$! && ls -la /proc/$BGPID/fd && kill $BGPID

Expected:

  $ sleep 100 <&- >&- 2>&- & BGPID=$! && ls -la /proc/$BGPID/fd && kill $BGPID
  [1] 3154
  total 0
  dr-x------ 2 shell shell  0 1970-04-17 12:15 .
  dr-xr-xr-x 9 shell shell  0 1970-04-17 12:15 ..
  lrwx------ 1 shell shell 64 1970-04-17 12:15 0 -> /dev/null
  lrwx------ 1 shell shell 64 1970-04-17 12:15 1 -> /dev/null
  lrwx------ 1 shell shell 64 1970-04-17 12:15 2 -> /dev/null
  $
  [1] + Terminated           \sleep 100 <&- >&- 2>&-

Actual:

  $ sleep 100 <&- >&- 2>&- & BGPID=$! && ls -la /proc/$BGPID/fd && kill $BGPID
  [1] 16345
  total 0
  dr-x------ 2 shell shell 0 2019-02-28 20:22 .
  dr-xr-xr-x 9 shell shell 0 2019-02-28 20:22 ..
  $
  [1] + Terminated           \sleep 100 <&- >&- 2>&-

Test: manual (see above)
Change-Id: I3e05700a1e8ebc7fc9d192211dd9fc030cc40139
2019-02-28 13:06:07 -08:00
Elliott Hughes
a21f6cca06 Log when malloc functions fail.
This shouldn't happen often, and resulting failures can be hard to debug.

From the bionic unit tests now:

  W libc    : malloc(18446744073709551615) failed: returning null pointer
  W libc    : calloc(18446744073709551615, 100) failed: returning null pointer
  W libc    : calloc(1, 18446744073709551615) failed: returning null pointer
  W libc    : calloc(18446744073709551615, 18446744073709551615) failed: returning null pointer
  W libc    : calloc(2, 18446744073709551615) failed: returning null pointer
  W libc    : calloc(18446744073709551615, 2) failed: returning null pointer
  W libc    : memalign(4096, 18446744073709551615) failed: returning null pointer
  W libc    : realloc(0x0, 18446744073709551615) failed: returning null pointer
  W libc    : realloc(0x75d7526070, 18446744073709551615) failed: returning null pointer
  W libc    : reallocaray(0x0, 9223372036854775812, 2) failed: returning null pointer
  W libc    : reallocaray(0x0, 2, 9223372036854775812) failed: returning null pointer

Bug: http://b/12821450
Test: ran tests
Change-Id: Ib176814404f4ba1297416dd3e1edd721bf59aeed
2019-02-26 12:22:38 -08:00
Christopher Ferris
503c17bae7 Fix wrong variable reference.
Bug: 125891203

Test: Verify that setting the program property to "" doesn't trigger
Test: heapprofd.
Change-Id: Id27cbb92c7a120d02151e29be993f369230c2de8
2019-02-22 12:47:23 -08:00
Dan Albert
a535d3ca65 Make static ifunc resolvers optional.
Gold isn't emitting these symbols, so we don't necessarily have the
support for them (gold is still the default for most architectures in
the NDK).

Test: bionic static unit tests
Bug: None
Change-Id: Ifc360cb6c26571fb3f0309adb0faf0af7ee5b36f
2019-02-20 12:44:19 -08:00
Elliott Hughes
f84d0a95bd Merge "libasync_safe: stop clobbering other folks' identifiers." 2019-02-16 00:12:51 +00:00
Elliott Hughes
3019d78d4a libasync_safe: stop clobbering other folks' identifiers.
The log priorities and ids are in an NDK header, available to everyone.

Move CHECK into its own header for now. This would be better if it was
more like the <android-base/logging.h> CHECK family, but I don't have an
easy way to do that without lots of copy & paste, so punting for now.

Bug: https://issuetracker.google.com/issues/119713191
Test: boots
Change-Id: I4566be8a0a024fede0e2d257c98b908ec67af2a8
2019-02-14 14:23:13 -08:00
Christopher Ferris
2822856e98 Avoid heapprofd init when other hooks enabled.
All of the heapprofd code assumes that it's the only hook that
has been enabled. Enforce that by disallowing heapprofd from
enabling if malloc debug or malloc hooks have been enabled.

Test: Ran all unit tests (bionic/malloc hooks/malloc debug/perfetto).
Test: Enabled malloc debug ran perfetto integration tests and verified
Test: that an error message goes to the log.
Change-Id: I506fbf1c5b8e4052855531fa0d161f5de06e6c1a
2019-02-14 10:23:58 -08:00
Christopher Ferris
883144719a Fix hwasan build.
Test: hwasan builds properly.
Change-Id: I3c911da08d1925b797423671e3beb72770a934b0
2019-02-13 22:15:28 -08:00
Christopher Ferris
e4cdbc4754 Refactor malloc common into distinct pieces.
The pieces:
- The malloc common shared by static and dynamic code (malloc_common.cpp).
- The code for shared libraries that includes any dlopen'ing
  (malloc_common_dynamic.cpp).
- The implementation of perfetto's heapprofd (malloc_heapprofd.cpp).

This makes it easier to see what's going on in the many different areas.
It should also make it easier to add the allocation capping option.

Other related changes:
- Update the unit tests for android_mallopt. All of the current options
  don't work on static binaries, so make sure that is reflected in the test.
- A few names changes to make sure that all code is consistent.

Test: Ran tests (malloc hooks/malloc debug/perfetto/bionic unit tests).
Change-Id: I0893bfbc0f83d82506fac5d1f37cf92fbdef6f59
2019-02-12 14:19:07 -08:00
Ryan Prichard
808d176e7e Merge "Fix linker self-exec detection" 2019-02-08 22:33:11 +00:00
Ryan Prichard
1990ba5601 Fix linker self-exec detection
When the linker is invoked on itself, (`linker64 /system/bin/linker64`),
the linker prints an error, because self-invocation isn't allowed. The
current method for detecting self-invocation fails because the second
linker instance can crash in a constructor function before reaching
__linker_init.

Fix the problem by moving the error check into a constructor function,
which finishes initializing libc sufficiently to call async_safe_fatal.
The only important thing missing is __libc_sysinfo on 32-bit x86. The aux
vector isn't readily accessible, so use the fallback int 0x80.

Bug: http://b/123637025
Test: bionic unit tests (32-bit x86)
Change-Id: I8be6369e8be3938906628ae1f82be13e6c510119
2019-02-07 21:48:42 -08:00
Christopher Ferris
62e1e2c7e3 Modify malloc common function pointers.
Instead of every function being its own atomic, have a single
pointer that can be used to flip all pointers at once. This avoid cases
where the set of pointers can be in an partial switched state.

Also fix a few inconsistent naming of functions in the file.

Test: Ran unit tests (malloc debug, malloc hooks, perfetto).
Change-Id: I3f66da395414586a3fa87874d80dcdf5f702ed39
Merged-In: I3f66da395414586a3fa87874d80dcdf5f702ed39
(cherry picked from commit 77184aedaf)
2019-02-07 14:48:34 -08:00
Treehugger Robot
5569bc7d34 Merge "Eliminate ICU's .dat lookup from bionic" 2019-02-07 10:21:06 +00:00
Nikita Iashchenko
45f2d03ae5 Eliminate ICU's .dat lookup from bionic
Before this CL bionic did the following:

 * Finds the ICU .dat file with scandir()
 * Extracts the ICU version number from the file name. e.g. _63
 * dlopen() libicuuc
 * dlsym() necessary symbols, e.g. <symbol name>_<icu version>

Right now such ICU symbols are stored in libandroidicu.so and suffixed
with "_android", so it is responsible for "redirecting" to functions
with approriate version and we do not need to lookup the version on our
own. libicuuc is still available for NDK (and apps), and libandroidicu is
a subset of libicuuc and libicui18n.

After this CL bionic will do the following:

 * dlopen() libandroiicu
 * dlsym() <symbol_name>_android (without specific version suffix)

Bug: 122822987
Test: cts-tradefed run cts-dev -m CtsBionicTestCases
Change-Id: Iabd9f35b9c3462739fd2b18e60dcdc3e202031ac
2019-02-06 20:49:03 +00:00
Elliott Hughes
3912efa503 Merge "Pass caller names to __pthread_internal_find for better errors." 2019-02-04 16:42:27 +00:00
Treehugger Robot
c1d579798e Merge "Implement ifunc support for static executables." 2019-02-03 00:04:46 +00:00
Elliott Hughes
5bb113cba2 Pass caller names to __pthread_internal_find for better errors.
On http://b/122082295 we had this abort:

  12-27 15:29:31.237 10222 10814 10848 F libc    : invalid pthread_t 0xb1907960 passed to libc

This wasn't super helpful. We can do better. Now you get something like
this instead:

  03-27 02:34:58.754 25329 25329 W libc    : invalid pthread_t (0) passed to pthread_join

Test: adb shell crasher
Bug: http://b/123255692
Change-Id: I1d545665a233308480cc3747ec3120e2b6de0453
2019-02-01 16:31:10 -08:00
Peter Collingbourne
f1ed31ffe1 Increase the size of the shadow call stack guard region to 16MB.
Increasing the size of the guard region helps with the security of SCS,
but it's blocked on landing [1], which in turn is blocked on landing
[2]. Once those two CLs land we will be able to land this one.

[1] https://android-review.googlesource.com/c/platform/frameworks/av/+/837745
[2] https://android-review.googlesource.com/c/platform/bionic/+/818973

Bug: 118642754
Change-Id: I35409cbb6bfcd77e632567dd755376e345cfe67b
2019-01-31 14:37:34 -08:00
Florian Mayer
ccc0922653 Merge "Do not dlclose after failed reinit." 2019-01-30 17:26:18 +00:00
Florian Mayer
f671e036b9 Do not dlclose after failed reinit.
Update stale comment.

The reinitialization logic is tested in HeapprofdEndToEnd::ReInit in https://android.googlesource.com/platform/external/perfetto/+/master/src/profiling/memory/heapprofd_end_to_end_test.cc

Change-Id: Id496ee02e208d4f4cea7129b47ef327fb2bb67f2
2019-01-30 17:25:57 +00:00
Treehugger Robot
c676377cd3 Merge "Make trace end conform with other trace end prints" 2019-01-28 21:01:28 +00:00
Treehugger Robot
d5076ba63a Merge "Add tracepoints for pthread_create and pthread_join" 2019-01-28 21:01:24 +00:00
Ryan Prichard
ecdc451ccf Merge changes I3c9b1292,I05c28d6a,I788c4a95,If8cd798f,I1c8d1cd7, ...
* changes:
  Implement dynamic TLS accesses and allocation
  Implement TLS_DTPMOD and TLS_DTPREL relocations
  Ignore DT_TLSDESC_GOT / DT_TLSDESC_PLT
  Disable the dlfcn.dlopen_library_with_ELF_TLS test
  Add BionicAllocator::memalign
  Move the linker allocator into libc
  Replace some of linker_allocator's header includes
2019-01-28 19:32:59 +00:00
Philip Cuadra
77d0f90c7a Add tracepoints for pthread_create and pthread_join
Add additional tracepoints for clarity.

Test: cpatured trace with bionic, confirmed trace points
Change-Id: I4f9952c38a2637d53edb69ad99b43beb5a892da6
2019-01-28 10:59:02 -08:00
Philip Cuadra
7fc82c24ee Make trace end conform with other trace end prints
Add | to make bionic's trace end print match other trace end prints.

Test:  took systrace with bionic tag enabled
Change-Id: Ieabb139dd224aa8045be914f21c0432d42a93755
2019-01-28 10:48:49 -08:00
Florian Mayer
543b4013e7 Merge "Allow to reset malloc hooks." 2019-01-28 18:14:48 +00:00
Florian Mayer
db59b891ca Allow to reset malloc hooks.
This is used to prevent the additional indirection even after heap
profiling has finished, preventing any performance impact on processes
that are not currently being profiled.

Test: m
Test: flash sailfish
Test: try tearing down & re-enabling hooks

Bug: 120186127

Change-Id: Idc5988111a47870d2c093fd6a017b47e65f5616b
2019-01-28 15:01:50 +00:00
Ryan Prichard
16455b5100 Implement dynamic TLS accesses and allocation
Initialize a thread's DTV to an empty zeroed DTV. Allocate the DTV and
any ELF module's TLS segment on-demand in __tls_get_addr. Use a generation
counter, incremented in the linker, to signal when threads should
update/reallocate their DTV objects.

A generation count of 0 always indicates the constant zero DTV.

Once a DTV is allocated, it isn't freed until the thread exits, because
a signal handler could interrupt the fast path of __tls_get_addr between
accessing the DTV slot and reading a field of the DTV. Bionic keeps a
linked list of DTV objects so it can free them at thread-exit.

Dynamic TLS memory is allocated using a BionicAllocator instance in
libc_shared_globals. For async-signal safety, access to the
linker/libc-shared state is protected by first blocking signals, then by
acquiring the reader-writer lock, TlsModules::rwlock. A write lock is
needed to allocate or free memory.

In pthread_exit, unconditionally block signals before freeing dynamic
TLS memory or freeing the shadow call stack.

ndk_cruft.cpp: Avoid including pthread_internal.h inside an extern "C".
(The header now includes a C++ template that doesn't compile inside
extern "C".)

Bug: http://b/78026329
Bug: http://b/123094171
Test: bionic unit tests
Change-Id: I3c9b12921c9e68b33dcc1d1dd276bff364eff5d7
2019-01-25 17:53:01 -08:00
Peter Collingbourne
7a0f04cb8d Implement ifunc support for static executables.
A static executable is almost entirely statically relocated by the
linker, with the exception of IRELATIVE relocations, which must be
resolved by libc by enumerating the relocations using the special
linker-defined symbols __rela?_iplt_{start,end}. This patch implements
ifunc support by enumerating the relocations in this way.

Bug: 112482891
Test: /data/nativetest{,64}/bionic-unit-tests-static/bionic-unit-tests-static on walleye_hwasan-userdebug
Change-Id: Ia5522a190da0b86e095b141d5d4e68dd7dd4b695
2019-01-25 16:25:27 -08:00
Ryan Prichard
96773a2daf Add BionicAllocator::memalign
Bionic needs this functionality to allocate a TLS segment with greater
than 16-byte alignment. For simplicity, this allocator only supports up
to one page of alignment.

The memory layout changes slightly when allocating an object of exactly
PAGE_SIZE alignment. Instead of allocating the page_info header at the
start of the page containing the pointer, it is allocated at the start
of the preceding page.

Bug: http://b/78026329
Test: linker-unit-tests{32,64}
Change-Id: I1c8d1cd7ca72d113bced5ee15ba8d831426b0081
2019-01-25 15:31:35 -08:00