我们使用以下代码来确定是否-fsanitize=address
在编译时为clang和gcc指定了.我们如何确定是否-fsanitize=undefined
已指定?
bool isSanitized = false;
#if defined(__has_feature)
#if __has_feature(address_sanitizer)
isSanitized = true;
#endif
#elif defined(__SANITIZE_ADDRESS__)
isSanitized = true;
#endif
Run Code Online (Sandbox Code Playgroud) 最近,当使用 '-fsanitize=address' 编译时,我在使用 valgrind 运行应用程序时遇到执行异常,即
“ASan 运行时不会出现在初始库列表中”
我有点不知道 valgrind 实际上做了什么。命令“ldd file.exe”提供
linux-gate.so.1 => (0xb7755000)
libasan.so.3 => /usr/lib/i386-linux-gnu/libasan.so.3 (0xb7199000)
libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xb6fdf000)
libdl.so.2 => /lib/i386-linux-gnu/libdl.so.2 (0xb6fd8000)
librt.so.1 => /lib/i386-linux-gnu/librt.so.1 (0xb6fcf000)
libpthread.so.0 => /lib/i386-linux-gnu/libpthread.so.0 (0xb6fb2000)
libm.so.6 => /lib/i386-linux-gnu/libm.so.6 (0xb6f5c000)
/lib/ld-linux.so.2 (0x80092000)
libgcc_s.so.1 => /lib/i386-linux-gnu/libgcc_s.so.1 (0xb6f3e000)
Run Code Online (Sandbox Code Playgroud)
任何提示?
我尝试将地址清理程序与 clang 一起使用来编译 C++ 应用程序,但出现以下错误:
/Class.so:未定义符号:__asan_memset
我已将 -fsanitize=address 添加到编译器标志中
/opt/llvm-3.8.0/bin/clang++ -M --gcc-toolchain=/opt/gcc-5.2.0 -fsanitize=地址
我已将 -fsanitize=address 和 -lasan 添加到链接器标志中:
-fsanitize=地址 -lasan -shared -fuse-ld=gold-2.25 -o Class.so Class.o
我还需要做什么才能让它发挥作用?
我试图让地址清理黑名单在 C++ 项目中工作,但它没有按预期工作。我在他们的网站上尝试了这个例子,如果我用 编译clang
,它工作得很好。
build % cat suppress.txt
fun:bad_foo
build % cat foo.c
#include <stdlib.h>
void bad_foo() {
int *a = (int*)malloc(40);
a[10] = 1;
}
int main() { bad_foo(); }
build % clang -fsanitize=address -fsanitize-blacklist=suppress.txt foo.c ; ./a.out
Exit code: 0
Run Code Online (Sandbox Code Playgroud)
但一旦我使用clang++
,它就会被忽略。
build % cp foo.c foo.cpp
build % clang++ -fsanitize=address -fsanitize-blacklist=suppress.txt foo.cpp ; ./a.out
=================================================================
==9943==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6040000003f8 at pc 0x00010ff93ee8 bp 0x7ffedfc6c340 sp 0x7ffedfc6c338
WRITE of size 4 at 0x6040000003f8 …
Run Code Online (Sandbox Code Playgroud) 我正在开发一个项目,该项目有一个“util”库,其中包含日志记录、断言处理等内容。它被编译成添加了静态库的静态库-fPIC
。我还有一个插件系统,其中插件是在运行时通过dlopen
. 这些插件和主要可执行文件都使用静态 util 库。
问题:现在我AddressSanitizer: odr-violation
在使用 ASAN 时遇到错误。该问题size=40 'vtable for StdStreamWriter'
报告了两次,其中 StdStreamWriter 是静态库内部使用的接口的实现。
我非常努力地在 MWE 中重现这一点:
dlopen
共享库的可执行文件cmake_minimum_required(VERSION 3.8)
project(proj)
set(sanitizer_flags "-fsanitize=address,undefined -fno-omit-frame-pointer")
string(APPEND CMAKE_CXX_FLAGS " ${sanitizer_flags}")
string(APPEND CMAKE_EXE_LINKER_FLAGS " ${sanitizer_flags}")
string(APPEND CMAKE_MODULE_LINKER_FLAGS " ${sanitizer_flags}")
add_library(foo STATIC foo.cpp)
target_compile_features(foo PUBLIC cxx_std_14)
set_target_properties(foo PROPERTIES CXX_EXTENSIONS OFF POSITION_INDEPENDENT_CODE ON)
add_library(lib SHARED lib.cpp)
target_link_libraries(lib foo)
add_executable(main main.cpp)
target_link_libraries(main foo dl)
Run Code Online (Sandbox Code Playgroud)
然而,无论我多么努力,这个问题都不会出现在 MWE 中。
我将差异追溯到以下方面的不同结果nm -C liblib.so …
我在 bazel 中有一个项目,具有非常简单的 bulid 规则
cc_binary(
name = "search",
srcs = [
"iterator_range.h",
"main.cpp",
"parse.cpp",
"parse.h",
"search_server.cpp",
"search_server.h",
"test_runner.h",
"profile.h",
]
)
Run Code Online (Sandbox Code Playgroud)
我越来越Segmentation fault 11
我尝试使用此配置但出现错误
every rule of type cc_binary implicitly depends upon the target '//tools/lrte:toolchain', but this target coul
d not be found because of: no such package 'tools/lrte': BUILD file not found in any of the following directories
Run Code Online (Sandbox Code Playgroud) 尝试构建启用了 Address Sanitizers 的 iOS 应用程序时出现此错误
==3850==ERROR: AddressSanitizer failed to allocate 0xffffffffff9fc000 (-6307840) bytes at address 2db624000 (errno: 22)
==3850==ReserveShadowMemoryRange failed while trying to map 0xffffffffff9fc000 bytes. Perhaps you're using ulimit -v
Run Code Online (Sandbox Code Playgroud)
如何解决这个问题以及什么是ulimit -v
据我了解,要ASAN_OPTIONS
与 clang 一起使用,ASAN_OPTIONS
必须在编译之前设置环境变量。
如何在 CMake 脚本中执行此操作而不添加包装器脚本?
仅当使用 clang 编译时,我才需要禁用某个特定测试项目的 ODR 违规检查。所以在CMakeLists.txt
文件中我有:
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
# Clange reports ODR Violation errors in mbedtls/library/certs.c. Need to disable this check.
set(ENV{ASAN_OPTIONS} detect_odr_violation=0)
endif()
Run Code Online (Sandbox Code Playgroud)
但运行后cmake
,如果我输入echo $ASAN_OPTIONS
,则未设置。
运行后cmake
,如果我输入:
export ASAN_OPTIONS=detect_odr_violation=0
make
Run Code Online (Sandbox Code Playgroud)
一切都很好。
是否可以cmake
设置一个环境变量,以便它在cmake
运行后仍然存在?抱歉我对环境的了解有限!
我尝试使用 C++ 地址清理程序进行编译(项目属性页/C/C++/启用地址清理程序 = YES),但在编译时收到以下错误:
LINK : fatal error LNK1104: cannot open file 'clang_rt.asan_dynamic_runtime_thunk-x86_64.lib'
Run Code Online (Sandbox Code Playgroud) 在tree-sitter 中,语法从C 和C++ 编译为共享库。然后,这些库由 tree-sitter CLI(一个 Rust 程序)动态加载。树守护者 CLI 通常负责编译语法,尽管它没有-fsanitize
标志。我手动编译了我的语法-fsanitize=address
(最终想要添加undefined
)来帮助调试我作为外部扫描器的一部分编写的一些 C++ 代码。然而,当我尝试使用 tree-sitter CLI rust 程序加载它时,我得到:
打开动态库“~/.cache/tree-sitter/lib/tlaplus.so”时出错
引起:~/.cache/tree-sitter/lib/tlaplus.so:未定义符号:__asan_report_store4
所以 Rust 没有加载 ASAN。我尝试使用 nightly rust 工具链重建树守护者 CLI,并且:
RUSTFLAGS="-Z sanitizer=address" cargo build --target x86_64-unknown-linux-gnu --release
Run Code Online (Sandbox Code Playgroud)
但这仍然给出相同的未定义符号错误。有没有办法从 rust 加载使用 ASAN 编译的 C++ 库,或者我应该将自己的 C++ 前端编写到语法中?