Search code examples
c++windowsassemblywinapivirtual-memory

Assumptions about dwPageSize on different systems


Can we make any assumptions about SYSTEM_INFO dwPageSize on different systems, when targeting the same architecture (ie. x86_64)?

I generate some custom native code, that is loaded alongside a C++-application via VirtualAlloc. This code has 3 different sections (code, cdata, dynamic static variables), that all need different protections (execute; read; read-write) via VirtualProtect, and thus need to be in different pages. Code references cdata and static variables via RIP-relative addressing.

I'm wondering if I can assume that, if say, when building on x64, dwPageSize is 4096, it will also have that same value on other x64-systems (or at least smaller, but never larger)? If so, I can just take the RIP-relative addresses as-is, because I can ensure that all data is placed at the same page-sized relative offsets. If the pages on the target-system could potentially be larger, I would need to fixup those offsets on loading the code.


Solution

  • Page size is usually dictated by the processor architecture itself, although sometimes a processor architecture may support multiple page sizes. Nonetheless, Microsoft decided for one particular page size per architecture, so it is indeed safe to assume that the page size will be the same for a given architecture (and bitness of Windows) regardless of which machine the code is running on.

    In all of the currently supported architectures the page size is 4KB, by the way. (Some discontinued architectures used 8KB.) See here for a full list.

    Especially in regards to x86-64 you can be sure it won't change because the processor doesn't offer any other choice than 4KB (for small pages). Microsoft may theoretically change it in some architecture like ARM but that would be quite unexpected to me as it would cause a ton of compatibility issues with no discernible advantage.