Skip to content

Commit 086224d

Browse files
Replace hardcoded numbers in codebase with named constants (Rust + C) (#855)
* replace hardcoded numbers in codebase with named constants * replace padding 0s with NOTUSED/UNUSED_ID/UNUSED_ARG
1 parent 89ef9bd commit 086224d

File tree

5 files changed

+93
-74
lines changed

5 files changed

+93
-74
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* lind_constants.h
3+
*
4+
* Named constants for the Lind syscall layer.
5+
*/
6+
7+
#ifndef _LIND_CONSTANTS_H
8+
#define _LIND_CONSTANTS_H
9+
10+
/* Define NOTUSED for unused arguments */
11+
#define NOTUSED 0xdeadbeefdeadbeefULL
12+
13+
/* Define flags for errno translation
14+
* See comments in lind_syscall/lind_syscall.c for details */
15+
#define TRANSLATE_ERRNO_ON 1
16+
#define TRANSLATE_ERRNO_OFF 0
17+
18+
/* Upper bound (exclusive) of valid errno values.
19+
* Return values in the range (-MAX_ERRNO, 0) are treated as -errno
20+
* by make_threei_call() when TRANSLATE_ERRNO_ON is active. */
21+
#define MAX_ERRNO 256
22+
23+
#endif /* _LIND_CONSTANTS_H */

src/glibc/lind_syscall/lind_syscall.c

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <stdint.h> // For uint64_t definition
33
#include "addr_translation.h"
44
#include "lind_syscall_num.h"
5+
#include "lind_constants.h"
56

67
// Entry point for wasmtime, lind_syscall is an imported function from wasmtime
78
int __lind_make_syscall_trampoline(unsigned int callnumber,
@@ -87,7 +88,7 @@ int make_threei_call (unsigned int callnumber,
8788
arg5, arg5cageid,
8889
arg6, arg6cageid);
8990
// if translate_errno is not enabled, we do not do any further process to errno handling and directly return the result
90-
if(translate_errno == 0) return ret;
91+
if(translate_errno == TRANSLATE_ERRNO_OFF) return ret;
9192
// handle the errno
9293
// in rawposix, we use -errno as the return value to indicate the error
9394
// but this may cause some issues for mmap syscall, because mmap syscall
@@ -96,7 +97,7 @@ int make_threei_call (unsigned int callnumber,
9697
// multiple of pages (typically 4096) even when overflow, therefore we can distinguish
9798
// the errno and mmap result by simply checking if the return value is
9899
// within the valid errno range
99-
if(ret < 0 && ret > -256)
100+
if(ret < 0 && ret > -MAX_ERRNO)
100101
{
101102
errno = -ret;
102103
return -1;
@@ -123,16 +124,16 @@ int register_handler (int64_t targetcage,
123124
{
124125
return make_threei_call(
125126
REGISTER_HANDLER_SYSCALL,
126-
0, // callname is not used in the trampoline, set to 0
127+
NOTUSED, // callname is not used in the trampoline
127128
targetcage, // pass targetcage as self_cageid
128129
targetcage, // pass targetcage as target_cageid. Self_cageid and target_cageid are the same to adapt with regular make_syscall lookup logic in 3i
129130
targetcage,
130131
targetcallnum,
131-
0, // runtime_id currently not used, set to 0
132+
NOTUSED, // runtime_id currently not used
132133
this_grate_id, // handlefunccage is the grate id of the handler function, which is the same as this_grate_id
133134
in_grate_fn_ptr_u64,
134-
0, 0, 0, 0, 0, 0, 0,
135-
0 /* translate_errno=0: we want to return the raw result without errno translation */
135+
NOTUSED, NOTUSED, NOTUSED, NOTUSED, NOTUSED, NOTUSED, NOTUSED,
136+
TRANSLATE_ERRNO_OFF /* do not translate errno: return the raw result */
136137
);
137138
}
138139

@@ -151,15 +152,15 @@ int copy_data_between_cages(uint64_t thiscage, uint64_t targetcage, uint64_t src
151152
{
152153
return make_threei_call(
153154
COPY_DATA_BETWEEN_CAGES_SYSCALL,
154-
0, // callname is not used in the trampoline, set to 0
155+
NOTUSED, // callname is not used in the trampoline
155156
thiscage, // self_cageid
156157
thiscage, // target_cageid. Self_cageid and target_cageid are the same to adapt with regular make_syscall lookup logic in 3i
157158
TRANSLATE_UADDR_TO_HOST(srcaddr, srccage), srccage,
158159
TRANSLATE_UADDR_TO_HOST(destaddr, destcage), destcage,
159-
len, 0,
160-
copytype, 0,
161-
0, 0, 0, 0,
162-
0 /* translate_errno=0: we want to return the raw result without errno translation */
160+
len, NOTUSED,
161+
copytype, NOTUSED,
162+
NOTUSED, NOTUSED, NOTUSED, NOTUSED,
163+
TRANSLATE_ERRNO_OFF /* do not translate errno: return the raw result */
163164
);
164165
}
165166

@@ -173,16 +174,16 @@ int copy_handler_table_to_cage(uint64_t thiscage, uint64_t targetcage)
173174
{
174175
return make_threei_call(
175176
COPY_HANDLER_TABLE_TO_CAGE_SYSCALL,
176-
0, // callname is not used in the trampoline, set to 0
177+
NOTUSED, // callname is not used in the trampoline
177178
thiscage, // self_cageid
178179
thiscage, // target_cageid. Self_cageid and target_cageid are the same to adapt with regular make_syscall lookup logic in 3i
179180
thiscage,
180181
targetcage,
181-
0, 0,
182-
0, 0,
183-
0, 0,
184-
0, 0,
185-
0, 0,
186-
0 /* translate_errno=0: we want to return the raw result without errno translation */
182+
NOTUSED, NOTUSED,
183+
NOTUSED, NOTUSED,
184+
NOTUSED, NOTUSED,
185+
NOTUSED, NOTUSED,
186+
NOTUSED, NOTUSED,
187+
TRANSLATE_ERRNO_OFF /* do not translate errno: return the raw result */
187188
);
188189
}

src/glibc/sysdeps/unix/syscall-template.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,7 @@
33
#include <unistd.h>
44
#include <lind_syscall.h>
55
#include <addr_translation.h>
6-
7-
// Define NOTUSED for unused arguments
8-
#define NOTUSED 0xdeadbeefdeadbeefULL
9-
10-
// Define flags for errno translation
11-
// See comments in [`lind_syscall/lind_syscall.c`] for details
12-
#define TRANSLATE_ERRNO_ON 1
13-
#define TRANSLATE_ERRNO_OFF 0
6+
#include <lind_constants.h>
147

158
/*
169
* MAKE_LEGACY_SYSCALL:

src/rawposix/src/init.rs

Lines changed: 46 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use std::path::PathBuf;
1010
use std::sync::atomic::{AtomicI32, AtomicU64, Ordering::*};
1111
use std::sync::Arc;
1212
use sysdefs::constants::{
13-
EXIT_SUCCESS, FDKIND_KERNEL, RAWPOSIX_CAGEID, STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO,
14-
THREEI_CAGEID, VERBOSE,
13+
EXIT_SUCCESS, FDKIND_KERNEL, INIT_CAGEID, MAIN_THREADID, RAWPOSIX_CAGEID, STDERR_FILENO,
14+
STDIN_FILENO, STDOUT_FILENO, THREEI_CAGEID, UNUSED_ARG, UNUSED_ID, VERBOSE,
1515
};
1616
use threei::{
1717
copy_data_between_cages, copy_handler_table_to_cage, register_handler,
@@ -65,20 +65,20 @@ pub fn register_rawposix_syscall(self_cageid: u64) -> i32 {
6565
let impl_fn_ptr = func as *const () as u64;
6666
// Register to handler table in 3i
6767
ret = register_handler(
68-
0,
68+
UNUSED_ID,
6969
RAWPOSIX_CAGEID, // target cageid for this syscall handler
7070
self_cageid, // cage to modify: current cageid
7171
sysno, // target callnum
7272
RUNTIME_TYPE_WASMTIME, // runtime id
7373
RAWPOSIX_CAGEID, // handler function is in the RawPOSIX
7474
impl_fn_ptr,
75-
0,
76-
0,
77-
0,
78-
0,
79-
0,
80-
0,
81-
0,
75+
UNUSED_ID,
76+
UNUSED_ARG,
77+
UNUSED_ID,
78+
UNUSED_ARG,
79+
UNUSED_ID,
80+
UNUSED_ARG,
81+
UNUSED_ID,
8282
);
8383
if ret != 0 {
8484
panic!(
@@ -116,58 +116,58 @@ pub fn register_threei_syscall(self_cageid: u64) -> i32 {
116116
// Register `register_handler` syscall for this cage
117117
let fp_register = register_handler as *const () as usize as u64;
118118
let register_ret = register_handler(
119-
0,
119+
UNUSED_ID,
120120
THREEI_CAGEID, // target cageid for this syscall handler
121121
self_cageid, // cage to modify: current cageid
122122
REGISTER_HANDLER_SYSCALL,
123123
RUNTIME_TYPE_WASMTIME, // runtime id
124124
THREEI_CAGEID, // handler function is in the 3i
125125
fp_register,
126-
0,
127-
0,
128-
0,
129-
0,
130-
0,
131-
0,
132-
0,
126+
UNUSED_ID,
127+
UNUSED_ARG,
128+
UNUSED_ID,
129+
UNUSED_ARG,
130+
UNUSED_ID,
131+
UNUSED_ARG,
132+
UNUSED_ID,
133133
);
134134

135135
// Register `copy_data_between_cages` syscall for this cage
136136
let fp_copy_data = copy_data_between_cages as *const () as usize as u64;
137137
let copy_data_ret = register_handler(
138-
0,
138+
UNUSED_ID,
139139
THREEI_CAGEID, // target cageid for this syscall handler
140140
self_cageid, // cage to modify: current cageid
141141
COPY_DATA_BETWEEN_CAGES_SYSCALL,
142142
RUNTIME_TYPE_WASMTIME, // runtime id
143143
THREEI_CAGEID, // handler function is in the 3i
144144
fp_copy_data,
145-
0,
146-
0,
147-
0,
148-
0,
149-
0,
150-
0,
151-
0,
145+
UNUSED_ID,
146+
UNUSED_ARG,
147+
UNUSED_ID,
148+
UNUSED_ARG,
149+
UNUSED_ID,
150+
UNUSED_ARG,
151+
UNUSED_ID,
152152
);
153153

154154
// Register `copy_handler_table_to_cage` syscall for this cage
155155
let fp_copy_handler_table = copy_handler_table_to_cage as *const () as usize as u64;
156156
let copy_handler_table_ret = register_handler(
157-
0,
157+
UNUSED_ID,
158158
THREEI_CAGEID, // target cageid for this syscall handler
159159
self_cageid, // cage to modify: current cageid
160160
COPY_HANDLER_TABLE_TO_CAGE_SYSCALL,
161161
RUNTIME_TYPE_WASMTIME, // runtime id
162162
THREEI_CAGEID, // handler function is in the 3i
163163
fp_copy_handler_table,
164-
0,
165-
0,
166-
0,
167-
0,
168-
0,
169-
0,
170-
0,
164+
UNUSED_ID,
165+
UNUSED_ARG,
166+
UNUSED_ID,
167+
UNUSED_ARG,
168+
UNUSED_ID,
169+
UNUSED_ARG,
170+
UNUSED_ID,
171171
);
172172

173173
// Check registration results and panic if either fails
@@ -208,13 +208,11 @@ pub fn rawposix_start(verbosity: isize) {
208208
fdtables::register_close_handlers(FDKIND_KERNEL, fdtables::NULL_FUNC, kernel_close);
209209

210210
// register syscalls for init cage
211-
register_rawposix_syscall(1);
211+
register_rawposix_syscall(INIT_CAGEID);
212212

213-
register_threei_syscall(1);
213+
register_threei_syscall(INIT_CAGEID);
214214

215215
// Set up standard file descriptors for the init cage
216-
// TODO:
217-
// Replace the hardcoded values with variables (possibly by adding a LIND-specific constants file)
218216
let dev_null = CString::new("/dev/null").unwrap();
219217

220218
// Make sure that the standard file descriptors (stdin, stdout, stderr) are always valid
@@ -229,12 +227,12 @@ pub fn rawposix_start(verbosity: isize) {
229227

230228
//init cage is its own parent
231229
let initcage = Cage {
232-
cageid: 1,
230+
cageid: INIT_CAGEID,
233231
cwd: RwLock::new(Arc::new(PathBuf::from("/"))),
234-
parent: 1,
232+
parent: INIT_CAGEID,
235233
rev_shm: Mutex::new(Vec::new()),
236234
main_threadid: RwLock::new(0),
237-
interval_timer: IntervalTimer::new(1),
235+
interval_timer: IntervalTimer::new(INIT_CAGEID),
238236
epoch_handler: DashMap::new(),
239237
signalhandler: DashMap::new(),
240238
pending_signals: RwLock::new(vec![]),
@@ -246,16 +244,16 @@ pub fn rawposix_start(verbosity: isize) {
246244

247245
// Add cage to cagetable
248246
add_cage(
249-
1, // cageid
247+
INIT_CAGEID, // cageid
250248
initcage,
251249
);
252250

253-
// init fdtables for cageid 1
254-
fdtables::init_empty_cage(1);
251+
// init fdtables for init cage
252+
fdtables::init_empty_cage(INIT_CAGEID);
255253
// Set the first 3 fd to STDIN / STDOUT / STDERR
256254
// STDIN
257255
fdtables::get_specific_virtual_fd(
258-
1,
256+
INIT_CAGEID,
259257
STDIN_FILENO as u64,
260258
FDKIND_KERNEL,
261259
STDIN_FILENO as u64,
@@ -265,7 +263,7 @@ pub fn rawposix_start(verbosity: isize) {
265263
.unwrap();
266264
// STDOUT
267265
fdtables::get_specific_virtual_fd(
268-
1,
266+
INIT_CAGEID,
269267
STDOUT_FILENO as u64,
270268
FDKIND_KERNEL,
271269
STDOUT_FILENO as u64,
@@ -275,7 +273,7 @@ pub fn rawposix_start(verbosity: isize) {
275273
.unwrap();
276274
// STDERR
277275
fdtables::get_specific_virtual_fd(
278-
1,
276+
INIT_CAGEID,
279277
STDERR_FILENO as u64,
280278
FDKIND_KERNEL,
281279
STDERR_FILENO as u64,
@@ -301,7 +299,7 @@ pub fn rawposix_shutdown() {
301299
cageid as u64, // target cageid
302300
EXIT_SUCCESS as u64, // status arg
303301
cageid as u64, // status arg's cageid
304-
1, // always main thread
302+
MAIN_THREADID, // always main thread
305303
0,
306304
0,
307305
0,

src/sysdefs/src/constants/lind_platform_const.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,7 @@ pub const WASMTIME_CAGEID: u64 = 888888;
8282
/// the call through its internal control-layer logic rather than
8383
/// forwarding it to RawPOSIX or Wasmtime.
8484
pub const THREEI_CAGEID: u64 = 999999;
85+
/// Cage ID for the initial (bootstrap) cage created during `rawposix_start`.
86+
pub const INIT_CAGEID: u64 = 1;
87+
/// Thread ID for the main thread of a cage.
88+
pub const MAIN_THREADID: u64 = 1;

0 commit comments

Comments
 (0)