使用Clang的“未定义”消毒剂

时间:2016-10-27 12:13:15

标签: c++ macos clang undefined-behavior clang++

这段代码编译好了,启用了clang的“未定义行为”清理程序:

➜  ~ cat test.cc 
#include <iostream>

int main(int argc, char **argv) {
  // Won't compile if next line will be uncommented!
  // std::cout << "Hello, World!" << std::endl;
  int k = 0x7fffffff;
  k += argc;
  return 0;
}
➜  ~ clang++ -fsanitize=address -fsanitize=undefined test.cc

然而,当我取消注释提到的行时,它会爆炸:

  ~ clang++ -fsanitize=address -fsanitize=undefined test.cc   
Undefined symbols for architecture x86_64:
  "___ubsan_handle_dynamic_type_cache_miss", referenced from:
      _main in test-5f28d3.o
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::endl<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) in test-5f28d3.o
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned long) in test-5f28d3.o
      std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> > std::__1::__pad_and_output<char, std::__1::char_traits<char> >(std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> >, char const*, char const*, char const*, std::__1::ios_base&, char) in test-5f28d3.o
  "___ubsan_vptr_type_cache", referenced from:
      _main in test-5f28d3.o
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::endl<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) in test-5f28d3.o
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned long) in test-5f28d3.o
      std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> > std::__1::__pad_and_output<char, std::__1::char_traits<char> >(std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> >, char const*, char const*, char const*, std::__1::ios_base&, char) in test-5f28d3.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Clang版本:

➜  ~ clang --version
Apple LLVM version 8.0.0 (clang-800.0.38)
Target: x86_64-apple-darwin15.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

详细编译器输出为here

我做错了什么?

1 个答案:

答案 0 :(得分:0)

使用来自llvm网站的clang / llvm二进制文件而不是Apple的clang解决了问题。