我如何 运行 在 Ubuntu 18.04 上使用 -msan 选项进行测试?

How do I run go test with the -msan option on Ubuntu 18.04?

当我尝试 运行 带有内存完整性检查的 go 单元测试系统时,我收到一条错误消息,我确定我通过将编译器设置为 CLANG 来解决这个问题。

Go Command Documenation 对此事的简要介绍。

-msan
    enable interoperation with memory sanitizer.
    Supported only on linux/amd64, linux/arm64
    and only with Clang/LLVM as the host C compiler.
    On linux/arm64, pie build mode will be used.

过去我是通过调用来使它工作的:

CC=clang go test -msan ./..

然而,当我现在这样做时,我会收到如下错误:

g++: error: unrecognized argument to -fsanitize= option: ‘memory’

我需要做什么才能 运行 我的 golang 在 Ubuntu 18:04 18:04 下使用内存清理器进行测试?


我目前正在使用以下版本的工具:

$ go version
go version go1.14 linux/amd64
$ clang --version
clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

它需要一个支持 -fsanitize=memory 的 LLVM 版本,在您的 Ubuntu 18.04 版本上该程序运行。请尝试更新,然后重试。

另请注意,在 Linux 上,您至少需要 LLVM 3.8 才能获得 - fsanitize 标志。

Go 工具根据 clang 的要求自动将 -fsanitize=memory 选项添加到 CGO_CPPFLAGS 标志中 linking,这就是导致您出错的地方。

此外,确保同时添加 CCCXX(对于 clang++)标志因此,当您将程序与 C/C++ 互操作时,使用 Clang 启用编译,即

CC=clang CXX=clang++ go build -msan

也请参考这个link:

https://go.googlesource.com/go/+/go1.7/misc/cgo/testsanitizers/test.bash(bash 脚本)

(或)

https://github.com/golang/go/tree/master/misc/cgo/testsanitizers(*.go 文件)

它将帮助您测试消毒剂是否适用于您的设置。