Skip to content

Instantly share code, notes, and snippets.

@jake-b
Forked from stuartcarnie/main.m
Created December 11, 2014 22:15
Show Gist options
  • Save jake-b/1d02749a0d82e7913296 to your computer and use it in GitHub Desktop.
Save jake-b/1d02749a0d82e7913296 to your computer and use it in GitHub Desktop.
- (IBAction)runTest:(id)sender {
// 32-bit assembly. Set your architecture to armv7 (not arm64)
uint32_t code[] = {
0xe2800001, // add r0, r0, #1
0xe12fff1e, // bx lr
};
uint32_t *p;
int length = (int32_t)round_page(1024);
vm_address_t bufferAddress;
kern_return_t result = vm_allocate(mach_task_self(), &bufferAddress, length, VM_FLAGS_ANYWHERE);
if ( result != ERR_SUCCESS ) {
NSLog(@"Couldn't allocate");
return;
}
p = (uint32_t *)bufferAddress;
// copy instructions to function
p[0] = code[0];
p[1] = code[1];
/* Mark the buffer read / execute. */
if (mprotect(p, 1024, PROT_READ | PROT_EXEC)) {
NSLog(@"Couldn't mprotect");
return;
}
_inc = (inc_t)p;
int a = 1;
a = _inc(a);
if (a==2) {
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"PROT_EXEC Test"
message:@"Test Passed"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment