[SDK] Fix asan errors (#308)

* fix several heap-use-after-free bugs

* rebase master

* add option for sanitizers

* rebase master

* fix conflicts
pull/1/head
lzhangzz 2021-12-21 10:47:21 +08:00 committed by GitHub
parent 547a160770
commit 43e6714f47
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
33 changed files with 80 additions and 70 deletions

View File

@ -9,6 +9,7 @@ project(MMDeploy VERSION 0.1.0)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
@ -28,6 +29,20 @@ if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release CACHE STRING "choose 'Release' as default build type" FORCE)
endif ()
# when CUDA devices are enabled, the environment variable ASAN_OPTIONS=protect_shadow_gap=0
# must be set at runtime
if (MMDEPLOY_ASAN_ENABLE)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fsanitize=address>)
add_link_options(-fsanitize=address)
endif ()
# notice that ubsan has linker issues for ubuntu < 18.04, see
# https://stackoverflow.com/questions/50024731/ld-unrecognized-option-push-state-no-as-needed
if (MMDEPLOY_UBSAN_ENABLE)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fsanitize=undefined>)
add_link_options(-fsanitize=undefined)
endif ()
include(${CMAKE_SOURCE_DIR}/cmake/common.cmake)
# set INTERFACE target to gather linked modules
add_library(MMDeployStaticModules INTERFACE)

View File

