From patchwork Wed Jul 24 18:00:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Raphael Moreira Zinsly X-Patchwork-Id: 1964456 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=ventanamicro.com header.i=@ventanamicro.com header.a=rsa-sha256 header.s=google header.b=FingnPx7; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; 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 [8.43.85.97]) (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 4WThfz0ryMz1yXx for ; Thu, 25 Jul 2024 04:02:02 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id ECDD33861029 for ; Wed, 24 Jul 2024 18:02:00 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.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 76BC93857000 for ; Wed, 24 Jul 2024 18:00:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 76BC93857000 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=ventanamicro.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=ventanamicro.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 76BC93857000 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=1721844039; cv=none; b=iDgnwURbeKtKWN7D6KsWhtZ0HfUCOCbkBb/saaSGxDA/OKITB65iJA3LRwANTKcaZnezAoOOvKyDhENF2EtvbwsFnLLeCAlEzZTIyBB3UD/8QtF0wV3S8WcAgySSdKqKuTcrgKcBNtePdt+iCgGLlEgtXHFzZfzu/RoOInJRT1g= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721844039; c=relaxed/simple; bh=hKDVm6MWn3O7HulIig8FvyexAcDOM5ffyXc7c2Xh0pU=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=HuoOQWqk6LL8Y4ccvAx536VtaAsdT0Kfvlnzfv+3gGM5ApfnjoAo0K7rbYIEjQUUVAN6KYJyvn8j8Z8PIBKmnGy1Cbb7duhCPJ7aZLFm6dyFeSktf0dO3NUzSAIi4bbIIdPTFVamwW14akcplmTfqRYT7V8tx1vpwLxXVTtDeTE= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-1fc587361b6so374415ad.2 for ; Wed, 24 Jul 2024 11:00:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1721844034; x=1722448834; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Og4+Sh8ynKTs/3BkY+U3Ck20QVGOl5PObmBb7gUtVto=; b=FingnPx7UturikZAJ1TIb2dEsEotkCRMgLZP8jTXBT5+v2YEgrnTWqsfQR8DauLl9E L3eeKYXDNgbr21xXpjNrGgtrS5ogQEsg601zgbHmt2PLjqg4+gEF8hWpRPx5/l1z8tqj Mfb828xAt28xG7IsGm09CKG8ajwpGD007p6IeM30HicBUwkeANP6hJPLf3X5DnzQGgmo arzKI43XnYw0ennifNZrMuXIV2pKxg0bs4x2yPEKwRjlleYI+63/R2uwUWUc6cOlXrcJ C/S4yQnYDkh5i0LOaOOcWjKT1doG2xRbf1fGzMzXHevr/07zVESbY01XqknWzsieHyVB hTKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721844034; x=1722448834; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Og4+Sh8ynKTs/3BkY+U3Ck20QVGOl5PObmBb7gUtVto=; b=NvLJ/18ioArRcCOxgbMssrck5s5M/EweSksTAMWKiYF6DgxCCdMo59OZug0QjL04hr f/VzQOIBvJZ6wiA4pwX8hahqxNqWI2MfPa2sCM6tssvZX4xPy+5wv0qFDORE+cQR2Iu5 sQrxGPUjPlS+3WJ++uuuMjWSkkLdUbmWNYKeE5nTP2RJBJhVMQjo/U1Jya7laTkse2Ah TwbFXG3aEz7Dqe857IHvKUfsQtbxahpCKVk2y6LVnp7PHTrWIoz5IvCoYAngML6dJsiy yu4Xeq0DBT8PCFjEmN6k39sOBKz3mIQDRW2+s+J725M7NWmC2/W0Gocb3bLnIOFLbhfK mVkA== X-Gm-Message-State: AOJu0Yzd7x+uSd9UaDuY84DnkpQKAkIxKGVq93/7qy53CJjrVdqcW9Iw Bx1jssiL9oyA6oyEKZItncQ1LJm0NV301CgfH6mE/x5TWbPogzm9JGhGOOIaxHXi9Vs0IZqMBVl O X-Google-Smtp-Source: AGHT+IG9cbar4bTZ6rlxzGI2RFyo6I/5owzuL3b6AjbWaELtyudE41D0N436BkWkeNg4EBHZn1iw7Q== X-Received: by 2002:a17:903:41cd:b0:1fc:726e:15a6 with SMTP id d9443c01a7336-1fed38bdb22mr3199815ad.34.1721844033648; Wed, 24 Jul 2024 11:00:33 -0700 (PDT) Received: from marvin.dc1.ventanamicro.com ([189.4.72.88]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fd6f4713f8sm97261705ad.268.2024.07.24.11.00.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jul 2024 11:00:33 -0700 (PDT) From: Raphael Moreira Zinsly To: gcc-patches@gcc.gnu.org Cc: jlaw@ventanamicro.com, Raphael Moreira Zinsly Subject: [PATCH 5/5] RISC-V: Enable stack clash in alloca Date: Wed, 24 Jul 2024 15:00:13 -0300 Message-ID: <1206ea55e95fef9c6a10cc8b9a5e85e2ec8d78bc.1721681845.git.rzinsly@ventanamicro.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-10.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, URIBL_BLACK 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 the TARGET_STACK_CLASH_PROTECTION_ALLOCA_PROBE_RANGE to riscv in order to enable stack clash protection when using alloca. The code and tests are the same used by aarch64. gcc/ChangeLog: * config/riscv/riscv.cc (riscv_compute_frame_info): Update outgoing args size. (riscv_stack_clash_protection_alloca_probe_range): New. (TARGET_STACK_CLASH_PROTECTION_ALLOCA_PROBE_RANGE): New. * config/riscv/riscv.h (STACK_CLASH_MIN_BYTES_OUTGOING_ARGS): New. (STACK_DYNAMIC_OFFSET): New. gcc/testsuite/ChangeLog: * gcc.target/riscv/stack-check-14.c: New test. * gcc.target/riscv/stack-check-15.c: New test. * gcc.target/riscv/stack-check-alloca-1.c: New test. * gcc.target/riscv/stack-check-alloca-2.c: New test. * gcc.target/riscv/stack-check-alloca-3.c: New test. * gcc.target/riscv/stack-check-alloca-4.c: New test. * gcc.target/riscv/stack-check-alloca-5.c: New test. * gcc.target/riscv/stack-check-alloca-6.c: New test. * gcc.target/riscv/stack-check-alloca-7.c: New test. * gcc.target/riscv/stack-check-alloca-8.c: New test. * gcc.target/riscv/stack-check-alloca-9.c: New test. * gcc.target/riscv/stack-check-alloca-10.c: New test. * gcc.target/riscv/stack-check-alloca.h: New. --- gcc/config/riscv/riscv.cc | 17 +++++++++++++ gcc/config/riscv/riscv.h | 17 +++++++++++++ .../gcc.target/riscv/stack-check-14.c | 24 +++++++++++++++++++ .../gcc.target/riscv/stack-check-15.c | 21 ++++++++++++++++ .../gcc.target/riscv/stack-check-alloca-1.c | 15 ++++++++++++ .../gcc.target/riscv/stack-check-alloca-10.c | 13 ++++++++++ .../gcc.target/riscv/stack-check-alloca-2.c | 11 +++++++++ .../gcc.target/riscv/stack-check-alloca-3.c | 11 +++++++++ .../gcc.target/riscv/stack-check-alloca-4.c | 12 ++++++++++ .../gcc.target/riscv/stack-check-alloca-5.c | 12 ++++++++++ .../gcc.target/riscv/stack-check-alloca-6.c | 12 ++++++++++ .../gcc.target/riscv/stack-check-alloca-7.c | 12 ++++++++++ .../gcc.target/riscv/stack-check-alloca-8.c | 14 +++++++++++ .../gcc.target/riscv/stack-check-alloca-9.c | 13 ++++++++++ .../gcc.target/riscv/stack-check-alloca.h | 15 ++++++++++++ 15 files changed, 219 insertions(+) create mode 100644 gcc/testsuite/gcc.target/riscv/stack-check-14.c create mode 100644 gcc/testsuite/gcc.target/riscv/stack-check-15.c create mode 100644 gcc/testsuite/gcc.target/riscv/stack-check-alloca-1.c create mode 100644 gcc/testsuite/gcc.target/riscv/stack-check-alloca-10.c create mode 100644 gcc/testsuite/gcc.target/riscv/stack-check-alloca-2.c create mode 100644 gcc/testsuite/gcc.target/riscv/stack-check-alloca-3.c create mode 100644 gcc/testsuite/gcc.target/riscv/stack-check-alloca-4.c create mode 100644 gcc/testsuite/gcc.target/riscv/stack-check-alloca-5.c create mode 100644 gcc/testsuite/gcc.target/riscv/stack-check-alloca-6.c create mode 100644 gcc/testsuite/gcc.target/riscv/stack-check-alloca-7.c create mode 100644 gcc/testsuite/gcc.target/riscv/stack-check-alloca-8.c create mode 100644 gcc/testsuite/gcc.target/riscv/stack-check-alloca-9.c create mode 100644 gcc/testsuite/gcc.target/riscv/stack-check-alloca.h diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc index 69c0e07f4c5..a110e011766 100644 --- a/gcc/config/riscv/riscv.cc +++ b/gcc/config/riscv/riscv.cc @@ -7245,6 +7245,10 @@ riscv_compute_frame_info (void) frame = &cfun->machine->frame; + /* Adjust the outgoing arguments size if required. Keep it in sync with what + the mid-end is doing. */ + crtl->outgoing_args_size = STACK_DYNAMIC_OFFSET (cfun); + /* In an interrupt function, there are two cases in which t0 needs to be used: 1, If we have a large frame, then we need to save/restore t0. We check for this before clearing the frame struct. @@ -11879,6 +11883,15 @@ riscv_c_mode_for_floating_type (enum tree_index ti) return default_mode_for_floating_type (ti); } +/* On riscv we have an ABI defined safe buffer. This constant is used to + determining the probe offset for alloca. */ + +static HOST_WIDE_INT +riscv_stack_clash_protection_alloca_probe_range (void) +{ + return STACK_CLASH_CALLER_GUARD; +} + /* Initialize the GCC target structure. */ #undef TARGET_ASM_ALIGNED_HI_OP #define TARGET_ASM_ALIGNED_HI_OP "\t.half\t" @@ -12187,6 +12200,10 @@ riscv_c_mode_for_floating_type (enum tree_index ti) #define TARGET_VECTORIZE_PREFERRED_VECTOR_ALIGNMENT \ riscv_vectorize_preferred_vector_alignment +#undef TARGET_STACK_CLASH_PROTECTION_ALLOCA_PROBE_RANGE +#define TARGET_STACK_CLASH_PROTECTION_ALLOCA_PROBE_RANGE \ + riscv_stack_clash_protection_alloca_probe_range + /* Mode switching hooks. */ #undef TARGET_MODE_EMIT diff --git a/gcc/config/riscv/riscv.h b/gcc/config/riscv/riscv.h index 0432beb81e0..7f20190e960 100644 --- a/gcc/config/riscv/riscv.h +++ b/gcc/config/riscv/riscv.h @@ -1270,4 +1270,21 @@ extern void riscv_remove_unneeded_save_restore_calls (void); generating stack clash probes. */ #define STACK_CLASH_MAX_UNROLL_PAGES 4 +/* This value represents the minimum amount of bytes we expect the function's + outgoing arguments to be when stack-clash is enabled. */ +#define STACK_CLASH_MIN_BYTES_OUTGOING_ARGS 8 + +/* Allocate a minimum of STACK_CLASH_MIN_BYTES_OUTGOING_ARGS bytes for the + outgoing arguments if stack clash protection is enabled. This is essential + as the extra arg space allows us to skip a check in alloca. */ +#undef STACK_DYNAMIC_OFFSET +#define STACK_DYNAMIC_OFFSET(FUNDECL) \ + ((flag_stack_clash_protection \ + && cfun->calls_alloca \ + && known_lt (crtl->outgoing_args_size, \ + STACK_CLASH_MIN_BYTES_OUTGOING_ARGS)) \ + ? ROUND_UP (STACK_CLASH_MIN_BYTES_OUTGOING_ARGS, \ + STACK_BOUNDARY / BITS_PER_UNIT) \ + : (crtl->outgoing_args_size + STACK_POINTER_OFFSET)) + #endif /* ! GCC_RISCV_H */ diff --git a/gcc/testsuite/gcc.target/riscv/stack-check-14.c b/gcc/testsuite/gcc.target/riscv/stack-check-14.c new file mode 100644 index 00000000000..8ca0488c468 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/stack-check-14.c @@ -0,0 +1,24 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -march=rv64gc -mabi=lp64d -fstack-clash-protection" } */ +/* { dg-require-effective-target supports_stack_clash_protection } */ + +int t1(int); + +int t2(int x) +{ + char *p = __builtin_alloca (2048); + x = t1 (x); + return p[x]; +} + + +/* This test has a constant sized alloca that is smaller than the + probe interval. Only one probe is required since the value is larger + than 1024 bytes but smaller than page size. + + The form can change quite a bit so we just check for one + probe without looking at the actual address. */ +/* { dg-final { scan-assembler-times {sd\tzero,1024\(sp\)} 1 } } */ + + + diff --git a/gcc/testsuite/gcc.target/riscv/stack-check-15.c b/gcc/testsuite/gcc.target/riscv/stack-check-15.c new file mode 100644 index 00000000000..a44b257ba75 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/stack-check-15.c @@ -0,0 +1,21 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -march=rv64gc -mabi=lp64d -fstack-clash-protection" } */ +/* { dg-require-effective-target supports_stack_clash_protection } */ + +int t1(int); + +int t2(int x) +{ + char *p = __builtin_alloca (x); + x = t1 (x); + return p[x]; +} + + +/* This test has a variable sized alloca. It requires 3 probes. + One in the loop, one for the residual, one for when it's < 1024 and one for + when it's not. + + The form can change quite a bit so we just check for three + probes without looking at the actual address. */ +/* { dg-final { scan-assembler-times {sd\tzero,} 3 } } */ diff --git a/gcc/testsuite/gcc.target/riscv/stack-check-alloca-1.c b/gcc/testsuite/gcc.target/riscv/stack-check-alloca-1.c new file mode 100644 index 00000000000..642840fb50c --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/stack-check-alloca-1.c @@ -0,0 +1,15 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -march=rv64gc -mabi=lp64d -fstack-clash-protection" } */ +/* { dg-require-effective-target supports_stack_clash_protection } */ +/* { dg-require-effective-target alloca } */ + +#define SIZE y +#include "stack-check-alloca.h" + +/* { dg-final { scan-assembler-times {sd\tzero,1024\(sp\)} 2 } } */ +/* { dg-final { scan-assembler-times {sd\tzero,0\(sp\)} 1 } } */ + +/* Dynamic alloca, expect loop, and 2 probes with 1kB offset and 1 at sp. + 1st probe is inside the loop for the full guard-size allocations, second + probe is for the case where residual is zero and the final probe for when + residiual is > 1024 bytes. */ diff --git a/gcc/testsuite/gcc.target/riscv/stack-check-alloca-10.c b/gcc/testsuite/gcc.target/riscv/stack-check-alloca-10.c new file mode 100644 index 00000000000..11844aad748 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/stack-check-alloca-10.c @@ -0,0 +1,13 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -march=rv64gc -mabi=lp64d -fstack-clash-protection" } */ +/* { dg-require-effective-target supports_stack_clash_protection } */ +/* { dg-require-effective-target alloca } */ + +#define SIZE 127.5 * 3 * 1024 +#include "stack-check-alloca.h" + +/* { dg-final { scan-assembler-times {sd\tzero,1024\(sp\)} 2 } } */ + +/* Large alloca of an amount which isn't a multiple of a guard-size, and + residiual is more than 1kB. Loop expected with one 1Kb probe offset and + one residual probe at offset 1kB. */ diff --git a/gcc/testsuite/gcc.target/riscv/stack-check-alloca-2.c b/gcc/testsuite/gcc.target/riscv/stack-check-alloca-2.c new file mode 100644 index 00000000000..5c7a158adec --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/stack-check-alloca-2.c @@ -0,0 +1,11 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -march=rv64gc -mabi=lp64d -fstack-clash-protection" } */ +/* { dg-require-effective-target supports_stack_clash_protection } */ +/* { dg-require-effective-target alloca } */ + +#define SIZE 0 +#include "stack-check-alloca.h" + +/* { dg-final { scan-assembler-not {sd\tzero,} } } */ + +/* Alloca of 0 should emit no probes, boundary condition. */ diff --git a/gcc/testsuite/gcc.target/riscv/stack-check-alloca-3.c b/gcc/testsuite/gcc.target/riscv/stack-check-alloca-3.c new file mode 100644 index 00000000000..a5db2679aef --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/stack-check-alloca-3.c @@ -0,0 +1,11 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -march=rv64gc -mabi=lp64d -fstack-clash-protection" } */ +/* { dg-require-effective-target supports_stack_clash_protection } */ +/* { dg-require-effective-target alloca } */ + +#define SIZE 100 +#include "stack-check-alloca.h" + +/* { dg-final { scan-assembler-times {sd\tzero,8\(sp\)} 1 } } */ + +/* Alloca is less than 1kB, 1 probe expected at word offset. */ diff --git a/gcc/testsuite/gcc.target/riscv/stack-check-alloca-4.c b/gcc/testsuite/gcc.target/riscv/stack-check-alloca-4.c new file mode 100644 index 00000000000..1841412ff36 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/stack-check-alloca-4.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -march=rv64gc -mabi=lp64d -fstack-clash-protection" } */ +/* { dg-require-effective-target supports_stack_clash_protection } */ +/* { dg-require-effective-target alloca } */ + +#define SIZE 1.5 * 1024 +#include "stack-check-alloca.h" + +/* { dg-final { scan-assembler-times {sd\tzero,1024\(sp\)} 1 } } */ + +/* Alloca is more than 1kB, but less than guard-size, 1 probe expected at + 1kB offset. */ diff --git a/gcc/testsuite/gcc.target/riscv/stack-check-alloca-5.c b/gcc/testsuite/gcc.target/riscv/stack-check-alloca-5.c new file mode 100644 index 00000000000..f8f9d944564 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/stack-check-alloca-5.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -march=rv64gc -mabi=lp64d -fstack-clash-protection" } */ +/* { dg-require-effective-target supports_stack_clash_protection } */ +/* { dg-require-effective-target alloca } */ + +#define SIZE 2 * 1024 +#include "stack-check-alloca.h" + +/* { dg-final { scan-assembler-times {sd\tzero,1024\(sp\)} 1 } } */ + +/* Alloca is more than 1kB, but less than guard-size, 1 probe expected at + 1kB offset. */ diff --git a/gcc/testsuite/gcc.target/riscv/stack-check-alloca-6.c b/gcc/testsuite/gcc.target/riscv/stack-check-alloca-6.c new file mode 100644 index 00000000000..d937e929d75 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/stack-check-alloca-6.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -march=rv64gc -mabi=lp64d -fstack-clash-protection" } */ +/* { dg-require-effective-target supports_stack_clash_protection } */ +/* { dg-require-effective-target alloca } */ + +#define SIZE 2.5 * 1024 +#include "stack-check-alloca.h" + +/* { dg-final { scan-assembler-times {sd\tzero,1024\(sp\)} 1 } } */ + +/* Alloca is more than 1kB, but less than guard-size, 1 probe expected at 1kB + offset. */ diff --git a/gcc/testsuite/gcc.target/riscv/stack-check-alloca-7.c b/gcc/testsuite/gcc.target/riscv/stack-check-alloca-7.c new file mode 100644 index 00000000000..cbb32f3157e --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/stack-check-alloca-7.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -march=rv64gc -mabi=lp64d -fstack-clash-protection" } */ +/* { dg-require-effective-target supports_stack_clash_protection } */ +/* { dg-require-effective-target alloca } */ + +#define SIZE 3 * 1024 +#include "stack-check-alloca.h" + +/* { dg-final { scan-assembler-times {sd\tzero,1024\(sp\)} 1 } } */ + +/* Alloca is exactly one guard-size, 1 probe expected at 1kB offset. + Boundary condition. */ diff --git a/gcc/testsuite/gcc.target/riscv/stack-check-alloca-8.c b/gcc/testsuite/gcc.target/riscv/stack-check-alloca-8.c new file mode 100644 index 00000000000..3cc3450355b --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/stack-check-alloca-8.c @@ -0,0 +1,14 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -march=rv64gc -mabi=lp64d -fstack-clash-protection --param stack-clash-protection-guard-size=16" } */ +/* { dg-require-effective-target supports_stack_clash_protection } */ +/* { dg-require-effective-target alloca } */ + +#define SIZE 65 * 1024 +#include "stack-check-alloca.h" + +/* { dg-final { scan-assembler-times {sd\tzero,1024\(sp\)} 1 } } */ +/* { dg-final { scan-assembler-times {sd\tzero,8\(sp\)} 1 } } */ + +/* Alloca is more than one guard-page, and residual is exactly 1Kb. 2 probes + expected. One at 1kB offset for the guard-size allocation and one at word + offset for the residual. */ diff --git a/gcc/testsuite/gcc.target/riscv/stack-check-alloca-9.c b/gcc/testsuite/gcc.target/riscv/stack-check-alloca-9.c new file mode 100644 index 00000000000..36466930e4e --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/stack-check-alloca-9.c @@ -0,0 +1,13 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -march=rv64gc -mabi=lp64d -fstack-clash-protection" } */ +/* { dg-require-effective-target supports_stack_clash_protection } */ +/* { dg-require-effective-target alloca } */ + +#define SIZE 127 * 3 * 1024 +#include "stack-check-alloca.h" + +/* { dg-final { scan-assembler-times {sd\tzero,1024\(sp\)} 1 } } */ + +/* Large alloca of a constant amount which is a multiple of a guard-size, + no residiual. Loop expected with one 1Kb probe offset and no residual probe + because residual is at compile time known to be zero. */ diff --git a/gcc/testsuite/gcc.target/riscv/stack-check-alloca.h b/gcc/testsuite/gcc.target/riscv/stack-check-alloca.h new file mode 100644 index 00000000000..8c75f6c0f70 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/stack-check-alloca.h @@ -0,0 +1,15 @@ + +/* Avoid inclusion of alloca.h, unavailable on some systems. */ +#define alloca __builtin_alloca + +__attribute__((noinline, noipa)) +void g (char* ptr, int y) +{ + ptr[y] = '\0'; +} + +void f_caller (int y) +{ + char* pStr = alloca(SIZE); + g (pStr, y); +}