From 1f46cdf06c2527391f9107809566795ade06fdcc Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Tue, 14 Feb 2017 13:29:29 -0800 Subject: [PATCH] Use the global stack protector guard for old x86. https://gcc.gnu.org/ml/gcc/2015-11/msg00060.html changed the default for this from using a global to using the TLS slot. As noted in https://github.com/android-ndk/ndk/issues/297 (and in that commit), this is not compatible with pre-4.2 devices, so we need to guard against that in the NDK. Test: ./validate.py --filter mstack-protector-guard Bug: https://github.com/android-ndk/ndk/issues/297 Change-Id: I48152b452b210ed5d8967c3cb617e6fe97027b10 --- build/core/build-binary.mk | 9 +++ .../project/jni/Android.mk | 6 ++ .../project/jni/foo.cpp | 1 + tests/build/mstack-protector-guard/test.py | 63 +++++++++++++++++++ 4 files changed, 79 insertions(+) create mode 100644 tests/build/mstack-protector-guard/project/jni/Android.mk create mode 100644 tests/build/mstack-protector-guard/project/jni/foo.cpp create mode 100644 tests/build/mstack-protector-guard/test.py diff --git a/build/core/build-binary.mk b/build/core/build-binary.mk index 3a3e95da..3ea5b598 100644 --- a/build/core/build-binary.mk +++ b/build/core/build-binary.mk @@ -245,6 +245,15 @@ ifeq ($(TARGET_ARCH_ABI),x86) LOCAL_CFLAGS += -mstackrealign endif +# https://github.com/android-ndk/ndk/issues/297 +ifeq ($(TARGET_ARCH_ABI),x86) + ifneq (,$(call lt,$(APP_PLATFORM_LEVEL),17)) + ifeq ($(NDK_TOOLCHAIN_VERSION),4.9) + LOCAL_CFLAGS += -mstack-protector-guard=global + endif + endif +endif + # # The original Android build system allows you to use the .arm prefix # to a source file name to indicate that it should be defined in either diff --git a/tests/build/mstack-protector-guard/project/jni/Android.mk b/tests/build/mstack-protector-guard/project/jni/Android.mk new file mode 100644 index 00000000..365bbad8 --- /dev/null +++ b/tests/build/mstack-protector-guard/project/jni/Android.mk @@ -0,0 +1,6 @@ +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) +LOCAL_MODULE := foo +LOCAL_SRC_FILES := foo.cpp +include $(BUILD_SHARED_LIBRARY) diff --git a/tests/build/mstack-protector-guard/project/jni/foo.cpp b/tests/build/mstack-protector-guard/project/jni/foo.cpp new file mode 100644 index 00000000..85e6cd8c --- /dev/null +++ b/tests/build/mstack-protector-guard/project/jni/foo.cpp @@ -0,0 +1 @@ +void foo() {} diff --git a/tests/build/mstack-protector-guard/test.py b/tests/build/mstack-protector-guard/test.py new file mode 100644 index 00000000..1b503633 --- /dev/null +++ b/tests/build/mstack-protector-guard/test.py @@ -0,0 +1,63 @@ +# +# Copyright (C) 2016 The Android Open Source Project +# +# 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. +# +"""Check for mstack-protector-guard=global when targeting old x86 targets. + +https://gcc.gnu.org/ml/gcc/2015-11/msg00060.html changed the default for this +from using a global to using the TLS slot. As noted in +https://github.com/android-ndk/ndk/issues/297 (and in that commit), this is not +compatible with pre-4.2 devices, so we need to guard against that in the NDK. +""" +import os +import subprocess +import sys + + +def run_test(abi=None, platform=None, toolchain=None, build_flags=None): + """Checks ndk-build V=1 output for mstackrealign flag.""" + if build_flags is None: + build_flags = [] + + ndk_dir = os.environ['NDK'] + ndk_build = os.path.join(ndk_dir, 'ndk-build') + if sys.platform == 'win32': + ndk_build += '.cmd' + project_path = 'project' + ndk_args = build_flags + [ + 'APP_ABI=' + abi, + 'APP_PLATFORM=android-{}'.format(platform), + 'NDK_TOOLCHAIN_VERSION=' + toolchain, + 'V=1', + ] + proc = subprocess.Popen([ndk_build, '-C', project_path] + ndk_args, + stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + out, _ = proc.communicate() + if proc.returncode != 0: + return proc.returncode == 0, out + + search_text = '-mstack-protector-guard=global' + out_words = out.split(' ') + if abi == 'x86' and platform < 17 and toolchain == '4.9': + if search_text in out_words: + return True, out + else: + out = 'Did not find {} in output:\n{}'.format(search_text, out) + return False, out + else: + if search_text in out_words: + print 'Found unexpceted {} in output:\n'.format(search_text, out) + return False, out + else: + return True, out