ada: Fix strict aliasing violation in parameter passing (continued)
commitccdef2aef6463159a596ef1a4afe6ddce9d0f2ea
authorEric Botcazou <ebotcazou@adacore.com>
Sun, 10 Mar 2024 12:22:55 +0000 (10 13:22 +0100)
committerMarc Poulhiès <poulhies@adacore.com>
Tue, 21 May 2024 07:27:57 +0000 (21 09:27 +0200)
tree9666c6b09928031b43f40edd962a4c3463871c28
parentd851e08ba97f5d0671711763f6a7d4c94afb8457
ada: Fix strict aliasing violation in parameter passing (continued)

This fixes another long-standing (implicit) violation of the strict aliasing
rules that occurs when the result of a value conversion is directly passed
as an actual parameter in a call to a subprogram and the passing mechanism
is by reference.  In this case, the reference passed to the subprogram may
be to a type that is too different from the type of the underlying object,
which is the definition of such a violation.

The change reworks and strengthens the previous fix as follows: first, the
detection of these violations is moved into a dedicated predicate; second,
an assertion is added to check that none of them has been missed, which is
triggered by either -fchecking or -fstrict-aliasing, as the closely related
assertion that is present in relate_alias_sets.

The assertion uncovered two internal sources of violations: implementation
types for packed array types with peculiar index types and interface types,
which are fixed by propagating alias sets in the first case and resorting to
universal aliasing in the second case.

Finally, an unconditional warning is implemented to inform the user that the
temporary is created and to suggest a possible solution to prevent that.

gcc/ada/

* gcc-interface/decl.cc (gnat_to_gnu_entity) <E_Array_Type>: For a
packed type implemented specially, temporarily save the XUA type as
equivalent to the entity before processing the implementation type.
For this implementation type, if its component type is the same as
that of the original type, copy the alias set from the latter.
<types>: Resort to universal aliasing for all interface types.
* gcc-interface/trans.cc (Call_to_gnu): Add GNU_ACTUAL_TYPE local
variable and rename existing one to GNU_UNPADDED_ACTUAL_TYPE.
If the formal is passed by reference and the actual is a conversion,
call aliasable_p to detect aliasing violations, issue a warning upon
finding one and create the temporary in the target type.
Add an assertion that no such violation has been missed above.
(addressable_p): Revert latest changes.
(aliasable_p): New predicate.
* gcc-interface/utils2.cc (build_binary_op) <ARRAY_RANGE_REF>: When
creating a new array type on the fly, preserve the alias set of the
operation type.
gcc/ada/gcc-interface/decl.cc
gcc/ada/gcc-interface/trans.cc
gcc/ada/gcc-interface/utils2.cc