我正在我的单元测试上运行地址清理程序。
Cmake 看起来像这样:
cmake -G"Unix Makefiles" \
-DCMAKE_CXX_COMPILER=clang++-9 \
-DCMAKE_C_COMPILER=clang-9 \
-DCMAKE_C_FLAGS='-fsanitize=address -fsanitize-address-use-after-scope' \
-DCMAKE_CXX_FLAGS='-fsanitize=address -fsanitize-address-use-after-scope' \
运行命令是
make clean
ASAN_OPTIONS=detect_stack_use_after_return=1 make a.out -j
ASAN_OPTIONS=detect_stack_use_after_return=1 LSAN_OPTIONS=verbosity=1 ./a.out
Unitests 是用 gtest 框架编写的。
根据定义,有一些unitest应该对数组边界之外进行非法访问。
我想知道如何抑制这个测试
TEST_F(classA, testA) {
some_struct a;
a.p = 100;
ASSERT_FALSE(&foo());
}
我在这里看到了抑制函数的选项,但我不知道如何将其应用到unitest函数上https://github.com/google/sanitizers/wiki/AddressSanitizer https://github.com/google/sanitizers/wiki/AddressSanitizer
#if defined(__clang__) || defined (__GNUC__)
# define ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
#else
# define ATTRIBUTE_NO_SANITIZE_ADDRESS
#endif
...
ATTRIBUTE_NO_SANITIZE_ADDRESS
void ThisFunctionWillNotBeInstrumented() {...}
我怎样才能抑制这个测试
最简单的方法可能是#ifdef
it out:
TEST_F(classA, testA) {
#if defined(__SANITIZE_ADDRESS__)
std::cerr << "classA.testA skipped under AddressSanitizer" << std::endl;
#else
some_struct a;
a.p = 100;
ASSERT_FALSE(&foo());
#endif
}
或者你可以#ifdef
整个测试:
#if !defined(__SANITIZE_ADDRESS__)
TEST_F(classA, testA) {
std::cerr << "classA.testA skipped under AddressSanitizer" << std::endl;
some_struct a;
a.p = 100;
ASSERT_FALSE(&foo());
}
#endif
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)