From patchwork Wed Nov 27 08:54:37 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Venkataramanan Kumar X-Patchwork-Id: 21803 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-pd0-f198.google.com (mail-pd0-f198.google.com [209.85.192.198]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 3406C23FD2 for ; Wed, 27 Nov 2013 08:54:39 +0000 (UTC) Received: by mail-pd0-f198.google.com with SMTP id g10sf16977673pdj.9 for ; Wed, 27 Nov 2013 00:54:38 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:mime-version:in-reply-to:references :date:message-id:subject:from:to:cc:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :list-post:list-help:list-archive:list-unsubscribe:content-type; bh=d6mneddYNZlgqi1yycHPTy5OnSYN2Ur5bCkzY28J5B0=; b=a/CMgxP7s7MzzqtDYAE+nE3IwXdy7ddV47QNTJ2JzqxUtH99dJx9uSrEv0P2SK/qeQ m5/MKIC+UsYbb6YNFm4ovJBNWogkHOIvuWClp8452TGQoNYCAA3Cf37F00v7r8o+uYf8 bywLZEt+c9k8rd5CA1F4wAQNOfahuUCf7QTzD/9okLadu+EIhHhChp0eXOkeTsKZLzw/ tdYEB6S1o4NS/gFVkcb1gNZ8S4Y8tfrazWUy08w8AkfskC46FlR4JBF4hmGUt2pYgc6l cAMTD7OtuY6oK65fcjHUZSRFKBQMnVyYpB7rUsokpD1BIorlgcWeUwyFYe3aT2AYonD1 rC6w== X-Gm-Message-State: ALoCoQn/t9hm2nmwzeQq/8e7b+nutDS35Kn1BSXP2SjgQDLABuEwPNykytLJ74jvoKmSEFDafZJt X-Received: by 10.66.240.4 with SMTP id vw4mr12049895pac.10.1385542478664; Wed, 27 Nov 2013 00:54:38 -0800 (PST) X-BeenThere: patchwork-forward@linaro.org Received: by 10.49.82.49 with SMTP id f17ls2261141qey.54.gmail; Wed, 27 Nov 2013 00:54:38 -0800 (PST) X-Received: by 10.52.163.165 with SMTP id yj5mr805858vdb.42.1385542478517; Wed, 27 Nov 2013 00:54:38 -0800 (PST) Received: from mail-vc0-f182.google.com (mail-vc0-f182.google.com [209.85.220.182]) by mx.google.com with ESMTPS id im16si18640600vec.130.2013.11.27.00.54.38 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 27 Nov 2013 00:54:38 -0800 (PST) Received-SPF: neutral (google.com: 209.85.220.182 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.220.182; Received: by mail-vc0-f182.google.com with SMTP id lc6so4787041vcb.27 for ; Wed, 27 Nov 2013 00:54:38 -0800 (PST) X-Received: by 10.221.24.70 with SMTP id rd6mr359884vcb.42.1385542478440; Wed, 27 Nov 2013 00:54:38 -0800 (PST) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.220.174.196 with SMTP id u4csp269653vcz; Wed, 27 Nov 2013 00:54:37 -0800 (PST) X-Received: by 10.49.24.163 with SMTP id v3mr3128033qef.78.1385542477678; Wed, 27 Nov 2013 00:54:37 -0800 (PST) Received: from mail-qe0-f42.google.com (mail-qe0-f42.google.com [209.85.128.42]) by mx.google.com with ESMTPS id gg10si3837130qeb.97.2013.11.27.00.54.37 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 27 Nov 2013 00:54:37 -0800 (PST) Received-SPF: neutral (google.com: 209.85.128.42 is neither permitted nor denied by best guess record for domain of venkataramanan.kumar@linaro.org) client-ip=209.85.128.42; Received: by mail-qe0-f42.google.com with SMTP id b4so6196047qen.1 for ; Wed, 27 Nov 2013 00:54:37 -0800 (PST) MIME-Version: 1.0 X-Received: by 10.49.35.52 with SMTP id e20mr64074539qej.63.1385542477209; Wed, 27 Nov 2013 00:54:37 -0800 (PST) Received: by 10.140.25.72 with HTTP; Wed, 27 Nov 2013 00:54:37 -0800 (PST) In-Reply-To: <5294B5D2.304@arm.com> References: <5294B5D2.304@arm.com> Date: Wed, 27 Nov 2013 14:24:37 +0530 Message-ID: Subject: Re: [RFC] [PATCH V2, AARCH64]: Re: [RFC] [PATCH, AARCH64] Machine descriptions to support stack smashing protection From: Venkataramanan Kumar To: Richard Earnshaw Cc: "Joseph S. Myers" , Jakub Jelinek , "gcc-patches@gcc.gnu.org" , Marcus Shawcroft , Patch Tracking X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: venkataramanan.kumar@linaro.org X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.220.182 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Precedence: list Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org List-ID: X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , Hi Richard, > I don't think it's good to have long lists of targets on generic tests. > Can we factor this out into a target-supports option? I have updated the patch as per your recommendation. Please let me know if it is fine. 2013-11-26 Venkataramanan Kumar * configure.ac (gcc_cv_libc_provides_tls_ssp): Add test to check TLS support in target C library for Aarch64. * configure: Regenerate. * config.in: Regenerate. * config/aarch64/aarch64.md (stack_protect_set, stack_protect_test) (stack_protect_set_, stack_protect_test_): Add initial machine description for Stack Smashing Protector. * config/aarch64/aarch64-linux.h (TARGET_THREAD_SSP_OFFSET): Define. 2013-11-26 Venkataramanan Kumar * lib/target-supports.exp (check_effective_target_stack_protection): New procedure. * g++.dg/fstack-protector-strong.C: Add target check for stack protection. * gcc.dg/fstack-protector-strong.c: Likewise. regards, Venkat. On 26 November 2013 20:23, Richard Earnshaw wrote: > On 26/11/13 14:16, Venkataramanan Kumar wrote: >> Index: gcc/testsuite/gcc.dg/fstack-protector-strong.c >> =================================================================== >> --- gcc/testsuite/gcc.dg/fstack-protector-strong.c (revision 205378) >> +++ gcc/testsuite/gcc.dg/fstack-protector-strong.c (working copy) >> @@ -1,6 +1,6 @@ >> /* Test that stack protection is done on chosen functions. */ >> >> -/* { dg-do compile { target i?86-*-* x86_64-*-* rs6000-*-* s390x-*-* } } */ >> +/* { dg-do compile { target i?86-*-* x86_64-*-* rs6000-*-* s390x-*-* aarch64-*-* } } */ >> /* { dg-options "-O2 -fstack-protector-strong" } */ >> > > I don't think it's good to have long lists of targets on generic tests. > Can we factor this out into a target-supports option? > > R. > Index: gcc/configure.ac =================================================================== --- gcc/configure.ac (revision 205378) +++ gcc/configure.ac (working copy) @@ -4896,6 +4896,24 @@ [Define if your target C library provides stack protector support]) fi +# Test for tls based stack protector support in target C library. +AC_CACHE_CHECK(TLS stack guard support in target C library, + gcc_cv_libc_provides_tls_ssp, + [gcc_cv_libc_provides_tls_ssp=no + case "$target" in + aarch64-*-linux* | aarch64-*-kfreebsd*-gnu | aarch64-*-knetbsd*-gnu) + # glibc 2.19 and later provides TLS access to stack guard canary + # currently set for Aarch64. + GCC_GLIBC_VERSION_GTE_IFELSE([2], [19], [gcc_cv_libc_provides_tls_ssp=yes]) + ;; + *) gcc_cv_libc_provides_tls_ssp=no ;; + esac]) + +if test x$gcc_cv_libc_provides_tls_ssp = xyes; then + AC_DEFINE(TARGET_LIBC_PROVIDES_TLS_SSP, 1, + [Define if your target C library provides TLS stack protector support.]) +fi + # Test for on the target. GCC_TARGET_TEMPLATE([HAVE_SYS_SDT_H]) AC_MSG_CHECKING(sys/sdt.h in the target C library) Index: gcc/configure =================================================================== --- gcc/configure (revision 205378) +++ gcc/configure (working copy) @@ -27127,6 +27127,35 @@ fi +# Test for tls based stack protector support in target C library. +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking TLS stack guard support in target C library" >&5 +$as_echo_n "checking TLS stack guard support in target C library... " >&6; } +if test "${gcc_cv_libc_provides_tls_ssp+set}" = set; then : + $as_echo_n "(cached) " >&6 +else + gcc_cv_libc_provides_tls_ssp=no + case "$target" in + aarch64-*-linux* | aarch64-*-kfreebsd*-gnu | aarch64-*-knetbsd*-gnu) + # glibc 2.19 and later provides TLS access to stack guard canary + # currently set for Aarch64. + +if test $glibc_version_major -gt 2 \ + || ( test $glibc_version_major -eq 2 && test $glibc_version_minor -ge 19 ); then : + gcc_cv_libc_provides_tls_ssp=yes +fi + ;; + *) gcc_cv_libc_provides_tls_ssp=no ;; + esac +fi +{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $gcc_cv_libc_provides_tls_ssp" >&5 +$as_echo "$gcc_cv_libc_provides_tls_ssp" >&6; } + +if test x$gcc_cv_libc_provides_tls_ssp = xyes; then + +$as_echo "#define TARGET_LIBC_PROVIDES_TLS_SSP 1" >>confdefs.h + +fi + # Test for on the target. { $as_echo "$as_me:${as_lineno-$LINENO}: checking sys/sdt.h in the target C library" >&5 Index: gcc/testsuite/lib/target-supports.exp =================================================================== --- gcc/testsuite/lib/target-supports.exp (revision 205378) +++ gcc/testsuite/lib/target-supports.exp (working copy) @@ -808,6 +808,17 @@ } "-fstack-protector"] } +# Return 1 the target supports stack protection. +proc check_effective_target_stack_protection { } { + if { [istarget i?86-*-*] + || [istarget x86_64-*-*] + || [istarget aarch64-*-*] + || [istarget s390x-*-*] } { + return 1; + } + return 0 +} + # Return 1 if compilation with -freorder-blocks-and-partition is error-free # for trivial code, 0 otherwise. Index: gcc/testsuite/gcc.dg/fstack-protector-strong.c =================================================================== --- gcc/testsuite/gcc.dg/fstack-protector-strong.c (revision 205378) +++ gcc/testsuite/gcc.dg/fstack-protector-strong.c (working copy) @@ -1,6 +1,6 @@ /* Test that stack protection is done on chosen functions. */ -/* { dg-do compile { target i?86-*-* x86_64-*-* rs6000-*-* s390x-*-* } } */ +/* { dg-do compile { target stack_protection } } */ /* { dg-options "-O2 -fstack-protector-strong" } */ #include Index: gcc/testsuite/g++.dg/fstack-protector-strong.C =================================================================== --- gcc/testsuite/g++.dg/fstack-protector-strong.C (revision 205378) +++ gcc/testsuite/g++.dg/fstack-protector-strong.C (working copy) @@ -1,6 +1,6 @@ /* Test that stack protection is done on chosen functions. */ -/* { dg-do compile { target i?86-*-* x86_64-*-* } } */ +/* { dg-do compile { target stack_protection } } */ /* { dg-options "-O2 -fstack-protector-strong" } */ class A Index: gcc/config.in =================================================================== --- gcc/config.in (revision 205378) +++ gcc/config.in (working copy) @@ -1828,6 +1828,12 @@ #endif +/* Define if your target C library provides TLS stack protector support. */ +#ifndef USED_FOR_TARGET +#undef TARGET_LIBC_PROVIDES_TLS_SSP +#endif + + /* Define to 1 if you can safely include both and . */ #ifndef USED_FOR_TARGET #undef TIME_WITH_SYS_TIME Index: gcc/config/aarch64/aarch64-linux.h =================================================================== --- gcc/config/aarch64/aarch64-linux.h (revision 205378) +++ gcc/config/aarch64/aarch64-linux.h (working copy) @@ -43,4 +43,9 @@ } \ while (0) +#ifdef TARGET_LIBC_PROVIDES_TLS_SSP +/* Aarch64 glibc provides __stack_chk_guard in [tp - 0x8]. */ +#define TARGET_THREAD_SSP_OFFSET (-1 * GET_MODE_SIZE (ptr_mode)) +#endif + #endif /* GCC_AARCH64_LINUX_H */ Index: gcc/config/aarch64/aarch64.md =================================================================== --- gcc/config/aarch64/aarch64.md (revision 205378) +++ gcc/config/aarch64/aarch64.md (working copy) @@ -99,6 +99,10 @@ UNSPEC_TLSDESC UNSPEC_USHL_2S UNSPEC_VSTRUCTDUMMY + UNSPEC_SP_SET + UNSPEC_SP_TEST + UNSPEC_SP_TLS_SET + UNSPEC_SP_TLS_TEST ]) (define_c_enum "unspecv" [ @@ -3625,6 +3629,80 @@ DONE; }) + +;; Named patterns for stack smashing protection. +(define_expand "stack_protect_set" + [(match_operand 0 "memory_operand") + (match_operand 1 "memory_operand")] + "" +{ + enum machine_mode mode = GET_MODE (operands[0]); + +#ifdef TARGET_THREAD_SSP_OFFSET + rtx tlsreg = gen_reg_rtx (Pmode); + emit_insn (gen_aarch64_load_tp_hard (tlsreg)); + rtx addr = gen_rtx_PLUS (Pmode, tlsreg, GEN_INT (TARGET_THREAD_SSP_OFFSET)); + operands[1] = gen_rtx_MEM (Pmode, addr); +#endif + + emit_insn ((mode == DImode + ? gen_stack_protect_set_di + : gen_stack_protect_set_si) (operands[0], operands[1])); + DONE; +}) + +(define_insn "stack_protect_set_" + [(set (match_operand:PTR 0 "memory_operand" "=m") + (unspec:PTR [(match_operand:PTR 1 "memory_operand" "m")] + UNSPEC_SP_SET)) + (set (match_scratch:PTR 2 "=&r") (const_int 0))] + "" + "ldr\\t%x2, %1\;str\\t%x2, %0\;mov\t%x2,0" + [(set_attr "length" "12")]) + +(define_expand "stack_protect_test" + [(match_operand 0 "memory_operand") + (match_operand 1 "memory_operand") + (match_operand 2)] + "" +{ + +#ifdef TARGET_THREAD_SSP_OFFSET + rtx tlsreg = gen_reg_rtx (Pmode); + emit_insn (gen_aarch64_load_tp_hard (tlsreg)); + rtx addr = gen_rtx_PLUS (Pmode, tlsreg, GEN_INT (TARGET_THREAD_SSP_OFFSET)); + operands[1] = gen_rtx_MEM (Pmode, addr); +#endif + + rtx result = gen_reg_rtx (Pmode); + + enum machine_mode mode = GET_MODE (operands[0]); + + emit_insn ((mode == DImode + ? gen_stack_protect_test_di + : gen_stack_protect_test_si) (result, + operands[0], + operands[1])); + + if (mode == DImode) + emit_jump_insn (gen_cbranchdi4 (gen_rtx_EQ (VOIDmode, result, const0_rtx), + result, const0_rtx, operands[2])); + else + emit_jump_insn (gen_cbranchsi4 (gen_rtx_EQ (VOIDmode, result, const0_rtx), + result, const0_rtx, operands[2])); + DONE; +}) + +(define_insn "stack_protect_test_" + [(set (match_operand:PTR 0 "register_operand") + (unspec:PTR [(match_operand:PTR 1 "memory_operand" "m") + (match_operand:PTR 2 "memory_operand" "m")] + UNSPEC_SP_TEST)) + (clobber (match_scratch:PTR 3 "=&r"))] + "" + "ldr\t%x3, %x1\;ldr\t%x0, %x2\;eor\t%x0, %x3, %x0" + [(set_attr "length" "12")]) + ;; AdvSIMD Stuff (include "aarch64-simd.md")