Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any invalid write (memory corruption) detection tools for iOS Xcode development?

Back when I was programming in C++, my go-to choice when I discovered that memory corruption was occurring was to fire up Valgrind and see if it caught an invalid write somewhere along the line. Now that the code I'm writing is supposed to run on 32-bit iPhones, and my development environment is Xcode on OSX Mavericks, Valgrind doesn't seem to be an option anymore; even putting aside architecture mismatches, Googling reveals many reports of incompatibility between Valgrind and Mavericks, especially with 32-bit binaries. However, I can't find anything in Xcode Instruments that has similar functionality - memory leak detectors are all well and good, but there's nothing to find those dastardly memory corruptions.

Does anyone know of a good tool for detecting invalid writes when developing/debugging an iOS app?

like image 223
Christopher Humphrey Avatar asked Jul 15 '14 23:07

Christopher Humphrey


People also ask

What is memory corruption error?

Definition: Memory corruption can be described as the vulnerability that may occur in a computer system when its memory is altered without an explicit assignment. The contents of a memory location are modified due to programming errors which enable attackers to execute an arbitrary code.

What is memory corruption in C?

Memory corruption is a process of unintentionally alterting a random area of memory by a program or process. A program written with C programming language can corrupt memory in different areas and in different ways. Memory Memory corruption creates many problems and program execution faces many abnormal behavior.


1 Answers

Use Guard Malloc, which is supplied with Xcode.

Click on your application name, to the right of the top button to bring up the list of schemes in your project. Select 'Edit Scheme...' at the bottom. In the dialogue that comes up select the 'Diagnostics' tab. In there, tick to enable Guard Malloc.

Apple's documentation on exactly what it and what some of the other options there do is here but the text logged to the console when you run pretty much explains it all:

Allocations will be placed on 16 byte boundaries. 
 - Some buffer overruns may not be noticed. 
 - Applications using vector instructions (e.g., SSE) should work.

Each new allocation will start on a new page. Pages will be boxed out with appropriate MMU flags set so that out-of-bounds accesses raise EXC_BAD_ACCESS. So it'll catch 90% of memory access errors at the moment the access occurs, at the cost of your program running significantly more slowly due to cache inefficiency.

Cynically crafting examples, this code:

char *array = (char *)malloc(17);
array[31] = 9;

... will probably not raise an exception with or without Guard Malloc. This code:

char *array = (char *)malloc(17);
array[32] = 9;

... will probably not raise an exception ordinarily. With Guard Malloc enabled it will raise an exception upon execution of the second line.

So it's not as thorough as Valgrind but it takes barely a second to switch on and will likely catch significant mistakes.

like image 136
Tommy Avatar answered Oct 16 '22 20:10

Tommy