From patchwork Tue Feb 13 16:40:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 1898323 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=DLheEOGG; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TZ6XX3WJhz23hT for ; Wed, 14 Feb 2024 03:41:16 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 524823858022 for ; Tue, 13 Feb 2024 16:41:14 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-oa1-x31.google.com (mail-oa1-x31.google.com [IPv6:2001:4860:4864:20::31]) by sourceware.org (Postfix) with ESMTPS id 458053858000 for ; Tue, 13 Feb 2024 16:40:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 458053858000 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 458053858000 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2001:4860:4864:20::31 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707842456; cv=none; b=nWiN88jyY2ytf3kShnmqyIUs7Ap63BVvI9BozcfXLTOneo8hOHOmeW50dN5FOCbO2WLhF99SEB4/Rx5whjjNCgsbZMP7RdvDH78qLLlsZEbNSiVf4rTOjHuiJ6oBSDzE2U83GZHaThcDYs0dgsRBdM8KoeNwjRKlEQ9sBgyHhJU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707842456; c=relaxed/simple; bh=H3pVGrvZuLhmfq23gD2sEJV2HxOP0sPAC4CT2u3C/Fs=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=QgVdNkY2sbQzOkIm54jzulCBNJvJnN0ZCCLn1uZVFzchLlf/sxOkF9MeMQi5umds2Re7poPpGTgeUPKK0z5g0G1+33OwlNRJ7Xu/jNAT+Pkj13dSLR1WBDgC30IxT0LYlYDB1KQRdSYUJr876jEMGoZo7OagleKFIQKSSPYG998= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-oa1-x31.google.com with SMTP id 586e51a60fabf-21d56bb3714so186718fac.2 for ; Tue, 13 Feb 2024 08:40:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707842454; x=1708447254; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=1HVBdFKYzveXqQX8QqB62afInbRhBG7j+4LCD4Og1D8=; b=DLheEOGGbgZOJ4nbgFWpHiOtAu2zNhSaApJQL6OO0W+lHoFL1E5cXUGVlcdayGbtkb Yf7Up/h1+uUJ34PU+xQ/F1MLXgh55pJf1Vjl4FcRfW2Yot0RAv/IP/SbVoiyYojrQR8x Ii+U913iu/xVXrfY/LNRIdGeV40zHuy00UEF0dPML7os/FOq76nMvbSfjRXyQSe/IoXL lVP6Y23Np9OKgJlB4k13Vd44987iS94xoyI771zgN5nw3Hcmkiq0p8JzzZz0YYgQz/mk kDQz9EU71LlqH5UISkp3/19Uo/Ll1OQElnY3evKQSM+BaxU3NATlxRLtM5koOv+hmD59 DQoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707842454; x=1708447254; 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=1HVBdFKYzveXqQX8QqB62afInbRhBG7j+4LCD4Og1D8=; b=tH7Mb04Ler38SlU6WSWpSmITilOfVD3lalERlW0zpNKK2An12DKhI1mr/tEOkg4lnf lnrGkK33TibnoIsCDM2h3pglv9d6Tl7uQvBajoIpPb8sJYCVIO0CvD24Aso58ofdqKnn rU+7HvbHZAOvqCGEihz+TlrHaEW9oakYFAaGi/ajSl7ajVK72YGtMM7y2Yzrw7gQd09a 59xFx91NAWzcaXXR7Yt1wPjWe+QAYRDl6c3dFREZF50wHrh916H6V4+cWRap/M/Gi987 uU0+ps+yVxJlAXKqsDzE3nHx4oVjrp5W0hFbMQVH3Ax1sHOmj5MY3hZ2URfDad592NC7 2Vlw== X-Gm-Message-State: AOJu0Ywm6/1HqRYW3GjL5xhA/D3H9fZUTkDTkXz2rNYHV3ZOiwMHcYQk y8Jjrvy4juyT/k6F0Ha24SbTSNcw864j2wodyRJ8REXM7uSV/KYc0TqvJ8sJ X-Google-Smtp-Source: AGHT+IH6QeJ1+CFPpOj1ljIfvSiH04pFIVpTzvnsLcQ8Xfvabc9hmaTTkBH6ksX5OiTwfKiUnRTaAg== X-Received: by 2002:a05:6870:be94:b0:218:4171:f0e1 with SMTP id nx20-20020a056870be9400b002184171f0e1mr12688852oab.59.1707842454225; Tue, 13 Feb 2024 08:40:54 -0800 (PST) Received: from gnu-tgl-2.localdomain ([172.56.168.224]) by smtp.gmail.com with ESMTPSA id 201-20020a6302d2000000b005d880b41598sm2517118pgc.94.2024.02.13.08.40.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 08:40:53 -0800 (PST) Received: from gnu-tgl-2.. (localhost [IPv6:::1]) by gnu-tgl-2.localdomain (Postfix) with ESMTP id E3A68300C97; Tue, 13 Feb 2024 08:40:52 -0800 (PST) From: "H.J. Lu" To: gcc-patches@gcc.gnu.org Cc: jakub@redhat.com Subject: [PATCH v2] x86: Support x32 and IBT in heap trampoline Date: Tue, 13 Feb 2024 08:40:52 -0800 Message-ID: <20240213164052.300183-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Spam-Status: No, score=-3021.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_SBL_CSS, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Add x32 and IBT support to x86 heap trampoline implementation with a testcase. 2024-02-13 Jakub Jelinek H.J. Lu libgcc/ PR target/113855 * config/i386/heap-trampoline.c (trampoline_insns): Add IBT support and pad to the multiple of 4 bytes. Use movabsq instead of movabs in comments. Add -mx32 variant. gcc/testsuite/ PR target/113855 * gcc.dg/heap-trampoline-1.c: New test. * lib/target-supports.exp (check_effective_target_heap_trampoline): New. --- gcc/testsuite/gcc.dg/heap-trampoline-1.c | 23 +++++++++++++ gcc/testsuite/lib/target-supports.exp | 12 +++++++ libgcc/config/i386/heap-trampoline.c | 42 ++++++++++++++++++++++-- 3 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/heap-trampoline-1.c diff --git a/gcc/testsuite/gcc.dg/heap-trampoline-1.c b/gcc/testsuite/gcc.dg/heap-trampoline-1.c new file mode 100644 index 00000000000..1aebe00d731 --- /dev/null +++ b/gcc/testsuite/gcc.dg/heap-trampoline-1.c @@ -0,0 +1,23 @@ +/* { dg-do run { target heap_trampoline } } */ +/* { dg-options "-ftrampoline-impl=heap" } */ + +__attribute__((noipa)) int +bar (int (*fn) (int)) +{ + return fn (42) + 1; +} + +int +main () +{ + int a = 0; + int foo (int x) { if (x != 42) __builtin_abort (); return ++a; } + if (bar (foo) != 2 || a != 1) + __builtin_abort (); + if (bar (foo) != 3 || a != 2) + __builtin_abort (); + a = 42; + if (bar (foo) != 44 || a != 43) + __builtin_abort (); + return 0; +} diff --git a/gcc/testsuite/lib/target-supports.exp b/gcc/testsuite/lib/target-supports.exp index 6ce8557c9a9..81715999f87 100644 --- a/gcc/testsuite/lib/target-supports.exp +++ b/gcc/testsuite/lib/target-supports.exp @@ -13477,3 +13477,15 @@ proc dg-require-python-h { args } { eval lappend extra-tool-flags $python_flags verbose "After appending, extra-tool-flags: ${extra-tool-flags}" 3 } + +# Return 1 if the target supports heap-trampoline, 0 otherwise. +proc check_effective_target_heap_trampoline {} { + if { [istarget aarch64*-*-linux*] + || [istarget i?86-*-darwin*] + || [istarget x86_64-*-darwin*] + || [istarget i?86-*-linux*] + || [istarget x86_64-*-linux*] } { + return 1 + } + return 0 +} diff --git a/libgcc/config/i386/heap-trampoline.c b/libgcc/config/i386/heap-trampoline.c index 1df0aa06108..a8637dc92d3 100644 --- a/libgcc/config/i386/heap-trampoline.c +++ b/libgcc/config/i386/heap-trampoline.c @@ -30,28 +30,64 @@ void __gcc_nested_func_ptr_created (void *chain, void *func, void *dst); void __gcc_nested_func_ptr_deleted (void); #if __x86_64__ + +#ifdef __LP64__ static const uint8_t trampoline_insns[] = { - /* movabs $,%r11 */ +#if defined __CET__ && (__CET__ & 1) != 0 + /* endbr64. */ + 0xf3, 0x0f, 0x1e, 0xfa, +#endif + + /* movabsq $,%r11 */ 0x49, 0xbb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - /* movabs $,%r10 */ + /* movabsq $,%r10 */ 0x49, 0xba, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* rex.WB jmpq *%r11 */ - 0x41, 0xff, 0xe3 + 0x41, 0xff, 0xe3, + + /* Pad to the multiple of 4 bytes. */ + 0x90 }; +#else +static const uint8_t trampoline_insns[] = { +#if defined __CET__ && (__CET__ & 1) != 0 + /* endbr64. */ + 0xf3, 0x0f, 0x1e, 0xfa, +#endif + + /* movl $,%r11d */ + 0x41, 0xbb, + 0x00, 0x00, 0x00, 0x00, + + /* movl $,%r10d */ + 0x41, 0xba, + 0x00, 0x00, 0x00, 0x00, + + /* rex.WB jmpq *%r11 */ + 0x41, 0xff, 0xe3, + + /* Pad to the multiple of 4 bytes. */ + 0x90 +}; +#endif union ix86_trampoline { uint8_t insns[sizeof(trampoline_insns)]; struct __attribute__((packed)) fields { +#if defined __CET__ && (__CET__ & 1) != 0 + uint8_t endbr64[4]; +#endif uint8_t insn_0[2]; void *func_ptr; uint8_t insn_1[2]; void *chain_ptr; uint8_t insn_2[3]; + uint8_t pad; } fields; };