4

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?

Dan Loewenherz
  • 10,879
  • 7
  • 50
  • 81
fluke
  • 660
  • 8
  • 16

2 Answers2

1

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.

fluke
  • 660
  • 8
  • 16
0

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

Glider
  • 164
  • 3