-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathwasi-wrapper-linux.c
90 lines (73 loc) · 2.08 KB
/
wasi-wrapper-linux.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
#include <stdio.h>
#include <stdlib.h>
extern void wasker_main();
long linear_memory_base;
int linear_memory_block_num = 0;
const int LINEAR_MEMORY_BLOCK_SIZE = 64 * 1024;
const int LINEAR_MEMORY_BLOCK_NUM_MAX = 32;
//////////////////////////////////////////////
/// Wrapper of following WASI functions
/// - fd_write
/// - environ_get
/// - environ_sizes_get
/// - proc_exit
//////////////////////////////////////////////
typedef struct {
int iov_base;
int iov_len;
} IoVec;
typedef enum {
SUCCESS,
// Add other error types here
} WasiError;
WasiError fd_write(int fd, int buf_iovec_addr, int vec_len, int size_addr) {
char* iovec_ptr = (char *)(linear_memory_base + buf_iovec_addr);
IoVec* iovec = (IoVec*)iovec_ptr;
int len = 0;
for (int i = 0; i < vec_len; i++){
char* buf_ptr = (char *)(linear_memory_base + iovec[i].iov_base);
size_t buf_len = iovec[i].iov_len;
for (int j = 0; j < buf_len; j++){
printf("%c", buf_ptr[j]);
}
len += buf_len;
}
int* size_ptr = (int *)(linear_memory_base + size_addr);
*size_ptr = len;
return SUCCESS;
}
WasiError environ_get(int env_addrs, int env_buf_addr){
// TODO: implement
return SUCCESS;
}
WasiError environ_sizes_get(int env_count_addr, int env_buf_size_addr){
// TODO: implement
return SUCCESS;
}
void proc_exit(int code){
exit(code);
}
//////////////////////////////////////////////
/// Provide linear memory with Wasm
//////////////////////////////////////////////
long memory_base(){
// malloc 32 Block for linear memory
char* memory = (char*)malloc(LINEAR_MEMORY_BLOCK_SIZE * LINEAR_MEMORY_BLOCK_NUM_MAX);
linear_memory_base = (long)memory;
return linear_memory_base;
}
long memory_grow(long num){
int old = linear_memory_block_num;
// check if there are enough memory
if (LINEAR_MEMORY_BLOCK_NUM_MAX < linear_memory_block_num + num) {
printf("memory_grow: failed to grow memory\n");
return -1;
}
linear_memory_block_num += num;
return old;
}
int main() {
// Entrypoint of ELF generated by Wasker
wasker_main();
return 0;
}