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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
|
// See LICENSE for license details.
#include "syscall.h"
#include "htif.h"
#include "byteorder.h"
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <limits.h>
#include <errno.h>
#include <stdlib.h>
#include <assert.h>
#include <termios.h>
#include <sstream>
#include <iostream>
using namespace std::placeholders;
#define RISCV_AT_FDCWD -100
struct riscv_stat
{
target_endian<uint64_t> dev;
target_endian<uint64_t> ino;
target_endian<uint32_t> mode;
target_endian<uint32_t> nlink;
target_endian<uint32_t> uid;
target_endian<uint32_t> gid;
target_endian<uint64_t> rdev;
target_endian<uint64_t> __pad1;
target_endian<uint64_t> size;
target_endian<uint32_t> blksize;
target_endian<uint32_t> __pad2;
target_endian<uint64_t> blocks;
target_endian<uint64_t> atime;
target_endian<uint64_t> __pad3;
target_endian<uint64_t> mtime;
target_endian<uint64_t> __pad4;
target_endian<uint64_t> ctime;
target_endian<uint64_t> __pad5;
target_endian<uint32_t> __unused4;
target_endian<uint32_t> __unused5;
riscv_stat(const struct stat& s, htif_t* htif)
: dev(htif->to_target<uint64_t>(s.st_dev)),
ino(htif->to_target<uint64_t>(s.st_ino)),
mode(htif->to_target<uint32_t>(s.st_mode)),
nlink(htif->to_target<uint32_t>(s.st_nlink)),
uid(htif->to_target<uint32_t>(s.st_uid)),
gid(htif->to_target<uint32_t>(s.st_gid)),
rdev(htif->to_target<uint64_t>(s.st_rdev)), __pad1(),
size(htif->to_target<uint64_t>(s.st_size)),
blksize(htif->to_target<uint32_t>(s.st_blksize)), __pad2(),
blocks(htif->to_target<uint64_t>(s.st_blocks)),
atime(htif->to_target<uint64_t>(s.st_atime)), __pad3(),
mtime(htif->to_target<uint64_t>(s.st_mtime)), __pad4(),
ctime(htif->to_target<uint64_t>(s.st_ctime)), __pad5(),
__unused4(), __unused5() {}
};
syscall_t::syscall_t(htif_t* htif)
: htif(htif), memif(&htif->memif()), table(2048)
{
table[17] = &syscall_t::sys_getcwd;
table[25] = &syscall_t::sys_fcntl;
table[34] = &syscall_t::sys_mkdirat;
table[35] = &syscall_t::sys_unlinkat;
table[37] = &syscall_t::sys_linkat;
table[38] = &syscall_t::sys_renameat;
table[46] = &syscall_t::sys_ftruncate;
table[48] = &syscall_t::sys_faccessat;
table[49] = &syscall_t::sys_chdir;
table[56] = &syscall_t::sys_openat;
table[57] = &syscall_t::sys_close;
table[62] = &syscall_t::sys_lseek;
table[63] = &syscall_t::sys_read;
table[64] = &syscall_t::sys_write;
table[67] = &syscall_t::sys_pread;
table[68] = &syscall_t::sys_pwrite;
table[79] = &syscall_t::sys_fstatat;
table[80] = &syscall_t::sys_fstat;
table[93] = &syscall_t::sys_exit;
table[1039] = &syscall_t::sys_lstat;
table[2011] = &syscall_t::sys_getmainvars;
register_command(0, std::bind(&syscall_t::handle_syscall, this, _1), "syscall");
int stdin_fd = dup(0), stdout_fd0 = dup(1), stdout_fd1 = dup(1);
if (stdin_fd < 0 || stdout_fd0 < 0 || stdout_fd1 < 0)
throw std::runtime_error("could not dup stdin/stdout");
fds.alloc(stdin_fd); // stdin -> stdin
fds.alloc(stdout_fd0); // stdout -> stdout
fds.alloc(stdout_fd1); // stderr -> stdout
}
std::string syscall_t::do_chroot(const char* fn)
{
if (!chroot.empty() && *fn == '/')
return chroot + fn;
return fn;
}
std::string syscall_t::undo_chroot(const char* fn)
{
if (chroot.empty())
return fn;
if (strncmp(fn, chroot.c_str(), chroot.size()) == 0
&& (chroot.back() == '/' || fn[chroot.size()] == '/'))
return fn + chroot.size() - (chroot.back() == '/');
return "/";
}
void syscall_t::handle_syscall(command_t cmd)
{
if (cmd.payload() & 1) // test pass/fail
{
htif->exitcode = cmd.payload();
if (htif->exit_code())
std::cerr << "*** FAILED *** (tohost = " << htif->exit_code() << ")" << std::endl;
return;
}
else // proxied system call
dispatch(cmd.payload());
cmd.respond(1);
}
reg_t syscall_t::sys_exit(reg_t code, reg_t a1, reg_t a2, reg_t a3, reg_t a4, reg_t a5, reg_t a6)
{
htif->exitcode = code << 1 | 1;
return 0;
}
static reg_t sysret_errno(sreg_t ret)
{
return ret == -1 ? -errno : ret;
}
reg_t syscall_t::sys_read(reg_t fd, reg_t pbuf, reg_t len, reg_t a3, reg_t a4, reg_t a5, reg_t a6)
{
std::vector<char> buf(len);
ssize_t ret = read(fds.lookup(fd), &buf[0], len);
reg_t ret_errno = sysret_errno(ret);
if (ret > 0)
memif->write(pbuf, ret, &buf[0]);
return ret_errno;
}
reg_t syscall_t::sys_pread(reg_t fd, reg_t pbuf, reg_t len, reg_t off, reg_t a4, reg_t a5, reg_t a6)
{
std::vector<char> buf(len);
ssize_t ret = pread(fds.lookup(fd), &buf[0], len, off);
reg_t ret_errno = sysret_errno(ret);
if (ret > 0)
memif->write(pbuf, ret, &buf[0]);
return ret_errno;
}
reg_t syscall_t::sys_write(reg_t fd, reg_t pbuf, reg_t len, reg_t a3, reg_t a4, reg_t a5, reg_t a6)
{
std::vector<char> buf(len);
memif->read(pbuf, len, &buf[0]);
reg_t ret = sysret_errno(write(fds.lookup(fd), &buf[0], len));
return ret;
}
reg_t syscall_t::sys_pwrite(reg_t fd, reg_t pbuf, reg_t len, reg_t off, reg_t a4, reg_t a5, reg_t a6)
{
std::vector<char> buf(len);
memif->read(pbuf, len, &buf[0]);
reg_t ret = sysret_errno(pwrite(fds.lookup(fd), &buf[0], len, off));
return ret;
}
reg_t syscall_t::sys_close(reg_t fd, reg_t a1, reg_t a2, reg_t a3, reg_t a4, reg_t a5, reg_t a6)
{
if (close(fds.lookup(fd)) < 0)
return sysret_errno(-1);
fds.dealloc(fd);
return 0;
}
reg_t syscall_t::sys_lseek(reg_t fd, reg_t ptr, reg_t dir, reg_t a3, reg_t a4, reg_t a5, reg_t a6)
{
return sysret_errno(lseek(fds.lookup(fd), ptr, dir));
}
reg_t syscall_t::sys_fstat(reg_t fd, reg_t pbuf, reg_t a2, reg_t a3, reg_t a4, reg_t a5, reg_t a6)
{
struct stat buf;
reg_t ret = sysret_errno(fstat(fds.lookup(fd), &buf));
if (ret != (reg_t)-1)
{
riscv_stat rbuf(buf, htif);
memif->write(pbuf, sizeof(rbuf), &rbuf);
}
return ret;
}
reg_t syscall_t::sys_fcntl(reg_t fd, reg_t cmd, reg_t arg, reg_t a3, reg_t a4, reg_t a5, reg_t a6)
{
return sysret_errno(fcntl(fds.lookup(fd), cmd, arg));
}
reg_t syscall_t::sys_ftruncate(reg_t fd, reg_t len, reg_t a2, reg_t a3, reg_t a4, reg_t a5, reg_t a6)
{
return sysret_errno(ftruncate(fds.lookup(fd), len));
}
reg_t syscall_t::sys_lstat(reg_t pname, reg_t len, reg_t pbuf, reg_t a3, reg_t a4, reg_t a5, reg_t a6)
{
std::vector<char> name(len);
memif->read(pname, len, &name[0]);
struct stat buf;
reg_t ret = sysret_errno(lstat(do_chroot(&name[0]).c_str(), &buf));
if (ret != (reg_t)-1)
{
riscv_stat rbuf(buf, htif);
memif->write(pbuf, sizeof(rbuf), &rbuf);
}
return ret;
}
#define AT_SYSCALL(syscall, fd, name, ...) \
(syscall(fds.lookup(fd), int(fd) == RISCV_AT_FDCWD ? do_chroot(name).c_str() : (name), __VA_ARGS__))
reg_t syscall_t::sys_openat(reg_t dirfd, reg_t pname, reg_t len, reg_t flags, reg_t mode, reg_t a5, reg_t a6)
{
std::vector<char> name(len);
memif->read(pname, len, &name[0]);
int fd = sysret_errno(AT_SYSCALL(openat, dirfd, &name[0], flags, mode));
if (fd < 0)
return sysret_errno(-1);
return fds.alloc(fd);
}
reg_t syscall_t::sys_fstatat(reg_t dirfd, reg_t pname, reg_t len, reg_t pbuf, reg_t flags, reg_t a5, reg_t a6)
{
std::vector<char> name(len);
memif->read(pname, len, &name[0]);
struct stat buf;
reg_t ret = sysret_errno(AT_SYSCALL(fstatat, dirfd, &name[0], &buf, flags));
if (ret != (reg_t)-1)
{
riscv_stat rbuf(buf, htif);
memif->write(pbuf, sizeof(rbuf), &rbuf);
}
return ret;
}
reg_t syscall_t::sys_faccessat(reg_t dirfd, reg_t pname, reg_t len, reg_t mode, reg_t a4, reg_t a5, reg_t a6)
{
std::vector<char> name(len);
memif->read(pname, len, &name[0]);
return sysret_errno(AT_SYSCALL(faccessat, dirfd, &name[0], mode, 0));
}
reg_t syscall_t::sys_renameat(reg_t odirfd, reg_t popath, reg_t olen, reg_t ndirfd, reg_t pnpath, reg_t nlen, reg_t a6)
{
std::vector<char> opath(olen), npath(nlen);
memif->read(popath, olen, &opath[0]);
memif->read(pnpath, nlen, &npath[0]);
return sysret_errno(renameat(fds.lookup(odirfd), int(odirfd) == RISCV_AT_FDCWD ? do_chroot(&opath[0]).c_str() : &opath[0],
fds.lookup(ndirfd), int(ndirfd) == RISCV_AT_FDCWD ? do_chroot(&npath[0]).c_str() : &npath[0]));
}
reg_t syscall_t::sys_linkat(reg_t odirfd, reg_t poname, reg_t olen, reg_t ndirfd, reg_t pnname, reg_t nlen, reg_t flags)
{
std::vector<char> oname(olen), nname(nlen);
memif->read(poname, olen, &oname[0]);
memif->read(pnname, nlen, &nname[0]);
return sysret_errno(linkat(fds.lookup(odirfd), int(odirfd) == RISCV_AT_FDCWD ? do_chroot(&oname[0]).c_str() : &oname[0],
fds.lookup(ndirfd), int(ndirfd) == RISCV_AT_FDCWD ? do_chroot(&nname[0]).c_str() : &nname[0],
flags));
}
reg_t syscall_t::sys_unlinkat(reg_t dirfd, reg_t pname, reg_t len, reg_t flags, reg_t a4, reg_t a5, reg_t a6)
{
std::vector<char> name(len);
memif->read(pname, len, &name[0]);
return sysret_errno(AT_SYSCALL(unlinkat, dirfd, &name[0], flags));
}
reg_t syscall_t::sys_mkdirat(reg_t dirfd, reg_t pname, reg_t len, reg_t mode, reg_t a4, reg_t a5, reg_t a6)
{
std::vector<char> name(len);
memif->read(pname, len, &name[0]);
return sysret_errno(AT_SYSCALL(mkdirat, dirfd, &name[0], mode));
}
reg_t syscall_t::sys_getcwd(reg_t pbuf, reg_t size, reg_t a2, reg_t a3, reg_t a4, reg_t a5, reg_t a6)
{
std::vector<char> buf(size);
char* ret = getcwd(&buf[0], size);
if (ret == NULL)
return sysret_errno(-1);
std::string tmp = undo_chroot(&buf[0]);
if (size <= tmp.size())
return -ENOMEM;
memif->write(pbuf, tmp.size() + 1, &tmp[0]);
return tmp.size() + 1;
}
reg_t syscall_t::sys_getmainvars(reg_t pbuf, reg_t limit, reg_t a2, reg_t a3, reg_t a4, reg_t a5, reg_t a6)
{
std::vector<std::string> args = htif->target_args();
std::vector<target_endian<uint64_t>> words(args.size() + 3);
words[0] = htif->to_target<uint64_t>(args.size());
words[args.size()+1] = target_endian<uint64_t>::zero; // argv[argc] = NULL
words[args.size()+2] = target_endian<uint64_t>::zero; // envp[0] = NULL
size_t sz = (args.size() + 3) * sizeof(words[0]);
for (size_t i = 0; i < args.size(); i++)
{
words[i+1] = htif->to_target<uint64_t>(sz + pbuf);
sz += args[i].length() + 1;
}
std::vector<char> bytes(sz);
memcpy(&bytes[0], &words[0], sizeof(words[0]) * words.size());
for (size_t i = 0; i < args.size(); i++)
strcpy(&bytes[htif->from_target(words[i+1]) - pbuf], args[i].c_str());
if (bytes.size() > limit)
return -ENOMEM;
memif->write(pbuf, bytes.size(), &bytes[0]);
return 0;
}
reg_t syscall_t::sys_chdir(reg_t path, reg_t a1, reg_t a2, reg_t a3, reg_t a4, reg_t a5, reg_t a6)
{
size_t size = 0;
while (memif->read_uint8(path + size++))
;
std::vector<char> buf(size);
for (size_t offset = 0;; offset++)
{
buf[offset] = memif->read_uint8(path + offset);
if (!buf[offset])
break;
}
return sysret_errno(chdir(buf.data()));
}
void syscall_t::dispatch(reg_t mm)
{
target_endian<reg_t> magicmem[8];
memif->read(mm, sizeof(magicmem), magicmem);
reg_t n = htif->from_target(magicmem[0]);
if (n >= table.size() || !table[n])
throw std::runtime_error("bad syscall #" + std::to_string(n));
magicmem[0] = htif->to_target((this->*table[n])(htif->from_target(magicmem[1]), htif->from_target(magicmem[2]), htif->from_target(magicmem[3]), htif->from_target(magicmem[4]), htif->from_target(magicmem[5]), htif->from_target(magicmem[6]), htif->from_target(magicmem[7])));
memif->write(mm, sizeof(magicmem), magicmem);
}
reg_t fds_t::alloc(int fd)
{
reg_t i;
for (i = 0; i < fds.size(); i++)
if (fds[i] == -1)
break;
if (i == fds.size())
fds.resize(i+1);
fds[i] = fd;
return i;
}
void fds_t::dealloc(reg_t fd)
{
fds[fd] = -1;
}
int fds_t::lookup(reg_t fd)
{
if (int(fd) == RISCV_AT_FDCWD)
return AT_FDCWD;
return fd >= fds.size() ? -1 : fds[fd];
}
void syscall_t::set_chroot(const char* where)
{
char buf1[PATH_MAX], buf2[PATH_MAX];
if (getcwd(buf1, sizeof(buf1)) == NULL
|| chdir(where) != 0
|| getcwd(buf2, sizeof(buf2)) == NULL
|| chdir(buf1) != 0)
{
fprintf(stderr, "could not chroot to %s\n", where);
exit(-1);
}
chroot = buf2;
}
|