有人能够在iOS上运行Address-Sanitizer(称为asan或-fsanitize = address)吗? [英] Is anyone able to get the Address-Sanitizer (known as asan or -fsanitize=address) working on iOS?

查看:655
本文介绍了有人能够在iOS上运行Address-Sanitizer(称为asan或-fsanitize = address)吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Address-Sanitizer https://code.google.com/p/address -sanitizer/wiki/AddressSanitizer

我已经编译了自己的llvm(相当简单的编译),因为苹果的llvm不支持此功能.

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

对于iOS,仍然存在一些问题:

  • 编译模拟器版本:报告预编译头文件的错误:

在/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)加速度计:(UIAccelerometer *)加速度计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:注意:声明已在此处明确标记为不可用 @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)加速度计:(UIAccelerometer *)加速度计didAccelerate:(UIAcceleration *)acceleration NS_DEPRECATED_IOS(2_0,5_0); ...

  • 针对设备版本进行编译,它报告缺少libarc(但实际上我未启用ARC)

ld:找不到文件:/Users/fluke/Documents/tools/asan/Debug+Asserts/lib/arc/libarclite_iphoneos.a 铛:错误:链接器命令失败,退出代码为1(使用-v查看调用)

  • 因此,我尝试将其用于单独的库-仅新建一个库目标并使用我自己的clang,而主要目标仍使用苹果的llvm.程序可以编译(可能需要链接到内置的llvm中的asan dylib),但是无法正常工作,因为我需要在程序进入之前加载asan.

谁有这样做的经验?

解决方案

我终于在朋友的帮助下为我准备了asan作品.

  • 将所有c/c ++代码移动到xcode项目的新目标(可可库目标).使项目生成并正常运行,因为它是一个单独的应用程序,然后将单独的c/c ++代码存储到一个库中.

  • 构建llvm. ref 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.然后进行构建,如果报告不支持某些api(例如opengl/system视频功能),则可以将其放入应用项目中,而您的clang不支持对其进行编译.

  • 如果通过编译,它将报告__asan_xxx函数的链接问题,在应用程序的链接依赖关系中添加一个名为"libclang_rt.asan_osx_dynamic.dylib"的库,它位于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文件:右键单击您的生产应用程序,选择在finder中显示",然后向上一级获取iphone模拟器目录,打开名为your.app.dSYM的捆绑包,然后即可在./Content中获取DWARF. /Resources/DWARF.

我在这里没有列出的唯一一件事是修改后的asan_symbolize.py,您可以自行修改它,没有魔术,只需更正某些路径即可使用.

Address-Sanitizer 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.

I have tested the clang for mac command line program, it works (but without showing the line the sourcecode).

for iOS, there is still some problems:

  • 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); ...

  • compile for device version, it reports lack of libarc (but actually I don't enable 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)

  • 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.

who have experience with doing this?

解决方案

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.

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.

这篇关于有人能够在iOS上运行Address-Sanitizer(称为asan或-fsanitize = address)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