From patchwork Sun Apr 5 08:26:24 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Botcazou X-Patchwork-Id: 132705 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id 0CCB04BA2E2F for ; Sun, 5 Apr 2026 08:27:34 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0CCB04BA2E2F Authentication-Results: sourceware.org; dkim=pass (2048-bit key, secure) header.d=adacore.com header.i=@adacore.com header.a=rsa-sha256 header.s=google header.b=BGkNnYoo X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wm1-f44.google.com (mail-wm1-f44.google.com [209.85.128.44]) by sourceware.org (Postfix) with ESMTPS id 5B22F4BA2E07 for ; Sun, 5 Apr 2026 08:26:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5B22F4BA2E07 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 5B22F4BA2E07 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=209.85.128.44 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1775377611; cv=none; b=upTtA0+ISBfxpzdqylbxgS2axQeGT7+It2V5QNldh3oChlq0s8ilmrT6hgw2AHtQxhlgGi/0QVclKt/04y3kY0kom+xeJevSn9ERx12ok2IJMa7dEQJpjRiZTqfTL17wo5/uZ5m5WjnbmV0cvfW+H5pZDgB0fPhGfVAykp+1xdI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1775377611; c=relaxed/simple; bh=aTl7hfktDSjPzNI9/pqHLAdGkSGj17ApfL88oqh6nr4=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=QKZQzVeQAkQdVAGkuwtlwAuqMd2MYgc6OnOm8+N19z/L/JFqpJpl72+hwbjryMJzDYroU5dfaMm4GhzVzT1IJM2CJt6CdI/l92bX6sHJEZKBy2wjDMv2mJtByiYSGubOHpRSGFTISJZ72ZDcc07ChRaHAuM+yh6uhY9wDmMTlWI= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5B22F4BA2E07 Received: by mail-wm1-f44.google.com with SMTP id 5b1f17b1804b1-482f454be5bso44328175e9.0 for ; Sun, 05 Apr 2026 01:26:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1775377610; x=1775982410; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=+CaYBmVL4WSpIfYIfMatWvLaWCtgHXNbdxK0tVQA7zs=; b=BGkNnYooC8FMEOQryYXfNBlr2H4ggS2o90MLfwatKQ4eahX+DjJecsEiF4dTxLo2Az ivFk6Hg0VDhdHW8ksXv5tOliZvirUGHl3Ng1JR35RO5zOk6ve7ECJfYIFG6mf+yCdOF0 T7IxyFwd1PB0XJ6Si7DFF1+q+JvXAiy/J50/Fo+vIdhFDhT+IDr3lqEst251HKaAHE/Y y1tmVJ9H6JzRnq4ShsjHQLUpFkQmwdirEmwC6YWJQaQM2MwNzqsS0cNohfUBFWWz5x2r sn5IihCtNo6HGhAvsZvJSkfJ237TaSRHckbDmUb8ts2TOt6o+CBNDTr3Ejir3d87eAnM 7m6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775377610; x=1775982410; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=+CaYBmVL4WSpIfYIfMatWvLaWCtgHXNbdxK0tVQA7zs=; b=COlDNjhOC7kjwHVrZJ9hAdcLQVI24+dKecIt/s3XTli4KzkIZVU0WaZCX0TAXx5fG8 7U62GZ9DySxbsWcXtVLbMb6uSEuj7JUxQqxETkh6SWkFDx4x4tUYS2GcOi7aH5sk/xLk IZZF5LbX8NLGa36SKSdjeWnvKycV9iB/e4vHVkzb3UT/fU/qyUtccqUx4zuGRtufUwpN Qoc8lXENLHh7n2BMi4K0W0p6aGqSr825DJJA74riHhiTSpFS9Y5c3E2nu7WwmqIwPJOw Z8P2DydHm21ouY7PBu1ErDR5OAjD6IcUMovtls61J806uWspL2AKaeLqZYyJTqTf07oX knkA== X-Gm-Message-State: AOJu0YzI0x+obdIoeKyKabFc1pqjUpgRk9kYn377kcZnCUxnRH1LihG4 lDuMqVTXyxoR6oxfto8bLGemH7c4zbUC33cB3o9VJc2W5Z7EDH7oG8kDfIiaQwSm5vfib5zBboU uIks= X-Gm-Gg: AeBDies3nCtyiLDDiBygZXnKmYYOf/YpGj8xy7ctfMM8SZ4yiuqgaQNjLvf1V43LHzf SLHNkb48YbhB+24bXvZEjUPiNr4Mwvx4aFanUrorSncRWIINHW95KCzjIyiyAR9JqgD4lK0nlUZ eRyvcINWN1CtoqBb19qzMZ7zmG3mD0FcgQGygtypVP8SkWwuS5kY/WEpgo4fjONrri8QccJI901 A+jQC8Ok1EWWKu3rWCaMcF00mgqjyBMO0cFXzAHFlztHffDzv3JRuGN1v+6Vb8sptBjlxAU5i+S Yn/t+Htb0Y19ALnxRKhHVXMZY2cj8rcyeH/nBsmlt8WVn07AmhAqf2caJIQjJcxuVZTXruxWSzE G2qIEj6Uz3Rb21zpkE11srkcJnbjFVMJa/pzQeaJvPGVyKNmDWfoTn5ArJatC4k0g4NNl1s88p/ AIm9RTo62EyBma6ToTQYi3GAo1ozVviP0RHKdW X-Received: by 2002:a05:600d:118:b0:487:34c3:c594 with SMTP id 5b1f17b1804b1-4888e0aaa82mr123303495e9.11.1775377610267; Sun, 05 Apr 2026 01:26:50 -0700 (PDT) Received: from fomalhaut.localnet ([2a01:e0a:107c:2f10:e654:e8ff:fe8f:2ce6]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-43d1e2a6f08sm32745914f8f.6.2026.04.05.01.26.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Apr 2026 01:26:49 -0700 (PDT) From: Eric Botcazou X-Google-Original-From: Eric Botcazou To: gcc-patches@gcc.gnu.org Subject: [Ada] Fix too large secondary stack allocation for aggregate return Date: Sun, 05 Apr 2026 10:26:24 +0200 Message-ID: <4725101.LvFx2qVVIh@fomalhaut> MIME-Version: 1.0 X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on 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~patchwork=sourceware.org@gcc.gnu.org This is a regression present on the mainline and 15 branch. The problematic allocation may occur when the result type of the function is a discriminated record type with defaulted discriminants. Although it had been present for a long time when the type is limited, the problem was recently propagated to nonlimited types because of an optimization. While the fix is a one-liner, the change also makes it so that SS_Allocate raises a Storage_Error when the size overflows, like the other allocators. Tested on x86-64/Linux, applied on the mainline and 15 branch. 2026-04-05 Eric Botcazou * exp_ch6.adb (Expand_Simple_Function_Return): Use a constant return object when the simple return is rewritten as an extended one. * libgnat/s-secsta.adb (SS_Allocate): Raise Storage_Error if the requested size is negative. * libgnat/s-secsta__cheri.adb (SS_Allocate): Likewise. 2026-04-05 Eric Botcazou * gnat.dg/aggr35.adb: New test. * gnat.dg/aggr35_pkg.ads, gnat.dg/aggr35_pkg.adb: New helper. diff --git a/gcc/ada/exp_ch6.adb b/gcc/ada/exp_ch6.adb index 9e1a68aef12..eb552ea2637 100644 --- a/gcc/ada/exp_ch6.adb +++ b/gcc/ada/exp_ch6.adb @@ -7404,7 +7404,7 @@ package body Exp_Ch6 is -- -- into -- - -- return _anonymous_ : := + -- return _anonymous_ : constant := -- The expansion produced by Expand_N_Extended_Return_Statement will -- contain simple return statements (for example, a block containing @@ -7436,6 +7436,7 @@ package body Exp_Ch6 is Obj_Decl : constant Node_Id := Make_Object_Declaration (Loc, Defining_Identifier => Make_Temporary (Loc, 'R'), + Constant_Present => True, Object_Definition => Subtype_Ind, Expression => Relocate_Node (Exp)); diff --git a/gcc/ada/libgnat/s-secsta.adb b/gcc/ada/libgnat/s-secsta.adb index 994485fd8ab..692133284ef 100644 --- a/gcc/ada/libgnat/s-secsta.adb +++ b/gcc/ada/libgnat/s-secsta.adb @@ -644,15 +644,17 @@ package body System.Secondary_Stack is -- calculated conservatively. end if; + -- Raise Storage_Error if the size has overflowed + + if Storage_Size < 0 then + raise Storage_Error with "object too large"; + end if; + -- Round the requested size (plus the needed padding in case of -- over-alignment) up to the nearest multiple of the default -- alignment to ensure efficient access and that the next available -- Byte is always aligned on the default alignement value. - -- It should not be possible to request an allocation of negative - -- size. - - pragma Assert (Storage_Size >= 0); Mem_Size := Round_Up (Storage_Size + Padding); if Sec_Stack_Dynamic then diff --git a/gcc/ada/libgnat/s-secsta__cheri.adb b/gcc/ada/libgnat/s-secsta__cheri.adb index b793d95fa45..bf6a33ffaba 100644 --- a/gcc/ada/libgnat/s-secsta__cheri.adb +++ b/gcc/ada/libgnat/s-secsta__cheri.adb @@ -673,10 +673,11 @@ package body System.Secondary_Stack is -- calculated conservatively. end if; - -- It should not be possible to request an allocation of negative - -- size. + -- Raise Storage_Error if the size has overflowed - pragma Assert (Storage_Size >= 0); + if Storage_Size < 0 then + raise Storage_Error with "object too large"; + end if; -- Round the requested size (plus the needed padding in case of -- over-alignment) to ensure that the CHERI bounds length will be