Skip to content

Commit 6949282

Browse files
Changes post test runs.
1 parent 3dd5bc7 commit 6949282

File tree

4 files changed

+170
-12
lines changed

4 files changed

+170
-12
lines changed

src/glibc/lind_syscall/addr_translation.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
#include <stddef.h>
66
#include <errno.h>
77

8+
// When we want the argument to be translated, the cageid supplied must have the MSB set to 1.
9+
// These masks help check this.
10+
#define LIND_ARG_TRANSLATE_FLAG (1ULL << 63)
11+
#define LIND_ARG_CAGEID_MASK (~LIND_ARG_TRANSLATE_FLAG)
12+
813
#ifdef __cplusplus
914
extern "C"
1015
{
@@ -59,12 +64,16 @@ extern "C"
5964

6065
// Converts (uaddr, cageid) pair to host address.
6166
// Useful when address space (cage vs host) is ambigious.
67+
//
68+
// This is called by copy data where the arguments are already addresses
69+
// so we implicitly update the cageid argument before passing it to the helper.
6270
#define TRANSLATE_UADDR_TO_HOST(uaddr, cageid) \
63-
__lind_translate_uaddr_to_host ((uaddr), (cageid))
71+
__lind_translate_uaddr_to_host ((uaddr), (cageid | LIND_ARG_TRANSLATE_FLAG)), (cageid)
6472

65-
// Per-argument translation signaling for threei calls.
66-
#define LIND_ARG_TRANSLATE_FLAG (1ULL << 63)
67-
#define LIND_ARG_CAGEID_MASK (~LIND_ARG_TRANSLATE_FLAG)
73+
// This is used by make_threei_call, we do not modify the flag before checking if translation
74+
// is needed. We do modify the cage on output so that other threei/lind calls see a correct cageid
75+
#define TRANSLATE_ARG_TO_HOST(uaddr, cageid) \
76+
__lind_translate_uaddr_to_host ((uaddr), (cageid)), (cageid & LIND_ARG_CAGEID_MASK)
6877

6978
/* Translate an array of guest iovec structures to host layout.
7079
Each iov_base is a wasm32 guest pointer; we split the translated

src/glibc/lind_syscall/lind_syscall.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ int make_threei_call (unsigned int callnumber,
8080
int ret = __lind_make_syscall_trampoline(callnumber,
8181
callname,
8282
self_cageid, target_cageid,
83-
TRANSLATE_UADDR_TO_HOST(arg1, arg1cageid), arg1cageid,
84-
TRANSLATE_UADDR_TO_HOST(arg1, arg1cageid), arg1cageid,
85-
TRANSLATE_UADDR_TO_HOST(arg1, arg1cageid), arg1cageid,
86-
TRANSLATE_UADDR_TO_HOST(arg1, arg1cageid), arg1cageid,
87-
TRANSLATE_UADDR_TO_HOST(arg1, arg1cageid), arg1cageid,
88-
TRANSLATE_UADDR_TO_HOST(arg1, arg1cageid), arg1cageid);
83+
TRANSLATE_ARG_TO_HOST(arg1, arg1cageid),
84+
TRANSLATE_ARG_TO_HOST(arg2, arg2cageid),
85+
TRANSLATE_ARG_TO_HOST(arg3, arg3cageid),
86+
TRANSLATE_ARG_TO_HOST(arg4, arg4cageid),
87+
TRANSLATE_ARG_TO_HOST(arg5, arg5cageid),
88+
TRANSLATE_ARG_TO_HOST(arg6, arg6cageid));
8989

9090
// if translate_errno is not enabled, we do not do any further process to errno handling and directly return the result
9191
if(translate_errno == 0) return ret;
@@ -155,8 +155,8 @@ int copy_data_between_cages(uint64_t thiscage, uint64_t targetcage, uint64_t src
155155
0, // callname is not used in the trampoline, set to 0
156156
thiscage, // self_cageid
157157
thiscage, // target_cageid. Self_cageid and target_cageid are the same to adapt with regular make_syscall lookup logic in 3i
158-
TRANSLATE_UADDR_TO_HOST(srcaddr, srccage), srccage,
159-
TRANSLATE_UADDR_TO_HOST(destaddr, destcage), destcage,
158+
TRANSLATE_UADDR_TO_HOST(srcaddr, srccage),
159+
TRANSLATE_UADDR_TO_HOST(destaddr, destcage),
160160
len, 0,
161161
copytype, 0,
162162
0, 0, 0, 0,

tests/grate-tests/diff-cage-args.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <stdio.h>
2+
#include <unistd.h>
3+
#include <fcntl.h>
4+
5+
int main() {
6+
int fd = open("redirected.txt", O_RDONLY, 0);
7+
printf("Hello world. FD=%d\n", fd);
8+
9+
char buf[10];
10+
int ret = read(1, buf, 10);
11+
12+
printf("Goodbye world! ret=%d buf=%s\n", ret, buf);
13+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
#include <errno.h>
2+
#include <lind_syscall.h>
3+
4+
#include <assert.h>
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
#include <sys/types.h>
8+
#include <sys/wait.h>
9+
#include <unistd.h>
10+
11+
// Dispatcher function
12+
int pass_fptr_to_wt(uint64_t fn_ptr_uint, uint64_t cageid, uint64_t arg1,
13+
uint64_t arg1cage, uint64_t arg2, uint64_t arg2cage,
14+
uint64_t arg3, uint64_t arg3cage, uint64_t arg4,
15+
uint64_t arg4cage, uint64_t arg5, uint64_t arg5cage,
16+
uint64_t arg6, uint64_t arg6cage) {
17+
if (fn_ptr_uint == 0) {
18+
fprintf(stderr,
19+
"[Grate|interpose-exec] Invalid function ptr\n");
20+
assert(0);
21+
}
22+
23+
printf("[Grate|interpose-exec] Handling function ptr: %llu from cage: "
24+
"%llu\n",
25+
fn_ptr_uint, cageid);
26+
27+
int (*fn)(uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t,
28+
uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t,
29+
uint64_t) =
30+
(int (*)(uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t,
31+
uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t,
32+
uint64_t))(uintptr_t)fn_ptr_uint;
33+
34+
return fn(cageid, arg1, arg1cage, arg2, arg2cage, arg3, arg3cage, arg4,
35+
arg4cage, arg5, arg5cage, arg6, arg6cage);
36+
}
37+
38+
int read_grate(uint64_t grateid, uint64_t arg1, uint64_t arg1cage,
39+
uint64_t arg2, uint64_t arg2cage, uint64_t arg3,
40+
uint64_t arg3cage, uint64_t arg4, uint64_t arg4cage,
41+
uint64_t arg5, uint64_t arg5cage, uint64_t arg6,
42+
uint64_t arg6cage) {
43+
int thiscage = getpid();
44+
int cageid = arg1cage;
45+
46+
int fd = (int)arg1;
47+
int count = (size_t)arg3;
48+
49+
ssize_t ret = 4321;
50+
51+
char buf[11] = "helloworld";
52+
53+
copy_data_between_cages(thiscage, arg2cage, (uint64_t)buf, thiscage,
54+
arg2, arg2cage, count,
55+
0 // Use copytype 0 so read exactly count
56+
// bytes instead of stopping at '\0'
57+
);
58+
59+
return ret;
60+
}
61+
62+
int open_grate(uint64_t cageid, uint64_t arg1, uint64_t arg1cage, uint64_t arg2,
63+
uint64_t arg2cage, uint64_t arg3, uint64_t arg3cage,
64+
uint64_t arg4, uint64_t arg4cage, uint64_t arg5,
65+
uint64_t arg5cage, uint64_t arg6, uint64_t arg6cage) {
66+
printf(
67+
"[Grate|interpose-exec] In exec_grate %d handler for cage: %llu\n",
68+
getpid(), cageid);
69+
70+
int self_grate_id = getpid();
71+
72+
// Overwrite the path supplied to open with a different path.
73+
char new_path[20] = "/tmp/redirected.txt";
74+
75+
int ret = make_threei_call(
76+
2, 0, self_grate_id, arg1cage,
77+
// We need to modify the cageid here to indicate that we want the
78+
// address translated.
79+
(uint64_t)&new_path, self_grate_id | (1ULL << 63), arg2, arg2cage,
80+
arg3, arg3cage, arg4, arg4cage, arg5, arg5cage, arg6, arg6cage,
81+
0 // we will handle the errno in this grate instead of translating
82+
// it to
83+
);
84+
85+
return ret;
86+
}
87+
88+
// Main function will always be same in all grates
89+
int main(int argc, char *argv[]) {
90+
// Should be at least one input (at least one grate file and one cage
91+
// file)
92+
if (argc < 2) {
93+
fprintf(stderr, "Usage: %s <cage_file> <grate_file>\n",
94+
argv[0]);
95+
assert(0);
96+
}
97+
98+
int grateid = getpid();
99+
100+
pid_t pid = fork();
101+
if (pid < 0) {
102+
perror("fork failed");
103+
assert(0);
104+
} else if (pid == 0) {
105+
int cageid = getpid();
106+
107+
// This is to test whether we can use arg, argcage from
108+
// different cages.
109+
uint64_t fn_ptr_addr = (uint64_t)(uintptr_t)&open_grate;
110+
int ret = register_handler(cageid, 2, grateid, fn_ptr_addr);
111+
112+
// This is to check copy_data for regression.
113+
fn_ptr_addr = (uint64_t)(uintptr_t)&read_grate;
114+
ret = register_handler(cageid, 0, grateid, fn_ptr_addr);
115+
116+
if (execv(argv[1], &argv[1]) == -1) {
117+
perror("execv failed");
118+
assert(0);
119+
}
120+
}
121+
122+
int status;
123+
int failed = 0;
124+
while (wait(&status) > 0) {
125+
if (status != 0) {
126+
fprintf(stderr,
127+
"[Grate|interpose-exec] FAIL: child exited "
128+
"with status %d\n",
129+
status);
130+
assert(0);
131+
}
132+
}
133+
134+
printf("[Grate|interpose-exec] PASS\n");
135+
return 0;
136+
}

0 commit comments

Comments
 (0)