Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import <Foundation/Foundation.h>
- struct Block_layout {
- void *isa;
- int flags;
- int reserved;
- void (*invoke)(void *, ...);
- struct Block_descriptor *descriptor;
- };
- int main(int argc, char *argv[]) {
- @autoreleasepool {
- // Block that doesn't take or return anything
- void(^block)() = ^{
- NSLog(@"Howdy %i", argc);
- };
- // Cast to a struct with the same memory layout
- struct Block_layout *blockStr = (struct Block_layout *)(__bridge void *)block;
- // Now do same as `block()':
- blockStr->invoke(blockStr);
- // Block that takes an int and returns an int
- int(^returnBlock)(int) = ^int(int a){
- return a;
- };
- // Cast to a struct with the same memory layout
- struct Block_layout *blockStr2 = (struct Block_layout *)(__bridge void *)returnBlock;
- // Now do same as `returnBlock(argc)':
- int ret = ((int(*)(void*, int a, ...))(blockStr2->invoke))(blockStr2, argc);
- NSLog(@"ret = %i", ret);
- }
- }
- Howdy 1
- ret = 1
- #import <mach/mach_init.h>
- #import <mach/vm_map.h>
- void *remap_address(void* address, int page_count)
- {
- vm_address_t source_address = (vm_address_t) address;
- vm_address_t source_page = source_address & ~PAGE_MASK;
- vm_address_t destination_page = 0;
- vm_prot_t cur_prot;
- vm_prot_t max_prot;
- kern_return_t status = vm_remap(mach_task_self(),
- &destination_page,
- PAGE_SIZE*(page_count ? page_count : 4),
- 0,
- VM_FLAGS_ANYWHERE,
- mach_task_self(),
- source_page,
- FALSE,
- &cur_prot,
- &max_prot,
- VM_INHERIT_NONE);
- if (status != KERN_SUCCESS)
- {
- return NULL;
- }
- vm_address_t destination_address = destination_page | (source_address & PAGE_MASK);
- return (void*) destination_address;
- }
Add Comment
Please, Sign In to add comment