1 // Copyright 2006 Google LLC
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 // * Neither the name of Google LLC nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 // ProtectedMemoryAllocator
30 //
31 // See the header file for documentation
32
33 #ifdef HAVE_CONFIG_H
34 #include <config.h> // Must come first
35 #endif
36
37 #include "protected_memory_allocator.h"
38 #include <assert.h>
39
40 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ProtectedMemoryAllocator(vm_size_t pool_size)41 ProtectedMemoryAllocator::ProtectedMemoryAllocator(vm_size_t pool_size)
42 : pool_size_(pool_size),
43 next_alloc_offset_(0),
44 valid_(false) {
45
46 kern_return_t result = vm_allocate(mach_task_self(),
47 &base_address_,
48 pool_size,
49 TRUE
50 );
51
52 valid_ = (result == KERN_SUCCESS);
53 assert(valid_);
54 }
55
56 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ProtectedMemoryAllocator()57 ProtectedMemoryAllocator::~ProtectedMemoryAllocator() {
58 vm_deallocate(mach_task_self(),
59 base_address_,
60 pool_size_
61 );
62 }
63
64 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Allocate(vm_size_t bytes)65 char *ProtectedMemoryAllocator::Allocate(vm_size_t bytes) {
66 if (valid_ && next_alloc_offset_ + bytes <= pool_size_) {
67 char *p = (char*)base_address_ + next_alloc_offset_;
68 next_alloc_offset_ += bytes;
69 return p;
70 }
71
72 return NULL; // ran out of memory in our allocation block
73 }
74
75 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Protect()76 kern_return_t ProtectedMemoryAllocator::Protect() {
77 kern_return_t result = vm_protect(mach_task_self(),
78 base_address_,
79 pool_size_,
80 FALSE,
81 VM_PROT_READ);
82
83 return result;
84 }
85
86 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unprotect()87 kern_return_t ProtectedMemoryAllocator::Unprotect() {
88 kern_return_t result = vm_protect(mach_task_self(),
89 base_address_,
90 pool_size_,
91 FALSE,
92 VM_PROT_READ | VM_PROT_WRITE);
93
94 return result;
95 }
96