https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128693
Bug ID: 128693
Summary: [14/15/16/17 Regression] Storage leak with -gnata:
conditionally evaluated 'Old of a tagged type or of a
type that needs finalization is never freed
Product: gcc
Version: 16.1.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: ada
Assignee: unassigned at gcc dot gnu.org
Reporter: gomurin0428 at comcomponent dot com
CC: dkm at gcc dot gnu.org
Target Milestone: ---
With assertions enabled (-gnata), a subprogram leaks one heap block per
call when its contract has a conditionally evaluated 'Old whose prefix
is of a tagged type, or of an untagged type that has a controlled
component. I see it in two situations:
- 'Old in a consequence of Contract_Cases;
- 'Old in a postcondition, when it is evaluated conditionally under
the Ada 2022 rules (AI12-0280-2).
The first one is a regression from GCC 10: 10.5 does not leak, and
every release I tried from 11.1 to 16.2 does, as does trunk.
Reproducer for the first situation (leak.adb):
-- Each call to Incr leaks the copy of V made for V'Old.
procedure Leak is
package P is
type T is tagged record
X : Integer := 0;
end record;
end P;
use P;
procedure Incr (V : in out T)
with
Contract_Cases =>
(V.X < Integer'Last => V.X = V'Old.X + 1,
others => True);
procedure Incr (V : in out T) is
begin
if V.X < Integer'Last then
V.X := V.X + 1;
end if;
end Incr;
V : T;
begin
for I in 1 .. 100 loop
Incr (V);
end loop;
end Leak;
With 16.1.0 on aarch64-darwin (gcc -v is at the end):
$ gnatmake -f -gnata leak.adb
gcc -c -gnata leak.adb
gnatbind -x leak.ali
gnatlink leak.ali
$ MallocStackLogging=1 leaks --atExit -- ./leak
[...]
Process 74942: 100 leaks for 3200 total leaked bytes.
STACK OF 100 INSTANCES OF 'ROOT LEAK: <malloc in __gnat_malloc>':
8 dyld start + 7184
7 leak main + 116
6 leak _ada_leak + 1020
5 leak leak__incr.13 + 528
4 leak leak__incr__P95b.14 + 96
3 leak system__storage_pools__subpools__allocate_any_controlled + 164
2 leak system__pool_global__allocate + 24
1 leak __gnat_malloc + 28
0 libsystem_malloc.dylib _malloc_zone_malloc_instrumented_or_legacy + 152
Expected behavior: the copy of V made for V'Old is reclaimed when Incr
returns, and the program does not leak.
Actual behavior: each call leaks one block of 32 bytes. With 10_000_000
iterations instead of 100, the maximum resident set size of the program
is about 163 MB.
There is no leak when the same contract is written as a postcondition.
Compiled the same way, so in Ada 2012 mode, V'Old is then evaluated on
every call:
procedure Incr (V : in out T)
with Post => (if V'Old.X < Integer'Last then V.X = V'Old.X + 1);
With that contract, leaks reports "0 leaks for 0 total leaked bytes",
and the maximum resident set size with 10_000_000 iterations is about
1.6 MB. With -gnat2022 added, leaks also reports 0 leaks for this form.
On x86_64-linux, I ran the attached leak_mallinfo.adb on Compiler
Explorer (as unit Example, the name required there), with -gnata. It is
leak.adb with 100_000 calls, and it prints by how much the heap in use,
as reported by mallinfo from glibc, grows across the loop:
gnat 9.5, 10.5:
growth of the heap in use: 0 bytes
gnat 11.1, 11.4, 12.5, 13.4, 14.3, 15.2, 16.1, 16.2:
growth of the heap in use: 3200656 bytes
gnat trunk (17.0.0 20261110):
growth of the heap in use: 3200656 bytes
That is again 32 bytes per call. With the postcondition form of the
contract, the same program prints a growth of 0 bytes with 10.5, 16.1
and trunk.
On aarch64-darwin, I get the results shown above for leak.adb and for
its postcondition form with the GNAT-FSF-builds packages of 13.2.0,
14.2.0, 15.3.0, 16.1.0 and 16.2.0.
GCC 11 is the first release with commit 4c5e9870f255 ("[Ada] Implement
AI12-0280 Making 'Old more flexible"). I have not bisected.
Second situation (attached leak_post.adb, which has pragma Ada_2022).
The prefix of 'Old is a function call, so it is evaluated only when
Enabled is True:
type Box is record
V : T;
end record;
function Get (B : Box) return T is (B.V);
procedure Incr (B : in out Box; Enabled : Boolean)
with Post => (if Enabled then B.V.X = Get (B)'Old.X + 1);
With 16.1.0 on aarch64-darwin, 100 calls of Incr (B, True) give "100
leaks for 3200 total leaked bytes". On x86_64-linux, the same program
with the mallinfo calls and 100_000 calls prints a growth of 3200656
bytes with 16.1 and with trunk.
Variants of leak.adb, 100 calls each, with 16.1.0 on aarch64-darwin:
- T derived from Ada.Finalization.Controlled: 100 leaks, 6400 bytes.
- Parameter of type T'Class: 100 leaks, 3200 bytes.
- T an untagged record with a component of a controlled type: 100
leaks, 6400 bytes.
- T an untagged record with only the Integer component: no leak.
- A String parameter, with S'Old (S'First) in the consequence: no
leak.
What I found while looking for the cause, in case it helps. The output
of -gnatG (16.1.0) for Incr in leak.adb has an allocator that is marked
for the secondary stack pool, but whose procedure_to_call is a
generated procedure that allocates from the global pool:
procedure leak__incr__P95b (A93b : out system__address; S96b :
system__storage_elements__storage_count; L94b :
system__storage_elements__storage_count) is
begin
$system__storage_pools__subpools__allocate_any_controlled (
system__pool_global__global_pool_object, null, C97b, A93b,
S96b, [alignment read from the tag of V], F99b, false);
return;
end leak__incr__P95b;
P90b : constant leak__incr__K77b := new leak__incr__T83b'(
leak__incr__T83b!(leak__p__TtC(v)))[storage_pool =
system__secondary_stack__ss_pool][procedure_to_call =
leak__incr__P95b];
The finalizer of Incr then calls Finalize_Object for the collection and
SS_Release. Nothing deallocates the block. In the String variant, which
does not leak, the allocator has procedure_to_call =
system__secondary_stack__ss_allocate.
In the sources of 16.1.0:
- Declare_Indirect_Temp_Via_Allocation (sem_util.adb) sets the
storage pool of the allocator to SS_Pool, with the comment
"Allocate saved prefix value on the secondary stack in order to
avoid introducing a storage leak. This allocated object is never
explicitly reclaimed."
- Build_Allocate_Deallocate_Proc (exp_util.adb) does not return early
for "access types used to implement indirect temps". It builds the
wrapper around Allocate_Any_Controlled with Associated_Storage_Pool
(Ptr_Typ) as the pool actual. For this access type, that is the
global pool object, as the expanded code shows. The storage pool of
the allocator is used only in the test for that early return and to
choose the profile of the wrapper.
Both subprograms are the same on trunk (d959aaea96ec). So the object
that is meant to go on the secondary stack is allocated on the heap,
and nothing frees it. I do not know which of the two places should
change.
$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=[...]/gnat-aarch64-darwin-16.1.0-1/bin/../libexec/gcc/aarch64-apple-darwin24.6.0/16.1.0/lto-wrapper
Target: aarch64-apple-darwin24.6.0
Configured with: ../src/configure
--prefix=/Users/runner/work/GNAT-FSF-builds/GNAT-FSF-builds/sbx/aarch64-darwin/gcc/install
--enable-languages=c,ada,c++ --enable-libstdcxx --enable-libstdcxx-threads
--enable-libada --with-pkgversion=GNAT-FSF-builds --disable-nls
--without-libiconv-prefix --disable-libstdcxx-pch --enable-lto
--disable-multilib --disable-libcilkrts
--with-build-sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
--with-specs='%{!-sysroot=*:--sysroot=%:if-exists-else(/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk)}'
--with-mpfr=/Users/runner/work/GNAT-FSF-builds/GNAT-FSF-builds/sbx/aarch64-darwin/mpfr/install
--with-gmp=/Users/runner/work/GNAT-FSF-builds/GNAT-FSF-builds/sbx/aarch64-darwin/gmp/install
--with-mpc=/Users/runner/work/GNAT-FSF-builds/GNAT-FSF-builds/sbx/aarch64-darwin/mpc/install
--build=aarch64-apple-darwin24.6.0
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 16.1.0 (GNAT-FSF-builds)
System: macOS 26.2 (25C56), arm64.