From patchwork Wed Jan 13 09:08:25 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: max X-Patchwork-Id: 566870 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 405B314032F for ; Wed, 13 Jan 2016 20:08:44 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=sEOqKPZd; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :subject:to:cc:message-id:date:mime-version:content-type; q=dns; s=default; b=oPnEV5PJ1Wos9gQI7o8E7SE4kIA4doRsTys+USd8V2c1tUeT7s DYbZ126fNKsIJWOIAgxZUmhrbRhZz69HyDt2w9ezX7CO2NolK5lfqJn7HA+3qmMd HjnkqpEz4ePO8UNsJ7VP40v8Vhzq0fSeXU0Jjp5XDTF2hAnJ3c55bTR/Y= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :subject:to:cc:message-id:date:mime-version:content-type; s= default; bh=U8tYfMb41uMa09L0RsUilrHJnkA=; b=sEOqKPZdeq5tMqQ8zUt8 8Sa5hnyOf+0M67hniNlRnTs+Agn3PuXuUP1TC6SYNmZCzc0rBHaHXE2kPNkTG9iw +y+m599yi634RyFMUNmCbkFC+pCE2GpbaPdkUcvGBVAHDqm0SHk+jEe1nJ7QxmoD 9skgaetd432fxLe1NSuh/mg= Received: (qmail 5009 invoked by alias); 13 Jan 2016 09:08:36 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 4984 invoked by uid 89); 13 Jan 2016 09:08:35 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.9 required=5.0 tests=BAYES_00, KAM_LAZY_DOMAIN_SECURITY, RP_MATCHES_RCVD, SPF_HELO_PASS, T_HDRS_LCASE, T_MANY_HDRS_LCASE autolearn=no version=3.3.2 spammy=1416, HContent-type:mixed, cherry, tied X-HELO: mailout1.w1.samsung.com Received: from mailout1.w1.samsung.com (HELO mailout1.w1.samsung.com) (210.118.77.11) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Wed, 13 Jan 2016 09:08:33 +0000 Received: from eucpsbgm1.samsung.com (unknown [203.254.199.244]) by mailout1.w1.samsung.com (Oracle Communications Messaging Server 7.0.5.31.0 64bit (built May 5 2014)) with ESMTP id <0O0V00B4VW25F240@mailout1.w1.samsung.com> for gcc-patches@gcc.gnu.org; Wed, 13 Jan 2016 09:08:29 +0000 (GMT) Received: from eusync2.samsung.com ( [203.254.199.212]) by eucpsbgm1.samsung.com (EUCPMTA) with SMTP id 8C.AF.16778.D0416965; Wed, 13 Jan 2016 09:08:29 +0000 (GMT) Received: from [106.109.128.194] by eusync2.samsung.com (Oracle Communications Messaging Server 7.0.5.31.0 64bit (built May 5 2014)) with ESMTPA id <0O0V00BKRW24WV70@eusync2.samsung.com>; Wed, 13 Jan 2016 09:08:29 +0000 (GMT) From: Maxim Ostapenko Subject: [PATCH][GCC 5][PR sanitizer/69147] Fix ASan failures on new Darwin platforms (OS X 10.11+, iOS 9.0+). To: GCC Patches Cc: Jakub Jelinek , Dominique Dhumieres , Yury Gribov Message-id: <56961409.1050506@partner.samsung.com> Date: Wed, 13 Jan 2016 12:08:25 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 MIME-version: 1.0 Content-type: multipart/mixed; boundary=------------070607040505050706090104 X-IsSubscribed: yes Hi, in OS X 10.11+ or iOS 9.0+, dyld will interpose even if DYLD_INSERT_LIBRARIES is not set. This means, that it's not safe to use "setenv" function to set DYLD_INSERT_LIBRARIES in MaybeReexec, because ASan interceptors already established and we can easily end up with something like this: AsanInitInternal -> MaybeReexec -> setenv -> some intercepted function (say, memmove) -> BOOM. In fact, there is no need to reexec at all on such platforms, because we already have working interceptors. Thus, for now we have completely broken ASan for OS X 10.11+ and iOS 9.0+ with GCC 5. Upstream ASan already has a fix for the issue, as well as GCC 6, so the proper solution would be just cherry-picking corresponding commits (r224315 and r241487) from upstream to gcc-5-branch. Regtested and bootstrapped on x86_64-unknown-linux-gnu by myself and on x86_64-apple-darwin15.2.0 by Dominique (https://gcc.gnu.org/ml/gcc-testresults/2016-01/msg01115.html). Ok for gcc-5-branch? -Maxim libsanitizer/ChangeLog: 2016-01-13 Maxim Ostapenko * PR sanitizer/69147 * asan/asan_mac.cc: Cherry pick upstream r241487. * sanitizer_common/sanitizer_mac.cc: Cherry pick upstream r224315. * sanitizer_common/sanitizer_mac.h: Likewise. diff --git a/libsanitizer/asan/asan_mac.cc b/libsanitizer/asan/asan_mac.cc index 70823bd..22f3f70 100644 --- a/libsanitizer/asan/asan_mac.cc +++ b/libsanitizer/asan/asan_mac.cc @@ -99,6 +99,23 @@ void LeakyResetEnv(const char *name, const char *name_value) { } } +bool DyldNeedsEnvVariable() { +// If running on OS X 10.11+ or iOS 9.0+, dyld will interpose even if +// DYLD_INSERT_LIBRARIES is not set. + +#if SANITIZER_IOSSIM + // GetMacosVersion will not work for the simulator, whose kernel version + // is tied to the host. Use a weak linking hack for the simulator. + // This API was introduced in the same version of the OS as the dyld + // optimization. + + // Check for presence of a symbol that is available on OS X 10.11+, iOS 9.0+. + return (dlsym(RTLD_NEXT, "mach_memory_info") == nullptr); +#else + return (GetMacosVersion() <= MACOS_VERSION_YOSEMITE); +#endif +} + void MaybeReexec() { if (!flags()->allow_reexec) return; // Make sure the dynamic ASan runtime library is preloaded so that the @@ -111,8 +128,9 @@ void MaybeReexec() { uptr old_env_len = dyld_insert_libraries ? internal_strlen(dyld_insert_libraries) : 0; uptr fname_len = internal_strlen(info.dli_fname); - if (!dyld_insert_libraries || - !REAL(strstr)(dyld_insert_libraries, info.dli_fname)) { + bool lib_is_in_env = + dyld_insert_libraries && REAL(strstr)(dyld_insert_libraries, info.dli_fname); + if (DyldNeedsEnvVariable() && !lib_is_in_env) { // DYLD_INSERT_LIBRARIES is not set or does not contain the runtime // library. char program_name[1024]; @@ -141,6 +159,10 @@ void MaybeReexec() { VReport(1, "Set ASAN_OPTIONS=allow_reexec=0 to disable this.\n"); execv(program_name, *_NSGetArgv()); } else { + + if (!lib_is_in_env) + return; + // DYLD_INSERT_LIBRARIES is set and contains the runtime library. if (old_env_len == fname_len) { // It's just the runtime library name - fine to unset the variable. diff --git a/libsanitizer/sanitizer_common/sanitizer_mac.cc b/libsanitizer/sanitizer_common/sanitizer_mac.cc index 17b931c..b53ffba 100644 --- a/libsanitizer/sanitizer_common/sanitizer_mac.cc +++ b/libsanitizer/sanitizer_common/sanitizer_mac.cc @@ -296,7 +296,11 @@ MacosVersion GetMacosVersionInternal() { case '2': return MACOS_VERSION_MOUNTAIN_LION; case '3': return MACOS_VERSION_MAVERICKS; case '4': return MACOS_VERSION_YOSEMITE; - default: return MACOS_VERSION_UNKNOWN; + default: + if (IsDigit(version[1])) + return MACOS_VERSION_UNKNOWN_NEWER; + else + return MACOS_VERSION_UNKNOWN; } } default: return MACOS_VERSION_UNKNOWN; diff --git a/libsanitizer/sanitizer_common/sanitizer_mac.h b/libsanitizer/sanitizer_common/sanitizer_mac.h index 47739f7..46a7b44 100644 --- a/libsanitizer/sanitizer_common/sanitizer_mac.h +++ b/libsanitizer/sanitizer_common/sanitizer_mac.h @@ -25,6 +25,7 @@ enum MacosVersion { MACOS_VERSION_MOUNTAIN_LION, MACOS_VERSION_MAVERICKS, MACOS_VERSION_YOSEMITE, + MACOS_VERSION_UNKNOWN_NEWER }; MacosVersion GetMacosVersion();