[llvm] [IR] Use alloc markers for operator delete variants (PR #138261)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Fri May 2 05:41:25 PDT 2025
================
@@ -191,28 +191,70 @@ void *User::operator new(size_t Size, HungOffOperandsAllocMarker) {
LLVM_NO_SANITIZE_MEMORY_ATTRIBUTE void User::operator delete(void *Usr) {
// Hung off uses use a single Use* before the User, while other subclasses
// use a Use[] allocated prior to the user.
- User *Obj = static_cast<User *>(Usr);
+ const auto *Obj = static_cast<User *>(Usr);
if (Obj->HasHungOffUses) {
- assert(!Obj->HasDescriptor && "not supported!");
-
- Use **HungOffOperandList = static_cast<Use **>(Usr) - 1;
- // drop the hung off uses.
- Use::zap(*HungOffOperandList, *HungOffOperandList + Obj->NumUserOperands,
- /* Delete */ true);
- ::operator delete(HungOffOperandList);
+ const HungOffOperandsAllocMarker Marker{
+ Obj->NumUserOperands,
+ };
+ operator delete(Usr, Marker);
} else if (Obj->HasDescriptor) {
- Use *UseBegin = static_cast<Use *>(Usr) - Obj->NumUserOperands;
- Use::zap(UseBegin, UseBegin + Obj->NumUserOperands, /* Delete */ false);
-
- auto *DI = reinterpret_cast<DescriptorInfo *>(UseBegin) - 1;
- uint8_t *Storage = reinterpret_cast<uint8_t *>(DI) - DI->SizeInBytes;
- ::operator delete(Storage);
+ const IntrusiveOperandsAndDescriptorAllocMarker Marker{
+ Obj->NumUserOperands,
+ Obj->HasDescriptor,
+ };
+ operator delete(Usr, Marker);
} else {
- Use *Storage = static_cast<Use *>(Usr) - Obj->NumUserOperands;
- Use::zap(Storage, Storage + Obj->NumUserOperands,
- /* Delete */ false);
- ::operator delete(Storage);
+ const IntrusiveOperandsAllocMarker Marker{
+ Obj->NumUserOperands,
+ };
+ operator delete(Usr, Marker);
}
}
+// Repress memory sanitization, due to use-after-destroy by operator
+// delete. Bug report 24578 identifies this issue.
+LLVM_NO_SANITIZE_MEMORY_ATTRIBUTE void
+User::operator delete(void *Usr, const HungOffOperandsAllocMarker Marker) {
+ // Note: If a subclass manipulates the information which is required to
+ // calculate the Usr memory pointer, e.g. NumUserOperands, the operator
+ // delete of that subclass has to restore the changed information to the
+ // original value, since the dtor of that class is not called if the ctor
+ // fails.
----------------
nikic wrote:
I don't think these comments make sense anymore (as the data is now taken from the marker).
https://github.com/llvm/llvm-project/pull/138261
More information about the llvm-commits
mailing list