Skip to content
This repository was archived by the owner on Mar 24, 2022. It is now read-only.

Commit 6e4a0b6

Browse files
committedMar 29, 2019
[lucet-wasi] Add CLI configuration for memory limits
Closes #74 and #76
1 parent a5f1583 commit 6e4a0b6

File tree

4 files changed

+77
-3
lines changed

4 files changed

+77
-3
lines changed
 

‎Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lucet-runtime/lucet-runtime-internals/src/module.rs

+7
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,13 @@ pub trait ModuleInternal: Send + Sync {
164164
bail_limits_exceeded!("heap spec initial size: {:?}", heap);
165165
}
166166

167+
if heap.initial_size > heap.reserved_size {
168+
return Err(lucet_incorrect_module!(
169+
"initial heap size greater than reserved size: {:?}",
170+
heap
171+
));
172+
}
173+
167174
if self.globals().len() * std::mem::size_of::<u64>() > limits.globals_size {
168175
bail_limits_exceeded!("globals exceed limits");
169176
}

‎lucet-wasi/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ license = "Apache-2.0 WITH LLVM-exception"
99
cast = "0.2"
1010
clap = "2.23"
1111
failure = "0.1"
12+
human-size = "0.4"
1213
libc = "0.2"
1314
lucet-runtime = { path = "../lucet-runtime" }
1415
lucet-runtime-internals = { path = "../lucet-runtime/lucet-runtime-internals" }

‎lucet-wasi/src/main.rs

+68-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
extern crate clap;
33

44
use clap::Arg;
5+
use human_size::{Byte, Size};
56
use lucet_runtime::{self, DlModule, Limits, MmapRegion, Module, Region};
67
use lucet_wasi::{hostcalls, WasiCtxBuilder};
78
use std::fs::File;
@@ -12,6 +13,7 @@ struct Config<'a> {
1213
guest_args: Vec<&'a str>,
1314
entrypoint: &'a str,
1415
preopen_dirs: Vec<(File, &'a str)>,
16+
limits: Limits,
1517
}
1618

1719
fn main() {
@@ -41,10 +43,10 @@ fn main() {
4143
virtual filesystem. Each directory is specified as \
4244
--dir `host_path:guest_path`, where `guest_path` specifies the path that will \
4345
correspond to `host_path` for calls like `fopen` in the guest.\
44-
\
46+
\n\n\
4547
For example, `--dir /home/host_user/wasi_sandbox:/sandbox` will make \
4648
`/home/host_user/wasi_sandbox` available within the guest as `/sandbox`.\
47-
\
49+
\n\n\
4850
Guests will be able to access any files and directories under the \
4951
`host_path`, but will be unable to access other parts of the host \
5052
filesystem through relative paths (e.g., `/sandbox/../some_other_file`) \
@@ -56,6 +58,39 @@ fn main() {
5658
.required(true)
5759
.help("Path to the `lucetc`-compiled WASI module"),
5860
)
61+
.arg(
62+
Arg::with_name("heap_memory_size")
63+
.long("max-heap-size")
64+
.takes_value(true)
65+
.default_value("1024 KiB")
66+
.help("Maximum heap size (must be a multiple of 4 KiB)"),
67+
)
68+
.arg(
69+
Arg::with_name("heap_address_space_size")
70+
.long("heap-address-space")
71+
.takes_value(true)
72+
.default_value("8 GiB")
73+
.help("Maximum heap address space size (must be a multiple of 4 KiB)")
74+
.long_help(
75+
"Maximum heap address space size. Must be a multiple of 4KiB, and \
76+
must be least as large as `max-heap-size`, `stack-size`, and \
77+
`globals-size`, combined.",
78+
),
79+
)
80+
.arg(
81+
Arg::with_name("stack_size")
82+
.long("stack-size")
83+
.takes_value(true)
84+
.default_value("128 KiB")
85+
.help("Maximum stack size (must be a multiple of 4 KiB)"),
86+
)
87+
.arg(
88+
Arg::with_name("globals_size")
89+
.long("globals-size")
90+
.takes_value(true)
91+
.default_value("4 KiB")
92+
.help("Maximum globals size (must be a multiple of 4 KiB)"),
93+
)
5994
.arg(
6095
Arg::with_name("guest_args")
6196
.required(false)
@@ -65,7 +100,9 @@ fn main() {
65100
.get_matches();
66101

67102
let entrypoint = matches.value_of("entrypoint").unwrap();
103+
68104
let lucet_module = matches.value_of("lucet_module").unwrap();
105+
69106
let preopen_dirs = matches
70107
.values_of("preopen_dirs")
71108
.map(|vals| {
@@ -84,24 +121,52 @@ fn main() {
84121
.collect()
85122
})
86123
.unwrap_or(vec![]);
124+
125+
let heap_memory_size = value_t!(matches, "heap_memory_size", Size)
126+
.unwrap_or_else(|e| e.exit())
127+
.into::<Byte>()
128+
.value() as usize;
129+
let heap_address_space_size = value_t!(matches, "heap_address_space_size", Size)
130+
.unwrap_or_else(|e| e.exit())
131+
.into::<Byte>()
132+
.value() as usize;
133+
let stack_size = value_t!(matches, "stack_size", Size)
134+
.unwrap_or_else(|e| e.exit())
135+
.into::<Byte>()
136+
.value() as usize;
137+
let globals_size = value_t!(matches, "globals_size", Size)
138+
.unwrap_or_else(|e| e.exit())
139+
.into::<Byte>()
140+
.value() as usize;
141+
142+
let limits = Limits {
143+
heap_memory_size,
144+
heap_address_space_size,
145+
stack_size,
146+
globals_size,
147+
};
148+
87149
let guest_args = matches
88150
.values_of("guest_args")
89151
.map(|vals| vals.collect())
90152
.unwrap_or(vec![]);
153+
91154
let config = Config {
92155
lucet_module,
93156
guest_args,
94157
entrypoint,
95158
preopen_dirs,
159+
limits,
96160
};
161+
97162
run(config)
98163
}
99164

100165
fn run(config: Config) {
101166
lucet_wasi::hostcalls::ensure_linked();
102167
let exitcode = {
103168
// doing all of this in a block makes sure everything gets dropped before exiting
104-
let region = MmapRegion::create(1, &Limits::default()).expect("region can be created");
169+
let region = MmapRegion::create(1, &config.limits).expect("region can be created");
105170
let module = DlModule::load(&config.lucet_module).expect("module can be loaded");
106171

107172
// put the path to the module on the front for argv[0]

0 commit comments

Comments
 (0)