This repository has been archived by the owner on Jan 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 261
/
Copy pathdb_memory.c
207 lines (171 loc) · 5.71 KB
/
db_memory.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
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/* Copyright (C) 2014 Stony Brook University */
/*
* This file contains APIs that allocate, free or protect virtual memory.
*/
#include <asm/fcntl.h>
#include <asm/mman.h>
#include "api.h"
#include "pal.h"
#include "pal_debug.h"
#include "pal_defs.h"
#include "pal_error.h"
#include "pal_flags_conv.h"
#include "pal_internal.h"
#include "pal_linux.h"
#include "pal_linux_defs.h"
#include "pal_linux_error.h"
bool _DkCheckMemoryMappable(const void* addr, size_t size) {
return (addr < DATA_END && addr + size > TEXT_START);
}
int _DkVirtualMemoryAlloc(void** paddr, size_t size, int alloc_type, int prot) {
assert(WITHIN_MASK(alloc_type, PAL_ALLOC_MASK));
assert(WITHIN_MASK(prot, PAL_PROT_MASK));
void* addr = *paddr;
void* mem = addr;
int flags = PAL_MEM_FLAGS_TO_LINUX(alloc_type, prot | PAL_PROT_WRITECOPY);
prot = PAL_PROT_TO_LINUX(prot);
flags |= MAP_ANONYMOUS | (addr ? MAP_FIXED : 0);
mem = (void*)ARCH_MMAP(addr, size, prot, flags, -1, 0);
if (IS_ERR_P(mem))
return unix_to_pal_error(-ERRNO_P(mem));
*paddr = mem;
return 0;
}
int _DkVirtualMemoryFree(void* addr, size_t size) {
int ret = INLINE_SYSCALL(munmap, 2, addr, size);
return ret < 0 ? unix_to_pal_error(ret) : 0;
}
int _DkVirtualMemoryProtect(void* addr, size_t size, int prot) {
int ret = INLINE_SYSCALL(mprotect, 3, addr, size, PAL_PROT_TO_LINUX(prot));
return ret < 0 ? unix_to_pal_error(ret) : 0;
}
static int read_proc_meminfo(const char* key, unsigned long* val) {
int fd = INLINE_SYSCALL(open, 3, "/proc/meminfo", O_RDONLY, 0);
if (fd < 0)
return -PAL_ERROR_DENIED;
char buffer[40];
int ret = 0;
size_t n;
size_t r = 0;
size_t len = strlen(key);
ret = -PAL_ERROR_DENIED;
while (1) {
ret = INLINE_SYSCALL(read, 3, fd, buffer + r, 40 - r);
if (ret < 0) {
ret = -PAL_ERROR_DENIED;
break;
}
for (n = r; n < r + ret; n++)
if (buffer[n] == '\n')
break;
r += ret;
if (n == r + ret || n <= len) {
ret = -PAL_ERROR_INVAL;
break;
}
if (!memcmp(key, buffer, len) && buffer[len] == ':') {
for (size_t i = len + 1; i < n; i++)
if (buffer[i] != ' ') {
*val = atol(buffer + i);
break;
}
ret = 0;
break;
}
memmove(buffer, buffer + n + 1, r - n - 1);
r -= n + 1;
}
INLINE_SYSCALL(close, 1, fd);
return ret;
}
unsigned long _DkMemoryQuota(void) {
if (g_linux_state.memory_quota == (unsigned long)-1)
return 0;
if (g_linux_state.memory_quota)
return g_linux_state.memory_quota;
unsigned long quota = 0;
if (read_proc_meminfo("MemTotal", "a) < 0) {
g_linux_state.memory_quota = (unsigned long)-1;
return 0;
}
return (g_linux_state.memory_quota = quota * 1024);
}
unsigned long _DkMemoryAvailableQuota(void) {
unsigned long quota = 0;
if (read_proc_meminfo("MemFree", "a) < 0)
return 0;
return quota * 1024;
}
/* Expects `line` to be in the same format as "/proc/self/maps" entries, i.e. starting with
* "hexadecimal_number-hexadecimalnumber", e.g. "1fe3-87cc ...". */
static void parse_line(const char* line, uintptr_t* start_ptr, uintptr_t* end_ptr) {
char* next = NULL;
long start = strtol(line, &next, 16);
long end = 0;
if (next && next[0] == '-') {
end = strtol(next + 1, NULL, 16);
}
if (start >= 0 && end > 0) {
*start_ptr = (uintptr_t)start;
*end_ptr = (uintptr_t)end;
}
}
/* This function is very fragile w.r.t. "/proc/self/maps" file format. */
int get_vdso_and_vvar_ranges(uintptr_t* vdso_start, uintptr_t* vdso_end, uintptr_t* vvar_start,
uintptr_t* vvar_end) {
int fd = INLINE_SYSCALL(open, 3, "/proc/self/maps", O_RDONLY, 0);
if (fd < 0) {
return unix_to_pal_error(fd);
}
const char* vdso_str = "[vdso]";
const size_t vdso_str_len = strlen(vdso_str);
const char* vvar_str = "[vvar]";
const size_t vvar_str_len = strlen(vvar_str);
int ret = 0;
/* Arbitrary size, must be big enough to hold lines containing "vdso" and "vvar". */
char buf[0x100];
size_t size = 0;
ssize_t got = 0;
do {
/* There should be no failures or partial reads from this fd. */
got = INLINE_SYSCALL(read, 3, fd, buf + size, sizeof(buf) - 1 - size);
if (got < 0) {
ret = unix_to_pal_error(got);
goto out;
}
size += (size_t)got;
buf[size] = '\0';
char* line_end = strchr(buf, '\n');
if (!line_end) {
line_end = buf + size;
}
assert(line_end < buf + sizeof(buf));
*line_end = '\0';
uintptr_t start = 0;
uintptr_t end = 0;
if (!memcmp(vdso_str, line_end - vdso_str_len, vdso_str_len)) {
parse_line(buf, &start, &end);
if (start || end) {
*vdso_start = start;
*vdso_end = end;
}
}
if (!memcmp(vvar_str, line_end - vvar_str_len, vvar_str_len)) {
parse_line(buf, &start, &end);
if (start || end) {
*vvar_start = start;
*vvar_end = end;
}
}
size_t new_size = 0;
if (buf + size > line_end + 1) {
new_size = buf + size - (line_end + 1);
memmove(buf, line_end + 1, new_size);
}
size = new_size;
} while (size > 0 || got > 0);
out:;
int tmp_ret = unix_to_pal_error(INLINE_SYSCALL(close, 1, fd));
return ret ?: tmp_ret;
}