@ -4,6 +4,7 @@
#include "codebase/mmcls/mmcls.h"
#include "core/tensor.h"
#include "core/utils/device_utils.h"
#include "core/utils/formatter.h"
#include "experimental/module_adapter.h"
@ -29,29 +30,27 @@ class LinearClsHead : public MMClassification {
assert(output_tensor.shape().size() >= 2);
auto class_num = (int)output_tensor.shape()[1];
if (output_tensor.device().is_host()) {
vector<float> scores(output_tensor.data<float>(),
output_tensor.data<float>() + output_tensor.size());
OUTCOME_TRY(stream().Wait());
return GetLabels(scores, class_num);
} else {
vector<float> scores(output_tensor.size());
OUTCOME_TRY(output_tensor.CopyTo(scores.data(), stream()));
OUTCOME_TRY(stream().Wait());
return GetLabels(scores, class_num);
if (output_tensor.data_type() != DataType::kFLOAT) {
return Status(eNotSupported);
}
OUTCOME_TRY(auto _scores, MakeAvailableOnDevice(output_tensor, kHost, stream()));
OUTCOME_TRY(stream().Wait());
return GetLabels(_scores, class_num);
}
private:
Value GetLabels(const vector<float>& scores, int class_num) const {
Value GetLabels(const Tensor& scores, int class_num) const {
auto scores_data = scores.data<float>();
ClassifyOutput output;
output.labels.reserve(topk_);
std::vector<int> idx(class_num);
iota(begin(idx), end(idx), 0);
partial_sort(begin(idx), begin(idx) + topk_, end(idx),
[&](int i, int j) { return scores[i] > scores[j]; });
[&](int i, int j) { return scores_data[i] > scores_data[j]; });
for (int i = 0; i < topk_; ++i) {
auto label = ClassifyOutput::Label{idx[i], scores[idx[i]]};
auto label = ClassifyOutput::Label{idx[i], scores_data[idx[i]]};
DEBUG("label_id: {}, score: {}", label.label_id, label.score);
output.labels.push_back(label);
}
@ -59,6 +58,8 @@ class LinearClsHead : public MMClassification {
}
private:
static constexpr const auto kHost = Device{0};
int topk_{1};
};

View File

@ -1,11 +1,11 @@
// Copyright (c) OpenMMLab. All rights reserved.
#include "core/registry.h"
#include "core/utils/device_utils.h"
#include "experimental/module_adapter.h"
#include "object_detection.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "preprocess/cpu/opencv_utils.h"
#include "preprocess/transform/transform_utils.h"
namespace mmdeploy::mmdet {

View File

@ -3,6 +3,7 @@
#include "object_detection.h"
#include "core/registry.h"
#include "core/utils/device_utils.h"
#include "experimental/module_adapter.h"
using namespace std;
@ -36,20 +37,13 @@ Result<Value> ResizeBBox::operator()(const Value& prep_res, const Value& infer_r
// 'bboxes_number'
assert(labels.shape().size() == 2);
if (dets.device().is_host()) {
OUTCOME_TRY(auto result, DispatchGetBBoxes(prep_res["img_metas"], dets, labels));
return to_value(result);
} else {
TensorDesc _dets_desc{Device{"cpu"}, dets.data_type(), dets.shape(), dets.name()};
TensorDesc _labels_desc{Device{"cpu"}, labels.data_type(), labels.shape(), labels.name()};
Tensor _dets(_dets_desc);
Tensor _labels(_labels_desc);
OUTCOME_TRY(dets.CopyTo(_dets, stream()));
OUTCOME_TRY(labels.CopyTo(_labels, stream()));
OUTCOME_TRY(stream().Wait());
OUTCOME_TRY(auto result, DispatchGetBBoxes(prep_res["img_metas"], _dets, _labels));
return to_value(result);
}
OUTCOME_TRY(auto _dets, MakeAvailableOnDevice(dets, kHost, stream()));
OUTCOME_TRY(auto _labels, MakeAvailableOnDevice(labels, kHost, stream()));
OUTCOME_TRY(stream().Wait());
OUTCOME_TRY(auto result, DispatchGetBBoxes(prep_res["img_metas"], _dets, _labels));
return to_value(result);
} catch (...) {
return Status(eFail);
}

View File

@ -1,11 +1,10 @@
// Copyright (c) OpenMMLab. All rights reserved.
#include <preprocess/transform/transform_utils.h>
#include <opencv2/core.hpp>
#include "codebase/mmedit/mmedit.h"
#include "core/tensor.h"
#include "core/utils/device_utils.h"
namespace mmdeploy::mmedit {

View File

@ -6,6 +6,7 @@
#include "core/model.h"
#include "core/registry.h"
#include "core/tensor.h"
#include "core/utils/device_utils.h"
#include "core/utils/formatter.h"
#include "core/value.h"
#include "experimental/module_adapter.h"
@ -58,23 +59,17 @@ class CTCConvertor : public MMOCR {
}
Result<Value> operator()(const Value& _data, const Value& _prob) {
// auto img = _data["img"].get<Tensor>();
// WARN("img shape: {}", img.shape());
auto d_conf = _prob["output"].get<Tensor>();
std::vector<float> h_conf;
float* data{};
if (d_conf.device() != kHost) {
h_conf.resize(d_conf.byte_size() / sizeof(float));
OUTCOME_TRY(d_conf.CopyTo(h_conf.data(), stream_));
OUTCOME_TRY(stream_.Wait());
data = h_conf.data();
} else {
OUTCOME_TRY(stream_.Wait());
data = d_conf.data<float>();
if (d_conf.data_type() != DataType::kFLOAT) {
return Status(eNotSupported);
}
OUTCOME_TRY(auto h_conf, MakeAvailableOnDevice(d_conf, Device{0}, stream()));
OUTCOME_TRY(stream().Wait());
auto data = h_conf.data<float>();
auto shape = d_conf.shape();
auto w = static_cast<int>(shape[1]);
auto c = static_cast<int>(shape[2]);

View File

@ -8,12 +8,12 @@
#include "core/registry.h"
#include "core/serialization.h"
#include "core/tensor.h"
#include "core/utils/device_utils.h"
#include "core/utils/formatter.h"
#include "core/value.h"
#include "experimental/module_adapter.h"
#include "mmocr.h"
#include "preprocess/cpu/opencv_utils.h"
#include "preprocess/transform/transform_utils.h"
namespace mmdeploy::mmocr {

View File

@ -5,11 +5,11 @@
#include "archive/json_archive.h"
#include "archive/value_archive.h"
#include "core/tensor.h"
#include "core/utils/device_utils.h"
#include "core/utils/formatter.h"
#include "opencv2/imgproc.hpp"
#include "preprocess/cpu/opencv_utils.h"
#include "preprocess/transform/resize.h"
#include "preprocess/transform/transform_utils.h"
using namespace std;

View File

@ -2,10 +2,10 @@
#include "codebase/mmseg/mmseg.h"
#include "core/tensor.h"
#include "core/utils/device_utils.h"
#include "core/utils/formatter.h"
#include "preprocess/cpu/opencv_utils.h"
#include "preprocess/transform/transform.h"
#include "preprocess/transform/transform_utils.h"
namespace mmdeploy::mmseg {

View File

@ -33,6 +33,7 @@ set(SRCS
operator.cpp
status_code.cpp
tensor.cpp
utils/device_utils.cpp
utils/formatter.cpp
utils/stacktrace.cpp)
build_object_target(${CORE_OBJ} "${SRCS}")

View File

@ -19,9 +19,14 @@ struct TensorDesc {
std::string name;
};
class Tensor final {
class Tensor {
public:
Tensor() = default;
Tensor(const Tensor&) = default;
Tensor(Tensor&&) noexcept = default;
Tensor& operator=(const Tensor&) = default;
Tensor& operator=(Tensor&&) noexcept = default;
Tensor(const TensorDesc& desc, Allocator allocator = {}); // NOLINT
Tensor(const TensorDesc& desc, Buffer buffer);
Tensor(const TensorDesc& desc, std::shared_ptr<void> data);

View File

@ -1,6 +1,6 @@
// Copyright (c) OpenMMLab. All rights reserved.
#include "transform_utils.h"
#include "device_utils.h"
namespace mmdeploy {
Result<Mat> MakeAvailableOnDevice(const Mat& src, const Device& device, Stream& stream) {

View File

@ -126,8 +126,8 @@ class Tree : public AllocatorImpl {
if (auto it = tree_.lower_bound(size); it != tree_.end()) {
if (size * thresh_denominator_ >= it->first * thresh_numerator_) {
Block block(it->second, it->first);
tree_.erase(it);
tree_bytes_ -= it->first;
tree_.erase(it);
return block;
}
}

View File

@ -169,10 +169,10 @@ struct NetModule::Impl {
}
if (output.size() > 1) {
for (int i = 0; i < output.size(); ++i) {
output[i].insert({name, tmp.Slice(i)});
output[i].emplace(name, tmp.Slice(i));
}
} else {
output[0].insert({name, std::move(tmp)});
output[0].emplace(name, std::move(tmp));
}
}

View File

@ -1,8 +1,8 @@
// Copyright (c) OpenMMLab. All rights reserved.
#include "core/utils/device_utils.h"
#include "opencv_utils.h"
#include "preprocess/transform/crop.h"
#include "preprocess/transform/transform_utils.h"
using namespace std;

View File

@ -1,8 +1,8 @@
// Copyright (c) OpenMMLab. All rights reserved.
#include "core/utils/device_utils.h"
#include "opencv_utils.h"
#include "preprocess/transform/image2tensor.h"
#include "preprocess/transform/transform_utils.h"
namespace mmdeploy {
namespace cpu {

View File

@ -1,9 +1,9 @@
// Copyright (c) OpenMMLab. All rights reserved.
#include "core/registry.h"
#include "core/utils/device_utils.h"
#include "opencv_utils.h"
#include "preprocess/transform/normalize.h"
#include "preprocess/transform/transform_utils.h"
using namespace std;

View File

@ -1,8 +1,8 @@
// Copyright (c) OpenMMLab. All rights reserved.
#include "core/utils/device_utils.h"
#include "opencv_utils.h"
#include "preprocess/transform/pad.h"
#include "preprocess/transform/transform_utils.h"
using namespace std;

View File

@ -2,9 +2,9 @@
#include "core/registry.h"
#include "core/tensor.h"
#include "core/utils/device_utils.h"
#include "opencv_utils.h"
#include "preprocess/transform/resize.h"
#include "preprocess/transform/transform_utils.h"
using namespace std;

View File

@ -2,8 +2,8 @@
#include <cuda_runtime.h>
#include "core/utils/device_utils.h"
#include "preprocess/transform/crop.h"
#include "preprocess/transform/transform_utils.h"
using namespace std;

View File

@ -2,8 +2,8 @@
#include <cuda_runtime.h>
#include "core/utils/device_utils.h"
#include "preprocess/transform/image2tensor.h"
#include "preprocess/transform/transform_utils.h"
namespace mmdeploy {
namespace cuda {

View File

@ -2,10 +2,10 @@
#include <cuda_runtime.h>
#include "core/utils/device_utils.h"
#include "core/utils/formatter.h"
#include "ppl/cv/cuda/cvtcolor.h"
#include "preprocess/transform/load.h"
#include "preprocess/transform/transform_utils.h"
using namespace std;
using namespace ppl::cv::cuda;

View File

@ -2,9 +2,9 @@
#include <cuda_runtime.h>
#include "core/utils/device_utils.h"
#include "core/utils/formatter.h"
#include "preprocess/transform/normalize.h"
#include "preprocess/transform/transform_utils.h"
using namespace std;

View File

@ -1,9 +1,9 @@
// Copyright (c) OpenMMLab. All rights reserved.
#include "core/utils/device_utils.h"
#include "core/utils/formatter.h"
#include "ppl/cv/cuda/copymakeborder.h"
#include "preprocess/transform/pad.h"
#include "preprocess/transform/transform_utils.h"
using namespace std;
using namespace ppl::cv::cuda;

View File

@ -1,9 +1,9 @@
// Copyright (c) OpenMMLab. All rights reserved.
#include "core/utils/device_utils.h"
#include "core/utils/formatter.h"
#include "ppl/cv/cuda/resize.h"
#include "preprocess/transform/resize.h"
#include "preprocess/transform/transform_utils.h"
using namespace std;

View File

@ -14,8 +14,8 @@ set(SRCS
normalize.cpp
pad.cpp
resize.cpp
transform.cpp
transform_utils.cpp)
transform.cpp)
build_object_target(${TRANSFORM_OBJ} "${SRCS}")
target_include_directories(${TRANSFORM_OBJ} PUBLIC
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/preprocess>)

View File

@ -3,9 +3,9 @@
#include "catch.hpp"
#include "core/mat.h"
#include "core/utils/device_utils.h"
#include "preprocess/cpu/opencv_utils.h"
#include "preprocess/transform/transform.h"
#include "preprocess/transform/transform_utils.h"
#include "test_resource.h"
#include "test_utils.h"

View File

@ -1,9 +1,9 @@
// Copyright (c) OpenMMLab. All rights reserved.
#include "catch.hpp"
#include "core/tensor.h"
#include "core/utils/device_utils.h"
#include "preprocess/cpu/opencv_utils.h"
#include "preprocess/transform/transform.h"
#include "preprocess/transform/transform_utils.h"
#include "test_resource.h"
#include "test_utils.h"

View File

@ -3,9 +3,9 @@
#include "catch.hpp"
#include "core/mat.h"
#include "core/tensor.h"
#include "core/utils/device_utils.h"
#include "preprocess/cpu/opencv_utils.h"
#include "preprocess/transform/transform.h"
#include "preprocess/transform/transform_utils.h"
#include "test_resource.h"
#include "test_utils.h"

View File

@ -2,9 +2,9 @@
#include "catch.hpp"
#include "core/mat.h"
#include "core/utils/device_utils.h"
#include "preprocess/cpu/opencv_utils.h"
#include "preprocess/transform/transform.h"
#include "preprocess/transform/transform_utils.h"
#include "test_resource.h"
#include "test_utils.h"

View File

@ -2,9 +2,9 @@
#include "catch.hpp"
#include "core/mat.h"
#include "core/utils/device_utils.h"
#include "preprocess/cpu/opencv_utils.h"
#include "preprocess/transform/transform.h"
#include "preprocess/transform/transform_utils.h"
#include "test_resource.h"
#include "test_utils.h"

View File

@ -2,9 +2,9 @@
#include "catch.hpp"
#include "core/mat.h"
#include "core/utils/device_utils.h"
#include "preprocess/cpu/opencv_utils.h"
#include "preprocess/transform/transform.h"
#include "preprocess/transform/transform_utils.h"
#include "test_resource.h"
#include "test_utils.h"