Overview
The main goal of this work is to allow developers to express the need to place instances of a class or structure in the read-only part of the program memory. Such a placement is desirable to prevent any further modifications to the instances of a given structure, by leveraging the read-only run time protection.
To achieve this, we are introducing a new attribute that can be attached to any record definition or a declaration. The compiler enforces that every instance of this type can be placed in the read-only segment of the program memory, provided the target triplet supports such a placement. If an instance of a given type bearing this attribute doesn’t satisfy such a placement, the compiler attaches an appropriate warning at suitable program locations. In other words, adding this attribute to a type requires every instance of this type to be a global const, which are placed in the read-only segments for most target triplets. However, this is *not a language feature* and it *need not* be true for *all target triplets*.
Read-only placement of instances
In this subsection, we briefly discuss when an instance generally qualifies to be placed in the read-only part of the program memory.
The instance must satisfy the following:
- The instance is globally declared and is not allocated on either the heap or stack.
- The variable declaration is const-qualified.
- No fields of the instance are mutable. This includes fields directly defined by the type and those inherited from other types.
- The type defines no explicit non-constexpr constructor for initialization.
- If another type defines an instance of the type bearing this attribute , then the owning instance should also be eligible to be placed in the read-only memory. In other words, the owning instance is const. qualified global, doesn’t define/inherit mutable fields and has no constructors that force the instance out of read-only memory.
- Every other type that inherits from this type must also adheres to all the above listed requirements.
Plan
The goal is to build checkers/analyses that can detect the violations of above requirements for a given type and emit warning during compilation. We plan to roll out this features with multiple. In the current patch, we are adding support to attach attributes to record declarations and definitions. The clang semantic analysis checks that every global declaration of this record type is const qualified, otherwise it emits suitable warnings and notes. The current patch relies on the user to ensure the attribute is only attached to appropriate types that already satisfy requirements (1, 3, 4, 5 and 6). However, as stated above we will be adding additional support to ensure they are satisfied by a given type.
Introducing new Attribute: enforce_read_only_placement
The attribute can be attached to either a type declaration or the type definition
*Example usage:*
- struct __*attribute__*(enforce_read_only_placement) Foo;
- struct __*attribute__*(enforce_read_only_placement) Bar { ... };
Warning Messages and Notes emitted:
- Current patch:
- Global variable declaration site without const qualification: (Part of current patch): “Variable of type ‘T’ will not be in the read only program memory.”
- Corresponding note attached to the type definition/declaration when a warning is emitted: “Type ‘T’ was declared as a read-only type here.”
- Future patches:
- Allocation on heap (stack): “Instance won't be placed in the read only program memory.”
- Mutable field definition: “Instances of type ‘T’ will not be in the read only program memory.”
- Explicit non-constexpr constructor definition: “Instances of type ‘T’ will not be in the read only program memory.”
- Inheriting from a structure that defines mutable fields.: “Inheriting from a type that can not be in the read only program memory”
- Type Q inherits from type T, but Q doesn’t define the attribute.: “‘Q’ not declared as a read-only program memory placeable type.”
- Type Q defines a field of type T, but Q doesn’t define the attribute.: “‘Q’ not declared as a read-only program memory placeable type.”
So as far as I understad, you don't want to support type aliases. I.e.:
is out of scope.