Search code examples
iosjailbreaktheos

Strange behavior on 64bit iOS devices when retrieving vm statistics


I made a tweak that shows free ram inside hooked SpringBoard method. I am using this code:

    mach_port_t host_port;
    mach_msg_type_number_t host_size;
    vm_size_t pagesize;

    host_port = mach_host_self();
    host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
    host_page_size(host_port, &pagesize);

    vm_statistics_data_t vm_stat;

    if (host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size) != KERN_SUCCESS){
        ram = @"N/A";
    }else{  
        natural_t bytes = (vm_stat.free_count * pagesize);
    }

On devices prior 5s and Air it works fine. But 64bit device users reported that they getting amount of free ram larger than max amount of RAM on device. I made command-line utility with the same code and asked to run it as root from terminal, and the command-line utility showed correct values. I checked why it is happening and found out that inside SpringBoard on 64bit devices host_page_size(host_port, &pagesize); returns pagesize = 16384 that is actually 4 times bigger than it shows in command-line utility. Again, it affects only 64 bit devices, on other devices it shows pagesize = 4096 (correct value) no matter where. It can be fixed with hardcoded pagesize = 4096 but I want to know why this is happening, maybe I am missing something important.


Solution

  • after #import <mach/mach.h> you can access to vm_page_size and to vm_kernel_page_size (only since OS X 10.9 + iOS 7)

    vm_kernel_page_size = 4096

    vm_page_size = 16384

    deprecated call getpagesize() return us 16384

    host_page_size(mach_host_self(), &pagesize) return 4096

    next code, return 16384:

    vm_size_t pagesize = 0;
    int mib[] = { CTL_HW, HW_PAGESIZE };
    size_t length = sizeof(pagesize);
    const int sysctlResult = sysctl(mib, 2, &pagesize, &length, NULL, 0);
    

    Tested on arm64 + iOS 9.0.2