Merge "Add some slack at the end of large allocations when target SDK level < S." am: 6ba27e04df

Original change: https://android-review.googlesource.com/c/platform/bionic/+/1621044

MUST ONLY BE SUBMITTED BY AUTOMERGER

Change-Id: I4c153c044cfa38e432e3a1e7b6eaca6a7cb8e761
This commit is contained in:
Peter Collingbourne 2021-03-09 01:42:11 +00:00 committed by Automerger Merge Worker
commit 34dcce7014
7 changed files with 41 additions and 1 deletions

View file

@ -132,6 +132,14 @@ void __libc_init_fork_handler() {
pthread_atfork(arc4random_fork_handler, _thread_arc4_unlock, _thread_arc4_unlock); pthread_atfork(arc4random_fork_handler, _thread_arc4_unlock, _thread_arc4_unlock);
} }
extern "C" void scudo_malloc_set_add_large_allocation_slack(int add_slack);
__BIONIC_WEAK_FOR_NATIVE_BRIDGE void __libc_set_target_sdk_version(int target __unused) {
#if defined(USE_SCUDO)
scudo_malloc_set_add_large_allocation_slack(target < __ANDROID_API_S__);
#endif
}
__noreturn static void __early_abort(int line) { __noreturn static void __early_abort(int line) {
// We can't write to stdout or stderr because we're aborting before we've checked that // We can't write to stdout or stderr because we're aborting before we've checked that
// it's safe for us to use those file descriptors. We probably can't strace either, so // it's safe for us to use those file descriptors. We probably can't strace either, so

View file

@ -66,4 +66,6 @@ __LIBC_HIDDEN__ void __libc_init_AT_SECURE(char** envp);
// pthread_atfork may call malloc() during its once-init. // pthread_atfork may call malloc() during its once-init.
__LIBC_HIDDEN__ void __libc_init_fork_handler(); __LIBC_HIDDEN__ void __libc_init_fork_handler();
__LIBC_HIDDEN__ void __libc_set_target_sdk_version(int target);
#endif #endif

View file

@ -46,6 +46,7 @@
#include <elf.h> #include <elf.h>
#include "libc_init_common.h" #include "libc_init_common.h"
#include "private/bionic_defs.h"
#include "private/bionic_elf_tls.h" #include "private/bionic_elf_tls.h"
#include "private/bionic_globals.h" #include "private/bionic_globals.h"
#include "platform/bionic/macros.h" #include "platform/bionic/macros.h"
@ -107,6 +108,8 @@ static void __libc_preinit_impl() {
__libc_shared_globals()->unload_hook = __hwasan_library_unloaded; __libc_shared_globals()->unload_hook = __hwasan_library_unloaded;
#endif #endif
__libc_shared_globals()->set_target_sdk_version_hook = __libc_set_target_sdk_version;
netdClientInit(); netdClientInit();
} }

View file

@ -400,6 +400,7 @@ extern "C" int android_get_application_target_sdk_version() {
extern "C" void android_set_application_target_sdk_version(int target) { extern "C" void android_set_application_target_sdk_version(int target) {
g_target_sdk_version = target; g_target_sdk_version = target;
__libc_set_target_sdk_version(target);
} }
// This function is called in the dynamic linker before ifunc resolvers have run, so this file is // This function is called in the dynamic linker before ifunc resolvers have run, so this file is

View file

@ -95,9 +95,10 @@ struct libc_shared_globals {
TlsModules tls_modules; TlsModules tls_modules;
BionicAllocator tls_allocator; BionicAllocator tls_allocator;
// Values passed from the HWASan runtime (via libc.so) to the loader. // Values passed from libc.so to the loader.
void (*load_hook)(ElfW(Addr) base, const ElfW(Phdr)* phdr, ElfW(Half) phnum) = nullptr; void (*load_hook)(ElfW(Addr) base, const ElfW(Phdr)* phdr, ElfW(Half) phnum) = nullptr;
void (*unload_hook)(ElfW(Addr) base, const ElfW(Phdr)* phdr, ElfW(Half) phnum) = nullptr; void (*unload_hook)(ElfW(Addr) base, const ElfW(Phdr)* phdr, ElfW(Half) phnum) = nullptr;
void (*set_target_sdk_version_hook)(int target) = nullptr;
// Values passed from the linker to libc.so. // Values passed from the linker to libc.so.
const char* init_progname = nullptr; const char* init_progname = nullptr;

View file

@ -31,6 +31,8 @@
#include <android/api-level.h> #include <android/api-level.h>
#include <android/fdsan.h> #include <android/fdsan.h>
#include "private/bionic_globals.h"
#include "linker.h" #include "linker.h"
static std::atomic<int> g_target_sdk_version(__ANDROID_API__); static std::atomic<int> g_target_sdk_version(__ANDROID_API__);
@ -45,6 +47,9 @@ void set_application_target_sdk_version(int target) {
if (target < 30) { if (target < 30) {
android_fdsan_set_error_level_from_property(ANDROID_FDSAN_ERROR_LEVEL_WARN_ONCE); android_fdsan_set_error_level_from_property(ANDROID_FDSAN_ERROR_LEVEL_WARN_ONCE);
} }
if (__libc_shared_globals()->set_target_sdk_version_hook) {
__libc_shared_globals()->set_target_sdk_version_hook(target);
}
} }
int get_application_target_sdk_version() { int get_application_target_sdk_version() {

View file

@ -46,6 +46,7 @@
#if defined(__BIONIC__) #if defined(__BIONIC__)
#include "SignalUtils.h" #include "SignalUtils.h"
#include "dlext_private.h"
#include "platform/bionic/malloc.h" #include "platform/bionic/malloc.h"
#include "platform/bionic/mte.h" #include "platform/bionic/mte.h"
@ -1351,3 +1352,22 @@ TEST(malloc, disable_mte) {
GTEST_SKIP() << "bionic extension"; GTEST_SKIP() << "bionic extension";
#endif #endif
} }
TEST(malloc, allocation_slack) {
#if defined(__BIONIC__)
bool allocator_scudo;
GetAllocatorVersion(&allocator_scudo);
if (!allocator_scudo) {
GTEST_SKIP() << "scudo allocator only test";
}
// Test that older target SDK levels let you access a few bytes off the end of
// a large allocation.
android_set_application_target_sdk_version(29);
auto p = std::make_unique<char[]>(131072);
volatile char *vp = p.get();
volatile char oob ATTRIBUTE_UNUSED = vp[131072];
#else
GTEST_SKIP() << "bionic extension";
#endif
}