From patchwork Fri Mar 28 19:05:15 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 876729 Delivered-To: patch@linaro.org Received: by 2002:a5d:6dae:0:b0:38f:210b:807b with SMTP id u14csp713991wrs; Fri, 28 Mar 2025 12:06:00 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXWo7JzxWGzB0AHzJhwnBZL9NzXli7T1W+zYHE/n2sWluT5Mrx6c4YZeVgTh0uybcoiHhNsAg==@linaro.org X-Google-Smtp-Source: AGHT+IGgii050wAXymcsitcha1+4Ei+vaGXg/rAu07mdFpsXQqJS0rDqnNRBOUTHoo22lleFkXHK X-Received: by 2002:a05:620a:3902:b0:7c5:5f38:ba4b with SMTP id af79cd13be357-7c6862ef4c7mr33058785a.10.1743188760185; Fri, 28 Mar 2025 12:06:00 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1743188760; cv=pass; d=google.com; s=arc-20240605; b=Go6kXErqGIflArPi42hoSfhePyDh/qilGxQgTMSxQ9knbGX4cdhKiF8sRdWi/CI+uO gHzIB+sXBTKlR/1KfgABvIYxtCcU6ahEqAIEsKmAdaMd4Q2P3ryiBAtXC/qnOnrIGw0H nYiLUWrku4bgOM8BEH21WkFLWzf19ojC5CaUDJNVaqftcYGuAu3s8ubg9CqdxeDFbJF6 hga9Ed+t3p9SeaZjLBYWSC0EqJ/4WZ/ZWv2FeOXELrz8HrHlxqSfM4QXIBNAFBJyXrma O4B84An74Dbgw3UWtFPVSEyiaZAX9SvSjCX2zDrg7lxp30tJ3p6XGciQfHyKKDDhsLb4 mW6w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :dkim-filter:arc-filter:dmarc-filter:delivered-to:dkim-filter; bh=cImSjS1yBrB9ZjoTMULS7sgpIxSfiO9Kl4c7lRR0BqE=; fh=RvcFWFDXNQH1fQJ+4H6t0aK73b3S/H7SkKPUuNpR/w8=; b=V6nmYApyF2Aqs9tzytpFgUChKmJN+1X0/uknZKuaCPNU5I3XEXB2lEoFcnxZJl3iCt gVoymwY1vU9Fftpyo7L/aHeEXUB8EZViBOz61oMiojVltttbctA7W+fz4Q/U7Eo03YNA UY4YMpPYPxdAXXP/FJ6ivCTVUDgQOJLyiYdqXf3eHr2xO8YkzslHAQ+sDds6F5Nc8AkB Firm9iJ3tvijKC8pWclv4vfFIpuNO3yfhK8hW259dVwHN/989qwrxbIp9hWZJd1Tyarm gaSOXC8zRI+0lBXAf9mek78XxoZX4NTCijEGMThmpgrvRbB2+XWsaQfGQ7VNH2Tdm0ur 8SvA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=RLTEzrPu; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="libc-alpha-bounces~patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id af79cd13be357-7c5f776956esi224119785a.306.2025.03.28.12.06.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Mar 2025 12:06:00 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=RLTEzrPu; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="libc-alpha-bounces~patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 82B213830612 for ; Fri, 28 Mar 2025 19:05:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 82B213830612 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=RLTEzrPu X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x62b.google.com (mail-pl1-x62b.google.com [IPv6:2607:f8b0:4864:20::62b]) by sourceware.org (Postfix) with ESMTPS id 222373857BA5 for ; Fri, 28 Mar 2025 19:05:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 222373857BA5 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 222373857BA5 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::62b ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1743188731; cv=none; b=W/QZlof0+D1y1QJEll25XeNSfXZW3Y5w5YoIRLJEXviiFzXcOwNFMVwLm7RZiX9qDfTevXBfFEVgA4NpW+lLfuu8xX4A2l2AiXK2elCl9vepy/UCjrRiDPsv1fjWSHbEYHsokoe+evKgMHBCjvWG6ElX3D6BVZqCwIfa9GHI8dM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1743188731; c=relaxed/simple; bh=F3Pz5Q52NnqKsDiuvJgLhkSdVmZycRJ0WX+hw9ONtgw=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=BYRLMLakeT8+CWZ49AJkmlOi11eROup4GXzz2buoLyIlI6aFXZbDWKu4r3unt3rLc0C+lwxgQHToVAEI0AtKt5L2fRkTC/olK9btLIbrZOQNdznSlIJL3vvWf3AtJQvzDU3sZOfkbwdz7ZegClKf47o7IulYMW1s7Bi6nrQCY60= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 222373857BA5 Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-227a8cdd241so13190585ad.3 for ; Fri, 28 Mar 2025 12:05:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1743188730; x=1743793530; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=cImSjS1yBrB9ZjoTMULS7sgpIxSfiO9Kl4c7lRR0BqE=; b=RLTEzrPujsxpxmjvGukZDMO9pyDBI5Oixf7Zah5W6i6oijoxOcQyylJI30U62jpBQ4 lnzg0qtXG/NVL9AtfWsreD2u0Z0stF0kmxnfXCWDHc52bO5cOiWT67M/alOh1IliRwgB Sx/AZH6MZ9SRQKuCGwkX1B7E0no8v2tqwItxApcjdsbatmCCLHZQUpXFzMrlmvgDzSTf 6t3qaA/m3P/7TVA342yvHytKQ1gcnGRn17UB1Y0/0FDNrYKszRyB5QHAYKBbwq91NsKz Eu4Cg7EIufbucpN2C7+XJDTly530rbxA8oRMHwNkVRicJ7PzQFj9iu0MlD+9zehEO3ng Zugw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743188730; x=1743793530; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=cImSjS1yBrB9ZjoTMULS7sgpIxSfiO9Kl4c7lRR0BqE=; b=wy4R6ih7IzTzMEg3oNNJcy8xC8afythAy1SgG2441dszkEdEAIDtmfxOodTfIAgvSG nqt93i4R2LwDS8fxGIqRxYMhTdCvUt3DKzhRz/dpu7FHX9xyLXQYcOgzwMfGbDkVaFAK /Zc7/+n+vQUt6EiLTIpijmQaAljR2olY7CI3Xto96WfNr288oiyrdbKNa62wwPazzk1G xpePwP4bR+fWd9oyWdjhgEOAlgDAAkMZHrtZJ5cs8aae1ZCkoYX9J30Ur4Q0KBT9IWTc lcYKxC3eZ8dfPa9Cjh+ZIH2paGzxG7DAeUwVcdUycq5vHkjc36kS3eqY7eSMHLvBq+qv NZUg== X-Gm-Message-State: AOJu0Yy/6sVYp0y4K6f5T5DUMOhdIB4d99j0ho346ITmOncmupxkzI96 H+3CxBVQPQVSnNzG7GWBIEi1P1ERrS+VcBiNuJDU9R8+nFkRivZ2x9eBPp9jpGc6CrhUJgRyzud X X-Gm-Gg: ASbGncv6q2kSan9fafNo61QxmRKfo57LkqXwDxskGLR3Jfk91kr1ZLiGKOjkAfFfZPy MHV/73kFpPvxzFkeHyXs3Q5Ivw8OToZwjLz+L8EUQ1yT93mdfy1+qTm2L2lNK3tytcTh6t6wW8/ meMQhRxeC61zy0atdFYL5Uo+MZlcJ81D/fj2uPY+ymmxtYBuc0Dw7VJc5IQFnHB2jsPOOmoTSIG 3FacoCh8VKldj5hGpBDgsJFW0EoNJHTYi0HbDBdeJP+CoHDGGm8u+QnSScb2nfVkwO9GN4JWzl0 idpyGncj6p4HXEbPCiF98gapH77LfSP51lfT5fiEEvryPyHOuL9/NJuxskH7hTJHW4SnVr6VRUp NQlInLPI3/LipStdAmghQ+CN05jE= X-Received: by 2002:a17:902:f645:b0:220:c164:6ee1 with SMTP id d9443c01a7336-2292f9e60c1mr4508475ad.32.1743188729693; Fri, 28 Mar 2025 12:05:29 -0700 (PDT) Received: from ubuntu-vm.. (201-92-183-252.dsl.telesp.net.br. [201.92.183.252]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2291f1cddb3sm21971535ad.138.2025.03.28.12.05.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Mar 2025 12:05:29 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Yury Khrustalev , Wilco Dijkstra Subject: [PATCH] aarch64: Fix _dl_tlsdesc_dynamic unwind for pac-ret (BZ 32612) Date: Fri, 28 Mar 2025 16:05:15 -0300 Message-ID: <20250328190525.2507863-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patch=linaro.org@sourceware.org When libgcc is built with pac-ret, it requires to autenticate the unwinding frame based on CFI information. The _dl_tlsdesc_dynamic uses a custom calling convention, where it is responsible to save and restore all registers it might use (even volatile). The pac-ret support added by 1be3d6eb823d8b952fa54b7bbc90cbecb8981380 was added only on the slow-path, but the fast path also adds DWARF Register Rule Instruction (cfi_adjust_cfa_offset) since it requires to save/restore some auxiliary register. It seems that this is not fully supported neither by libgcc nor AArch64 ABI [1]. Instead, move paciasp/autiasp to function prologue/epilogue to be used on both fast and slow paths. I also corrected the _dl_tlsdesc_dynamic comment description, it was copied from i386 implementation without any adjustment. Checked on aarch64-linux-gnu with a toolchain built with --enable-standard-branch-protection on a system with pac-ret support. [1] https://github.com/ARM-software/abi-aa/blob/main/aadwarf64/aadwarf64.rst#id1 Reviewed-by: Yury Khrustalev --- sysdeps/aarch64/dl-tlsdesc.S | 24 +++++----- sysdeps/unix/sysv/linux/aarch64/Makefile | 13 +++++ .../sysv/linux/aarch64/tst-tlsdesc-pac-mod.c | 27 +++++++++++ .../unix/sysv/linux/aarch64/tst-tlsdesc-pac.c | 48 +++++++++++++++++++ 4 files changed, 100 insertions(+), 12 deletions(-) create mode 100644 sysdeps/unix/sysv/linux/aarch64/tst-tlsdesc-pac-mod.c create mode 100644 sysdeps/unix/sysv/linux/aarch64/tst-tlsdesc-pac.c diff --git a/sysdeps/aarch64/dl-tlsdesc.S b/sysdeps/aarch64/dl-tlsdesc.S index 68afc443f7..fc40d66c07 100644 --- a/sysdeps/aarch64/dl-tlsdesc.S +++ b/sysdeps/aarch64/dl-tlsdesc.S @@ -119,20 +119,19 @@ _dl_tlsdesc_undefweak: object referenced by the argument. ptrdiff_t - __attribute__ ((__regparm__ (1))) _dl_tlsdesc_dynamic (struct tlsdesc *tdp) { struct tlsdesc_dynamic_arg *td = tdp->arg; - dtv_t *dtv = *(dtv_t **)((char *)__thread_pointer + TCBHEAD_DTV); + dtv_t *dtv = *(dtv_t **)((char *)__thread_pointer() + TCBHEAD_DTV); if (__builtin_expect (td->gen_count <= dtv[0].counter && (dtv[td->tlsinfo.ti_module].pointer.val != TLS_DTV_UNALLOCATED), 1)) return dtv[td->tlsinfo.ti_module].pointer.val + td->tlsinfo.ti_offset - - __thread_pointer; + - __thread_pointer(); - return ___tls_get_addr (&td->tlsinfo) - __thread_pointer; + return __tls_get_addr (&td->tlsinfo) - __thread_pointer(); } */ @@ -142,7 +141,12 @@ _dl_tlsdesc_undefweak: cfi_startproc .align 2 _dl_tlsdesc_dynamic: +# if HAVE_AARCH64_PAC_RET + PACIASP + cfi_window_save +# else BTI_C +# endif /* Save just enough registers to support fast path, if we fall into slow path we will save additional registers. */ @@ -173,6 +177,10 @@ _dl_tlsdesc_dynamic: 1: ldp x3, x4, [sp, #16] ldp x1, x2, [sp], #32 +# if HAVE_AARCH64_PAC_RET + AUTIASP + cfi_window_save +# endif cfi_adjust_cfa_offset (-32) RET 2: @@ -182,10 +190,6 @@ _dl_tlsdesc_dynamic: /* Save the remaining registers that we must treat as caller save. */ cfi_restore_state -# if HAVE_AARCH64_PAC_RET - PACIASP - cfi_window_save -# endif # define NSAVEXREGPAIRS 8 stp x29, x30, [sp,#-16*NSAVEXREGPAIRS]! cfi_adjust_cfa_offset (16*NSAVEXREGPAIRS) @@ -236,10 +240,6 @@ _dl_tlsdesc_dynamic: cfi_adjust_cfa_offset (-16*NSAVEXREGPAIRS) cfi_restore (x29) cfi_restore (x30) -# if HAVE_AARCH64_PAC_RET - AUTIASP - cfi_window_save -# endif b 1b cfi_endproc .size _dl_tlsdesc_dynamic, .-_dl_tlsdesc_dynamic diff --git a/sysdeps/unix/sysv/linux/aarch64/Makefile b/sysdeps/unix/sysv/linux/aarch64/Makefile index 0839f0b08c..15a2b4471d 100644 --- a/sysdeps/unix/sysv/linux/aarch64/Makefile +++ b/sysdeps/unix/sysv/linux/aarch64/Makefile @@ -1,3 +1,16 @@ +ifeq ($(subdir),elf) +tests += \ + tst-tlsdesc-pac \ + # tests +modules-names += \ + tst-tlsdesc-pac-mod \ + # modules-names + +LDFLAGS-tst-tlsdesc-pac = -rdynamic + +$(objpfx)tst-tlsdesc-pac.out: $(objpfx)tst-tlsdesc-pac-mod.so +endif + ifeq ($(subdir),misc) sysdep_headers += sys/elf.h tests += \ diff --git a/sysdeps/unix/sysv/linux/aarch64/tst-tlsdesc-pac-mod.c b/sysdeps/unix/sysv/linux/aarch64/tst-tlsdesc-pac-mod.c new file mode 100644 index 0000000000..d34c8beda9 --- /dev/null +++ b/sysdeps/unix/sysv/linux/aarch64/tst-tlsdesc-pac-mod.c @@ -0,0 +1,27 @@ +/* AArch64 tests for unwinding TLSDESC (BZ 32612) + Copyright (C) 2025 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +_Thread_local int foo; +/* Make the TLS segment large enough to trigger _dl_tlsdesc_dynamic. */ +_Thread_local int foobar[1000]; + +void +bar (void) +{ + foo = 1; +} diff --git a/sysdeps/unix/sysv/linux/aarch64/tst-tlsdesc-pac.c b/sysdeps/unix/sysv/linux/aarch64/tst-tlsdesc-pac.c new file mode 100644 index 0000000000..24d656aafc --- /dev/null +++ b/sysdeps/unix/sysv/linux/aarch64/tst-tlsdesc-pac.c @@ -0,0 +1,48 @@ +/* AArch64 tests for unwinding TLSDESC (BZ 32612) + Copyright (C) 2025 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include + +static _Unwind_Reason_Code +unwind_callback (struct _Unwind_Context* context, void* closure) +{ + return _URC_NO_REASON; +} + +/* Assume that TLS variable from tst-tlsdesc-pac-mod.so will trigger + the slow-path that allocates the required memory with malloc. */ +void * +malloc (size_t s) +{ + _Unwind_Backtrace (unwind_callback, NULL); + return calloc (1, s); +} + +static int +do_test (void) +{ + void *h = xdlopen ("tst-tlsdesc-pac-mod.so", RTLD_LAZY); + void (*func)(void) = xdlsym (h, "bar"); + func (); + + return 0; +} + +#include