forked from dbaeumges/android_platform_dalvik
/
SysUtil.c
286 lines (240 loc) · 7.07 KB
/
SysUtil.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* System utilities.
*/
#include "DexFile.h"
#include "SysUtil.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#ifdef HAVE_POSIX_FILEMAP
#include <sys/mman.h>
#endif
#include <limits.h>
#include <errno.h>
/*
* Having trouble finding a portable way to get this. sysconf(_SC_PAGE_SIZE)
* seems appropriate, but we don't have that on the device. Some systems
* have getpagesize(2), though the linux man page has some odd cautions.
*/
#define DEFAULT_PAGE_SIZE 4096
/*
* Create an anonymous shared memory segment large enough to hold "length"
* bytes. The actual segment may be larger because mmap() operates on
* page boundaries (usually 4K).
*/
static void* sysCreateAnonShmem(size_t length)
{
#ifdef HAVE_POSIX_FILEMAP
void* ptr;
ptr = mmap(NULL, length, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANON, -1, 0);
if (ptr == MAP_FAILED) {
LOGW("mmap(%d, RW, SHARED|ANON) failed: %s\n", (int) length,
strerror(errno));
return NULL;
}
return ptr;
#else
LOGE("sysCreateAnonShmem not implemented.\n");
return NULL;
#endif
}
static int getFileStartAndLength(int fd, off_t *start_, size_t *length_)
{
off_t start, end;
size_t length;
assert(start_ != NULL);
assert(length_ != NULL);
start = lseek(fd, 0L, SEEK_CUR);
end = lseek(fd, 0L, SEEK_END);
(void) lseek(fd, start, SEEK_SET);
if (start == (off_t) -1 || end == (off_t) -1) {
LOGE("could not determine length of file\n");
return -1;
}
length = end - start;
if (length == 0) {
LOGE("file is empty\n");
return -1;
}
*start_ = start;
*length_ = length;
return 0;
}
/*
* Pull the contents of a file into an new shared memory segment. We grab
* everything from fd's current offset on.
*
* We need to know the length ahead of time so we can allocate a segment
* of sufficient size.
*/
int sysLoadFileInShmem(int fd, MemMapping* pMap)
{
#ifdef HAVE_POSIX_FILEMAP
off_t start;
size_t length, actual;
void* memPtr;
assert(pMap != NULL);
if (getFileStartAndLength(fd, &start, &length) < 0)
return -1;
memPtr = sysCreateAnonShmem(length);
if (memPtr == NULL)
return -1;
actual = read(fd, memPtr, length);
if (actual != length) {
LOGE("only read %d of %d bytes\n", (int) actual, (int) length);
sysReleaseShmem(pMap);
return -1;
}
pMap->baseAddr = pMap->addr = memPtr;
pMap->baseLength = pMap->length = length;
return 0;
#else
LOGE("sysLoadFileInShmem not implemented.\n");
return -1;
#endif
}
/*
* Map a file (from fd's current offset) into a shared, read-only memory
* segment. The file offset must be a multiple of the page size.
*
* On success, returns 0 and fills out "pMap". On failure, returns a nonzero
* value and does not disturb "pMap".
*/
int sysMapFileInShmem(int fd, MemMapping* pMap)
{
#ifdef HAVE_POSIX_FILEMAP
off_t start;
size_t length;
void* memPtr;
assert(pMap != NULL);
if (getFileStartAndLength(fd, &start, &length) < 0)
return -1;
memPtr = mmap(NULL, length, PROT_READ, MAP_FILE | MAP_SHARED, fd, start);
if (memPtr == MAP_FAILED) {
LOGW("mmap(%d, R, FILE|SHARED, %d, %d) failed: %s\n", (int) length,
fd, (int) start, strerror(errno));
return -1;
}
pMap->baseAddr = pMap->addr = memPtr;
pMap->baseLength = pMap->length = length;
return 0;
#else
/* No MMAP, just fake it by copying the bits.
For Win32 we could use MapViewOfFile if really necessary
(see libs/utils/FileMap.cpp).
*/
off_t start;
size_t length;
void* memPtr;
assert(pMap != NULL);
if (getFileStartAndLength(fd, &start, &length) < 0)
return -1;
memPtr = malloc(length);
if (read(fd, memPtr, length) < 0) {
LOGW("read(fd=%d, start=%d, length=%d) failed: %s\n", (int) length,
fd, (int) start, strerror(errno));
return -1;
}
pMap->baseAddr = pMap->addr = memPtr;
pMap->baseLength = pMap->length = length;
return 0;
#endif
}
/*
* Map part of a file (from fd's current offset) into a shared, read-only
* memory segment.
*
* On success, returns 0 and fills out "pMap". On failure, returns a nonzero
* value and does not disturb "pMap".
*/
int sysMapFileSegmentInShmem(int fd, off_t start, long length,
MemMapping* pMap)
{
#ifdef HAVE_POSIX_FILEMAP
off_t dummy;
size_t fileLength, actualLength;
off_t actualStart;
int adjust;
void* memPtr;
assert(pMap != NULL);
if (getFileStartAndLength(fd, &dummy, &fileLength) < 0)
return -1;
if (start + length > (long)fileLength) {
LOGW("bad segment: st=%d len=%ld flen=%d\n",
(int) start, length, (int) fileLength);
return -1;
}
/* adjust to be page-aligned */
adjust = start % DEFAULT_PAGE_SIZE;
actualStart = start - adjust;
actualLength = length + adjust;
memPtr = mmap(NULL, actualLength, PROT_READ, MAP_FILE | MAP_SHARED,
fd, actualStart);
if (memPtr == MAP_FAILED) {
LOGW("mmap(%d, R, FILE|SHARED, %d, %d) failed: %s\n",
(int) actualLength, fd, (int) actualStart, strerror(errno));
return -1;
}
pMap->baseAddr = memPtr;
pMap->baseLength = actualLength;
pMap->addr = (char*)memPtr + adjust;
pMap->length = length;
LOGVV("mmap seg (st=%d ln=%d): bp=%p bl=%d ad=%p ln=%d\n",
(int) start, (int) length,
pMap->baseAddr, (int) pMap->baseLength,
pMap->addr, (int) pMap->length);
return 0;
#else
LOGE("sysMapFileSegmentInShmem not implemented.\n");
return -1;
#endif
}
/*
* Release a memory mapping.
*/
void sysReleaseShmem(MemMapping* pMap)
{
#ifdef HAVE_POSIX_FILEMAP
if (pMap->baseAddr == NULL && pMap->baseLength == 0)
return;
if (munmap(pMap->baseAddr, pMap->baseLength) < 0) {
LOGW("munmap(%p, %d) failed: %s\n",
pMap->baseAddr, (int)pMap->baseLength, strerror(errno));
} else {
LOGV("munmap(%p, %d) succeeded\n", pMap->baseAddr, pMap->baseLength);
pMap->baseAddr = NULL;
pMap->baseLength = 0;
}
#else
/* Free the bits allocated by sysMapFileInShmem. */
if (pMap->baseAddr != NULL) {
free(pMap->baseAddr);
pMap->baseAddr = NULL;
}
pMap->baseLength = 0;
#endif
}
/*
* Make a copy of a MemMapping.
*/
void sysCopyMap(MemMapping* dst, const MemMapping* src)
{
memcpy(dst, src, sizeof(MemMapping));
}