From 78311a713218b9923cd5ab73b62a9fe2485c7d22 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Mon, 14 Oct 2019 15:36:15 -0500 Subject: [PATCH] Add function to error with enabled isolation --- src/helpers.rs | 9 +++++++++ src/shims/env.rs | 8 ++------ src/shims/fs.rs | 24 ++++++------------------ src/shims/time.rs | 8 ++------ 4 files changed, 19 insertions(+), 30 deletions(-) diff --git a/src/helpers.rs b/src/helpers.rs index 9107958e01..e3f818414d 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -336,7 +336,16 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx )?; offset += imm.layout.size; } + Ok(()) + } + /// Helper function used inside the shims of foreign functions to check that isolation is + /// disabled. It returns an error using the `name` of the foreign function if this is not the + /// case. + fn check_no_isolation(&mut self, name: &str) -> InterpResult<'tcx> { + if !self.eval_context_mut().machine.communicate { + throw_unsup_format!("`{}` not available when isolation is enabled. Pass the flag `-Zmiri-disable-isolation` to disable it.", name) + } Ok(()) } } diff --git a/src/shims/env.rs b/src/shims/env.rs index 2ccbc0238e..ae800c2315 100644 --- a/src/shims/env.rs +++ b/src/shims/env.rs @@ -120,9 +120,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx ) -> InterpResult<'tcx, Scalar> { let this = self.eval_context_mut(); - if !this.machine.communicate { - throw_unsup_format!("`getcwd` not available when isolation is enabled") - } + this.check_no_isolation("getcwd")?; let tcx = &{ this.tcx.tcx }; @@ -158,9 +156,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx fn chdir(&mut self, path_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> { let this = self.eval_context_mut(); - if !this.machine.communicate { - throw_unsup_format!("`chdir` not available when isolation is enabled") - } + this.check_no_isolation("chdir")?; let path_bytes = this .memory() diff --git a/src/shims/fs.rs b/src/shims/fs.rs index 7e684489b5..414ae35e96 100644 --- a/src/shims/fs.rs +++ b/src/shims/fs.rs @@ -36,9 +36,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx ) -> InterpResult<'tcx, i32> { let this = self.eval_context_mut(); - if !this.machine.communicate { - throw_unsup_format!("`open` not available when isolation is enabled") - } + this.check_no_isolation("open")?; let flag = this.read_scalar(flag_op)?.to_i32()?; @@ -91,9 +89,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx ) -> InterpResult<'tcx, i32> { let this = self.eval_context_mut(); - if !this.machine.communicate { - throw_unsup_format!("`fcntl` not available when isolation is enabled") - } + this.check_no_isolation("fcntl")?; let fd = this.read_scalar(fd_op)?.to_i32()?; let cmd = this.read_scalar(cmd_op)?.to_i32()?; @@ -126,9 +122,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx fn close(&mut self, fd_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> { let this = self.eval_context_mut(); - if !this.machine.communicate { - throw_unsup_format!("`close` not available when isolation is enabled") - } + this.check_no_isolation("close")?; let fd = this.read_scalar(fd_op)?.to_i32()?; @@ -145,9 +139,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx ) -> InterpResult<'tcx, i64> { let this = self.eval_context_mut(); - if !this.machine.communicate { - throw_unsup_format!("`read` not available when isolation is enabled") - } + this.check_no_isolation("read")?; let tcx = &{ this.tcx.tcx }; @@ -182,9 +174,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx ) -> InterpResult<'tcx, i64> { let this = self.eval_context_mut(); - if !this.machine.communicate { - throw_unsup_format!("`write` not available when isolation is enabled") - } + this.check_no_isolation("write")?; let tcx = &{ this.tcx.tcx }; @@ -210,9 +200,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx fn unlink( &mut self, path_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> { let this = self.eval_context_mut(); - if !this.machine.communicate { - throw_unsup_format!("`write` not available when isolation is enabled") - } + this.check_no_isolation("unlink")?; let path_bytes = this .memory() diff --git a/src/shims/time.rs b/src/shims/time.rs index 0153c1a912..d75cb7bad3 100644 --- a/src/shims/time.rs +++ b/src/shims/time.rs @@ -41,9 +41,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx ) -> InterpResult<'tcx, i32> { let this = self.eval_context_mut(); - if !this.machine.communicate { - throw_unsup_format!("`clock_gettime` not available when isolation is enabled") - } + this.check_no_isolation("clock_gettime")?; let clk_id = this.read_scalar(clk_id_op)?.to_i32()?; if clk_id != this.eval_libc_i32("CLOCK_REALTIME")? { @@ -75,9 +73,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx ) -> InterpResult<'tcx, i32> { let this = self.eval_context_mut(); - if !this.machine.communicate { - throw_unsup_format!("`gettimeofday` not available when isolation is enabled") - } + this.check_no_isolation("gettimeofday")?; // Using tz is obsolete and should always be null let tz = this.read_scalar(tz_op)?.not_undef()?; if !this.is_null(tz)? {