Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the quickest way to force an iOS app to crash?

People also ask

What causes an app to crash iPhone?

Causes of iPhone App CrashLow device memory. Network issues. Device incompatibility problems. Software issues such as outdated software.

Can an app crash my iPhone?

Most often, apps crash because they aren't updated and have unaddressed bugs. To fix this, go to the Updates section of the App Store and update the app if an update is available.


@throw NSInternalInconsistencyException;

So many ways to kill an app! Here are two one liners:

[self performSelector:@selector(die_die)];

also

@[][666];

Just write assert(NO). This checks the condition given as parameter and crashes the app if it is false.

Edit:

exit(0) will also do the trick


int* p = 0;
*p = 0;

Gives a EXC_BAD_ACCESS (code=2, address=0x0)

Edit:

After Greg Parkers comment that a compiler is allowed to optimize away the above statements, it made me think more thoroughly about the above statements, and why Greg Parker is right:

In fact, dereferencing the NULL pointer is "undefined behavior" in C and C++ (see also C99 §6.5.3.2/4).

This means, the effect of the above statements depend on the compiler. This "undefined behavior" also means, that the compiler is allowed to apply a couple of optimizations, which may have the effect that the above statements will be "optimized aways" - as Greg Parker asserts.

Well, now that made me curious what clang would actually do:

This is the small test program:

int main(int argc, const char * argv[])
{
    int* p = 0;
    *p = 0;
    return 0;
}

with optimization set to "-Ofast", we get this disassembly:

0x100000f90:  pushq  %rbp
0x100000f91:  movq   %rsp, %rbp
0x100000f94:  ud2    

where ud2 is an opcode meaning "undefined opcode" and causes a CPU exception:

`EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)`

(Maybe @GregParker can comment why clang chooses this approach?)

While this is interesting, it refers to "dereferencing the NULL pointer" only. If we have this instead:

int* p = (int*)1;
*p = 0;

the program crashes as expected - but requires the "prerequisite" that the hardware refuses writes to this (invalid) address.


I think the good old array index out of range is a guarantee of "successful crash", so here my favourite list:

Swift 4:

  1. [][0]
  2. fatalError()

Objective-C:

  1. @[][0];
  2. int *x = nil; *x = 0;

Although @throw NSInternalInconsistencyException; fixes your problem, is an exception (not a crash), hence might be caught.