From b46ae44087b1cf6a90f70b62d5e1bd3d37fcbcb5 Mon Sep 17 00:00:00 2001 From: "shengjun.li" Date: Fri, 30 Apr 2021 12:21:02 +0800 Subject: [PATCH] Zero size allocation in StackDeviceMemory (#5100) When the remaining space is empty, the zero size allocation will get the pointer `end_`. However, `cudafree` the pointer `end_` will cause to crash. Fix: #5078 #4770 #4412 #4340 #3646 Signed-off-by: shengjun.li --- .../thirdparty/faiss/gpu/utils/StackDeviceMemory.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/internal/core/src/index/thirdparty/faiss/gpu/utils/StackDeviceMemory.cpp b/internal/core/src/index/thirdparty/faiss/gpu/utils/StackDeviceMemory.cpp index f462b02048..e10042ebc3 100644 --- a/internal/core/src/index/thirdparty/faiss/gpu/utils/StackDeviceMemory.cpp +++ b/internal/core/src/index/thirdparty/faiss/gpu/utils/StackDeviceMemory.cpp @@ -65,6 +65,10 @@ StackDeviceMemory::Stack::getSizeAvailable() const { char* StackDeviceMemory::Stack::getAlloc(size_t size, cudaStream_t stream) { + if (size == 0) { + return nullptr; + } + if (size > (end_ - head_)) { // Too large for our stack DeviceScope s(device_); @@ -133,6 +137,10 @@ void StackDeviceMemory::Stack::returnAlloc(char* p, size_t size, cudaStream_t stream) { + if (size == 0) { + return; + } + if (p < start_ || p >= end_) { // This is not on our stack; it was a one-off allocation DeviceScope s(device_);