From patchwork Wed Jan 29 09:49:36 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yury Khrustalev X-Patchwork-Id: 105599 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 48F3B3857C47 for ; Wed, 29 Jan 2025 09:52:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 48F3B3857C47 X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by sourceware.org (Postfix) with ESMTP id AA68B3858D20 for ; Wed, 29 Jan 2025 09:50:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AA68B3858D20 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=arm.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org AA68B3858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1738144205; cv=none; b=CsZREJ3qSc8Lb5fFOpuxhlG6h6XB+6H3yfBBoRTfPT6tWSqymCJd575HhnycVpNvGqLkFFZefP0zy5EAXYbM1E5xq/F+IDnBPmhJLyMJTjEraxLsyIiTiekUTvSBozIVWK9evUMcTegysVzJs8QUkAt7aWveWkpCzdRBlR31du4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1738144205; c=relaxed/simple; bh=EblHcETQiO2eBubwTzlWswl0UWogbst9f/QEs/KVP04=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=YL4W3yShaHv0T4Bd4YJEewg0aS4k5PvDs6FQO3xIuCiRWo6rk/kyacs6VIbwpDi3W9Mb9IEIF2KKBT+cm8KONjnCfe6V7bzGO1gk8apcfRr52S3bvO5dG4DgSjFrYU2u8dPb/Ap9Cll1DgXFC/mNH1Z7UY3T4nF661l8DtvxsHk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AA68B3858D20 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id A745D497; Wed, 29 Jan 2025 01:50:31 -0800 (PST) Received: from udebian.localdomain (unknown [10.1.37.167]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 1CE813F694; Wed, 29 Jan 2025 01:50:04 -0800 (PST) From: Yury Khrustalev To: libc-alpha@sourceware.org Cc: adhemerval.zanella@linaro.org Subject: [PATCH v3 1/5] aarch64: Add configure check for linker support for -z gcs Date: Wed, 29 Jan 2025 09:49:36 +0000 Message-Id: <20250129094940.2643944-2-yury.khrustalev@arm.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250129094940.2643944-1-yury.khrustalev@arm.com> References: <20250129094940.2643944-1-yury.khrustalev@arm.com> MIME-Version: 1.0 X-Spam-Status: No, score=-13.3 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, KAM_LAZY_DOMAIN_SECURITY, SPF_HELO_NONE, SPF_NONE, TXREP 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: 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~patchwork=sourceware.org@sourceware.org --- configure | 36 ++++++++++++++++++++++++++++++++++++ configure.ac | 5 +++++ 2 files changed, 41 insertions(+) diff --git a/configure b/configure index eb8abd0054..19cae4aec7 100755 --- a/configure +++ b/configure @@ -8908,6 +8908,42 @@ printf "%s\n" "$libc_linker_feature" >&6; } config_vars="$config_vars load-address-ldflag = $libc_cv_load_address_ldflag" +# Check if linker supports GCS marking +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for linker that supports -z gcs=always" >&5 +printf %s "checking for linker that supports -z gcs=always... " >&6; } +libc_linker_feature=no +cat > conftest.c <&5 + (eval $ac_try) 2>&5 + ac_status=$? + printf "%s\n" "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 + test $ac_status = 0; }; } +then + if ${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS $no_ssp -Wl,-z,gcs=always -nostdlib \ + -nostartfiles -fPIC -shared -o conftest.so conftest.c 2>&1 \ + | grep "warning: -z gcs=always ignored" > /dev/null 2>&1; then + true + else + libc_linker_feature=yes + fi +fi +rm -f conftest* +if test $libc_linker_feature = yes; then + libc_cv_test_gcs=yes +else + libc_cv_test_gcs=no +fi +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $libc_linker_feature" >&5 +printf "%s\n" "$libc_linker_feature" >&6; } +config_vars="$config_vars +have-test-cc-gcs = $libc_cv_test_gcs" + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking if we can build programs as PIE" >&5 printf %s "checking if we can build programs as PIE... " >&6; } cat confdefs.h - <<_ACEOF >conftest.$ac_ext diff --git a/configure.ac b/configure.ac index 050bfa65e3..4c30d5a5b7 100644 --- a/configure.ac +++ b/configure.ac @@ -1992,6 +1992,11 @@ LIBC_LINKER_FEATURE([-Ttext-segment=$libc_cv_pde_load_address], [libc_cv_load_address_ldflag=]) LIBC_CONFIG_VAR([load-address-ldflag], [$libc_cv_load_address_ldflag]) +# Check if linker supports GCS marking +LIBC_LINKER_FEATURE([-z gcs=always], [-Wl,-z,gcs=always], + [libc_cv_test_gcs=yes], [libc_cv_test_gcs=no]) +LIBC_CONFIG_VAR([have-test-cc-gcs], [$libc_cv_test_gcs]) + AC_MSG_CHECKING(if we can build programs as PIE) AC_COMPILE_IFELSE([AC_LANG_SOURCE([[#ifdef PIE_UNSUPPORTED # error PIE is not supported From patchwork Wed Jan 29 09:49:37 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yury Khrustalev X-Patchwork-Id: 105597 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5C6CA3857B94 for ; Wed, 29 Jan 2025 09:51:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5C6CA3857B94 X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by sourceware.org (Postfix) with ESMTP id D35A93857BA3 for ; Wed, 29 Jan 2025 09:50:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D35A93857BA3 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=arm.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D35A93857BA3 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1738144210; cv=none; b=BgbQ+GuJVkVUr2+soNWf570chlDMwe7CdQ5srlpLpS/E/BLwGfsnTOehq9rPL/7GDfbCt2r6lN55r5zwKvwX2Qp88z5ccCQJJ1xRUblsYIab27VFKhJVRRTV/L/vZf8FQJ8RVwQr97ogpHa+dZ8GpaQukuHIJwXeKFWFTAxDimQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1738144210; c=relaxed/simple; bh=kH4G/YLbYq3wKPwSW2WPTfosWRO5ZMyfPh7s44Z0Ceg=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=ozdcpWRroNFnlTFXBD+U0qiXHMLpHt3rcNqBZkU2txzU7lSO7P0r0mKybi/MEFYhahsa8qFxrRH9VeRbCD31Qn+ARZaaAdusF/fsMyz76nX6X1SxSk4gqH+RZX5jVi6bRalSspPJpUpzsOJ9LU3Q2f+ZBlt5UlozH7nOK+etCAE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D35A93857BA3 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id C5C4F497; Wed, 29 Jan 2025 01:50:36 -0800 (PST) Received: from udebian.localdomain (unknown [10.1.37.167]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 1BAE43F694; Wed, 29 Jan 2025 01:50:09 -0800 (PST) From: Yury Khrustalev To: libc-alpha@sourceware.org Cc: adhemerval.zanella@linaro.org Subject: [PATCH v3 2/5] aarch64: Add tests for Guarded Control Stack Date: Wed, 29 Jan 2025 09:49:37 +0000 Message-Id: <20250129094940.2643944-3-yury.khrustalev@arm.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250129094940.2643944-1-yury.khrustalev@arm.com> References: <20250129094940.2643944-1-yury.khrustalev@arm.com> MIME-Version: 1.0 X-Spam-Status: No, score=-13.3 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, KAM_LAZY_DOMAIN_SECURITY, KAM_SHORT, SPF_HELO_NONE, SPF_NONE, TXREP 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: 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~patchwork=sourceware.org@sourceware.org These tests validate that GCS tunable works as expected depending on the GCS markings in the test binaries. Tests validate both static and dynamically linked binaries. These new tests are AArch64 specific. Moreover, they are included only if linker supports the "-z gcs=" option. If built, these tests will run on systems with and without HWCAP_GCS. In the latter case the tests will be reported as UNSUPPORTED. --- sysdeps/unix/sysv/linux/aarch64/Makefile | 72 ++++++++++++++++++- .../unix/sysv/linux/aarch64/tst-gcs-abort.sh | 40 +++++++++++ .../linux/aarch64/tst-gcs-disabled-static.c | 1 + .../sysv/linux/aarch64/tst-gcs-disabled.c | 2 + .../linux/aarch64/tst-gcs-enforced-abort.c | 2 + .../aarch64/tst-gcs-enforced-static-abort.c | 1 + .../linux/aarch64/tst-gcs-enforced-static.c | 1 + .../sysv/linux/aarch64/tst-gcs-enforced.c | 2 + .../sysv/linux/aarch64/tst-gcs-optional-off.c | 2 + .../sysv/linux/aarch64/tst-gcs-optional-on.c | 2 + .../aarch64/tst-gcs-optional-static-off.c | 1 + .../aarch64/tst-gcs-optional-static-on.c | 1 + .../linux/aarch64/tst-gcs-override-static.c | 1 + .../sysv/linux/aarch64/tst-gcs-override.c | 2 + .../sysv/linux/aarch64/tst-gcs-skeleton.c | 58 +++++++++++++++ 15 files changed, 187 insertions(+), 1 deletion(-) create mode 100644 sysdeps/unix/sysv/linux/aarch64/tst-gcs-abort.sh create mode 100644 sysdeps/unix/sysv/linux/aarch64/tst-gcs-disabled-static.c create mode 100644 sysdeps/unix/sysv/linux/aarch64/tst-gcs-disabled.c create mode 100644 sysdeps/unix/sysv/linux/aarch64/tst-gcs-enforced-abort.c create mode 100644 sysdeps/unix/sysv/linux/aarch64/tst-gcs-enforced-static-abort.c create mode 100644 sysdeps/unix/sysv/linux/aarch64/tst-gcs-enforced-static.c create mode 100644 sysdeps/unix/sysv/linux/aarch64/tst-gcs-enforced.c create mode 100644 sysdeps/unix/sysv/linux/aarch64/tst-gcs-optional-off.c create mode 100644 sysdeps/unix/sysv/linux/aarch64/tst-gcs-optional-on.c create mode 100644 sysdeps/unix/sysv/linux/aarch64/tst-gcs-optional-static-off.c create mode 100644 sysdeps/unix/sysv/linux/aarch64/tst-gcs-optional-static-on.c create mode 100644 sysdeps/unix/sysv/linux/aarch64/tst-gcs-override-static.c create mode 100644 sysdeps/unix/sysv/linux/aarch64/tst-gcs-override.c create mode 100644 sysdeps/unix/sysv/linux/aarch64/tst-gcs-skeleton.c diff --git a/sysdeps/unix/sysv/linux/aarch64/Makefile b/sysdeps/unix/sysv/linux/aarch64/Makefile index 1fdad67fae..41110ab496 100644 --- a/sysdeps/unix/sysv/linux/aarch64/Makefile +++ b/sysdeps/unix/sysv/linux/aarch64/Makefile @@ -3,7 +3,77 @@ sysdep_headers += sys/elf.h tests += \ tst-aarch64-pkey \ # tests -endif + +ifeq ($(have-test-cc-gcs),yes) + +gcs-tests-dynamic = \ + tst-gcs-disabled \ + tst-gcs-enforced \ + tst-gcs-enforced-abort \ + tst-gcs-optional-off \ + tst-gcs-optional-on \ + tst-gcs-override \ + # gcs-tests-dynamic + +gcs-tests-static = \ + tst-gcs-disabled-static \ + tst-gcs-enforced-static \ + tst-gcs-enforced-static-abort \ + tst-gcs-optional-static-off \ + tst-gcs-optional-static-on \ + tst-gcs-override-static \ + # gcs-tests-static + +tests += \ + $(gcs-tests-dynamic) \ + $(gcs-tests-static) \ + # tests + +tests-static += \ + $(gcs-tests-static) \ + # tests-static + +define run-gcs-abort-test + $(test-wrapper-env) $(run-program-env) \ + $(tst-gcs-$*-abort-ENV) $(host-test-program-cmd) +endef + +$(objpfx)tst-gcs-%-abort.out: $(..)sysdeps/unix/sysv/linux/aarch64/tst-gcs-abort.sh \ + $(objpfx)tst-gcs-%-abort + $(SHELL) $< $(common-objpfx) $(test-name) '$(run-gcs-abort-test)'; \ + $(evaluate-test) + +LDFLAGS-tst-gcs-disabled += -Wl,-z gcs=always +LDFLAGS-tst-gcs-enforced += -Wl,-z gcs=always +LDFLAGS-tst-gcs-enforced-abort += -Wl,-z gcs=never +LDFLAGS-tst-gcs-optional-on += -Wl,-z gcs=always +LDFLAGS-tst-gcs-optional-off += -Wl,-z gcs=never +LDFLAGS-tst-gcs-override += -Wl,-z gcs=never + +LDFLAGS-tst-gcs-disabled-static += -Wl,-z gcs=always +LDFLAGS-tst-gcs-enforced-static += -Wl,-z gcs=always +LDFLAGS-tst-gcs-enforced-static-abort += -Wl,-z gcs=never +LDFLAGS-tst-gcs-optional-static-on += -Wl,-z gcs=always +LDFLAGS-tst-gcs-optional-static-off += -Wl,-z gcs=never +LDFLAGS-tst-gcs-override-static += -Wl,-z gcs=never + +tst-gcs-disabled-ENV = GLIBC_TUNABLES=glibc.cpu.aarch64_gcs=0 +tst-gcs-enforced-ENV = GLIBC_TUNABLES=glibc.cpu.aarch64_gcs=1 +tst-gcs-enforced-abort-ENV = GLIBC_TUNABLES=glibc.cpu.aarch64_gcs=1 +tst-gcs-optional-on-ENV = GLIBC_TUNABLES=glibc.cpu.aarch64_gcs=2 +tst-gcs-optional-off-ENV = GLIBC_TUNABLES=glibc.cpu.aarch64_gcs=2 +tst-gcs-override-ENV = GLIBC_TUNABLES=glibc.cpu.aarch64_gcs=3 + +tst-gcs-disabled-static-ENV = GLIBC_TUNABLES=glibc.cpu.aarch64_gcs=0 +tst-gcs-enforced-static-ENV = GLIBC_TUNABLES=glibc.cpu.aarch64_gcs=1 +tst-gcs-enforced-static-abort-ENV = GLIBC_TUNABLES=glibc.cpu.aarch64_gcs=1 +tst-gcs-optional-static-on-ENV = GLIBC_TUNABLES=glibc.cpu.aarch64_gcs=2 +tst-gcs-optional-static-off-ENV = GLIBC_TUNABLES=glibc.cpu.aarch64_gcs=2 +tst-gcs-override-static-ENV = GLIBC_TUNABLES=glibc.cpu.aarch64_gcs=3 + +endif # ifeq ($(have-test-cc-gcs),yes) + +endif # ifeq ($(subdir),misc) ifeq ($(subdir),stdlib) gen-as-const-headers += ucontext_i.sym diff --git a/sysdeps/unix/sysv/linux/aarch64/tst-gcs-abort.sh b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-abort.sh new file mode 100644 index 0000000000..0f43843969 --- /dev/null +++ b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-abort.sh @@ -0,0 +1,40 @@ +#!/bin/sh +# Test wrapper for AArch64 tests for GCS that are expected to abort. +# 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 +# . + +objpfx=$1; shift +tstname=$1; shift +tstrun=$1; shift + +logfile=$objpfx/$tstname.out + +rm -vf $logfile +touch $logfile + +${tstrun} 2>> $logfile >> $logfile; status=$? + +if test $status -eq 127 \ + && grep -q -w "not GCS compatible" "$logfile" ; then + exit 0 +elif test $status -eq 77; then + exit 77 +else + echo "unexpected test output or exit status $status" + exit 1 +fi + diff --git a/sysdeps/unix/sysv/linux/aarch64/tst-gcs-disabled-static.c b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-disabled-static.c new file mode 100644 index 0000000000..c71d68cb86 --- /dev/null +++ b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-disabled-static.c @@ -0,0 +1 @@ +#include "tst-gcs-disabled.c" diff --git a/sysdeps/unix/sysv/linux/aarch64/tst-gcs-disabled.c b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-disabled.c new file mode 100644 index 0000000000..bd688785bb --- /dev/null +++ b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-disabled.c @@ -0,0 +1,2 @@ +#define TEST_GCS_EXPECT_ENABLED 0 +#include "tst-gcs-skeleton.c" diff --git a/sysdeps/unix/sysv/linux/aarch64/tst-gcs-enforced-abort.c b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-enforced-abort.c new file mode 100644 index 0000000000..608318f26d --- /dev/null +++ b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-enforced-abort.c @@ -0,0 +1,2 @@ +#define TEST_GCS_EXPECT_ENABLED 1 +#include "tst-gcs-skeleton.c" diff --git a/sysdeps/unix/sysv/linux/aarch64/tst-gcs-enforced-static-abort.c b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-enforced-static-abort.c new file mode 100644 index 0000000000..c20a999f6b --- /dev/null +++ b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-enforced-static-abort.c @@ -0,0 +1 @@ +#include "tst-gcs-enforced-abort.c" diff --git a/sysdeps/unix/sysv/linux/aarch64/tst-gcs-enforced-static.c b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-enforced-static.c new file mode 100644 index 0000000000..bb39dada55 --- /dev/null +++ b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-enforced-static.c @@ -0,0 +1 @@ +#include "tst-gcs-enforced.c" diff --git a/sysdeps/unix/sysv/linux/aarch64/tst-gcs-enforced.c b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-enforced.c new file mode 100644 index 0000000000..608318f26d --- /dev/null +++ b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-enforced.c @@ -0,0 +1,2 @@ +#define TEST_GCS_EXPECT_ENABLED 1 +#include "tst-gcs-skeleton.c" diff --git a/sysdeps/unix/sysv/linux/aarch64/tst-gcs-optional-off.c b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-optional-off.c new file mode 100644 index 0000000000..bd688785bb --- /dev/null +++ b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-optional-off.c @@ -0,0 +1,2 @@ +#define TEST_GCS_EXPECT_ENABLED 0 +#include "tst-gcs-skeleton.c" diff --git a/sysdeps/unix/sysv/linux/aarch64/tst-gcs-optional-on.c b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-optional-on.c new file mode 100644 index 0000000000..608318f26d --- /dev/null +++ b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-optional-on.c @@ -0,0 +1,2 @@ +#define TEST_GCS_EXPECT_ENABLED 1 +#include "tst-gcs-skeleton.c" diff --git a/sysdeps/unix/sysv/linux/aarch64/tst-gcs-optional-static-off.c b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-optional-static-off.c new file mode 100644 index 0000000000..54e3b9a0d5 --- /dev/null +++ b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-optional-static-off.c @@ -0,0 +1 @@ +#include "tst-gcs-optional-off.c" diff --git a/sysdeps/unix/sysv/linux/aarch64/tst-gcs-optional-static-on.c b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-optional-static-on.c new file mode 100644 index 0000000000..11b884b429 --- /dev/null +++ b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-optional-static-on.c @@ -0,0 +1 @@ +#include "tst-gcs-optional-on.c" diff --git a/sysdeps/unix/sysv/linux/aarch64/tst-gcs-override-static.c b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-override-static.c new file mode 100644 index 0000000000..09055dcdc4 --- /dev/null +++ b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-override-static.c @@ -0,0 +1 @@ +#include "tst-gcs-override.c" diff --git a/sysdeps/unix/sysv/linux/aarch64/tst-gcs-override.c b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-override.c new file mode 100644 index 0000000000..608318f26d --- /dev/null +++ b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-override.c @@ -0,0 +1,2 @@ +#define TEST_GCS_EXPECT_ENABLED 1 +#include "tst-gcs-skeleton.c" diff --git a/sysdeps/unix/sysv/linux/aarch64/tst-gcs-skeleton.c b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-skeleton.c new file mode 100644 index 0000000000..37692c96d0 --- /dev/null +++ b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-skeleton.c @@ -0,0 +1,58 @@ +/* AArch64 tests for GCS. + 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 + +#include +#include + +static bool __check_gcs_status (void) +{ + register unsigned long x16 asm ("x16"); + asm volatile ( + "mov x16, #1 /* _CHKFEAT_GCS */\n" + "hint 40 /* CHKFEAT_X16 */\n" + : "=r" (x16)); + return x16 ^ 1; +} + +static int +do_test (void) +{ + /* Check if GCS could possible by enabled. */ + if (!(getauxval (AT_HWCAP) & HWCAP_GCS)) + { + puts ("kernel or CPU does not support GCS"); + return EXIT_UNSUPPORTED; + } + bool gcs_enabled = __check_gcs_status (); + if (gcs_enabled) + puts ("GCS enabled"); + else + puts ("GCS not enabled"); +#if TEST_GCS_EXPECT_ENABLED + TEST_VERIFY (gcs_enabled); +#else + TEST_VERIFY (!gcs_enabled); +#endif + return 0; +} + +#include From patchwork Wed Jan 29 09:49:38 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yury Khrustalev X-Patchwork-Id: 105600 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C1B673857B90 for ; Wed, 29 Jan 2025 09:52:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C1B673857B90 X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by sourceware.org (Postfix) with ESMTP id 0E9A93857C58 for ; Wed, 29 Jan 2025 09:50:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0E9A93857C58 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=arm.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 0E9A93857C58 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1738144216; cv=none; b=d7UBiLiwwtOIZdxskz11qbwJhyfPt9Pc2CzyBBmfUReROW2i66WyPKxZVqcPBkyOJkKrml+g5HvJxli+j6QFyUV2MKMLMGVKtU+gMfovexkYkbk9HD1vtBARjw5eZztLmLD9n5jCFtKHXQtGzZguqoBTiuFvoh9TcnkSFqzCSgU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1738144216; c=relaxed/simple; bh=R1SSCqmgaY8PEy/B2sE451csGuQ4Ea72FaXu7gTDAbI=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=BqRYqK9ARuZHYI7ram8qj5E9JlI6WR+BGJNi5aBogN5RG3zWs9me/kAvuuvn0M6vJP5EtfnAtF/mHgczAUtmAiWbnjv37k8YN/feiQU79vPV3Fa4fvz6jJxgHSrO+ttmknzOSsTjhMKmoNkrdDLyMNfyFznwr0vGHXUYRE1Rab8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0E9A93857C58 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id F0DD51007; Wed, 29 Jan 2025 01:50:41 -0800 (PST) Received: from udebian.localdomain (unknown [10.1.37.167]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 459183F694; Wed, 29 Jan 2025 01:50:15 -0800 (PST) From: Yury Khrustalev To: libc-alpha@sourceware.org Cc: adhemerval.zanella@linaro.org Subject: [PATCH v3 3/5] aarch64: Add GCS tests for transitive dependencies Date: Wed, 29 Jan 2025 09:49:38 +0000 Message-Id: <20250129094940.2643944-4-yury.khrustalev@arm.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250129094940.2643944-1-yury.khrustalev@arm.com> References: <20250129094940.2643944-1-yury.khrustalev@arm.com> MIME-Version: 1.0 X-Spam-Status: No, score=-13.3 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, KAM_LAZY_DOMAIN_SECURITY, KAM_SHORT, SPF_HELO_NONE, SPF_NONE, TXREP 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: 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~patchwork=sourceware.org@sourceware.org --- sysdeps/unix/sysv/linux/aarch64/Makefile | 29 +++++++++++++ .../unix/sysv/linux/aarch64/tst-gcs-helper.h | 39 ++++++++++++++++++ .../unix/sysv/linux/aarch64/tst-gcs-mod1.c | 27 ++++++++++++ .../unix/sysv/linux/aarch64/tst-gcs-mod2.c | 25 +++++++++++ .../unix/sysv/linux/aarch64/tst-gcs-mod3.c | 25 +++++++++++ .../linux/aarch64/tst-gcs-shared-disabled.c | 2 + .../aarch64/tst-gcs-shared-enforced-abort.c | 2 + .../linux/aarch64/tst-gcs-shared-optional.c | 2 + .../linux/aarch64/tst-gcs-shared-override.c | 2 + .../unix/sysv/linux/aarch64/tst-gcs-shared.c | 41 +++++++++++++++++++ .../sysv/linux/aarch64/tst-gcs-skeleton.c | 17 +------- 11 files changed, 195 insertions(+), 16 deletions(-) create mode 100644 sysdeps/unix/sysv/linux/aarch64/tst-gcs-helper.h create mode 100644 sysdeps/unix/sysv/linux/aarch64/tst-gcs-mod1.c create mode 100644 sysdeps/unix/sysv/linux/aarch64/tst-gcs-mod2.c create mode 100644 sysdeps/unix/sysv/linux/aarch64/tst-gcs-mod3.c create mode 100644 sysdeps/unix/sysv/linux/aarch64/tst-gcs-shared-disabled.c create mode 100644 sysdeps/unix/sysv/linux/aarch64/tst-gcs-shared-enforced-abort.c create mode 100644 sysdeps/unix/sysv/linux/aarch64/tst-gcs-shared-optional.c create mode 100644 sysdeps/unix/sysv/linux/aarch64/tst-gcs-shared-override.c create mode 100644 sysdeps/unix/sysv/linux/aarch64/tst-gcs-shared.c diff --git a/sysdeps/unix/sysv/linux/aarch64/Makefile b/sysdeps/unix/sysv/linux/aarch64/Makefile index 41110ab496..68e97a95c7 100644 --- a/sysdeps/unix/sysv/linux/aarch64/Makefile +++ b/sysdeps/unix/sysv/linux/aarch64/Makefile @@ -13,6 +13,10 @@ gcs-tests-dynamic = \ tst-gcs-optional-off \ tst-gcs-optional-on \ tst-gcs-override \ + tst-gcs-shared-disabled \ + tst-gcs-shared-enforced-abort \ + tst-gcs-shared-optional \ + tst-gcs-shared-override \ # gcs-tests-dynamic gcs-tests-static = \ @@ -71,6 +75,31 @@ tst-gcs-optional-static-on-ENV = GLIBC_TUNABLES=glibc.cpu.aarch64_gcs=2 tst-gcs-optional-static-off-ENV = GLIBC_TUNABLES=glibc.cpu.aarch64_gcs=2 tst-gcs-override-static-ENV = GLIBC_TUNABLES=glibc.cpu.aarch64_gcs=3 +# force one of the dependencies to be unmarked +LDFLAGS-tst-gcs-mod2.so += -Wl,-z gcs=never + +LDFLAGS-tst-gcs-shared-disabled = -Wl,-z gcs=always +LDFLAGS-tst-gcs-shared-enforced-abort = -Wl,-z gcs=always +LDFLAGS-tst-gcs-shared-optional = -Wl,-z gcs=always +LDFLAGS-tst-gcs-shared-override = -Wl,-z gcs=always + +modules-names += \ + tst-gcs-mod1 \ + tst-gcs-mod2 \ + tst-gcs-mod3 \ + # modules-names + +$(objpfx)tst-gcs-shared-disabled: $(objpfx)tst-gcs-mod1.so $(objpfx)tst-gcs-mod3.so +$(objpfx)tst-gcs-shared-enforced-abort: $(objpfx)tst-gcs-mod1.so $(objpfx)tst-gcs-mod3.so +$(objpfx)tst-gcs-shared-optional: $(objpfx)tst-gcs-mod1.so $(objpfx)tst-gcs-mod3.so +$(objpfx)tst-gcs-shared-override: $(objpfx)tst-gcs-mod1.so $(objpfx)tst-gcs-mod3.so +$(objpfx)tst-gcs-mod1.so: $(objpfx)tst-gcs-mod2.so + +tst-gcs-shared-disabled-ENV = GLIBC_TUNABLES=glibc.cpu.aarch64_gcs=0 +tst-gcs-shared-enforced-abort-ENV = GLIBC_TUNABLES=glibc.cpu.aarch64_gcs=1 +tst-gcs-shared-optional-ENV = GLIBC_TUNABLES=glibc.cpu.aarch64_gcs=2 +tst-gcs-shared-override-ENV = GLIBC_TUNABLES=glibc.cpu.aarch64_gcs=3 + endif # ifeq ($(have-test-cc-gcs),yes) endif # ifeq ($(subdir),misc) diff --git a/sysdeps/unix/sysv/linux/aarch64/tst-gcs-helper.h b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-helper.h new file mode 100644 index 0000000000..ba092d47f5 --- /dev/null +++ b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-helper.h @@ -0,0 +1,39 @@ +/* AArch64 tests for GCS. + 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 + . */ + +#ifndef TST_GCS_HELPER_H +#define POINTER_GUARD_H + +#include +#include +#include + +#include +#include + +static bool __check_gcs_status (void) +{ + register unsigned long x16 asm ("x16"); + asm volatile ( + "mov x16, #1 /* _CHKFEAT_GCS */\n" + "hint 40 /* CHKFEAT_X16 */\n" + : "=r" (x16)); + return x16 ^ 1; +} + +#endif // POINTER_GUARD_H diff --git a/sysdeps/unix/sysv/linux/aarch64/tst-gcs-mod1.c b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-mod1.c new file mode 100644 index 0000000000..931ff81797 --- /dev/null +++ b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-mod1.c @@ -0,0 +1,27 @@ +/* DSO for testing GCS. + 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 + +int fun2 (void); // tst-gcs-mod2.c + +int fun1 (void) +{ + puts ("called function fun1"); + return fun2 (); +} diff --git a/sysdeps/unix/sysv/linux/aarch64/tst-gcs-mod2.c b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-mod2.c new file mode 100644 index 0000000000..f9370eb8ff --- /dev/null +++ b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-mod2.c @@ -0,0 +1,25 @@ +/* DSO for testing GCS. + 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 + +int fun2 (void) +{ + puts ("called function fun2"); + return 0; +} diff --git a/sysdeps/unix/sysv/linux/aarch64/tst-gcs-mod3.c b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-mod3.c new file mode 100644 index 0000000000..38bb357547 --- /dev/null +++ b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-mod3.c @@ -0,0 +1,25 @@ +/* DSO for testing GCS. + 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 + +int fun3 (void) +{ + puts ("called function fun3"); + return 0; +} diff --git a/sysdeps/unix/sysv/linux/aarch64/tst-gcs-shared-disabled.c b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-shared-disabled.c new file mode 100644 index 0000000000..8598dc44b5 --- /dev/null +++ b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-shared-disabled.c @@ -0,0 +1,2 @@ +#define TEST_GCS_EXPECT_ENABLED 0 +#include "tst-gcs-shared.c" diff --git a/sysdeps/unix/sysv/linux/aarch64/tst-gcs-shared-enforced-abort.c b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-shared-enforced-abort.c new file mode 100644 index 0000000000..f1333cee9d --- /dev/null +++ b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-shared-enforced-abort.c @@ -0,0 +1,2 @@ +#define TEST_GCS_EXPECT_ENABLED 1 +#include "tst-gcs-shared.c" diff --git a/sysdeps/unix/sysv/linux/aarch64/tst-gcs-shared-optional.c b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-shared-optional.c new file mode 100644 index 0000000000..8598dc44b5 --- /dev/null +++ b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-shared-optional.c @@ -0,0 +1,2 @@ +#define TEST_GCS_EXPECT_ENABLED 0 +#include "tst-gcs-shared.c" diff --git a/sysdeps/unix/sysv/linux/aarch64/tst-gcs-shared-override.c b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-shared-override.c new file mode 100644 index 0000000000..f1333cee9d --- /dev/null +++ b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-shared-override.c @@ -0,0 +1,2 @@ +#define TEST_GCS_EXPECT_ENABLED 1 +#include "tst-gcs-shared.c" diff --git a/sysdeps/unix/sysv/linux/aarch64/tst-gcs-shared.c b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-shared.c new file mode 100644 index 0000000000..1192de69fc --- /dev/null +++ b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-shared.c @@ -0,0 +1,41 @@ +/* AArch64 tests for GCS. + 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 "tst-gcs-helper.h" + +int fun1 (void); // tst-gcs-mod1.c +int fun3 (void); // tst-gcs-mod3.c + +static int +do_test (void) +{ + /* Check if GCS could possible by enabled. */ + if (!(getauxval (AT_HWCAP) & HWCAP_GCS)) + { + puts ("kernel or CPU does not support GCS"); + return EXIT_UNSUPPORTED; + } +#if TEST_GCS_EXPECT_ENABLED + TEST_VERIFY (__check_gcs_status ()); +#else + TEST_VERIFY (!__check_gcs_status ()); +#endif + return fun1 () + fun3 (); +} + +#include diff --git a/sysdeps/unix/sysv/linux/aarch64/tst-gcs-skeleton.c b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-skeleton.c index 37692c96d0..feb5e33ebf 100644 --- a/sysdeps/unix/sysv/linux/aarch64/tst-gcs-skeleton.c +++ b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-skeleton.c @@ -16,22 +16,7 @@ License along with the GNU C Library; if not, see . */ -#include -#include -#include - -#include -#include - -static bool __check_gcs_status (void) -{ - register unsigned long x16 asm ("x16"); - asm volatile ( - "mov x16, #1 /* _CHKFEAT_GCS */\n" - "hint 40 /* CHKFEAT_X16 */\n" - : "=r" (x16)); - return x16 ^ 1; -} +#include "tst-gcs-helper.h" static int do_test (void) From patchwork Wed Jan 29 09:49:39 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yury Khrustalev X-Patchwork-Id: 105601 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CD18B3857BB9 for ; Wed, 29 Jan 2025 09:54:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CD18B3857BB9 X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by sourceware.org (Postfix) with ESMTP id 78B523857BBB for ; Wed, 29 Jan 2025 09:50:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 78B523857BBB Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=arm.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 78B523857BBB Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1738144221; cv=none; b=dHx4o10Itqn/yeHZyOQe9lABuHeEx+S5bVyBLyJ90umP6D60WWQS+hgxQD+Kj3oLRKehrAlDv6X2bPEcu/hLVhVKjRxC9KNOIfjemp8OuGw/eItl7fUmA8p0i3fwUFfc17AiZEIjKfWsMMrAdpUCMU4kbO9u3iz6d9/F5JuB6zU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1738144221; c=relaxed/simple; bh=bC4WvupES75N+sMimIgqn/M/zhscL3wpbn+d2nbQ+tg=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=H1fbbcUD7TrPfmK2zIv1slduMHm08mmO8/JOKtvtVjnyjY8NdgnIu4heiiG4pMmTfQoVHzOd4kgBSg/lizORRX1fHrlXUFCtlpwwnEAozHOs6u+U3eCBIBgI3rAL+q6YU43bqwhUvCl3V2PsF2GAnSga2R473K63wzwO4zXa8AU= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 78B523857BBB Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 6ABA4497; Wed, 29 Jan 2025 01:50:47 -0800 (PST) Received: from udebian.localdomain (unknown [10.1.37.167]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id B4AD83F694; Wed, 29 Jan 2025 01:50:20 -0800 (PST) From: Yury Khrustalev To: libc-alpha@sourceware.org Cc: adhemerval.zanella@linaro.org Subject: [PATCH v3 4/5] aarch64: Add GCS tests for dlopen Date: Wed, 29 Jan 2025 09:49:39 +0000 Message-Id: <20250129094940.2643944-5-yury.khrustalev@arm.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250129094940.2643944-1-yury.khrustalev@arm.com> References: <20250129094940.2643944-1-yury.khrustalev@arm.com> MIME-Version: 1.0 X-Spam-Status: No, score=-13.3 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, KAM_LAZY_DOMAIN_SECURITY, KAM_SHORT, SPF_HELO_NONE, SPF_NONE, TXREP 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: 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~patchwork=sourceware.org@sourceware.org --- Makeconfig | 2 +- sysdeps/unix/sysv/linux/aarch64/Makefile | 23 +++++++ .../linux/aarch64/tst-gcs-dlopen-disabled.c | 3 + .../linux/aarch64/tst-gcs-dlopen-enforced.c | 3 + .../aarch64/tst-gcs-dlopen-optional-off.c | 3 + .../aarch64/tst-gcs-dlopen-optional-on.c | 3 + .../linux/aarch64/tst-gcs-dlopen-override.c | 3 + .../unix/sysv/linux/aarch64/tst-gcs-dlopen.c | 62 +++++++++++++++++++ 8 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 sysdeps/unix/sysv/linux/aarch64/tst-gcs-dlopen-disabled.c create mode 100644 sysdeps/unix/sysv/linux/aarch64/tst-gcs-dlopen-enforced.c create mode 100644 sysdeps/unix/sysv/linux/aarch64/tst-gcs-dlopen-optional-off.c create mode 100644 sysdeps/unix/sysv/linux/aarch64/tst-gcs-dlopen-optional-on.c create mode 100644 sysdeps/unix/sysv/linux/aarch64/tst-gcs-dlopen-override.c create mode 100644 sysdeps/unix/sysv/linux/aarch64/tst-gcs-dlopen.c diff --git a/Makeconfig b/Makeconfig index d0108d2caa..aa547a443f 100644 --- a/Makeconfig +++ b/Makeconfig @@ -633,7 +633,7 @@ link-libc-printers-tests = $(link-libc-rpath) \ $(link-libc-tests-after-rpath-link) # This is how to find at build-time things that will be installed there. -rpath-dirs = math elf dlfcn nss nis rt resolv mathvec support +rpath-dirs = math elf dlfcn nss nis rt resolv mathvec support misc rpath-link = \ $(common-objdir):$(subst $(empty) ,:,$(patsubst ../$(subdir),.,$(rpath-dirs:%=$(common-objpfx)%))) else # build-static diff --git a/sysdeps/unix/sysv/linux/aarch64/Makefile b/sysdeps/unix/sysv/linux/aarch64/Makefile index 68e97a95c7..1954849a8d 100644 --- a/sysdeps/unix/sysv/linux/aarch64/Makefile +++ b/sysdeps/unix/sysv/linux/aarch64/Makefile @@ -8,6 +8,11 @@ ifeq ($(have-test-cc-gcs),yes) gcs-tests-dynamic = \ tst-gcs-disabled \ + tst-gcs-dlopen-disabled \ + tst-gcs-dlopen-enforced \ + tst-gcs-dlopen-optional-off \ + tst-gcs-dlopen-optional-on \ + tst-gcs-dlopen-override \ tst-gcs-enforced \ tst-gcs-enforced-abort \ tst-gcs-optional-off \ @@ -100,6 +105,24 @@ tst-gcs-shared-enforced-abort-ENV = GLIBC_TUNABLES=glibc.cpu.aarch64_gcs=1 tst-gcs-shared-optional-ENV = GLIBC_TUNABLES=glibc.cpu.aarch64_gcs=2 tst-gcs-shared-override-ENV = GLIBC_TUNABLES=glibc.cpu.aarch64_gcs=3 +LDFLAGS-tst-gcs-dlopen-disabled = -Wl,-z gcs=always +LDFLAGS-tst-gcs-dlopen-enforced = -Wl,-z gcs=always +LDFLAGS-tst-gcs-dlopen-optional-on = -Wl,-z gcs=always +LDFLAGS-tst-gcs-dlopen-optional-off = -Wl,-z gcs=never +LDFLAGS-tst-gcs-dlopen-override = -Wl,-z gcs=always + +tst-gcs-dlopen-disabled-ENV = GLIBC_TUNABLES=glibc.cpu.aarch64_gcs=0 +tst-gcs-dlopen-enforced-ENV = GLIBC_TUNABLES=glibc.cpu.aarch64_gcs=1 +tst-gcs-dlopen-optional-on-ENV = GLIBC_TUNABLES=glibc.cpu.aarch64_gcs=2 +tst-gcs-dlopen-optional-off-ENV = GLIBC_TUNABLES=glibc.cpu.aarch64_gcs=2 +tst-gcs-dlopen-override-ENV = GLIBC_TUNABLES=glibc.cpu.aarch64_gcs=3 + +$(objpfx)tst-gcs-dlopen-disabled.out: $(objpfx)tst-gcs-mod2.so +$(objpfx)tst-gcs-dlopen-enforced.out: $(objpfx)tst-gcs-mod2.so +$(objpfx)tst-gcs-dlopen-optional-on.out: $(objpfx)tst-gcs-mod2.so +$(objpfx)tst-gcs-dlopen-optional-off.out: $(objpfx)tst-gcs-mod2.so +$(objpfx)tst-gcs-dlopen-override.out: $(objpfx)tst-gcs-mod2.so + endif # ifeq ($(have-test-cc-gcs),yes) endif # ifeq ($(subdir),misc) diff --git a/sysdeps/unix/sysv/linux/aarch64/tst-gcs-dlopen-disabled.c b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-dlopen-disabled.c new file mode 100644 index 0000000000..34395280a3 --- /dev/null +++ b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-dlopen-disabled.c @@ -0,0 +1,3 @@ +#define TEST_GCS_EXPECT_ENABLED 0 +#define TEST_GCS_EXPECT_DLOPEN 1 +#include "tst-gcs-dlopen.c" diff --git a/sysdeps/unix/sysv/linux/aarch64/tst-gcs-dlopen-enforced.c b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-dlopen-enforced.c new file mode 100644 index 0000000000..d8489ecd24 --- /dev/null +++ b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-dlopen-enforced.c @@ -0,0 +1,3 @@ +#define TEST_GCS_EXPECT_ENABLED 1 +#define TEST_GCS_EXPECT_DLOPEN 0 +#include "tst-gcs-dlopen.c" diff --git a/sysdeps/unix/sysv/linux/aarch64/tst-gcs-dlopen-optional-off.c b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-dlopen-optional-off.c new file mode 100644 index 0000000000..34395280a3 --- /dev/null +++ b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-dlopen-optional-off.c @@ -0,0 +1,3 @@ +#define TEST_GCS_EXPECT_ENABLED 0 +#define TEST_GCS_EXPECT_DLOPEN 1 +#include "tst-gcs-dlopen.c" diff --git a/sysdeps/unix/sysv/linux/aarch64/tst-gcs-dlopen-optional-on.c b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-dlopen-optional-on.c new file mode 100644 index 0000000000..d8489ecd24 --- /dev/null +++ b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-dlopen-optional-on.c @@ -0,0 +1,3 @@ +#define TEST_GCS_EXPECT_ENABLED 1 +#define TEST_GCS_EXPECT_DLOPEN 0 +#include "tst-gcs-dlopen.c" diff --git a/sysdeps/unix/sysv/linux/aarch64/tst-gcs-dlopen-override.c b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-dlopen-override.c new file mode 100644 index 0000000000..152ffcf207 --- /dev/null +++ b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-dlopen-override.c @@ -0,0 +1,3 @@ +#define TEST_GCS_EXPECT_ENABLED 1 +#define TEST_GCS_EXPECT_DLOPEN 1 +#include "tst-gcs-dlopen.c" diff --git a/sysdeps/unix/sysv/linux/aarch64/tst-gcs-dlopen.c b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-dlopen.c new file mode 100644 index 0000000000..6e0801c63a --- /dev/null +++ b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-dlopen.c @@ -0,0 +1,62 @@ +/* AArch64 tests for GCS for dlopen use case. + 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 "tst-gcs-helper.h" + +#include +#include + +static int +do_test (void) +{ + /* Check if GCS could possible by enabled. */ + if (!(getauxval (AT_HWCAP) & HWCAP_GCS)) + { + puts ("kernel or CPU does not support GCS"); + return EXIT_UNSUPPORTED; + } + /* The tst-gcs-mod2.so test library does not have GCS marking. */ + void *h = dlopen ("tst-gcs-mod2.so", RTLD_NOW); + const char *err = dlerror (); + +#if TEST_GCS_EXPECT_DLOPEN + TEST_VERIFY (h != NULL); +#else + TEST_VERIFY (h == NULL); + /* Only accept expected GCS-related errors. */ + TEST_VERIFY (strstr (err, "not GCS compatible") != NULL); +#endif + +#if TEST_GCS_EXPECT_ENABLED + TEST_VERIFY (__check_gcs_status ()); +#else + TEST_VERIFY (!__check_gcs_status ()); +#endif + + if (h == NULL) + printf ("dlopen error: %s\n", err); + else + { + puts ("library loaded normally"); + dlclose (h); + } + + return 0; +} + +#include From patchwork Wed Jan 29 09:49:40 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yury Khrustalev X-Patchwork-Id: 105598 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B0AA03857B9B for ; Wed, 29 Jan 2025 09:51:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B0AA03857B9B X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by sourceware.org (Postfix) with ESMTP id B53A93858D34 for ; Wed, 29 Jan 2025 09:50:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B53A93858D34 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=arm.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org B53A93858D34 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1738144226; cv=none; b=Cwrhi5Dt7X/RNLs8/yce9+wWYzdlA7qUxBZyy2GeFjjfz1Z59NN5UhbvS8ha+yZSyXVlPFEcHFOF6liND/R8TGV87rMT7aAzqGkfoRbrbQU4owzVTHz6jLBbdrA52GAbw3f3IjFjlCwegJh49UJBdEoaGvwg6SqCpSYlXsfdB+E= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1738144226; c=relaxed/simple; bh=5yuQGSJmAb1Nlap9VaSD7Mtg36V6sCwlf9/+eWd1WZs=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=GvcxmHPbK9dHs/PYy8QHHAC6KbYTnIF8ZVA0b5B76SOOKN0W5otkUyy5uvmz8qKf5ivPyrnbFED/AtF9mFP/ZzCjrr+d7e5nBkxOVNB7VABE9dCDLEjamg6H0uRxIOvo+yPwTgESAXaH/k42U/l/D9nhJN99C8z1W7mCjAnsf48= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B53A93858D34 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id A80F4497; Wed, 29 Jan 2025 01:50:52 -0800 (PST) Received: from udebian.localdomain (unknown [10.1.37.167]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 1E7C73F694; Wed, 29 Jan 2025 01:50:25 -0800 (PST) From: Yury Khrustalev To: libc-alpha@sourceware.org Cc: adhemerval.zanella@linaro.org Subject: [PATCH v3 5/5] aarch64: Add GCS test with signal handler Date: Wed, 29 Jan 2025 09:49:40 +0000 Message-Id: <20250129094940.2643944-6-yury.khrustalev@arm.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250129094940.2643944-1-yury.khrustalev@arm.com> References: <20250129094940.2643944-1-yury.khrustalev@arm.com> MIME-Version: 1.0 X-Spam-Status: No, score=-13.3 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, KAM_LAZY_DOMAIN_SECURITY, KAM_SHORT, SPF_HELO_NONE, SPF_NONE, TXREP 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: 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~patchwork=sourceware.org@sourceware.org Test that when we return from a function that enabled GCS at runtime we get SIGSEGV. Also test that ucontext contains GCS block with the GCS pointer. --- sysdeps/unix/sysv/linux/aarch64/Makefile | 5 + .../sysv/linux/aarch64/tst-gcs-noreturn.c | 100 ++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 sysdeps/unix/sysv/linux/aarch64/tst-gcs-noreturn.c diff --git a/sysdeps/unix/sysv/linux/aarch64/Makefile b/sysdeps/unix/sysv/linux/aarch64/Makefile index 1954849a8d..819fd65b71 100644 --- a/sysdeps/unix/sysv/linux/aarch64/Makefile +++ b/sysdeps/unix/sysv/linux/aarch64/Makefile @@ -15,6 +15,7 @@ gcs-tests-dynamic = \ tst-gcs-dlopen-override \ tst-gcs-enforced \ tst-gcs-enforced-abort \ + tst-gcs-noreturn \ tst-gcs-optional-off \ tst-gcs-optional-on \ tst-gcs-override \ @@ -123,6 +124,10 @@ $(objpfx)tst-gcs-dlopen-optional-on.out: $(objpfx)tst-gcs-mod2.so $(objpfx)tst-gcs-dlopen-optional-off.out: $(objpfx)tst-gcs-mod2.so $(objpfx)tst-gcs-dlopen-override.out: $(objpfx)tst-gcs-mod2.so +LDFLAGS-tst-gcs-noreturn = -Wl,-z gcs=always + +tst-gcs-noreturn-ENV = GLIBC_TUNABLES=glibc.cpu.aarch64_gcs=0 + endif # ifeq ($(have-test-cc-gcs),yes) endif # ifeq ($(subdir),misc) diff --git a/sysdeps/unix/sysv/linux/aarch64/tst-gcs-noreturn.c b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-noreturn.c new file mode 100644 index 0000000000..80b2d0b8c3 --- /dev/null +++ b/sysdeps/unix/sysv/linux/aarch64/tst-gcs-noreturn.c @@ -0,0 +1,100 @@ +/* AArch64 test for GCS abort when returning to non-GCS address. + 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 "tst-gcs-helper.h" + +#include +#include +#include + +# ifndef PR_SET_SHADOW_STACK_STATUS +# define PR_SET_SHADOW_STACK_STATUS 75 +# define PR_SHADOW_STACK_ENABLE (1UL << 0) +# endif + +static void +run_with_gcs (void) +{ + int r = prctl(PR_SET_SHADOW_STACK_STATUS, PR_SHADOW_STACK_ENABLE, 0, 0, 0); + /* Syscall should succeed. */ + TEST_VERIFY (r == 0); + bool gcs_enabled = __check_gcs_status (); + /* Now GCS should be enabled. */ + TEST_VERIFY (gcs_enabled); + printf ("GCS is %s\n", gcs_enabled ? "enabled" : "disabled"); +} + +static struct _aarch64_ctx * +extension (void *p) +{ + return p; +} + +#ifndef GCS_MAGIC +#define GCS_MAGIC 0x47435300 +#endif + +static void +handler (int sig, siginfo_t *si, void *ctx) +{ + TEST_VERIFY (sig == SIGSEGV); + ucontext_t *uc = ctx; + void *p = uc->uc_mcontext.__reserved; + if (extension (p)->magic == FPSIMD_MAGIC) + p = (char *)p + extension (p)->size; + if (extension (p)->magic == GCS_MAGIC) + { + struct { uint64_t x, gcspr, y, z; } *q = p; + printf ("GCS pointer: %016lx\n", q->gcspr); + exit (0); + } + else + exit (3); +} + +static int +do_test (void) +{ + /* Check if GCS could possible by enabled. */ + if (!(getauxval (AT_HWCAP) & HWCAP_GCS)) + { + puts ("kernel or CPU does not support GCS"); + return EXIT_UNSUPPORTED; + } + bool gcs_enabled = __check_gcs_status (); + /* This test should be rung with GCS initially disabled. */ + TEST_VERIFY (!gcs_enabled); + + /* We can't use EXPECTED_SIGNAL because of cases when + this test runs on a system that does not support GCS + which is being detected at runtime. */ + struct sigaction sigact; + sigemptyset (&sigact.sa_mask); + sigact.sa_flags = 0; + sigact.sa_flags = sigact.sa_flags | SA_SIGINFO; + sigact.sa_sigaction = handler; + sigaction (SIGSEGV, &sigact, NULL); + + run_with_gcs (); + /* If we reached this point, then something went wrong. + Returning from a function that enabled GCS should result in + SIGSEGV that we catch with the handler set up above. */ + return 2; +} + +#include