You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by yi...@apache.org on 2022/10/11 08:00:30 UTC

[doris] branch master updated: [fix](memory): avoid coredump when list pointer is null (#12919)

This is an automated email from the ASF dual-hosted git repository.

yiguolei pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 334708dc8c [fix](memory): avoid coredump when list pointer is null (#12919)
334708dc8c is described below

commit 334708dc8c4faa288cc173c2aaea36367d3100c3
Author: 赵立伟 <zl...@163.com>
AuthorDate: Tue Oct 11 16:00:23 2022 +0800

    [fix](memory): avoid coredump when list pointer is null (#12919)
---
 be/src/runtime/free_pool.hpp       | 13 ++++++--
 be/test/CMakeLists.txt             |  1 +
 be/test/runtime/free_pool_test.cpp | 67 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 78 insertions(+), 3 deletions(-)

diff --git a/be/src/runtime/free_pool.hpp b/be/src/runtime/free_pool.hpp
index db23d98fc6..8cc5b0677d 100644
--- a/be/src/runtime/free_pool.hpp
+++ b/be/src/runtime/free_pool.hpp
@@ -25,6 +25,7 @@
 
 #include <string>
 
+#include "common/compiler_util.h"
 #include "common/logging.h"
 #include "runtime/mem_pool.h"
 #include "util/bit_util.h"
@@ -128,9 +129,15 @@ public:
 #ifndef NDEBUG
         check_valid_allocation(list);
 #endif
-        // Add node to front of list.
-        node->next = list->next;
-        list->next = node;
+        if (UNLIKELY(nullptr == list)) {
+            // free memory directly if the pointer to free list is null
+            LOG(ERROR) << "The free list was released, and this may cause memory leak.";
+            free(ptr);
+        } else {
+            // Add node to front of list.
+            node->next = list->next;
+            list->next = node;
+        }
     }
 
     // Returns an allocation that is at least 'size'. If the current allocation
diff --git a/be/test/CMakeLists.txt b/be/test/CMakeLists.txt
index b33b0a0fee..88ddab1715 100644
--- a/be/test/CMakeLists.txt
+++ b/be/test/CMakeLists.txt
@@ -245,6 +245,7 @@ set(RUNTIME_TEST_FILES
     runtime/memory/system_allocator_test.cpp
     runtime/cache/partition_cache_test.cpp
     runtime/collection_value_test.cpp
+    runtime/free_pool_test.cpp
     #runtime/array_test.cpp
 )
 set(TESTUTIL_TEST_FILES
diff --git a/be/test/runtime/free_pool_test.cpp b/be/test/runtime/free_pool_test.cpp
new file mode 100644
index 0000000000..5dc89be45b
--- /dev/null
+++ b/be/test/runtime/free_pool_test.cpp
@@ -0,0 +1,67 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "runtime/free_pool.hpp"
+
+#include <gtest/gtest.h>
+
+#include <memory>
+
+namespace doris {
+
+class FreePoolTest : public testing::Test {
+public:
+    FreePoolTest() {}
+
+protected:
+    virtual void SetUp() override {
+        memPool.reset(new MemPool());
+        freePool.reset(new FreePool(memPool.get()));
+    }
+    virtual void TearDown() override {
+        memPool.reset(nullptr);
+        freePool.reset(nullptr);
+    }
+
+protected:
+    std::unique_ptr<MemPool> memPool;
+    std::unique_ptr<FreePool> freePool;
+};
+
+inline uint16_t getFreeListNodeCount(FreePool::FreeListNode* list) {
+    auto count = 0;
+    auto current = list;
+    while (nullptr != current) {
+        count++;
+        current = current->next;
+    }
+    return count;
+}
+
+TEST_F(FreePoolTest, free) {
+    const int64_t size = 16;
+    auto ptr = freePool->allocate(size);
+
+    auto node = reinterpret_cast<FreePool::FreeListNode*>(ptr - sizeof(FreePool::FreeListNode));
+    auto list = node->list;
+    auto beforeCount = getFreeListNodeCount(list);
+    freePool->free(ptr);
+    auto afterCount = getFreeListNodeCount(list);
+
+    ASSERT_EQ(beforeCount + 1, afterCount);
+}
+} // namespace doris


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org