Commit graph

9962 commits

Author SHA1 Message Date
Elliott Hughes
4a6899ce0c Add ffsl(3), ffsll(3).
POSIX added these GNU extensions for issue 8.

I've made these always inline without the usual "until API level X"
proviso because they're single instructions that the compiler can inline
and there's really no point providing these if they add function call
overhead --- everyone should just use __builtin_ffs() and friends
instead in that case.

Bug: https://austingroupbugs.net/view.php?id=617
Test: treehugger
Change-Id: I33fc4b8648ea25917329e81c1b4c60eb9a66d667
2020-03-26 11:43:05 -07:00
Peter Collingbourne
2528dab741 Clean up untag_address.
We had two copies of this function, one (not quite correct) in tests/utils.h
and another in bionic/macros.h. Delete the former and have the users include
the latter.

Also, create an overload of the function that takes a uintptr_t, which will
be useful for out-of-process scenarios such as the MTE support in debuggerd.

Bug: 135772972
Change-Id: Ia3c2652c97797663146b3f05fa786afe09f7ea97
2020-03-20 15:35:54 -07:00
Mitch Phillips
d92b03887d Merge "[GWP-ASan] Intercept calloc() again." 2020-03-16 20:18:04 +00:00
Mitch Phillips
32abb0b81e [GWP-ASan] Intercept calloc() again.
Bug: 150456936
Test: N/A

Change-Id: Id273e1811f9919c8bdbdbf0ad850421e7c1cd8da
2020-03-16 20:17:39 +00:00
Florian Mayer
c84d295f7c Merge "Add explicit state for heapprofd hooking." 2020-03-12 17:12:17 +00:00
Florian Mayer
fdd5eb1977 Add explicit state for heapprofd hooking.
The double hooking is currently controlled by two separate atomic
booleans. In this state, it is very hard to guard against very subtle
race conditions. Adding an explicit enum to encode the state makes it
easier to reason about the behaviour of the program.

Also introduce a MaybeModifyGlobals to consistently guard accesses to
globals. We would sometimes modify them without swapping the
gGlobalsMutating, introducing a window for race with
HandleHeapprofdSignal.

Test: while true; do atest HeapprofdCtsTest; done
Test: flash blueline and manually test profile
Bug: 150741354
Change-Id: I77102b0bd906b8ec2d6806bd43ba6cbf7191e37c
2020-03-11 10:21:27 -07:00
Ryan Prichard
7aa6ba4aec Merge "Rewrite __cxa_atexit / __cxa_finalize" 2020-03-09 21:00:58 +00:00
Treehugger Robot
7d97c36b3f Merge "Add 29 to stubs.versions of libc/libdl/libm" 2020-03-07 08:44:32 +00:00
Ryan Prichard
afa983c8d4 Rewrite __cxa_atexit / __cxa_finalize
Simplify:
 - Use a single memory-mapped region to hold the table of destructors.
   Double its capacity each time it is expanded.
 - Add a recompaction pass at the end of __cxa_finalize that shifts
   entries forward and uses madvise to clean pages.

Bug: http://b/148055738
Test: bionic-unit-tests
Change-Id: Ieb9da2b88640a8a5277d217b43826b5b7e246781
2020-03-06 21:04:32 -08:00
Chih-Hung Hsieh
fa658eb094 Fix bugprone-macro-parentheses warnings
Bug: 150783705
Test: WITH_TIDY=1 make
Change-Id: If47b7adaa5f3ba3c518fb0be48f0ffdc81d204b2
2020-03-04 13:22:05 -08:00
Christopher Ferris
93517d1a27 Merge "Update the scudo wrapper for bionic changes." 2020-03-03 18:52:00 +00:00
Mitch Phillips
0d303b42b5 Merge "[GWP-ASan] [heapprofd] Fix infinite recursion between malloc-racing threads." 2020-02-28 23:18:14 +00:00
Christopher Ferris
2c447b0586 Update the scudo wrapper for bionic changes.
Bug: 137795072

Test: Built using jemalloc, and verified that the two processes that
Test: use libc_scudo do not crash.
Change-Id: Icf773b656d7e2bcdf41b4979e9b6cd980b02d34d
2020-02-28 14:34:13 -08:00
Treehugger Robot
95c772f734 Merge "[GWP-ASan] Don't intercept calloc." 2020-02-28 21:19:51 +00:00
Peter Collingbourne
ddedd9828b Merge "Switch to the arm-optimized-routines string routines on aarch64 where possible." 2020-02-28 21:15:24 +00:00
Mitch Phillips
5c65e87579 [GWP-ASan] Don't intercept calloc.
See linked bug for more information.

