From 777634b1ebad7149d0af41686f1f36126b4ea984 Mon Sep 17 00:00:00 2001 From: Mark Mentovai Date: Tue, 22 Nov 2016 14:27:51 -0500 Subject: [PATCH] Use ADDRESS_SANITIZER instead of __has_feature(address_sanitizer) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit __has_feature() is a Clang-ism not implemented by GCC. base/compiler_specific.h provides a HAS_FEATURE() macro that always returns 0 when __has_feature() is not implemented. Use this macro for compatibility with GCC and other compilers that do not implement this Clang extension. http://clang.llvm.org/docs/LanguageExtensions.html#has-feature-and-has-extension For GCC’s Address Sanitizer implementation, test the __SANITIZE_ADDRESS__ macro that it provides as an alternative to __has_feature(address_sanitizer). Note that in Chrome builds, ADDRESS_SANITIZER is pushed in by the build system. The definition of ADDRESS_SANITIZER provides another way for that macro to be set. It’s supplementary, not exclusive. https://chromium.googlesource.com/chromium/src/+/cb33b243721b0e7dbdfa8c4301702e319360e88d/build/config/BUILD.gn#118 BUG=crashpad:30 Change-Id: I5c3145d29bbc966925369c03a37b1ecb5622a004 Reviewed-on: https://chromium-review.googlesource.com/413109 Reviewed-by: Robert Sesek --- client/capture_context_mac_test.cc | 6 ++++-- client/crashpad_info.cc | 5 +++-- util/misc/address_sanitizer.h | 28 ++++++++++++++++++++++++++++ util/util.gyp | 1 + 4 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 util/misc/address_sanitizer.h diff --git a/client/capture_context_mac_test.cc b/client/capture_context_mac_test.cc index 436ac5ad..1904b2b6 100644 --- a/client/capture_context_mac_test.cc +++ b/client/capture_context_mac_test.cc @@ -21,6 +21,7 @@ #include "build/build_config.h" #include "gtest/gtest.h" +#include "util/misc/address_sanitizer.h" #include "util/misc/implicit_cast.h" namespace crashpad { @@ -103,13 +104,14 @@ void TestCaptureContext() { // captured program counter should be slightly greater than or equal to the // reference program counter. uintptr_t pc = ProgramCounterFromContext(context_1); -#if !__has_feature(address_sanitizer) + +#if !defined(ADDRESS_SANITIZER) // AddressSanitizer can cause enough code bloat that the “nearby” check would // likely fail. const uintptr_t kReferencePC = reinterpret_cast(TestCaptureContext); EXPECT_LT(pc - kReferencePC, 64u); -#endif +#endif // !defined(ADDRESS_SANITIZER) // Declare sp and context_2 here because all local variables need to be // declared before computing the stack pointer reference value, so that the diff --git a/client/crashpad_info.cc b/client/crashpad_info.cc index e8a6a9ee..e517f7b1 100644 --- a/client/crashpad_info.cc +++ b/client/crashpad_info.cc @@ -14,6 +14,7 @@ #include "client/crashpad_info.h" +#include "util/misc/address_sanitizer.h" #include "util/stdlib/cxx.h" #if defined(OS_MACOSX) @@ -72,14 +73,14 @@ __attribute__(( #error Port #endif // !defined(OS_MACOSX) && !defined(OS_LINUX) && !defined(OS_ANDROID) -#if __has_feature(address_sanitizer) +#if defined(ADDRESS_SANITIZER) // AddressSanitizer would add a trailing red zone of at least 32 bytes, // which would be reflected in the size of the custom section. This confuses // MachOImageReader::GetCrashpadInfo(), which finds that the section’s size // disagrees with the structure’s size_ field. By specifying an alignment // greater than the red zone size, the red zone will be suppressed. aligned(64), -#endif // __has_feature(address_sanitizer) +#endif // defined(ADDRESS_SANITIZER) // The “used” attribute prevents the structure from being dead-stripped. used, diff --git a/util/misc/address_sanitizer.h b/util/misc/address_sanitizer.h new file mode 100644 index 00000000..75c72399 --- /dev/null +++ b/util/misc/address_sanitizer.h @@ -0,0 +1,28 @@ +// Copyright 2016 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_UTIL_MISC_ADDRESS_SANITIZER_H_ +#define CRASHPAD_UTIL_MISC_ADDRESS_SANITIZER_H_ + +#include "base/compiler_specific.h" +#include "build/build_config.h" + +#if !defined(ADDRESS_SANITIZER) +#if HAS_FEATURE(address_sanitizer) || \ + (defined(COMPILER_GCC) && defined(__SANITIZE_ADDRESS__)) +#define ADDRESS_SANITIZER 1 +#endif +#endif // !defined(ADDRESS_SANITIZER) + +#endif // CRASHPAD_UTIL_MISC_ADDRESS_SANITIZER_H_ diff --git a/util/util.gyp b/util/util.gyp index 5768788a..418256d3 100644 --- a/util/util.gyp +++ b/util/util.gyp @@ -84,6 +84,7 @@ 'mach/task_for_pid.h', 'mach/task_memory.cc', 'mach/task_memory.h', + 'misc/address_sanitizer.h', 'misc/arraysize_unsafe.h', 'misc/clock.h', 'misc/clock_mac.cc',