Skip to content

Instantly share code, notes, and snippets.

@zackradisic
Last active January 17, 2022 18:16
Show Gist options
  • Save zackradisic/5e38560d542ce9b4b9da9b0b4e136eaa to your computer and use it in GitHub Desktop.
Save zackradisic/5e38560d542ce9b4b9da9b0b4e136eaa to your computer and use it in GitHub Desktop.
code to reproduce undefined behaviour detected by Miri when using SWC. Run with this command `RUSTCFLAGS="-C debug-assertions" RUSTFLAGS="-Zrandomize-layout" MIRIFLAGS="-Zmiri-symbolic-alignment-check -Zmiri-check-number-validity -Zmiri-tag-raw-pointers -Zmiri-disable-isolation" cargo miri run`
[package]
name = "swc-miri-ub"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
swc = "0.102.2"
swc_ecmascript = { version="0.104.3", features = ["visit", "parser", "typescript"] }
anyhow = "1.0.52"
[dependencies.swc_ecma_parser]
version = "0.83.0"
use std::{fs, sync::Arc};
use anyhow::Context;
use swc::{
common::{sync::Lazy, FileName, FilePathMapping, SourceMap},
config::ParseOptions,
ecmascript::ast::{TsKeywordType, TsKeywordTypeKind, TsType},
try_with_handler, Compiler,
};
use swc_ecma_parser::{Syntax, TsConfig};
pub fn main() {
let c = compiler();
let str = "console.log('foo')";
let prog = try_with_handler(c.cm.clone(), false, |handler| {
let opts: ParseOptions = ParseOptions {
syntax: Syntax::Typescript(TsConfig::default()),
..ParseOptions::default()
};
let fm = c.cm.new_source_file(FileName::Anon, str.to_string());
let program = c
.parse_js(
fm,
handler,
opts.target,
opts.syntax,
opts.is_module,
opts.comments,
)
.context("failed to parse code")?;
Ok(program)
})
.expect("Program failed");
println!("Parsed program: {:?}", prog);
}
/// Get global sourcemap
fn compiler() -> Arc<Compiler> {
static C: Lazy<Arc<Compiler>> = Lazy::new(|| {
let cm = Arc::new(SourceMap::new(FilePathMapping::empty()));
Arc::new(Compiler::new(cm))
});
C.clone()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment