Is anyone able to get the Address-Sanitizer (known as asan or -fsanitize=address) working on iOS?

陌路散爱 提交于 2019-12-04 10:17:48

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.

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 address-sanitizer@googlegroups.com

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!