-
Notifications
You must be signed in to change notification settings - Fork 212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a magic comment syntax to set MIRIFLAGS #781
base: main
Are you sure you want to change the base?
Changes from 1 commit
73f7c7c
e1c07ef
605f1fe
0e47158
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,4 @@ | |
set -eu | ||
|
||
export MIRI_SYSROOT=~/.cache/miri/HOST | ||
export MIRIFLAGS="-Zmiri-disable-isolation" | ||
exec cargo miri run |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
use regex::Regex; | ||
use serde_derive::Deserialize; | ||
use snafu::{ResultExt, Snafu}; | ||
use std::{ffi::OsStr, fmt, io, os::unix::fs::PermissionsExt, string, time::Duration}; | ||
|
@@ -280,6 +281,27 @@ fn set_execution_environment( | |
cmd.apply_backtrace(&req); | ||
} | ||
|
||
fn parse_magic_comments(code: &str) -> Vec<(&'static str, String)> { | ||
lazy_static::lazy_static! { | ||
pub static ref MIRIFLAGS: Regex = Regex::new(r#"(///|//!|//)\s*MIRIFLAGS\s*=\s*(.*)"#).unwrap(); | ||
} | ||
|
||
fn match_of(line: &str, pat: &Regex) -> Option<String> { | ||
pat.captures(line.trim()) | ||
.and_then(|caps| caps.get(2)) | ||
.map(|mat| mat.as_str().trim_matches(&['\'', '"'][..]).to_string()) | ||
saethlin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
let mut settings = Vec::new(); | ||
if let Some(first_line) = code.trim().lines().next() { | ||
if let Some(miri_flags) = match_of(first_line, &*MIRIFLAGS) { | ||
settings.push(("MIRIFLAGS", miri_flags)); | ||
} | ||
} | ||
|
||
settings | ||
} | ||
|
||
pub mod fut { | ||
use snafu::prelude::*; | ||
use std::{ | ||
|
@@ -292,9 +314,9 @@ pub mod fut { | |
use tokio::{fs, process::Command, time}; | ||
|
||
use super::{ | ||
basic_secure_docker_command, build_execution_command, set_execution_environment, | ||
vec_to_str, wide_open_permissions, BacktraceRequest, Channel, ClippyRequest, | ||
ClippyResponse, CompileRequest, CompileResponse, CompileTarget, | ||
basic_secure_docker_command, build_execution_command, parse_magic_comments, | ||
set_execution_environment, vec_to_str, wide_open_permissions, BacktraceRequest, Channel, | ||
ClippyRequest, ClippyResponse, CompileRequest, CompileResponse, CompileTarget, | ||
CompilerExecutionTimedOutSnafu, CrateInformation, CrateInformationInner, CrateType, | ||
CrateTypeRequest, DemangleAssembly, DockerCommandExt, EditionRequest, ExecuteRequest, | ||
ExecuteResponse, FormatRequest, FormatResponse, MacroExpansionRequest, | ||
|
@@ -454,7 +476,9 @@ pub mod fut { | |
|
||
pub async fn miri(&self, req: &MiriRequest) -> Result<MiriResponse> { | ||
self.write_source_code(&req.code).await?; | ||
let command = self.miri_command(req); | ||
|
||
let settings = parse_magic_comments(&req.code); | ||
let command = self.miri_command(settings, req); | ||
|
||
let output = run_command_with_timeout(command).await?; | ||
|
||
|
@@ -651,10 +675,23 @@ pub mod fut { | |
cmd | ||
} | ||
|
||
fn miri_command(&self, req: impl EditionRequest) -> Command { | ||
fn miri_command( | ||
&self, | ||
env_settings: Vec<(&'static str, String)>, | ||
req: impl EditionRequest, | ||
) -> Command { | ||
let mut cmd = self.docker_command(None); | ||
cmd.apply_edition(req); | ||
|
||
let miri_env = | ||
if let Some((_, flags)) = env_settings.iter().find(|(k, _)| k == &"MIRIFLAGS") { | ||
format!("MIRIFLAGS={} -Zmiri-disable-isolation", flags) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changing the flags like this seems potentially confusing, in particular a flag like this that can heavily affect debugging (since it introduces non-determinism). If people can set their own flag I am not sure if we even still need the default flag? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good point. I'm concerned that if this flag is removed by default, that people will find programs that used to work and don't anymore. How would we direct them to add the magic comment? I can definitely add something to the help page, but is that enough? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stuff already breaks on the playground over time, as crate versions change. I especially hope there's nobody relying on miri-in-playground not changing in any real way... Edit: That said, I do kind of think -Zmiri-disable-isolation makes sense to be on by default in the playground, since it's already sandboxed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you want to be really fancy, when Miri shows the error saying "pass -Zmiri-disable-isolation to run this operation", the playground could turn that into a link that adds the relevant magic command. ;) The playground has such links for other situations. Those are easier though since here, if there already is a MIRIFLAGS comment, one has to now change the existing comment... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I could probably handle most cases. Would it be better to do this edit which is almost always correct, or append something to the output that points to documentation? Both options seem sort of bad in slight ways. I like that currently the playground just echoes cargo output. |
||
} else { | ||
"MIRIFLAGS=-Zmiri-disable-isolation".to_string() | ||
}; | ||
|
||
cmd.args(&["--env", &miri_env]); | ||
|
||
cmd.arg("miri").args(&["cargo", "miri-playground"]); | ||
|
||
log::debug!("Miri command is {:?}", cmd); | ||
|
@@ -1653,6 +1690,111 @@ mod test { | |
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn magic_comments() { | ||
let _singleton = one_test_at_a_time(); | ||
|
||
let expected = vec![("MIRIFLAGS", "-Zmiri-tag-raw-pointers".to_string())]; | ||
|
||
// Every comment syntax | ||
let code = r#"//MIRIFLAGS=-Zmiri-tag-raw-pointers"#; | ||
assert_eq!(parse_magic_comments(code), expected); | ||
|
||
let code = r#"///MIRIFLAGS=-Zmiri-tag-raw-pointers"#; | ||
assert_eq!(parse_magic_comments(code), expected); | ||
|
||
let code = r#"//!MIRIFLAGS=-Zmiri-tag-raw-pointers"#; | ||
assert_eq!(parse_magic_comments(code), expected); | ||
|
||
// Whitespace is ignored | ||
let code = r#"// MIRIFLAGS = -Zmiri-tag-raw-pointers"#; | ||
assert_eq!(parse_magic_comments(code), expected); | ||
|
||
// Both quotes are stripped | ||
let code = r#"//MIRIFLAGS='-Zmiri-tag-raw-pointers'"#; | ||
assert_eq!(parse_magic_comments(code), expected); | ||
|
||
let code = r#"//MIRIFLAGS="-Zmiri-tag-raw-pointers""#; | ||
assert_eq!(parse_magic_comments(code), expected); | ||
|
||
// Leading spaces are ignored | ||
let code = r#" | ||
//MIRIFLAGS=-Zmiri-tag-raw-pointers"#; | ||
assert_eq!(parse_magic_comments(code), expected); | ||
|
||
let expected = vec![( | ||
"MIRIFLAGS", | ||
"-Zmiri-tag-raw-pointers -Zmiri-symbolic-alignment-check".to_string(), | ||
)]; | ||
|
||
// Doesn't break up flags even if they contain whitespace | ||
let code = r#"//MIRIFLAGS="-Zmiri-tag-raw-pointers -Zmiri-symbolic-alignment-check""#; | ||
assert_eq!(parse_magic_comments(code), expected); | ||
|
||
// Even if they aren't wrapped in quoted (possibly dubious) | ||
let code = r#"//MIRIFLAGS=-Zmiri-tag-raw-pointers -Zmiri-symbolic-alignment-check"#; | ||
assert_eq!(parse_magic_comments(code), expected); | ||
} | ||
|
||
#[test] | ||
fn miriflags_work() -> Result<()> { | ||
let _singleton = one_test_at_a_time(); | ||
|
||
let code = r#" | ||
fn main() { | ||
let a = 0u8; | ||
let ptr = &a as *const u8 as usize as *const u8; | ||
unsafe { | ||
println!("{}", *ptr); | ||
} | ||
} | ||
"#; | ||
|
||
let req = MiriRequest { | ||
code: code.to_string(), | ||
edition: None, | ||
}; | ||
|
||
let sb = Sandbox::new()?; | ||
let resp = sb.miri(&req)?; | ||
|
||
assert!(resp.success); | ||
|
||
let code = r#" | ||
// MIRIFLAGS=-Zmiri-tag-raw-pointers | ||
fn main() { | ||
let a = 0u8; | ||
let ptr = &a as *const u8 as usize as *const u8; | ||
unsafe { | ||
println!("{}", *ptr); | ||
} | ||
} | ||
"#; | ||
|
||
let req = MiriRequest { | ||
code: code.to_string(), | ||
edition: None, | ||
}; | ||
|
||
let sb = Sandbox::new()?; | ||
let resp = sb.miri(&req)?; | ||
|
||
assert!(!resp.success); | ||
|
||
assert!( | ||
resp.stderr | ||
.contains("trying to reborrow for SharedReadOnly"), | ||
"was: {}", | ||
resp.stderr | ||
); | ||
assert!( | ||
resp.stderr.contains("but parent tag"), | ||
"was: {}", | ||
resp.stderr | ||
); | ||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn network_connections_are_disabled() { | ||
let _singleton = one_test_at_a_time(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An outer attribute seems weird, here:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems plausible to me. My instinct here is to err on the side of accepting as much as possible, because I think the only alternative we have is silently ignoring things.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree... whenever I use magic comments on ui tests I am nervous that they might be silently ignored.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, the thing is that:
would fail, IIUC, since it wouldn't be the first-line of a file. And I can imagine the transition from your snippet to mine occurring quite easily, especially given that
would not be valid Rust.
I'd rather have Miri start saying with which
MIRIFLAGS
it is being run (which seems like a nice addition overall), rather than have an outer attribute be accepter where an inner one should be used: it is all too reminiscent of:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm... good point. Playground will also add
feature
lines at the very top of the snippet when you click the error message that complains about a missing feature gate.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
#![feature(...)]
is pretty problematic here. I was afraid magic comments would run into this kind of complexity.Parsing the magic comment on lines other than the first makes some sense in response to this, but then we run into this:
I agree that Miri should report its configuration somewhere.
Other things we could do...
cargo-miri
is run. All other build options we would want as arguments tocargo rustc
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is unreasonable. It's not like people are shipping code using the playground, and this seems to be quite an edge case (https://grep.app/search?q=//%20MIRIFLAGS and https://cs.github.com/?scopeName=All+repos&scope=&q=%22%2F%2F+MIRIFLAGS%22 say enough of one that it doesn't exist in open source rust code).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@thomcc Can you clarify what exactly "it" is here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue you are describing here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's fine if that turns on that MIRIFLAG