Strange behavior on 64bit iOS devices when retrieving vm statistics

时光总嘲笑我的痴心妄想 提交于 2019-12-12 15:12:59

问题


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.


回答1:


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




回答2:


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...



来源:https://stackoverflow.com/questions/21552747/strange-behavior-on-64bit-ios-devices-when-retrieving-vm-statistics

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!