From f6082af255a4aacdf80c302dd77e2a1d47ce253f Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=A2=85=ED=98=84/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Staff=20Engineer/=EC=82=BC=EC=84=B1?= =?utf8?q?=EC=A0=84=EC=9E=90?= Date: Thu, 6 Dec 2018 09:23:02 +0900 Subject: [PATCH] [coco] Deallocate Op in OpManager properly (#2500) The current implementation of OpManager just deallocates all the Op in sequential order without considering their dependencies. However, this implementation may result in USE-AFTER-FREE errors. This commit guarantees that OpManager deallocates Op nodes from the root to leaves. Signed-off-by: Jonghyun Park --- contrib/coco/core/include/coco/IR/OpManager.h | 3 +++ contrib/coco/core/src/IR/OpManager.cpp | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/contrib/coco/core/include/coco/IR/OpManager.h b/contrib/coco/core/include/coco/IR/OpManager.h index 95769f9..5f4f33c 100644 --- a/contrib/coco/core/include/coco/IR/OpManager.h +++ b/contrib/coco/core/include/coco/IR/OpManager.h @@ -37,6 +37,9 @@ public: OpManager(Module *m = nullptr) { module(m); } public: + ~OpManager(); + +public: template T *create(void); public: diff --git a/contrib/coco/core/src/IR/OpManager.cpp b/contrib/coco/core/src/IR/OpManager.cpp index ad20375..3911eb6 100644 --- a/contrib/coco/core/src/IR/OpManager.cpp +++ b/contrib/coco/core/src/IR/OpManager.cpp @@ -20,12 +20,35 @@ #include #include +#include using nncc::foundation::make_unique; namespace coco { +OpManager::~OpManager() +{ + std::set roots; + + for (uint32_t n = 0; n < size(); ++n) + { + auto op = at(n); + + if (op->up() != nullptr) + { + continue; + } + + roots.insert(op); + } + + for (const auto &op : roots) + { + destroy_all(op); + } +} + // // Each Op class SHOULD be default constructible // -- 2.7.4