xcodeaddress-sanitizer

How to add a build config to xcode that switches on all ASAN diagnostics and environment options


In an XCode project, it seems that in order to build and run with ASAN address sanitizer and memory checks, it is necessary to do 3 things:

  1. in Info, switch Build Configuration to Debug
  2. in Arguments, Environment Variables, add and switch on DYLD_INSERT_LIBRARIES
  3. in Diagnostics, switch on Address Sanitizer, Malloc Scribble, and 8 other flags

When switching back to normal release or debug builds, all these options have to be switched off again.

That's a lot of clicks. Is there a way to have all this switched on and off just with the choice of the Build Configuration, or some other trick?


Solution

  • Those configurations are not global, but per scheme. You may have reached the scheme edition dialog from a shortcut, but it's actually reached from sub-menus of the menu bar, in:

    Product > Scheme > Edit Scheme...
    

    As you can see, there are many other options in Product > Scheme and we can have many schemes! Go to

    Product > Scheme > Manage Schemes...
    

    Select the scheme you want to have two variants of, click on the burger menu (the ellipsis in a circle at the bottom left) and hit Duplicate. You now have two schemes with the same target and options. You can select on and click Edit... on the bottom left of the scheme management dialog, or you can select the scheme you want using

    Product > Scheme > Choose Scheme...
    

    And then Edit Scheme... as above. Have one scheme configured with debug and the other options, and one in release without those other options. Now, you just have to switch between the debug and the release scheme, either going through the menus or setting up a shortcut for Choose Scheme... or using ⌘⌃[ / ⌘⌃].