Skip to content

Commit 78311a7

Browse files
committed
Add function to error with enabled isolation
1 parent 0d01c30 commit 78311a7

File tree

4 files changed

+19
-30
lines changed

4 files changed

+19
-30
lines changed

src/helpers.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,16 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
336336
)?;
337337
offset += imm.layout.size;
338338
}
339+
Ok(())
340+
}
339341

342+
/// Helper function used inside the shims of foreign functions to check that isolation is
343+
/// disabled. It returns an error using the `name` of the foreign function if this is not the
344+
/// case.
345+
fn check_no_isolation(&mut self, name: &str) -> InterpResult<'tcx> {
346+
if !self.eval_context_mut().machine.communicate {
347+
throw_unsup_format!("`{}` not available when isolation is enabled. Pass the flag `-Zmiri-disable-isolation` to disable it.", name)
348+
}
340349
Ok(())
341350
}
342351
}

src/shims/env.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
120120
) -> InterpResult<'tcx, Scalar<Tag>> {
121121
let this = self.eval_context_mut();
122122

123-
if !this.machine.communicate {
124-
throw_unsup_format!("`getcwd` not available when isolation is enabled")
125-
}
123+
this.check_no_isolation("getcwd")?;
126124

127125
let tcx = &{ this.tcx.tcx };
128126

@@ -158,9 +156,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
158156
fn chdir(&mut self, path_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
159157
let this = self.eval_context_mut();
160158

161-
if !this.machine.communicate {
162-
throw_unsup_format!("`chdir` not available when isolation is enabled")
163-
}
159+
this.check_no_isolation("chdir")?;
164160

165161
let path_bytes = this
166162
.memory()

src/shims/fs.rs

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
3636
) -> InterpResult<'tcx, i32> {
3737
let this = self.eval_context_mut();
3838

39-
if !this.machine.communicate {
40-
throw_unsup_format!("`open` not available when isolation is enabled")
41-
}
39+
this.check_no_isolation("open")?;
4240

4341
let flag = this.read_scalar(flag_op)?.to_i32()?;
4442

@@ -91,9 +89,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
9189
) -> InterpResult<'tcx, i32> {
9290
let this = self.eval_context_mut();
9391

94-
if !this.machine.communicate {
95-
throw_unsup_format!("`fcntl` not available when isolation is enabled")
96-
}
92+
this.check_no_isolation("fcntl")?;
9793

9894
let fd = this.read_scalar(fd_op)?.to_i32()?;
9995
let cmd = this.read_scalar(cmd_op)?.to_i32()?;
@@ -126,9 +122,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
126122
fn close(&mut self, fd_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
127123
let this = self.eval_context_mut();
128124

129-
if !this.machine.communicate {
130-
throw_unsup_format!("`close` not available when isolation is enabled")
131-
}
125+
this.check_no_isolation("close")?;
132126

133127
let fd = this.read_scalar(fd_op)?.to_i32()?;
134128

@@ -145,9 +139,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
145139
) -> InterpResult<'tcx, i64> {
146140
let this = self.eval_context_mut();
147141

148-
if !this.machine.communicate {
149-
throw_unsup_format!("`read` not available when isolation is enabled")
150-
}
142+
this.check_no_isolation("read")?;
151143

152144
let tcx = &{ this.tcx.tcx };
153145

@@ -182,9 +174,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
182174
) -> InterpResult<'tcx, i64> {
183175
let this = self.eval_context_mut();
184176

185-
if !this.machine.communicate {
186-
throw_unsup_format!("`write` not available when isolation is enabled")
187-
}
177+
this.check_no_isolation("write")?;
188178

189179
let tcx = &{ this.tcx.tcx };
190180

@@ -210,9 +200,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
210200
fn unlink( &mut self, path_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
211201
let this = self.eval_context_mut();
212202

213-
if !this.machine.communicate {
214-
throw_unsup_format!("`write` not available when isolation is enabled")
215-
}
203+
this.check_no_isolation("unlink")?;
216204

217205
let path_bytes = this
218206
.memory()

src/shims/time.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
4141
) -> InterpResult<'tcx, i32> {
4242
let this = self.eval_context_mut();
4343

44-
if !this.machine.communicate {
45-
throw_unsup_format!("`clock_gettime` not available when isolation is enabled")
46-
}
44+
this.check_no_isolation("clock_gettime")?;
4745

4846
let clk_id = this.read_scalar(clk_id_op)?.to_i32()?;
4947
if clk_id != this.eval_libc_i32("CLOCK_REALTIME")? {
@@ -75,9 +73,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
7573
) -> InterpResult<'tcx, i32> {
7674
let this = self.eval_context_mut();
7775

78-
if !this.machine.communicate {
79-
throw_unsup_format!("`gettimeofday` not available when isolation is enabled")
80-
}
76+
this.check_no_isolation("gettimeofday")?;
8177
// Using tz is obsolete and should always be null
8278
let tz = this.read_scalar(tz_op)?.not_undef()?;
8379
if !this.is_null(tz)? {

0 commit comments

Comments
 (0)