-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathmaps.c
258 lines (219 loc) · 5.16 KB
/
maps.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
#include <myst/maps.h>
#include <myst/strings.h>
void myst_maps_dump1(const myst_maps_t* maps)
{
if (maps)
{
const myst_maps_t* p = maps;
char r = (p->prot & PROT_READ) ? 'r' : '-';
char w = (p->prot & PROT_WRITE) ? 'w' : '-';
char x = (p->prot & PROT_EXEC) ? 'x' : '-';
char f = '-';
if (p->flags & MAP_SHARED)
f = 's';
else if (p->flags & MAP_PRIVATE)
f = 'p';
printf(
"%lx-%lx %c%c%c%c %08lu %02u:%02u %lu %s\n",
p->start,
p->end,
r,
w,
x,
f,
p->offset,
p->major,
p->minor,
p->inode,
p->path);
}
}
void myst_maps_dump(const myst_maps_t* maps)
{
for (const myst_maps_t* p = maps; p; p = p->next)
myst_maps_dump1(p);
}
void myst_maps_free(myst_maps_t* maps)
{
for (myst_maps_t* p = maps; p;)
{
myst_maps_t* next = p->next;
free(p);
p = next;
}
}
int myst_maps_load(myst_maps_t** maps_out)
{
int ret = 0;
char path[PATH_MAX];
FILE* is = NULL;
myst_maps_t* head = NULL;
myst_maps_t* tail = NULL;
char line[4096];
if (maps_out)
*maps_out = NULL;
if (!maps_out)
{
ret = -EINVAL;
goto done;
}
snprintf(path, sizeof(path), "/proc/%d/maps", getpid());
if (!(is = fopen(path, "r")))
{
ret = -ENOENT;
goto done;
}
/* read line-by-line */
while (fgets(line, sizeof(line), is))
{
uint64_t start;
uint64_t end;
char r;
char w;
char x;
char f;
size_t o;
int n;
uint32_t major;
uint32_t minor;
uint64_t inode;
myst_maps_t* maps;
char path[PATH_MAX] = "";
int path_offset = 0;
n = sscanf(
line,
"%lx-%lx %c%c%c%c %lu %u:%u %lu %n",
&start,
&end,
&r,
&w,
&x,
&f,
&o,
&minor,
&major,
&inode,
&path_offset);
if (n < 10)
{
ret = -ENOSYS;
goto done;
}
// This is to avoid overflow when user's input is bigger than path
if (myst_strlcpy(path, &line[path_offset], sizeof(path)) >=
sizeof(path))
{
// path is not big enough to store input
ret = -ENOSYS;
goto done;
}
// Since we are not using scanf("%s")
// we have to remove newline at the end of string
{
int path_length = strlen(path);
if (path_length > 0 && path[path_length - 1] == '\n')
path[path_length - 1] = 0;
}
if (!(maps = calloc(1, sizeof(myst_maps_t))))
{
ret = -ENOMEM;
goto done;
}
maps->start = start;
maps->end = end;
if (r == 'r')
maps->prot |= PROT_READ;
if (w == 'w')
maps->prot |= PROT_WRITE;
if (x == 'x')
maps->prot |= PROT_EXEC;
if (f == 's')
maps->flags |= MAP_SHARED;
else if (f == 'p')
maps->flags |= MAP_PRIVATE;
else
{
ret = -EINVAL;
goto done;
}
maps->offset = o;
maps->major = major;
maps->minor = minor;
maps->inode = inode;
if (myst_strlcpy(maps->path, path, sizeof(maps->path)) >=
sizeof(maps->path))
{
// maps->path not big enough to store path
ret = -ENOSYS;
goto done;
}
if (tail)
{
tail->next = maps;
tail = maps;
}
else
{
head = maps;
tail = maps;
}
}
*maps_out = head;
head = NULL;
done:
if (is)
fclose(is);
if (head)
myst_maps_free(head);
return ret;
}
void myst_mstat_dump(const struct myst_mstat* buf)
{
if (buf)
{
char r = (buf->prot & PROT_READ) ? 'r' : '-';
char w = (buf->prot & PROT_WRITE) ? 'w' : '-';
char x = (buf->prot & PROT_EXEC) ? 'x' : '-';
char f = '-';
if (buf->flags & MAP_SHARED)
f = 's';
else if (buf->flags & MAP_PRIVATE)
f = 'p';
printf("%c%c%c%c\n", r, w, x, f);
}
}
int myst_mstat(
const myst_maps_t* maps,
const void* addr,
struct myst_mstat* buf)
{
int ret = 0;
if (buf)
memset(buf, 0, sizeof(struct myst_mstat));
if (!maps || !addr || !buf)
{
ret = -EINVAL;
goto done;
}
for (const myst_maps_t* p = maps; p; p = p->next)
{
if ((uint64_t)addr >= p->start && (uint64_t)addr < p->end)
{
buf->prot = p->prot;
buf->flags = p->flags;
goto done;
}
}
ret = -ENOMEM;
done:
return ret;
}