Guest User

Untitled

a guest
Nov 19th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. #import <Foundation/Foundation.h>
  2.  
  3. struct Block_layout {
  4. void *isa;
  5. int flags;
  6. int reserved;
  7. void (*invoke)(void *, ...);
  8. struct Block_descriptor *descriptor;
  9. };
  10.  
  11. int main(int argc, char *argv[]) {
  12. @autoreleasepool {
  13. // Block that doesn't take or return anything
  14. void(^block)() = ^{
  15. NSLog(@"Howdy %i", argc);
  16. };
  17.  
  18. // Cast to a struct with the same memory layout
  19. struct Block_layout *blockStr = (struct Block_layout *)(__bridge void *)block;
  20.  
  21. // Now do same as `block()':
  22. blockStr->invoke(blockStr);
  23.  
  24.  
  25.  
  26.  
  27. // Block that takes an int and returns an int
  28. int(^returnBlock)(int) = ^int(int a){
  29. return a;
  30. };
  31.  
  32. // Cast to a struct with the same memory layout
  33. struct Block_layout *blockStr2 = (struct Block_layout *)(__bridge void *)returnBlock;
  34.  
  35. // Now do same as `returnBlock(argc)':
  36. int ret = ((int(*)(void*, int a, ...))(blockStr2->invoke))(blockStr2, argc);
  37. NSLog(@"ret = %i", ret);
  38. }
  39. }
  40.  
  41. Howdy 1
  42. ret = 1
  43.  
  44. #import <mach/mach_init.h>
  45. #import <mach/vm_map.h>
  46.  
  47. void *remap_address(void* address, int page_count)
  48. {
  49. vm_address_t source_address = (vm_address_t) address;
  50. vm_address_t source_page = source_address & ~PAGE_MASK;
  51.  
  52. vm_address_t destination_page = 0;
  53. vm_prot_t cur_prot;
  54. vm_prot_t max_prot;
  55. kern_return_t status = vm_remap(mach_task_self(),
  56. &destination_page,
  57. PAGE_SIZE*(page_count ? page_count : 4),
  58. 0,
  59. VM_FLAGS_ANYWHERE,
  60. mach_task_self(),
  61. source_page,
  62. FALSE,
  63. &cur_prot,
  64. &max_prot,
  65. VM_INHERIT_NONE);
  66.  
  67. if (status != KERN_SUCCESS)
  68. {
  69. return NULL;
  70. }
  71.  
  72. vm_address_t destination_address = destination_page | (source_address & PAGE_MASK);
  73.  
  74. return (void*) destination_address;
  75. }
Add Comment
Please, Sign In to add comment