From 3ba0ab113cd81705a3e5962ba807f30656987935 Mon Sep 17 00:00:00 2001 From: Georgios Pinitas Date: Wed, 12 Dec 2018 19:01:46 +0000 Subject: COMPMID-1710: Avoid undefined behavior in GCC 8.2 Undefined behavior when pushing object which contain unique_ptr to a vector container. Vector dynamic resizing was making all the unique_ptr members of the inserted objects up to this point invalid. As a workaround, memory is reserved to avoid vector reallocation. Change-Id: I74f7641a7f36981ebe51720a924b865bb7f54c91 Reviewed-on: https://review.mlplatform.org/390 Tested-by: Arm Jenkins Reviewed-by: Pablo Marquez --- arm_compute/graph/Workload.h | 14 ++++++++++++++ src/graph/detail/ExecutionHelpers.cpp | 8 ++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/arm_compute/graph/Workload.h b/arm_compute/graph/Workload.h index e9368eefd0..682b08d88d 100644 --- a/arm_compute/graph/Workload.h +++ b/arm_compute/graph/Workload.h @@ -69,6 +69,20 @@ public: */ struct ExecutionTask { + ExecutionTask(std::unique_ptr &&f, INode *n) + : task(std::move(f)), node(n) + { + } + /** Prevent instances of this class from being copied (As this class contains pointers) */ + ExecutionTask(const ExecutionTask &) = delete; + /** Prevent instances of this class from being copied (As this class contains pointers) */ + ExecutionTask &operator=(const ExecutionTask &) = delete; + /** Default Move Constructor. */ + ExecutionTask(ExecutionTask &&) noexcept = default; + /** Default move assignment operator */ + ExecutionTask &operator=(ExecutionTask &&) noexcept = default; + /** Default destructor */ + ~ExecutionTask() = default; // TODO (geopin01) : Support vector of functions? std::unique_ptr task = {}; /**< Task to execute */ INode *node = {}; /**< Node bound to this workload */ diff --git a/src/graph/detail/ExecutionHelpers.cpp b/src/graph/detail/ExecutionHelpers.cpp index f2c381b7df..767154b45e 100644 --- a/src/graph/detail/ExecutionHelpers.cpp +++ b/src/graph/detail/ExecutionHelpers.cpp @@ -135,6 +135,9 @@ ExecutionWorkload configure_all_nodes(Graph &g, GraphContext &ctx, const std::ve workload.graph = &g; workload.ctx = &ctx; + // Reserve memory for tasks + workload.tasks.reserve(node_order.size()); + // Create tasks for(auto &node_id : node_order) { @@ -146,10 +149,7 @@ ExecutionWorkload configure_all_nodes(Graph &g, GraphContext &ctx, const std::ve std::unique_ptr func = backend.configure_node(*node, ctx); if(func != nullptr) { - ExecutionTask task; - task.task = std::move(func); - task.node = node; - workload.tasks.push_back(std::move(task)); + workload.tasks.emplace_back(ExecutionTask(std::move(func), node)); } } } -- cgit v1.2.1