The non-placement new
expression is specified to invoke an allocation function to allocate storage for an object of the specified type. When successful, the allocation function, in turn, is required to return a pointer to storage with alignment suitable for any object with a fundamental alignment requirement. Although the global operator new
, the default allocation function invoked by the new expression, is specified by the C++ standard [ISO/IEC 14882-2014] to allocate sufficient storage suitably aligned to represent any object of the specified size, since the expected alignment isn't part of the function's interface, the most a program can safely assume is that the storage allocated by the default operator new
defined by the implementation is aligned for an object with a fundamental alignment. In particular, it is unsafe to use the storage for an object of a type with a stricter alignment requirement – anrequirement—an over-aligned type.
Furthermore, the array form of the non-placement new
expression may increase the amount of storage it attempts to obtain by invoking the corresponding allocation function by an unspecified amount. This amount, referred to as overhead in the C++ standard, is commonly known as a cookie. The cookie is used to store the number of elements in the array so that the array delete expression or the exception unwinding mechanism can invoke the type's destructor on each successfully constructed element of the array. While the specific conditions under which the cookie is required by the array new expression aren't described in the C++ standard, they may be outlined in other specifications such as the application binary interface (ABI) document for the target environment. For example, the Itanium C++ ABI describes the rules for computing the size of a cookie, its location, and achieving the correct alignment of the array elements. When these rules require that a cookie be created, it is possible to obtain a suitably aligned array of elements of an overaligned type [CodeSourcery 2016a]. However, the rules are involved complex and the Itanium C++ ABI isn't universally applicable.
Avoid relying on the default operator new
to obtain storage for objects of over-aligned types. Doing so may result in an object being constructed at a misaligned location, which has undefined behavior and can result in abnormal termination when the object is accessed, even on architectures otherwise known to tolerate misaligned accesses.
...
In the following noncompliant code example, the new expression is used to invoke the default operator new
to obtain storage in which to then construct an object of the user-defined type Vector
with alignment that exceeds the fundamental alignment of most implementations (typically 16 bytes). Objects of such over-aligned types are typically required by SIMD vectorization instructions (single instruction, multiple data) vectorization instructions, which can trap when passed unsuitably aligned arguments.
...
Using improperly aligned pointers results in undefined behavior, typically leading to abnormal termination.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
MEM57-CPP | Medium | Unlikely | Low | P6 | L2 |
Automated Detection
Tool | Version | Checker | Description |
---|
Astrée |
| default-new-overaligned-type | Fully checked | ||||||
Helix QAC |
| C++3129 | |||||||
Parasoft C/C++test |
| CERT_CPP-MEM57-a | Avoid using the default operator 'new' for over-aligned types | ||||||
Polyspace Bug Finder |
| CERT C++: MEM57-CPP | Checks for situations where operator new is not overloaded for possibly overaligned types (rule fully covered) | ||||||
RuleChecker |
| default-new-overaligned-type | Fully checked |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Related Guidelines
SEI CERT C++ Coding Standard | MEM54-CPP. Provide placement new with properly aligned pointers to sufficient storage capacity |
Bibliography
[ISO/IEC 14882-2014] | Subclause 3.7.4, "Dynamic Storage Duration" |
Allocation and |
Deallocation" |
[ |
CodeSourcery 2016a] | Itanium C++ ABI, version 1.86 |
[ |
INCITS 2012] | Dynamic memory allocation for over-aligned data, WG14 proposal |
...
...