【问题标题】:sanitizer-leak is NOT detected when golang using cgo to call c librarygolang 使用 cgo 调用 c 库时未检测到 sanitizer-leak
【发布时间】:2020-08-02 19:47:17
【问题描述】:

总结

之前我使用了 clang-3.8.1,使用 AddressSanitizer 时 sanitizer 崩溃了。而且leakSanitizer 根本不起作用。

然后我尝试使用clang-llvm-10.0,AddressSanitizer可以检测到地址问题并正常工作。

但是 golang 使用 cgo 调用 C 时无法检测到泄漏问题。 是否可以在 golang 使用 CGO 时使用泄漏清理器来检测 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 (mockbuild@x86-020.build.eng.bos.redhat.com) (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

【问题讨论】:

  • 为什么是c++ 标签?你的问题是什么?
  • 无法检测c或c++中的泄漏问题。

标签: c++ c go memory-leaks address-sanitizer


【解决方案1】:

我已经通过在进程退出时显式调用__lsan_do_leak_check() 解决了这个问题。

__lsan_do_leak_check() 在

中声明

https://github.com/llvm/llvm-project/blob/master/compiler-rt/include/sanitizer/lsan_interface.h

我猜这和c-main启动机制有关,__lsan_do_leak_check()没有注册golang启动。

欢迎任何可以继续挖掘它的人。

【讨论】:

    【解决方案2】:

    @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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-25
      • 1970-01-01
      • 1970-01-01
      • 2013-09-22
      • 2016-12-26
      • 1970-01-01
      • 2022-11-15
      • 1970-01-01
      相关资源
      最近更新 更多