所以我正在使用 AddressSanitizer。但是在描述问题时它使用了一些密集的术语。
Shadow bytes around the buggy address:
0x0c067fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c067fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c067fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c067fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c067fff7ff0: 00 00 00 …
Run Code Online (Sandbox Code Playgroud) 默认情况下,AddressSanitizer 会将所有错误抛出到 shell 本身,因此我尝试使用以下命令运行我的 ASAN 构建;
>MCTester_ASAN>asan.log
==15619==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x61400000f9d0 at pc 0x46cff2 bp 0x7fffc062cb90 sp 0x7fffc062cb88
#0 0x46cff1 in heapOutOfBoundWrite() /home/MemTest/main.cpp:49
#1 0x46d68f in main /home/MemTest/main.cpp:116
#2 0x7fbd3365bc35 in __libc_start_main (/lib64/libc.so.6+0x1ec35)
#3 0x40a0f8 (/x01/exd10/bin/MCTester_ASAN+0x40a0f8)
ASAN:SIGSEGV
==15619==ERROR: AddressSanitizer: SEGV on unknown address 0x00000044ff97 (pc 0x00000046cff2 sp 0x7fffc062cba0 bp 0x7fffc062cbb0 T0)
#0 0x46cff1 in heapOutOfBoundWrite() /home/MemTest/main.cpp:49
#1 0x46d68f in main /home/MemTest/main.cpp:116
#2 0x7fbd3365bc35 in __libc_start_main (/lib64/libc.so.6+0x1ec35)
#3 0x40a0f8 (/x01/exd10/bin/MCTester_ASAN+0x40a0f8)
AddressSanitizer can not provide additional info.
Segmentation fault
Run Code Online (Sandbox Code Playgroud)
但我仍然将输出发送到 …
目前在Windows上的Ubuntu Bash上运行llvm,clang,clang-format和clang-modernize.我想使用谷歌发布的一套清理工具,包括地址,内存和线程清理.fsanitize选项似乎都不起作用.
以下是ASAN的代码示例:
#include <stdlib.h>
int main() {
char *x = (char *)malloc(10 * sizeof(char *));
free(x);
return x[5];// purposely accessing deallocated memory
}
Run Code Online (Sandbox Code Playgroud)
这是windows上bash的clang调用:
$clang++-3.5 -fsanitize=address -o1 -fno-omit-frame-pointer -g main.cpp -o main
$./main
Run Code Online (Sandbox Code Playgroud)
结果
==70==Sanitizer CHECK failed: build/buildd/llvm-toolchain-snapshot-3.5/projects/compiler-rt/lib/sanitizer_common/sanitizer_procmaps_linux.cc:211 ((IsOneOf(*current_, 's', 'p'))) != (0)(0,0)
Run Code Online (Sandbox Code Playgroud)
我喜欢关于如何使其工作的建议,或者我是否缺少工具链的一部分或其他东西.
如果失败,我想我将双启动Ubuntu或Debian,因为Windows的clang缺少像std:out支持这样的简单功能,尽管理想情况下我希望能够为Windows目标和Linux目标编译.我想避免双启动,因为Ubuntu无法安装Windows存储空间,但它们似乎很好地服务于Windows上的Ubuntu bash.
当使用带有 gcc 的消毒剂时,可以提供一个异常/抑制列表来处理误报等。
抑制文件格式记录不全。
每个抑制的形式都是
name_of_check:path_or_name
Run Code Online (Sandbox Code Playgroud)
的有效值是name_of_check
什么?
gcc sanitizer thread-sanitizer address-sanitizer leak-sanitizer
我想用 LeakSanitizer 来检测泄漏的内存,但是我使用的程序的样式之前并没有释放内存exit
。这在我的经验中相当普遍。
我想检测这个泄漏:
int main(int argc, char const *argv[])
{
char *p = malloc(5);
p = 0;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
并忽略此泄漏:
int main(int argc, char const *argv[])
{
char *p = malloc(5);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我正在解决 leetcode 问题并收到此错误。我不知道这意味着什么,因为我对 C++ 还比较陌生。当我删除 else if 中的 else 时,它似乎消失了。
AddressSanitizer:DEADLYSIGNAL
=================================================================
==32==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x000000383e8c bp 0x7ffc55bebe50 sp 0x7ffc55bebd20 T0)
==32==The signal is caused by a READ memory access.
==32==Hint: address points to the zero page.
#3 0x7f2222e3982f (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
AddressSanitizer can not provide additional info.
==32==ABORTING
Run Code Online (Sandbox Code Playgroud)
我的代码:
class Solution {
public:
bool isValid(string s) {
stack<char> stk;
int flag=1;
for(int i=0; i< s.length();i++){
if(s[i]=='('||s[i]=='{'||s[i]=='['){
stk.push(s[i]);
flag=0;
}
else { //if (s[i]==')'||s[i]=='}'||s[i]==']'){
if(s[i]==')'&&stk.top()=='('){
stk.pop();
flag=1; …
Run Code Online (Sandbox Code Playgroud) 之前我使用过 clang-3.8.1,并且在使用 AddressSanitizer 时 sanitizer 崩溃了。而且leakSanitizer根本不起作用。
然后我尝试使用clang-llvm-10.0,AddressSanitizer可以检测到地址问题并正常工作。
但是golang使用cgo调用C时无法检测到泄漏问题。当golang使用CGO时,是否可以使用leak-sanitizer来检测C/C++库中的内存泄漏问题?
package main
// #include <stdlib.h>
//
// int test()
// {
// int *p = (int *)malloc(10 * sizeof(int));
// free(p);
// p[1] = 42;
// return p[1];
// }
import "C"
import "fmt"
func main() {
fmt.Println(int(C.test()))
// Output: 42
}
Run Code Online (Sandbox Code Playgroud)
[root@380c7770b175 cplusplus]# CC="clang" CGO_CFLAGS="-O0 -g -fsanitize=address" CGO_LDFLAGS="-fsanitize=address" go run cgo-sanitizer.go
=================================================================
==25680==ERROR: AddressSanitizer: heap-use-after-free on address 0x604000000014 at pc 0x00000054fc2d bp 0x7ffd96a943b0 sp 0x7ffd96a943a8
WRITE …
Run Code Online (Sandbox Code Playgroud) Microsoft 最近将 Address Sanitizer (ASan) 引入了 Microsoft Visual Studio 2019,我一直在尝试它。这个问题特定于 C 和 C++。我的问题是:是否有任何理由为发布版本启用 ASan,而不是仅为调试版本启用它?打开 ASan 对我的程序的性能和内存使用量来说是毁灭性的。(CPU 性能比减半还要差,内存使用量增加了两倍。)因此,我希望如果我启用 ASan 只是为了检查潜在问题,并且它在调试版本中没有检测到任何问题,那么我可以放心地假设不会有发布版本中是否存在任何可能被 ASan 发现的问题?当然,我们不打算在发布/生产版本中启用 ASan 吗?
感谢您的任何见解。
我正在为 Linux 开发一个静态独立的 nolibc/nostdlib 程序,并希望使用 C 编译器的内存、地址和未定义行为清理程序来改进我的代码。
当我尝试时,我无法让它工作:
clang -static -ffreestanding -nostdlib -fno-omit-frame-pointer -fsanitize=undefined -g -o program program.c
Run Code Online (Sandbox Code Playgroud)
这会导致编译器发出调用诸如__ubsan_handle_type_mismatch_v1@plt
. 它编译和链接成功,但程序在运行时在这些引用附近出现段错误。更具体地说,在我的内存分配器中:
Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) up
(gdb) disas
Dump of assembler code for function lone_reallocate:
...
0x00000000002116d0 <+160>: bl 0x206470 <__ubsan_handle_pointer_overflow@plt>
=> 0x00000000002116d4 <+164>: b 0x2116d8 <lone_reallocate+168>
0x00000000002116d8 <+168>: ldur x8, [x29, #-72]
...
Run Code Online (Sandbox Code Playgroud)
我认为这些函数由于缺乏 libc 支持而丢失。当我尝试使用该-static-libsan
选项时,出现许多未定义的符号错误:
error: undefined symbol: __aarch64_cas8_acq_rel
error: undefined symbol: pthread_self
error: undefined symbol: dl_iterate_phdr
error: …
Run Code Online (Sandbox Code Playgroud) 我正在调试 OpenCL 应用程序的崩溃。我试图使用 asan 来确定问题的根源。但是后来我发现打开asan并重新编译后,我的应用程序找不到任何OpenCL设备。简单地添加-fsanitize=address
到编译器选项使我的程序无法使用 OpenCL。
通过进一步测试,我发现内存清理器不适用于 OpenCL。
为什么会这样?如何在 OpenCL 中使用 asan?
编辑:一个最小的例子
#include <CL/cl.hpp>
#include <vector>
#include <iostream>
int main() {
std::vector<cl::Platform> platforms;
cl::Platform::get(&platforms);
if(platforms.size() == 0) std::cerr << "in asas\n";
else std::cout << "compiled normally\n";
}
Run Code Online (Sandbox Code Playgroud)
编辑2:
cl::Platform::get
CL_SUCCESS 正常返回。获取平台的过程中没有错误。
还有一些关于我的设置的信息。
GPU:GTX 780Ti
驱动程序:418.56
OpenCL SDK:Nvidia OpenCL / POCL 1.3,带 CPU 和 CUDA 后端
编译器:GCC 8.2.1
操作系统:Arch Linux(内核 5.0.7 x64)