Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions flang-rt/lib/runtime/pointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "flang/Runtime/pointer.h"
#include "flang-rt/runtime/allocator-registry.h"
#include "flang-rt/runtime/assign-impl.h"
#include "flang-rt/runtime/derived.h"
#include "flang-rt/runtime/environment.h"
Expand Down Expand Up @@ -121,13 +122,15 @@ void RTDEF(PointerAssociateRemapping)(Descriptor &pointer,
}
}

RT_API_ATTRS void *AllocateValidatedPointerPayload(std::size_t byteSize) {
RT_API_ATTRS void *AllocateValidatedPointerPayload(
std::size_t byteSize, int allocatorIdx) {
// Add space for a footer to validate during deallocation.
constexpr std::size_t align{sizeof(std::uintptr_t)};
byteSize = ((byteSize + align - 1) / align) * align;
std::size_t total{byteSize + sizeof(std::uintptr_t)};
void *p{std::malloc(total)};
if (p) {
AllocFct alloc{allocatorRegistry.GetAllocator(allocatorIdx)};
void *p{alloc(total)};
if (p && allocatorIdx == 0) {
// Fill the footer word with the XOR of the ones' complement of
// the base address, which is a value that would be highly unlikely
// to appear accidentally at the right spot.
Expand All @@ -151,7 +154,7 @@ int RTDEF(PointerAllocate)(Descriptor &pointer, bool hasStat,
elementBytes = pointer.raw().elem_len = 0;
}
std::size_t byteSize{pointer.Elements() * elementBytes};
void *p{AllocateValidatedPointerPayload(byteSize)};
void *p{AllocateValidatedPointerPayload(byteSize, pointer.GetAllocIdx())};
if (!p) {
return ReturnError(terminator, CFI_ERROR_MEM_ALLOCATION, errMsg, hasStat);
}
Expand Down Expand Up @@ -211,6 +214,7 @@ int RTDEF(PointerDeallocate)(Descriptor &pointer, bool hasStat,
return ReturnError(terminator, StatBaseNull, errMsg, hasStat);
}
if (executionEnvironment.checkPointerDeallocation &&
pointer.GetAllocIdx() == kDefaultAllocator &&
!ValidatePointerPayload(pointer.raw())) {
return ReturnError(terminator, StatBadPointerDeallocation, errMsg, hasStat);
}
Expand Down
3 changes: 2 additions & 1 deletion flang/include/flang/Runtime/pointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ bool RTDECL(PointerIsAssociatedWith)(

// Fortran POINTERs are allocated with an extra validation word after their
// payloads in order to detect erroneous deallocations later.
RT_API_ATTRS void *AllocateValidatedPointerPayload(std::size_t);
RT_API_ATTRS void *AllocateValidatedPointerPayload(
std::size_t, int allocatorIdx = 0);
RT_API_ATTRS bool ValidatePointerPayload(const ISO::CFI_cdesc_t &);

} // extern "C"
Expand Down