Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 1.60 KB  |  hits: 71  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <mach/mach_init.h>
  2. #include <mach/vm_map.h>
  3. #include <mach/vm_region.h>
  4. #include <sys/stat.h>
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7.  
  8. int main() {
  9.     void *mem = malloc(getpagesize() + 15);
  10.     void *ptr = (void *)(((uintptr_t)mem+15) & ~ 0x0F);
  11.  
  12.     vm_size_t vmsize = getpagesize();
  13.     vm_address_t address = (uintptr_t)ptr / getpagesize() * getpagesize();
  14.     mach_msg_type_number_t info_count = VM_REGION_BASIC_INFO_COUNT;
  15.     vm_region_basic_info_data_t info;
  16.     vm_region_flavor_t flavor = VM_REGION_BASIC_INFO;
  17.     memory_object_name_t object;
  18.     task_t the_task = mach_task_self();
  19.     kern_return_t kr;
  20.  
  21.     kr = vm_protect(
  22.         the_task,
  23.         (vm_address_t) address,
  24.         vmsize,
  25.         FALSE,
  26.         VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
  27.  
  28.     if (kr != KERN_SUCCESS)
  29.         printf("vm_protect() failed\n");
  30.     else
  31.         printf("vm_protect() success\n");
  32.  
  33.     kr = vm_region(
  34.         the_task,
  35.         &address,
  36.         &vmsize,
  37.         VM_REGION_BASIC_INFO,
  38.         &info,
  39.         &info_count,
  40.         &object);
  41.  
  42.     if (kr != KERN_SUCCESS)
  43.         printf("vm_region() failed\n");
  44.     else
  45.         printf("vm_region() success\n");
  46.  
  47.  
  48.     printf("%c%c%c %c%c%c\n",
  49.         (info.protection & VM_PROT_READ)        ? 'r' : '-',
  50.         (info.protection & VM_PROT_WRITE)       ? 'w' : '-',
  51.         (info.protection & VM_PROT_EXECUTE)     ? 'x' : '-',
  52.         (info.max_protection & VM_PROT_READ)    ? 'r' : '-',
  53.         (info.max_protection & VM_PROT_WRITE)   ? 'w' : '-',
  54.         (info.max_protection & VM_PROT_EXECUTE) ? 'x' : '-');
  55. }