Bug: 150456936
Test: N/A
Change-Id: Ibdd4331d005cf488e24c238e67e46cee13198334
2020-02-28 16:58:23 +00:00
Peter Collingbourne
2dd382d9b6 Merge "Update bionic to the v2 MTE patch set." 2020-02-28 16:43:22 +00:00
Mitch Phillips
449c26a47d [GWP-ASan] [heapprofd] Fix infinite recursion between malloc-racing threads.
When the heapprofd lazy-initialization dispatch table is installed, two
threads can call malloc() at the same time. One will do the
lazy-initialization, the other will fail the atomic_exchange of
gHeapprofdInitHookInstalled and go to system malloc. The system malloc
still contains the lazy-init malloc, and will recurse.

Fix it so the second thread goes directly to the previous dispatch
table, or to the system allocator directly (instead of through libc
malloc()).

Bug: 150085813
Test: atest perfetto_integrationtests
Test: TracedPerfCtsTest.SystemWideDebuggableApp on cuttlefish x86.
Change-Id: Ia85ad619a0d5e3f558136d84c34dbada4e8b845d
2020-02-28 08:33:27 -08:00
Bram Bonné
188f87a1b6 Merge "Cleans up logging and comments in ifaddrs." 2020-02-28 09:07:20 +00:00
Peter Collingbourne
80af1b0a2c Update bionic to the v2 MTE patch set.
The values of some constants have changed, and the tag mask has changed from
being an exclusion mask to an inclusion mask.

Bug: 135772972
Change-Id: I322fceae7003bee6b391e7187194ed4c0cb28c58
2020-02-28 01:55:20 +00:00
Peter Collingbourne
b8d1348788 Handle the alternate signal stack correctly in android_unsafe_frame_pointer_chase.
If an alternate signal stack is set and the frame record is in bounds of
that stack, we need to use the top of the alternate signal stack for bounds
checking rather than the normal stack.

Bug: 150215618
Change-Id: I78b760d61b27da44f8e0cfee3fe94a791011fe58
2020-02-27 10:44:15 -08:00
Bram Bonné
918bd72225 Cleans up logging and comments in ifaddrs.
To prevent unnecessary SELinux denials, the check referred to by the
comment is kept in place. This allows us to keep auditing the
SELinux denial in order to catch apps that intentionally send
RTM_GETLINK messages.

Fix: 141455849
Test: atest bionic-unit-tests-static
Test: atest NetworkInterfaceTest
Change-Id: I41e5ad6f071c820a8246177a0c629b3be788a942
2020-02-27 16:02:30 +01:00
Jooyung Han
26ddc4dccf Add 29 to stubs.versions of libc/libdl/libm
Stubs of version 29 should be provided to those APEX modules targeting
previous SDK release.

