android: Fix 32-bit test to build at API [21, 24) with unified headers

Although API 21 introduced support for 64-bit off_t in many system calls
or their wrappers, <stdio.h> support for 64-bit off_t is absent until
API 24.

This is a partial revert of 5969d6b1eb22, because with this more
targeted fix applying only to gtest, the rest of Crashpad will work with
a 64-bit off_t even at API levels lacking NDK support by going through
the mmap() shim in compat.

This includes a mini_chromium update to 96e32dd499a4.

85cbec19ffc0 fuchsia: Make EINTR macros no-ops
fbf410cd4d40 fuchsia: Use koid instead of getpid() for process field in
             logging
96e32dd499a4 Revert "android: Don’t use _FILE_OFFSET_BITS=64 until API
             21"

Bug: crashpad:211
Change-Id: I34c3c8b42eb315605e6775962b44c3c4573b7462
Reviewed-on: https://chromium-review.googlesource.com/811204
Commit-Queue: Mark Mentovai <mark@chromium.org>
Reviewed-by: Joshua Peraza <jperaza@chromium.org>
This commit is contained in:
Mark Mentovai 2017-12-06 12:53:54 -05:00 committed by Commit Bot
parent 6719610b8c
commit 612237a032
5 changed files with 164 additions and 1 deletions

2
DEPS
View File

@ -28,7 +28,7 @@ deps = {
'5e2b3ddde7cda5eb6bc09a5546a76b00e49d888f',
'crashpad/third_party/mini_chromium/mini_chromium':
Var('chromium_git') + '/chromium/mini_chromium@' +
'88e056258a01450b07414642fa5fb98493c1f6ce',
'96e32dd499a49bf6f9cdabf73e8ead6a89f0f634',
'crashpad/third_party/zlib/zlib':
Var('chromium_git') + '/chromium/src/third_party/zlib@' +
'13dc246a58e4b72104d35f9b1809af95221ebda7',

103
compat/android/sys/mman.cc Normal file
View File

@ -0,0 +1,103 @@
// Copyright 2017 The Crashpad Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <sys/mman.h>
#include <dlfcn.h>
#include <errno.h>
#include <stdint.h>
#include <unistd.h>
#if defined(__USE_FILE_OFFSET64) && __ANDROID_API__ < 21
// Bionic has provided a wrapper for __mmap2() since the beginning of time. See
// bionic/libc/SYSCALLS.TXT in any Android version.
extern "C" void* __mmap2(void* addr,
size_t size,
int prot,
int flags,
int fd,
size_t pgoff);
namespace {
template <typename T>
T Align(T value, uint8_t alignment) {
return (value + alignment - 1) & ~(alignment - 1);
}
// Adapted from Android 8.0.0 bionic/libc/bionic/mmap.cpp.
void* LocalMmap64(void* addr,
size_t size,
int prot,
int flags,
int fd,
off64_t offset) {
constexpr int kMmap2Shift = 12;
if (offset < 0 || (offset & ((1UL << kMmap2Shift) - 1)) != 0) {
errno = EINVAL;
return MAP_FAILED;
}
const size_t rounded = Align(size, getpagesize());
if (rounded < size || rounded > PTRDIFF_MAX) {
errno = ENOMEM;
return MAP_FAILED;
}
const bool is_private_anonymous =
(flags & (MAP_PRIVATE | MAP_ANONYMOUS)) == (MAP_PRIVATE | MAP_ANONYMOUS);
const bool is_stack_or_grows_down =
(flags & (MAP_STACK | MAP_GROWSDOWN)) != 0;
void* const result =
__mmap2(addr, size, prot, flags, fd, offset >> kMmap2Shift);
static bool kernel_has_MADV_MERGEABLE = true;
if (result != MAP_FAILED && kernel_has_MADV_MERGEABLE &&
is_private_anonymous && !is_stack_or_grows_down) {
const int saved_errno = errno;
const int rc = madvise(result, size, MADV_MERGEABLE);
if (rc == -1 && errno == EINVAL) {
kernel_has_MADV_MERGEABLE = false;
}
errno = saved_errno;
}
return result;
}
} // namespace
extern "C" {
void* mmap(void* addr, size_t size, int prot, int flags, int fd, off_t offset) {
// Use the systems mmap64() wrapper if available. It will be available on
// Android 5.0 (“Lollipop”) and later.
using Mmap64Type = void* (*)(void*, size_t, int, int, int, off64_t);
static const Mmap64Type mmap64 =
reinterpret_cast<Mmap64Type>(dlsym(RTLD_DEFAULT, "mmap64"));
if (mmap64) {
return mmap64(addr, size, prot, flags, fd, offset);
}
// Otherwise, use the local implementation, which should amount to exactly the
// same thing.
return LocalMmap64(addr, size, prot, flags, fd, offset);
}
} // extern "C"
#endif // defined(__USE_FILE_OFFSET64) && __ANDROID_API__ < 21

43
compat/android/sys/mman.h Normal file
View File

@ -0,0 +1,43 @@
// Copyright 2017 The Crashpad Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef CRASHPAD_COMPAT_ANDROID_SYS_MMAN_H_
#define CRASHPAD_COMPAT_ANDROID_SYS_MMAN_H_
#include_next <sys/mman.h>
#include <android/api-level.h>
#include <sys/cdefs.h>
// Theres no mmap() wrapper compatible with a 64-bit off_t for 32-bit code
// until API 21 (Android 5.0/“Lollipop”). A custom mmap() wrapper is provided
// here. Note that this scenario is only possible with NDK unified headers.
//
// https://android.googlesource.com/platform/bionic/+/0bfcbaf4d069e005d6e959d97f8d11c77722b70d/docs/32-bit-abi.md#is-32_bit-1
#if defined(__USE_FILE_OFFSET64) && __ANDROID_API__ < 21
#ifdef __cplusplus
extern "C" {
#endif
void* mmap(void* addr, size_t size, int prot, int flags, int fd, off_t offset);
#ifdef __cplusplus
} // extern "C"
#endif
#endif // defined(__USE_FILE_OFFSET64) && __ANDROID_API__ < 21
#endif // CRASHPAD_COMPAT_ANDROID_SYS_MMAN_H_

View File

@ -26,6 +26,8 @@
'android/linux/prctl.h',
'android/linux/ptrace.h',
'android/sched.h',
'android/sys/mman.cc',
'android/sys/mman.h',
'android/sys/syscall.h',
'android/sys/user.h',
'linux/signal.h',

View File

@ -40,6 +40,21 @@
'cflags!': [
'-Wexit-time-destructors',
],
'conditions': [
['OS=="android" and android_api_level!="" and android_api_level<24', {
'defines!': [
# Although many system interfaces are available to 32-bit code with
# 64-bit off_t at API 21, the routines in <stdio.h> are not until API
# 24. gtest doesnt make use of these functions directly, but can
# reach them indirectly via the C++ standard library. Disable 64-bit
# off_t prior to API 24 so that these uses can work. Since nothing
# dependent on the size of off_t should escape gtests own API, this
# should be safe even in a program that otherwise uses a 64-bit off_t.
'_FILE_OFFSET_BITS=64',
],
}],
],
},
'targets': [