Ada fixes finalize address for arrays of protected types
GCC's Ada compiler now correctly calculates finalization addresses for arrays of protected types, preventing crashes.
This commit fixes an issue in the Ada front end where the Make_Address_For_Finalize function incorrectly calculated the finalization address for arrays containing protected types. Previously, it would apply a descriptor size shift without verifying if the object actually possessed a controlled component. This could lead to memory access violations or silent errors when an incorrect address was dereferenced during scope exit, especially for arrays of protected types which lack a dope vector.
In Details
In the GCC Ada front end, exp_ch7.adb contains the Make_Address_For_Finalize function, responsible for determining the correct address for finalization routines on objects, particularly for arrays. The bug arose because this function would apply a -Descriptor_Size offset when handling unconstrained arrays but failed to check for the Has_Controlled_Component predicate. For protected types, Has_Controlled_Component is false, meaning no dope vector is allocated. Consequently, the incorrect address calculation could lead to an EXCEPTION_ACCESS_VIOLATION or other silent corruption at s…
For Context
Finalization is a process where a program cleans up resources used by an object when it's no longer needed, similar to a destructor in C++. This commit fixes a bug in how the Ada compiler (part of GCC) calculates the memory address where this cleanup should happen for certain complex data structures: arrays that hold 'protected types.' Protected types are a concurrency control mechanism in Ada. The compiler was making a wrong assumption about the memory layout for these arrays, leading it to try and clean up memory at an incorrect address. This could cause the program to crash (an 'access violation') or silently corrupt data, so this fix improves the stability and correctness of Ada programs that use such types.