Strange behavior on 64bit iOS devices when retriev

2019-07-06 21:34发布

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.

2条回答
劳资没心,怎么记你
2楼-- · 2019-07-06 21:55

Should use int pagesize = getpagesize();

Therefore, always use the getpagesize() function to get the size of the page.

(from here)

Also problem may be in using 32-bit function host_statistics instead of host_statistics64 and so on.

Sadly, host_statistics64 does not corrects that problem (whenever call host_statistics or host_statistics64) :( Page size is 4096 or 16384 (depends on device), but pages counts are same always. May be it is worth to workaround by hardcode 4096...

查看更多
beautiful°
3楼-- · 2019-07-06 22:09

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

查看更多
登录 后发表回答