当前位置: > > > > golang使用cgo调用c库时未检测到sanitizer-leak
来源:stackoverflow
2024-05-01 09:57:42
0浏览
收藏
Golang不知道大家是否熟悉?今天我将给大家介绍《golang使用cgo调用c库时未检测到sanitizer-leak》,这篇文章主要会讲到等等知识点,如果你在看完本篇文章后,有更好的建议或者发现哪里有问题,希望大家都能积极评论指出,谢谢!希望我们能一起加油进步!
问题内容
摘要
之前我使用过 clang-3.8.1,但在使用 addresssanitizer 时,sanitizer 崩溃了。而且leaksanitizer根本不起作用。
然后我尝试使用clang-llvm-10.0,addresssanitizer可以检测到地址问题并正常工作。
但是golang使用cgo调用c时无法检测到泄漏问题当golang使用cgo时是否可以使用leak-sanitizer来检测c/c++库中的内存泄漏问题? p>
示例
- cgo-sanitizer.go:按预期检测到地址问题。
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 }
- 输出
[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 of size 4 at 0x604000000014 thread t0 #0 0x54fc2c in test (/tmp/go-build237509829/b001/exe/cgo-sanitizer+0x54fc2c) #1 0x54fcc1 in _cgo_a3187169dba5_cfunc_test (/tmp/go-build237509829/b001/exe/cgo-sanitizer+0x54fcc1) #2 0x5159df (/tmp/go-build237509829/b001/exe/cgo-sanitizer+0x5159df)
- cgo-sanitizer-leak.go:未检测到泄漏问题。 为什么?
package main // #include <stdlib.h> // // int *p; // int test() // { // p = (int *)malloc(10 * sizeof(int)); // p = 0; // return 52; // } import "c" import "fmt" func main() { fmt.println(int(c.test())) // output: 52 }
[root@380c7770b175 cplusplus]# cc="clang" cgo_cflags="-o0 -g -fsanitize=leak" cgo_ldflags="-fsanitize=address" go run cgo-sanitizer-leak.go 52
环境
[root@380c7770b175 cplusplus]# cat /proc/version Linux version 3.10.0-493.el7.x86_64 ([email protected]) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-9) (GCC) ) #1 SMP Tue Aug 16 11:45:26 EDT 2016 [root@380c7770b175 cplusplus]# clang -v clang version 10.0.0 Target: x86_64-unknown-linux-gnu Thread model: posix InstalledDir: /usr/local/llvm-10.0/bin Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/4.8.2 Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/4.8.5 Selected GCC installation: /usr/lib/gcc/x86_64-redhat-linux/4.8.5 Candidate multilib: .;@m64 Candidate multilib: 32;@m32 Selected multilib: .;@m64 [root@380c7770b175 cplusplus]# go version go version go1.13.6 linux/amd64
原始问题
https://github.com/google/sanitizers/issues/1223
解决方案
我通过在进程退出时显式调用 __lsan_do_leak_check()
解决了这个问题。
__lsan_do_leak_check() 在
中声明
我猜这与c-main启动机制有关,并且__lsan_do_leak_check()没有为golang启动注册。
欢迎任何能够持续挖掘它的人。
@shawn 是正确的,如果你的主程序是用 go 编写的,你需要手动调用 __lsan_do_leak_check 。如果您的 main 是用 c/c++ 编写的,并且 go 作为库调用,那么就不需要它。
这是一个简单的例子
package main // #include <stdio.h> // #include <stdlib.h> // // void __lsan_do_leak_check(void); // // void leak_a_bit(void) // { // char* p = malloc(2000); // printf("%p\n", p); // } import "c" func main() { c.leak_a_bit() c.__lsan_do_leak_check() }
这样编译并运行
$ CC=clang CGO_ENABLED=1 CGO_LDFLAGS='-fsanitize=address' CGO_CFLAGS='-fsanitize=address' go build -o main $ ./main
今天关于《golang使用cgo调用c库时未检测到sanitizer-leak》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注公众号!