Home | History | Annotate | Download | only in wtf
      1 /*
      2  * Copyright (C) 2013 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #ifndef WTF_PageAllocator_h
     32 #define WTF_PageAllocator_h
     33 
     34 namespace WTF {
     35 
     36 // Our granulatity of page allocation is 64KB. This is a Windows limitation,
     37 // but we apply the same requirement for all platforms in order to keep
     38 // things simple and consistent.
     39 // We term these 64KB allocations "super pages". They're just a clump of
     40 // underlying 4KB system pages.
     41 static const size_t kSuperPageSize = 1 << 16; // 64KB
     42 static const size_t kSuperPageOffsetMask = kSuperPageSize - 1;
     43 static const size_t kSuperPageBaseMask = ~kSuperPageOffsetMask;
     44 
     45 // All Blink-supported systems have 4096 sized system pages and can handle
     46 // permissions and commit / decommit at this granularity.
     47 static const size_t kSystemPageSize = 4096;
     48 static const size_t kSystemPageOffsetMask = kSystemPageSize - 1;
     49 
     50 static const size_t kNumSystemPagesPerSuperPage = kSuperPageSize / kSystemPageSize;
     51 
     52 // Allocate one or more super pages. Addresses in the range will be readable and
     53 // writeable but not executable.
     54 // The requested address is just a hint; the actual address returned may
     55 // differ. The returned address will be aligned to kSuperPageSize.
     56 // len is in bytes, and must be a multiple of kSuperPageSize.
     57 // This call will exit the process if the allocation cannot be satisfied.
     58 void* allocSuperPages(void* addr, size_t len);
     59 
     60 // Free one or more super pages.
     61 // addr and len must match a previous call to allocPages().
     62 void freeSuperPages(void* addr, size_t len);
     63 
     64 // Mark one or more system pages as being inaccessible. This is not reversible.
     65 // Subsequently accessing any address in the range will fault, the addresses
     66 // will not be re-used by future allocations.
     67 // len must be a multiple of kSystemPageSize bytes.
     68 void setSystemPagesInaccessible(void* addr, size_t len);
     69 
     70 // Decommit one or more system pages. Decommitted means that the physical memory
     71 // is released to the system, but the virtual address space remains reserved.
     72 // System pages are re-committed by writing to them.
     73 // Clients should not make any assumptions about the contents of decommitted
     74 // system pages, before or after they write to the page. The only guarantee
     75 // provided is that the contents of the system page will be deterministic again // after writing to it. In particlar note that system pages are not guaranteed
     76 // to be zero-filled upon re-commit.
     77 // len must be a multiple of kSystemPageSize bytes.
     78 void decommitSystemPages(void* addr, size_t len);
     79 
     80 // Returns a suitable pointer for starting to allocate super pages.
     81 // The pointer is not guaranteed to be "unused", but does represent an address
     82 // that has a good chance of being unused. The pointer is also randomized to
     83 // provide reasonable ASLR.
     84 char* getRandomSuperPageBase();
     85 
     86 } // namespace WTF
     87 
     88 #endif // WTF_PageAllocator_h
     89