int NaClFindAddressSpace(uintptr_t *addr, size_t memory_size) { return NaClFindAddressSpaceRandomized(addr, memory_size, 0); }
/* * NaClAllocatePow2AlignedMemory is for allocating a large amount of * memory of mem_sz bytes that must be address aligned, so that * log_alignment low-order address bits must be zero. * * Returns the aligned region on success, or NULL on failure. */ static void *NaClAllocatePow2AlignedMemory(size_t mem_sz, size_t log_alignment, enum NaClAslrMode aslr_mode) { uintptr_t pow2align; size_t request_size; uintptr_t unrounded_addr; uintptr_t rounded_addr; size_t extra; int found_memory; pow2align = ((uintptr_t) 1) << log_alignment; request_size = mem_sz + pow2align; NaClLog(4, "%"MSGWIDTH"s %016"NACL_PRIxS"\n", " Ask:", request_size); if (NACL_ENABLE_ASLR == aslr_mode) { found_memory = NaClFindAddressSpaceRandomized( &unrounded_addr, request_size, MAX_ADDRESS_RANDOMIZATION_ATTEMPTS); } else { found_memory = NaClFindAddressSpace(&unrounded_addr, request_size); } if (!found_memory) { NaClLog(LOG_FATAL, "NaClAllocatePow2AlignedMemory: Failed to reserve %"NACL_PRIxS " bytes of address space\n", request_size); } NaClLog(4, "%"MSGWIDTH"s %016"NACL_PRIxPTR"\n", "orig memory at", unrounded_addr); rounded_addr = (unrounded_addr + (pow2align - 1)) & ~(pow2align - 1); extra = rounded_addr - unrounded_addr; if (0 != extra) { NaClLog(4, "%"MSGWIDTH"s %016"NACL_PRIxPTR", %016"NACL_PRIxS"\n", "Freeing front:", unrounded_addr, extra); if (-1 == munmap((void *) unrounded_addr, extra)) { perror("munmap (front)"); NaClLog(LOG_FATAL, "NaClAllocatePow2AlignedMemory: munmap front failed\n"); } } extra = pow2align - extra; if (0 != extra) { NaClLog(4, "%"MSGWIDTH"s %016"NACL_PRIxPTR", %016"NACL_PRIxS"\n", "Freeing tail:", rounded_addr + mem_sz, extra); if (-1 == munmap((void *) (rounded_addr + mem_sz), extra)) { perror("munmap (end)"); NaClLog(LOG_FATAL, "NaClAllocatePow2AlignedMemory: munmap tail failed\n"); } } NaClLog(4, "%"MSGWIDTH"s %016"NACL_PRIxPTR"\n", "Aligned memory:", rounded_addr); /* * we could also mmap again at rounded_addr w/o MAP_NORESERVE etc to * ensure that we have the memory, but that's better done in another * utility function. the semantics here is no paging space * reserved, as in Windows MEM_RESERVE without MEM_COMMIT. */ return (void *) rounded_addr; }