Bug: 145796956
Test: m
Change-Id: I9454fbf81377aba25e75a6fdfc77cbb070eaadde
2020-02-27 09:57:50 +00:00
Mitch Phillips
5f91bf42e3 [GWP-ASan] [heapprofd] Nullptr deref in dispatch.
Heapprofd uses an ephemeral dispatch table to ensure that GWP-ASan can
be called during heapprofd initialization. Previously, we grabbed the
backup dispatch table from the globals, which is wrong if GWP-ASan isn't
installed (as the malloc_dispatch_table is linker-initialised and not
set to a known good value if a malloc hooker isn't used).

Instead, grab the correct ephemeral dispatch table. When GWP-ASan is
installed, use a copy of its table as the dispatch. When nothing is
installed, fall back to using the native allocator dispatch.

Bug: 135634846
Bug: 150085813
Test: atest perfetto_integrationtests
Test: TracedPerfCtsTest.SystemWideDebuggableApp on cuttlefish x86.

Change-Id: I4beaf9192acadbe00adc02de2e0b0aab6f7a2190
2020-02-26 16:50:58 -08:00
Peter Collingbourne
337a5b3f9a Switch to the arm-optimized-routines string routines on aarch64 where possible.
This includes optimized strrchr and strchrnul routines, and an MTE-compatible
strlen routine.

Bug: 135772972
Change-Id: I48499f757cdc6d3e77e5649123d45b17dfa3c6b0
2020-02-25 13:11:55 -08:00
Ryan Savitski
1dc4122a1d Override SIGSYS during profiling signal handler
This is a best-effort mitigation for potential crashes when the
profiling signal handler is triggered within certain secomp'd processes.
In particular, we're working around cases where the seccomp policy
doesn't allow some of the handler syscalls, and has a crashing
disposition towards violations via SECCOMP_RET_TRAP, plus a crashing
SIGSYS handler. While not general, this covers the configurations seen
in practice on Android (which are all using minijail in the same way).

By overriding the SIGSYS handling for the duration of the profiling
handler, we can instead receive such SIGSYS signals, and instead recover
from them in a non-crashing manner (the handler is responsible for
filling the syscall return register, since the syscall itself was
skipped).

For simplicity, we're swallowing all SIGSYS signals during this window,
without trying to figure out whether they're something that could be
caused by the profiling signal handler. I've quite convinced myself that
-ENOSYS seems to be safe to return to all of bionic's syscall wrappers
across the four architectures (looking at gensyscalls + the
special-cases like vfork and clone). It is theoretically possible for
all kinds of conflicting (ab)uses of SIGSYS to exist, but I'm assuming
it's not a realistic concern until proven otherwise.

Tested: manually sigqueue'd configstore on crosshatch, confirmed that
        the SIGSYS override log was printed, and the process did not
        crash (as it does on master).
Bug: 149328505
Change-Id: Iab8f09e51169807c9d3e1e0bcfd042f09f7df6a4
2020-02-25 19:00:18 +00:00
Elliott Hughes
33318f5197 Merge "Add a libc wrapper for statx(2)." 2020-02-25 16:37:09 +00:00
Elliott Hughes
521dab3b36 Merge "Make the HWCAP/HWCAP2 constants available from <sys/auxv.h>." 2020-02-25 16:36:26 +00:00
Bram Bonné
9f8e1d52cb Merge "Try to resolve interface names before removing nameless interfaces." 2020-02-25 08:59:49 +00:00
Elliott Hughes
c5d9036f80 Make the HWCAP/HWCAP2 constants available from <sys/auxv.h>.
Noticed by ARM when adding BTI support to the linker.

Test: treehugger
Change-Id: If3b87d0bd4682ff478b7805251fc9a88a7f439f9
2020-02-24 09:52:14 -08:00
Bram Bonné
14e5c3c3b6 Try to resolve interface names before removing nameless interfaces.
For interfaces that didn't get their name set, try to resolve their name
using if_indextoname first, before removing them from the list.

This allows getifaddrs() to keep returning interfaces that only have an
IPv6 address set (as opposed to the previous behavior where only
interfaces with an IPv4 address would be returned).

Change-Id: I0e4e6611948b12794cd3e354538f2964fbf31078
Fix: 148886805
Bug: 141455849
Test: atest NetworkInterfaceTest
Test: atest bionic-unit-tests-static
Test: atest IpSecManagerTunnelTest
2020-02-24 10:56:57 +01:00
Elliott Hughes
733cedd1c4 Add a libc wrapper for statx(2).
Bug: http://b/127675384
Bug: http://b/146676114
Test: treehugger
Change-Id: I844edc12f62717e579870a040cf03dfe60dc280b
2020-02-23 11:36:53 -08:00
Elliott Hughes
5403bd647b Fix wrong link in getauxval(3) doc comment.
Test: treehugger
Change-Id: I8646251edaa07fa8682127428f4fdb1fa0cf80e5
2020-02-21 16:23:33 -08:00
Kiyoung Kim
fcbdba22ab Merge "Rename APEX Namespace" 2020-02-21 01:04:37 +00:00
Peter Collingbourne
45819dde10 Allow android_mallopt(M_SET_HEAP_TAGGING_LEVEL) to control scudo heap tagging.
The tag level may now be async, which is now the default. When the tag level
is set to none, memory tagging is disabled in the allocator using the new
API proposed in https://reviews.llvm.org/D70762 .

Bug: 135772972
Change-Id: I847f5822a70913c446ed9ffa13792177bbfc96af
2020-02-20 15:25:08 -08:00
Kiyoung Kim
8116b70fee Rename APEX Namespace
Current APEX Namespace is named with APEX name itself, which also uses
.(dot) so linker configuration can keep the syntax safe.

For example, if there are APEX modules named 'A' and 'A.link.A', then
'namespace.A.link.A.link.A = a.so' phrase can be ambiguous from the
linker. To allow any additional linker syntax in the future, we should
avoid dot separator from the namespace name.

Bug: 148826508
Test: m -j passed
Test: boot succeeded from cuttlefish and walleye
Change-Id: Ic3fe396aef6366fc6c7a0677bc7f92a57fd4e229
2020-02-20 01:27:35 +00:00
Mitch Phillips
2121f60d97 Merge "[GWP-ASan] Export GWP-ASan regions to libdebuggerd." 2020-02-19 20:35:10 +00:00
George Burgess IV
db5c45b457 Merge "fortify: simplify strlen for LLVM's new optimization" 2020-02-19 18:12:31 +00:00
Mitch Phillips
4c9293f29a [GWP-ASan] Export GWP-ASan regions to libdebuggerd.
Exports GWP-ASan allocator information callbacks to libdebuggerd so that
tombstoned can get information from the GWP-ASan allocator in the case
of a crash.

Bug: 135634846
Test: atest bionic-unit-tests

Change-Id: Ie16426af55602fb2a76c4e69217773354c365843
2020-02-19 09:01:27 -08:00
Mitch Phillips
679d8e19cf Merge "[GWP-ASan] Remove log line from init." 2020-02-19 01:24:49 +00:00
Mitch Phillips
4d62ffc5b1 [GWP-ASan] Remove log line from init.
The notification that GWP-ASan is enabled causes ART tests to break.
Remove the log for now.

Bug: 135634846
Bug: 149790891
Test: (Attempt to fix the ART tests).
Change-Id: I8a7751a838a64f160b3b7b9f07752bb64644b9db
2020-02-18 17:16:23 -08:00
Elliott Hughes
1c2efaaa11 Merge "Remove unused declarations." 2020-02-19 01:01:51 +00:00
Peter Collingbourne
6ed60cc1ab Merge "Allow nested usage of ScopedDisableMTE." 2020-02-18 22:41:17 +00:00
Mitch Phillips
f73778abae Merge "[GWP-ASan] Enable GWP-ASan w/ process sampling ~1%." 2020-02-18 21:47:48 +00:00
Elliott Hughes
36359f47a2 Remove unused declarations.
Test: treehugger
Change-Id: Ia96dc01df0d26e2e3023bebab23de82ca8e389f7
2020-02-18 13:25:22 -08:00
Peter Collingbourne
c8cef93f24 Allow nested usage of ScopedDisableMTE.
An upcoming change to scudo will cause us to start calling
android_unsafe_frame_pointer_chase() from within the allocator. Since this
function uses ScopedDisableMTE, this would otherwise make it unsafe to use
the allocator from within ScopedDisableMTE. This seems like an unreasonable
restriction, so make ScopedDisableMTE save the PSTATE.TCO state in the
constructor and restore it in the destructor.

Bug: 135772972
Change-Id: I47e18d5fb2929efd5a58676488180cd85731007b
2020-02-18 13:13:57 -08:00
Mitch Phillips
3d74451cf1 Merge "[GWP-ASan] [heapprofd] Use ephemeral dispatch table when death prof." 2020-02-18 20:13:11 +00:00
Mitch Phillips
0083b0fcb4 [GWP-ASan] Enable GWP-ASan w/ process sampling ~1%.
This patch enables GWP-ASan with process sampling.

**Note**: If you are visiting this patch because this broke a test or
otherwise is causing failures, please contact mitchp@ directly (or
respond to this patchset). GWP-ASan is designed to cause heap-based
memory safety bugs to manifest in SEGV on a sampled basis.

Bug: 135634846
Test: atest bionic-unit-tests gwp_asan_unittest
Change-Id: I58ca9373def105fdd718cf283482b3220b770698
2020-02-18 09:18:15 -08:00
Mitch Phillips
c03856c58e [GWP-ASan] [heapprofd] Use ephemeral dispatch table when death prof.
GWP-ASan + heapprofd don't currently play nice together in some
circumstances. heapprofd thinks it's still an only child, and refuses to
accept the existence of its little brother, GWP-ASan.

If GWP-ASan is installed before heapprofd, then heapprofd is *required*
to respect that libc has a favourite child. If an allocation/free is passed
to heapprofd, then heapprofd *must* (eventually) pass that allocation/free to
GWP-ASan. If heapprofd doesn't do this, then a free() of a GWP-ASan
allocation can be passed to the system allocator.

This can happen in two places right now:
 1. The heapprofd hooks simply clobber any trace of what was
 previously in the default_dispatch_table when enabled through the
 heapprofd signal.
 2. Heapprofd can die when the system is under significant pressure.
 Some pipes can timeout, which ends up in the client calling ShutdownLazy()
 -> mallopt(M_RESET_HOOKS) -> DispatchReset(). This also clobbers any
 trace of the previous default_dispatch_table.

To fix both these problems, we fix heapprofd to restore the previous
default_dispatch_table whenever either circumstance happens. We do some
tricky copying to avoid race conditions on the malloc_dispatch_table in
fixing #1.

Bug: 135634846
Test: Run HeapprofdEndToEnd.NativeProfilingActiveAtProcessExit/ForkMode
a significant number of times with large amounts of system pressure (I
just run bionic-unit-tests-scudo in parallel). You will see some test
failures where heapprofd died due to system pressure, but never a death
from the allocator. Tests should never fail when the system isn't under
immense pressure.

Change-Id: I20ab340d4bdc35d6d1012da5ee1a25634428d097
2020-02-17 15:29:33 -08:00
Elliott Hughes
56a9fda610 Remove dead code.
We haven't built any of this for years.

Test: treehugger
Change-Id: I3f8a85e9530af68587f47931d850eb60631a9481
2020-02-13 22:25:02 -08:00