I'm writing an iPhone application using Monotouch and recently the app has started crashing stating
Mprotect failed at 0x863a000 (length 8192) with errno 12
followed by a rather lengthly stack trace and Springboard informing that "the application exited abormally with signal 6".
I've read this question which states that the app has exhaused all the memory available on the iPhone. We have applied some general Dispose patterns to the app and generally disposed of any heavy objects as soon as we could. This meant the app now runs using less memory. However we are still getting the MProtect failed message.
Also curious to note is that when running the app under instruments, instruments is reporting that there is plenty of free memory available to the device (~40mb).
I was wondering whether anyone would be able to explain MProtect and this failure as I don't think I've quite understood it properly.
mprotect(2)
asks the operating system kernel to change the protection mode for some portion of address space.
mprotect(2)
is often used to make data sections of an address space non-executable, so that buffer overflows, format string vulnerabilities, use after free or freeing unallocated memory errors, or similar attacks cannot return into attacker-supplied data. Also, mprotect(2)
is used to ensure that the program text
space cannot be modified by those same vulnerabilities. (If an attacker can simply overwrite the functions you've supplied, that's no good.)
But mprotect(2)
isn't magic; it cannot prevent against return to libc attacks, or improper use of system(3)
or other code interpreters, etc.
What is the C symbol for the errno
value 12
on the iPhone? Where and why does Monotouch use mprotect(2)
itself? Any chance your software uses mprotect(2)
?
Does your app use Generics?
Beware of having virtual methods on types with Generics, for Monotouch, which has to do lots of hacks while pre-jitting and some more magic with trampolines, it can cause some method hijacking, or memory corruption, on my experience, YMMV.
Make all methods non-virtual on Generic classes for safety.
来源:https://stackoverflow.com/questions/5389947/explanation-of-mprotect-errno-12-enomem