5

I need to debug heap overflows in a very large project. After playing with valgrind a bit, it seems like the perfect tool for detecting heap block overruns in C, so I'd like to run our iOS app with it.

I've built and installed valgrind from trunk on OS X Yosemite and have written a test program with intentional heap overflows and verified that valgrind catches and reports them.

Now I want to run a test app in the simulator with it. I've read that it can be run in the iOS simulator by using execl(), but when I do so, I see the following error in the console.

dyld: missing LC_DYLD_INFO load command

After that, the app will crash in dyldbootstrap::rebaseDyld() with EXC_BAD_ACCESS. Is something else necessary here? Valgrind apparently also supports arm64 now as well. Is it possible to package the valgrind executable with my app and run it on a device?

#define VALGRIND "/usr/local/bin/valgrind"

int main(int argc, char * argv[]) {

    if ( argc >= 2 && strcmp(argv[1], "-valgrind") == 0 ) {
        if ( execl(VALGRIND, VALGRIND, argv[0], NULL) < 0 ) {
            NSLog(@"Failed to relaunch under valgrind");
            exit(1);
        }
        NSLog(@"Running under valgrind!!");
    }

    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}
Fabian Köbel
  • 457
  • 1
  • 7
  • 21
Joey Carson
  • 2,973
  • 7
  • 36
  • 60
  • 2
    I got the exact same problem. Valgrind runs on non-simulator executables fine. This execl trick used to work before... – dbv Jan 25 '15 at 01:55
  • 2
    Hi, have you found the solution yet? :) Would really appreciate a workaround for this issue. – Kristupas A. Feb 05 '15 at 13:50

1 Answers1

0

You can use xcrun simctl spawn to start an arbitrary process on the simulator but you cannot run macOS executables in the simulator environment. It shares the kernel with macOS but is otherwise distinct. You'd have to build Valgrind for iOS, then drop that in the device's data directory and try spawning it.

russbishop
  • 16,587
  • 7
  • 61
  • 74