ios - 是否有人能够在 iOS 上使用 Address-Sanitizer(称为 asan 或 -fsanitize=address)?

标签 ios xcode memory-management clang llvm

地址 sanitizer https://code.google.com/p/address-sanitizer/wiki/AddressSanitizer

我已经编译了我自己的 llvm(非常直接的编译),因为苹果的 llvm 不支持这个功能。

我已经测试了 clang for mac 命令行程序,它可以工作(但没有显示源代码行)。

对于iOS,还存在一些问题:

  • 编译模拟器版本:预编译头报错:

In file included from /Users/fluke/Documents/projects/tmp/testAsanNoARC/testAsanNoARC/testAsanNoARC-Prefix.pch:12: In file included from /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/Frameworks/UIKit.framework/Headers/UIKit.h:9: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/Frameworks/UIKit.framework/Headers/UIAccelerometer.h:53:24: error: 'UIAccelerometer' is unavailable: not available on OS X - (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration NS_DEPRECATED_IOS(2_0, 5_0); ^ /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/Frameworks/UIKit.framework/Headers/UIAccelerometer.h:33:12: note: declaration has been explicitly marked unavailable here @interface UIAccelerometer : NSObject { ^ /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/Frameworks/UIKit.framework/Headers/UIAccelerometer.h:53:71: error: 'UIAcceleration' is unavailable: not available on OS X - (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration NS_DEPRECATED_IOS(2_0, 5_0); ...

  • 编译设备版本,它报告缺少libarc(但实际上我没有启用ARC)

ld: file not found: /Users/fluke/Documents/tools/asan/Debug+Asserts/lib/arc/libarclite_iphoneos.a clang: error: linker command failed with exit code 1 (use -v to see invocation)

  • 所以我尝试将它用于一个单独的库 - 只是新建一个库目标并使用我自己的 clang,而主要目标仍然使用苹果的 llvm。程序编译(可能需要链接到构建的 llvm 中的 asan dylib),但无法运行,因为需要在我们的程序进入之前加载 Iasan。

谁有这方面的经验?

最佳答案

在 friend 的帮助下,我终于得到了牙山。

  • 将所有 c/c++ 代码移动到 xcode 项目的新目标(cocoa lib 目标)。使项目正常构建和运行,因为它是单独的应用程序,然后将 c/c++ 代码分离到库中。

  • 构建 llvm。引用 http://blog.wadetregaskis.com/tot-clang-llvm-in-xcode/

  • 向 xcode 添加 clang 选项。为了方便起见,您可以使用此模板:http://blog.wadetregaskis.com/tot-clang-llvm-in-xcode/ .将 clang 路径更改为上一步中刚刚构建的 clang。

  • 更改 xcode 中的 lib 目标以使用新的 clang/llvm,添加 cflag -fsanitize=address。然后build,如果有的api(比如opengl/system video function)报不支持,可以放到app工程中,你的clang不支持编译。

  • 如果编译通过,会报__asan_xxx函数的联动问题,在app的联动依赖中添加名为“libclang_rt.asan_osx_dynamic.dylib”的lib,它位于你的llvm的 ./Debug+Asserts/lib/clang/3.4/lib/darwin/文件夹。

  • 然后您需要指定输出文件,否则报告将转到带有彩色字符的标准输出,这会让您感到困惑。将此行放入您的 main.m 中:

    extern void __sanitizer_set_report_path(const char *path); __sanitizer_set_report_path("/tmp/asan.txt");

  • 然后您可以让您的程序出现一些内存错误,例如释放后使用或堆缓冲区溢出。 asan 会让程序在第一个错误中崩溃,并生成/tmp/asan.txt.number 报告。

  • 您就快完成了,报告显示了带有文件偏移量的错误堆栈。您需要做的就是再执行一步 - 将地址解析为代码行。您需要找到项目的 DWARF 文件,然后使用名为 asan_symbolize.py 的工具生成带有源代码行的新报告。您可以访问 asan_symbolize.py,然后获取并修复此脚本以使用 DWARF 文件。您可以通过右键单击您的生产应用程序找到 DWARF 文件,选择 show in finder,然后向上一级获取 iphone 模拟器目录,打开名为 your.app.dSYM 的包,然后您可以在 ./Content 中获取 DWARF/Resources/DWARF。

唯一没有在这里列出的是修改后的asan_symbolize.py,你可以自己修改它,它没有魔法,你只需要修改一些路径就可以了。

关于ios - 是否有人能够在 iOS 上使用 Address-Sanitizer(称为 asan 或 -fsanitize=address)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17359624/

相关文章:

Swift 3 - 检测 UIWebView 中的非事件

iphone - 在 IB 中,如何让 subview 在调整父级大小时保持与其父级相同的大小?

c++ - 创建本地 opencv mat 对象时内存泄漏

objective-c - AFHTTPSessionManager 未解除分配

iphone - 在objective-C中同步sqlite数据库

ios - UICollectionViewCell 上的 CGAffineTransform 没有给出预期的结果

ios - Quickblox 中的音频通话错误

ios - 苹果评论中应用程序在 Facebook 上崩溃

ios - 在 View 中创建、拖动和选择一些图标

c - 使用 libjpeg 读取图像像素数据的 3 维数组的内存分配