-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstubs.c
379 lines (296 loc) · 8.15 KB
/
stubs.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
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
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <stdbool.h>
#include <reent.h>
#include <stdlib.h>
#include "mc_mman.h"
#include "mcinterface.h"
/* ======================================== */
/* ============== malloc ================== */
/* ======================================== */
// You can adjust this to change the amount of memory available to malloc.
// However, setting this to much higher values (like 128 * 65536)
// will cause severe lag and possibly make the world unloadable.
#define MEMORY_SIZE (4 * 65536)
void *_calloc_r(struct _reent* r, size_t num, size_t size) {
return calloc(num, size);
}
void *_malloc_r(struct _reent* r, size_t size) {
return malloc(size);
}
void _free_r(struct _reent* r, void *ptr) {
free(ptr);
}
void *_realloc_r(struct _reent* r, void *ptr, size_t new_size) {
return realloc(ptr, new_size);
}
uint8_t memory[MEMORY_SIZE] = {};
uint8_t *next = memory;
void *sbrk(intptr_t increment) {
uint8_t *result = next;
intptr_t current_size = (intptr_t)next - (intptr_t)memory;
if (current_size + increment >= MEMORY_SIZE) {
errno = ENOMEM;
return NULL;
}
next += increment;
return result;
}
/* ================================ */
/* ========= Filesystem =========== */
/* ================================ */
// Minecraft doesn't have a real filesystem [Citation Needed].
// In order to emulate programs that want to open specific files with minimal modification,
// the simplest solution is to store each file in the binary itself as a C array,
// and hardcode the path each file would have.
// This represents one of our fake files.
typedef struct {
const char *name;
const unsigned char *data;
int size;
} stub_file;
// There are many programs available to convert a file into a C array, so take your pick.
const unsigned char file_foo_data[] = { 'T','h','i','s',' ','i','s',' ','f','o','o','\n' };
const int file_foo_size = sizeof(file_foo_data) / sizeof(file_foo_data[0]);
const unsigned char file_bar_data[] = { 'B', 'A', 'R' };
const int file_bar_size = sizeof(file_bar_data) / sizeof(file_bar_data[0]);
#define NUM_STUB_FILES 2
stub_file stub_files[NUM_STUB_FILES] = {
{ "some/path/foo.txt", file_foo_data, file_foo_size },
{ "another/file/bar.txt", file_bar_data, file_bar_size }
};
// Here's the info for a single open file descriptor.
typedef struct {
stub_file *file;
off_t offset;
bool valid;
} fd_info;
#define MAX_FDS 16
// A file descriptor returned from e.g. `open` is just an index into this array.
fd_info fd_table[MAX_FDS] = {};
#define STDOUT_FD 0
#define STDERR_FD 1
#define STDIN_FD 2
int _open(const char *pathname, int flags, mode_t mode) {
int file_idx = -1;
for (int i = 0; i < NUM_STUB_FILES; i++) {
if (strcmp(pathname, stub_files[i].name) == 0) {
file_idx = i;
break;
}
}
if (file_idx == -1) {
errno = ENOENT;
return -1;
}
int fd = -1;
for (int i = 3; i < MAX_FDS; ++i) {
if (!fd_table[i].valid) {
fd = i;
break;
}
}
if (fd == -1) {
errno = ENFILE;
return -1;
}
fd_table[fd].file = &stub_files[file_idx];
fd_table[fd].valid = true;
fd_table[fd].offset = 0;
return fd;
}
int open(const char *pathname, int flags, mode_t mode) {
return _open(pathname, flags, mode);
}
int _close(int fd) {
if (0 <= fd && fd < MAX_FDS) {
if (fd_table[fd].valid) {
fd_table[fd].valid = false;
return 0;
}
}
errno = EBADF;
return -1;
}
int close(int fd) {
return _close(fd);
}
int _write(int fd, const void *buf, size_t count);
int _link(const char *oldpath, const char *newpath) {
const char msg[] = "ignoring link\n";
_write(STDOUT_FD, msg, sizeof(msg) - 1);
errno = ENOSYS;
return -1;
}
int _unlink(const char *pathname) {
const char msg[] = "ignoring unlink\n";
_write(STDOUT_FD, msg, sizeof(msg) - 1);
errno = ENOSYS;
return -1;
}
/* ===================================== */
/* ======== Read/Write/Seek ============ */
/* ===================================== */
#define MAX_READ_BATCH_SIZE (1000)
int _read(int fd, void *buf, size_t count) {
uint8_t *buf_bytes = (uint8_t *)buf;
if (fd < 0 || fd >= MAX_FDS) {
errno = EBADF;
return -1;
}
fd_info *info = &fd_table[fd];
if (!info->valid) {
errno = EBADF;
return -1;
}
int file_size = info->file->size;
uint8_t *file_data = (uint8_t*)info->file->data;
ssize_t num_read = 0;
while (count > 0) {
if (info->offset == file_size) {
break;
}
ssize_t file_bytes_remaining = file_size - info->offset;
ssize_t max_read_ahead = file_bytes_remaining;
if (max_read_ahead > count) {
max_read_ahead = count;
}
if (max_read_ahead > MAX_READ_BATCH_SIZE) {
max_read_ahead = MAX_READ_BATCH_SIZE;
}
memcpy(buf_bytes, file_data + info->offset, max_read_ahead);
buf_bytes += max_read_ahead;
info->offset += max_read_ahead;
num_read += max_read_ahead;
count -= max_read_ahead;
if (max_read_ahead == MAX_READ_BATCH_SIZE) {
mc_sleep();
}
}
return num_read;
}
int read(int fd, void *buf, size_t count) {
return _read(fd, buf, count);
}
int _write(int fd, const void *buf, size_t count) {
if (fd == STDOUT_FD || fd == STDERR_FD) {
uint8_t *buf_bytes = (uint8_t*)buf;
for (size_t i = 0; i < count; ++i) {
mc_putc(buf_bytes[i]);
}
return count;
} else {
errno = ENOSYS;
return -1;
}
}
int write(int fd, const void *buf, size_t count) {
return _write(fd, buf, count);
}
void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) {
if (prot != (PROT_READ | PROT_WRITE)) {
errno = ENOSYS;
return (void*)-1;
}
if (flags != MAP_PRIVATE) {
errno = ENOSYS;
return (void*)-1;
}
if (addr != NULL) {
errno = ENOSYS;
return (void*)-1;
}
if (fd < 0 || fd >= MAX_FDS) {
errno = EBADF;
return (void*)-1;
}
fd_info *info = &fd_table[fd];
if (!info->valid) {
errno = EBADF;
return (void*)-1;
}
// Discards qualifiers but oh well.
// hope you don't write to it!
return (void*)info->file->data;
}
off_t _lseek(int fd, off_t offset, int whence) {
if (fd < 0 || fd >= MAX_FDS) {
errno = EBADF;
return (off_t)-1;
}
fd_info *info = &fd_table[fd];
if (!info->valid) {
errno = EBADF;
return (off_t)-1;
}
int file_size = info->file->size;
off_t new_offset;
switch (whence) {
case SEEK_SET:
new_offset = offset;
break;
case SEEK_CUR:
new_offset = info->offset + offset;
break;
case SEEK_END:
new_offset = file_size + offset;
break;
default:
errno = EINVAL;
return (off_t)-1;
}
if (new_offset < 0 || new_offset > file_size) {
errno = EINVAL;
return (off_t)-1;
}
info->offset = new_offset;
return info->offset;
}
/* ===================================== */
/* =========== Miscellaneous =========== */
/* ===================================== */
int munmap(void *addr, size_t length) {
return 0;
}
void _exit(int code) {
const char msg[] = "exit\n";
_write(STDOUT_FD, msg, sizeof(msg) - 1);
// We don't have arbitrary control flow, so this is really the only thing you can do.
while (1) {}
}
// wasmcraft doesn't support doubles anyway; this is just to make the linker happy.
long double __extenddftf2(double a) { return 0.0; }
double __trunctfdf2(long double a) { return 0.0; }
int mkdir(const char *pathname, mode_t mode) {
const char msg[] = "ignoring mkdir\n";
_write(STDOUT_FD, msg, sizeof(msg) - 1);
errno = ENOSYS;
return -1;
}
int _fstat(int fd, struct stat *statbuf) {
if (fd < 0 || fd >= MAX_FDS) {
errno = EBADF;
return -1;
}
fd_info *info = &fd_table[fd];
if (!info->valid) {
errno = EBADF;
return -1;
}
int size = info->file->size;
//statbuf->st_mode = ???
statbuf->st_nlink = 1;
statbuf->st_size = size;
statbuf->st_blksize = 512;
statbuf->st_blocks = size / (512 * 1024);
return 0;
}
int raise(int sig) {
const char msg[] = "signal raised\n";
_write(STDOUT_FD, msg, sizeof(msg) - 1);
// TODO:
while (1) {}
}