xcode 是否有人能够在 iOS 上使用 Address-Sanitizer(称为 asan 或 -fsanitize=address)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17359624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is anyone able to get the Address-Sanitizer (known as asan or -fsanitize=address) working on iOS?
提问by fluke
Address-Sanitizer https://code.google.com/p/address-sanitizer/wiki/AddressSanitizer
地址消毒器https://code.google.com/p/address-sanitizer/wiki/AddressSanitizer
I have compile my own llvm (pretty straight forward compiling) because apple's llvm not support this function.
我已经编译了我自己的 llvm(非常直接的编译),因为苹果的 llvm 不支持这个功能。
I have tested the clang for mac command line program, it works (but without showing the line the sourcecode).
我已经测试了 mac 命令行程序的 clang,它可以工作(但没有显示源代码行)。
for iOS, there is still some problems:
对于iOS,还存在一些问题:
- compile simulator version : report error for pre-compiled header:
- 编译模拟器版本:预编译头报告错误:
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); ...
在 /Users/fluke/Documents/projects/tmp/testAsanNoARC/testAsanNoARC/testAsanNoARC-Prefix.pch 中包含的文件中:12:在 /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: 错误:'UIAccelerometer' 不可用:在 OS X 上不可用 - (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:( UIAcceleration *) 加速度 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:注意:声明已在此处明确标记为不可用@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: 错误:'UIAcceleration' 不可用:在 OS X 上不可用 - ( void)accelerationometer:(UIAccelerometer *)accelerationometer didAccelerate:(UIAcceleration *)acceleration NS_DEPRECATED_IOS(2_0, 5_0); ... app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/Frameworks/UIKit.framework/Headers/UIAccelerometer.h:53:71:错误:“UIAcceleration”不可用:在 OS X 上不可用 - (void)accelerationometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration NS_DEPRECATED_IOS(2_0, 5_0); ... app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/Frameworks/UIKit.framework/Headers/UIAccelerometer.h:53:71:错误:“UIAcceleration”不可用:在 OS X 上不可用 - (void)accelerationometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration NS_DEPRECATED_IOS(2_0, 5_0); ...
- compile for device version, it reports lack of libarc (but actually I don't enable ARC)
- 编译设备版本,它报告缺少 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)
ld:找不到文件:/Users/fluke/Documents/tools/asan/Debug+Asserts/lib/arc/libarclite_iphoneos.a clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- so I try use it for a separate lib - just new a lib target and use my own clang while the main target still use apple's llvm. the program compiles ( may need to link to the asan dylib in the built llvm), but not work because I asan need to be loaded before our program entry.
- 所以我尝试将它用于单独的库 - 只是新建一个库目标并使用我自己的 clang 而主要目标仍然使用苹果的 llvm。程序编译(可能需要链接到构建的 llvm 中的 asan dylib),但不起作用,因为我需要在进入程序之前加载 asan。
who have experience with doing this?
谁有这方面的经验?
采纳答案by fluke
I finally get the asan work for me with my friend's help.
在朋友的帮助下,我终于为我完成了阿三的工作。
move all c/c++ code to a new target (cocoa lib target) of xcode project. make the project build and run normally as it was a single app before separate c/c++ codes to a lib.
build llvm. ref http://blog.wadetregaskis.com/tot-clang-llvm-in-xcode/
add a clang option to xcode. for convenient you can use this template: http://blog.wadetregaskis.com/tot-clang-llvm-in-xcode/. change clang path to the clang just build in the previous step.
change the lib target in the xcode to use the new clang/llvm, add a cflag -fsanitize=address. then build, if some api (such as opengl/system video function) is reported not supported, then you can put it into the app project, your clang doesn't support compiling it.
if you pass the compile, it will report linkage problem of __asan_xxx function, add a lib called "libclang_rt.asan_osx_dynamic.dylib" to the app's linkage dependency, and it's located in your llvm's ./Debug+Asserts/lib/clang/3.4/lib/darwin/ folder.
then you need to specified the out put file or else the report will goes to the stdout with color characters which will confuse you. put this lines into your main.m:
extern void __sanitizer_set_report_path(const char *path); __sanitizer_set_report_path("/tmp/asan.txt");
then you can make your program some memory error such as use after free or heap buffer overflow. the asan will let the program crash in the first error, with /tmp/asan.txt.number report generated.
you're almost there, the report show's the error stack with the file's offset. all you need to do is one more step - resolve the address to code line. you need to find the DWARF file of your project, then use a tool called asan_symbolize.py to generate the new report with source code line. you can goole asan_symbolize.py then get and fix this script to use the DWARF file. you can find the DWARF file by right click your production app, select show in finder, then to up a level to get the iphone simulator directory, open the bundle called your.app.dSYM, then you can get the DWARF in ./Content/Resources/DWARF.
将所有 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/达尔文/文件夹。
那么您需要指定输出文件,否则报告将进入带有颜色字符的标准输出,这会让您感到困惑。将此行放入您的 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 文件,选择在 finder 中显示,然后向上一级获取 iphone 模拟器目录,打开名为 your.app.dSYM 的包,然后您可以在 ./Content 中获取 DWARF /资源/矮人。
The only thing that I haven't list here is the modified asan_symbolize.py, you can modify it by your self, it has no magic, you just correct some path and it will work.
我这里唯一没有列出的是修改后的asan_symbolize.py,你可以自己修改它,它没有魔法,你只要改正一些路径就可以了。
回答by Glider
The errors listed in the original post have little to do with ASan itself. Most certainly you would've got them without the -fsanitize=address flag. Building and running for iOS isn't supported yet, however you can build an app targeting the iOS simulator - it should work just fine. Please don't hesitate to direct further questions to [email protected]
原帖中列出的错误与 ASan 本身关系不大。如果没有 -fsanitize=address 标志,您肯定会得到它们。尚不支持为 iOS 构建和运行,但是您可以构建针对 iOS 模拟器的应用程序 - 它应该可以正常工作。请不要犹豫,将更多问题发送至 [email protected]