• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2011, The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "MemChunk.h"
18 
19 #include "utils/flush_cpu_cache.h"
20 #include "utils/helper.h"
21 
22 #include <llvm/Support/raw_ostream.h>
23 
24 #ifndef USE_MINGW       /* TODO create a proper HAVE_MMAN_H */
25 #include <sys/mman.h>
26 #else
27 #include "mmanWindows.h"
28 #endif
29 
30 #include <stdlib.h>
31 
32 #ifndef MAP_32BIT
33 #define MAP_32BIT 0
34 // Note: If the <sys/mman.h> does not come with MAP_32BIT, then we
35 // define it as zero, so that it won't manipulate the flags.
36 #endif
37 
38 //#define USE_FIXED_ADDR_MEM_CHUNK 1
39 
40 #if USE_FIXED_ADDR_MEM_CHUNK
41 static uintptr_t StartAddr = 0x7e000000UL;
42 #endif
43 
44 AllocFunc MemChunk::VendorAlloc = NULL;
45 FreeFunc MemChunk::VendorFree = NULL;
46 
MemChunk()47 MemChunk::MemChunk() : buf(NULL), buf_size(0), bVendorBuf(true) {
48 }
49 
~MemChunk()50 MemChunk::~MemChunk() {
51   if (!invalidBuf() && bVendorBuf && VendorFree) {
52     (*VendorFree)(buf);
53     return;
54   }
55   if (!invalidBuf()) {
56     munmap(buf, buf_size);
57   }
58 }
59 
invalidBuf() const60 bool MemChunk::invalidBuf() const {
61   return (buf == 0 || buf == (unsigned char *)MAP_FAILED);
62 }
63 
allocate(size_t size)64 bool MemChunk::allocate(size_t size) {
65   if (size == 0) {
66     return true;
67   }
68   if (VendorAlloc) {
69     buf = (unsigned char*)(*VendorAlloc)(size, 0);
70   }
71   if (invalidBuf()) {
72     bVendorBuf = false;
73 #if USE_FIXED_ADDR_MEM_CHUNK
74     buf = (unsigned char *)mmap((void *)StartAddr, size,
75                                 PROT_READ | PROT_WRITE,
76                                 MAP_PRIVATE | MAP_ANON | MAP_32BIT,
77                                 -1, 0);
78 #else
79     buf = (unsigned char *)mmap(0, size,
80                                 PROT_READ | PROT_WRITE,
81                                 MAP_PRIVATE | MAP_ANON | MAP_32BIT,
82                                 -1, 0);
83 #endif
84   }
85 
86   if (invalidBuf()) {
87     return false;
88   }
89 
90 #if USE_FIXED_ADDR_MEM_CHUNK
91   StartAddr += (size + 4095) / 4096 * 4096;
92 #endif
93 
94   buf_size = size;
95   return true;
96 }
97 
print() const98 void MemChunk::print() const {
99   if (!invalidBuf()) {
100     dump_hex(buf, buf_size, 0, buf_size);
101   }
102 }
103 
protect(int prot)104 bool MemChunk::protect(int prot) {
105   if (buf_size > 0) {
106     int ret = mprotect((void *)buf, buf_size, prot);
107     if (ret == -1) {
108       llvm::errs() << "Error: Can't mprotect.\n";
109       return false;
110     }
111 
112     if (prot & PROT_EXEC) {
113       FLUSH_CPU_CACHE(buf, buf + buf_size);
114     }
115   }
116 
117   return true;
118 }
119