From ed5b645e3ba696c255a8078849fc342b0affb95b Mon Sep 17 00:00:00 2001 From: Denes Tarjan Date: Mon, 4 Mar 2024 15:57:21 +0100 Subject: [PATCH] [test] Mock malloc for more stable allocation test --- scripts/ci.sh | 2 -- test/CMakeLists.txt | 3 +++ test/api/test_gaussian_blur.cpp | 2 ++ test/api/test_morphology.cpp | 4 +++- test/api/test_sobel.cpp | 4 ++++ test/framework/utils.cpp | 4 +++- test/framework/utils.h | 10 ++++++++++ test/framework/wrap_malloc.cpp | 19 +++++++++++++++++++ 8 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 test/framework/wrap_malloc.cpp diff --git a/scripts/ci.sh b/scripts/ci.sh index ff8d0321f..1d86b8799 100755 --- a/scripts/ci.sh +++ b/scripts/ci.sh @@ -68,8 +68,6 @@ scripts/prefix_testsuite_names.py build/test-results/gcc-neon/intrinsiccv-api-te LLVM_COV=llvm-cov scripts/generate_coverage_report.py # Clang address & undefined behaviour sanitizers -# Allow malloc to return NULL -export ASAN_OPTIONS="allocator_may_return_null=1" cmake -S . -B build/sanitize -G Ninja \ -DINTRINSICCV_ENABLE_SME2=OFF \ -DCMAKE_CXX_FLAGS="-fsanitize=address,undefined -fno-sanitize-recover=all -Wno-pass-failed" diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index b0264496c..af5c11849 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -23,10 +23,13 @@ else() list(APPEND INTRINSICCV_TEST_CXX_FLAGS "-O2" "-g0") endif() +SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--wrap,malloc") + set(INTRINSICCV_TEST_FRAMEWORK_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/framework/border.cpp ${CMAKE_CURRENT_SOURCE_DIR}/framework/test_main.cpp ${CMAKE_CURRENT_SOURCE_DIR}/framework/utils.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/framework/wrap_malloc.cpp ) include(FetchContent) diff --git a/test/api/test_gaussian_blur.cpp b/test/api/test_gaussian_blur.cpp index 0aa79e30d..51f29b428 100644 --- a/test/api/test_gaussian_blur.cpp +++ b/test/api/test_gaussian_blur.cpp @@ -357,10 +357,12 @@ TYPED_TEST(GaussianBlur, InvalidContextImageSize) { } TEST(FilterCreate, TooBigImage) { + MockMallocToFail::enable(); intrinsiccv_filter_context_t *context = nullptr; intrinsiccv_rectangle_t rect{INTRINSICCV_MAX_IMAGE_PIXELS, 1}; EXPECT_EQ(INTRINSICCV_ERROR_ALLOCATION, intrinsiccv_filter_create(&context, 1, 1, rect)); + MockMallocToFail::disable(); } TEST(FilterCreate, ImageSize) { diff --git a/test/api/test_morphology.cpp b/test/api/test_morphology.cpp index 738c80e80..0d189337d 100644 --- a/test/api/test_morphology.cpp +++ b/test/api/test_morphology.cpp @@ -358,8 +358,9 @@ TYPED_TEST(Morphology, UnsupportedSize) { } TYPED_TEST(Morphology, TooBigImage) { + MockMallocToFail::enable(); intrinsiccv_morphology_context_t *context = nullptr; - intrinsiccv_rectangle_t kernel{3, 10000}, image{1UL << 34, 10000}; + intrinsiccv_rectangle_t kernel{3, 3}, image{3072, 2048}; intrinsiccv_border_type_t border = INTRINSICCV_BORDER_TYPE_REPLICATE; intrinsiccv_border_values_t border_values{0, 0, 1, 1}; intrinsiccv_point_t anchor{1, 1}; @@ -374,6 +375,7 @@ TYPED_TEST(Morphology, TooBigImage) { intrinsiccv_morphology_create(&context, kernel2, anchor, border, border_values, 1, 1, sizeof(TypeParam), image2)); + MockMallocToFail::disable(); } TYPED_TEST(Morphology, InvalidAnchors) { diff --git a/test/api/test_sobel.cpp b/test/api/test_sobel.cpp index 53db4631a..7c1829435 100644 --- a/test/api/test_sobel.cpp +++ b/test/api/test_sobel.cpp @@ -151,6 +151,7 @@ TYPED_TEST(Sobel, MisalignmentVertical) { } TYPED_TEST(Sobel, ImageSizeHorizontal) { + MockMallocToFail::enable(); using KernelTestParams = SobelKernelTestParams; typename KernelTestParams::InputType src[1] = {}; typename KernelTestParams::OutputType dst[1]; @@ -168,9 +169,11 @@ TYPED_TEST(Sobel, ImageSizeHorizontal) { sobel_3x3_horizontal()(src, sizeof(src), dst, sizeof(dst), INTRINSICCV_MAX_IMAGE_PIXELS, 1, INTRINSICCV_MAXIMUM_CHANNEL_COUNT)); + MockMallocToFail::disable(); } TYPED_TEST(Sobel, ImageSizeVertical) { + MockMallocToFail::enable(); using KernelTestParams = SobelKernelTestParams; typename KernelTestParams::InputType src[1] = {}; typename KernelTestParams::OutputType dst[1]; @@ -187,6 +190,7 @@ TYPED_TEST(Sobel, ImageSizeVertical) { sobel_3x3_vertical()(src, sizeof(src), dst, sizeof(dst), INTRINSICCV_MAX_IMAGE_PIXELS, 1, INTRINSICCV_MAXIMUM_CHANNEL_COUNT)); + MockMallocToFail::disable(); } TYPED_TEST(Sobel, ChannelNumberHorizontal) { diff --git a/test/framework/utils.cpp b/test/framework/utils.cpp index ce034e060..541c2c7a7 100644 --- a/test/framework/utils.cpp +++ b/test/framework/utils.cpp @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2023 Arm Limited and/or its affiliates +// SPDX-FileCopyrightText: 2023 - 2024 Arm Limited and/or its affiliates // // SPDX-License-Identifier: Apache-2.0 @@ -14,6 +14,8 @@ #include #include +bool MockMallocToFail::enabled = false; + namespace test { template diff --git a/test/framework/utils.h b/test/framework/utils.h index 438b23223..23216c786 100644 --- a/test/framework/utils.h +++ b/test/framework/utils.h @@ -39,6 +39,16 @@ return; \ } +class MockMallocToFail { + public: + static void enable() { enabled = true; } + static void disable() { enabled = false; } + static bool is_enabled() { return enabled; } + + private: + static bool enabled; +}; + namespace test { class Options { diff --git a/test/framework/wrap_malloc.cpp b/test/framework/wrap_malloc.cpp new file mode 100644 index 000000000..c3ea12eff --- /dev/null +++ b/test/framework/wrap_malloc.cpp @@ -0,0 +1,19 @@ +// SPDX-FileCopyrightText: 2024 Arm Limited and/or its affiliates +// +// SPDX-License-Identifier: Apache-2.0 + +#include + +#include "framework/utils.h" + +// NOLINTBEGIN(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) +extern "C" void* __real_malloc(size_t); + +extern "C" void* __wrap_malloc(size_t size) { + if (MockMallocToFail::is_enabled()) { + return nullptr; + } + + return __real_malloc(size); +} +// NOLINTEND(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) -- GitLab