Path: blob/main/contrib/llvm-project/clang/lib/AST/Interp/DynamicAllocator.cpp
35291 views
//==-------- DynamicAllocator.cpp - Dynamic allocations ----------*- C++ -*-==//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "DynamicAllocator.h"9#include "InterpBlock.h"10#include "InterpState.h"1112using namespace clang;13using namespace clang::interp;1415DynamicAllocator::~DynamicAllocator() { cleanup(); }1617void DynamicAllocator::cleanup() {18// Invoke destructors of all the blocks and as a last restort,19// reset all the pointers pointing to them to null pointees.20// This should never show up in diagnostics, but it's necessary21// for us to not cause use-after-free problems.22for (auto &Iter : AllocationSites) {23auto &AllocSite = Iter.second;24for (auto &Alloc : AllocSite.Allocations) {25Block *B = reinterpret_cast<Block *>(Alloc.Memory.get());26B->invokeDtor();27if (B->hasPointers()) {28while (B->Pointers) {29Pointer *Next = B->Pointers->Next;30B->Pointers->PointeeStorage.BS.Pointee = nullptr;31B->Pointers = Next;32}33B->Pointers = nullptr;34}35}36}3738AllocationSites.clear();39}4041Block *DynamicAllocator::allocate(const Expr *Source, PrimType T,42size_t NumElements, unsigned EvalID) {43// Create a new descriptor for an array of the specified size and44// element type.45const Descriptor *D = allocateDescriptor(46Source, T, Descriptor::InlineDescMD, NumElements, /*IsConst=*/false,47/*IsTemporary=*/false, /*IsMutable=*/false);4849return allocate(D, EvalID);50}5152Block *DynamicAllocator::allocate(const Descriptor *ElementDesc,53size_t NumElements, unsigned EvalID) {54// Create a new descriptor for an array of the specified size and55// element type.56const Descriptor *D = allocateDescriptor(57ElementDesc->asExpr(), ElementDesc, Descriptor::InlineDescMD, NumElements,58/*IsConst=*/false, /*IsTemporary=*/false, /*IsMutable=*/false);59return allocate(D, EvalID);60}6162Block *DynamicAllocator::allocate(const Descriptor *D, unsigned EvalID) {63assert(D);64assert(D->asExpr());6566auto Memory =67std::make_unique<std::byte[]>(sizeof(Block) + D->getAllocSize());68auto *B = new (Memory.get()) Block(EvalID, D, /*isStatic=*/false);69B->invokeCtor();7071InlineDescriptor *ID = reinterpret_cast<InlineDescriptor *>(B->rawData());72ID->Desc = D;73ID->IsActive = true;74ID->Offset = sizeof(InlineDescriptor);75ID->IsBase = false;76ID->IsFieldMutable = false;77ID->IsConst = false;78ID->IsInitialized = false;7980B->IsDynamic = true;8182if (auto It = AllocationSites.find(D->asExpr()); It != AllocationSites.end())83It->second.Allocations.emplace_back(std::move(Memory));84else85AllocationSites.insert(86{D->asExpr(), AllocationSite(std::move(Memory), D->isArray())});87return B;88}8990bool DynamicAllocator::deallocate(const Expr *Source,91const Block *BlockToDelete, InterpState &S) {92auto It = AllocationSites.find(Source);93if (It == AllocationSites.end())94return false;9596auto &Site = It->second;97assert(Site.size() > 0);9899// Find the Block to delete.100auto AllocIt = llvm::find_if(Site.Allocations, [&](const Allocation &A) {101const Block *B = reinterpret_cast<const Block *>(A.Memory.get());102return BlockToDelete == B;103});104105assert(AllocIt != Site.Allocations.end());106107Block *B = reinterpret_cast<Block *>(AllocIt->Memory.get());108B->invokeDtor();109110S.deallocate(B);111Site.Allocations.erase(AllocIt);112113if (Site.size() == 0)114AllocationSites.erase(It);115116return true;117}118119120