microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
fuzz/fuzz_targets/compile.rs
44lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | #![no_main] |
| 5 | |
| 6 | allocator::assign_global!(); |
| 7 | |
| 8 | #[cfg(feature = "do_fuzz")] |
| 9 | use libfuzzer_sys::fuzz_target; |
| 10 | use qsc::{hir::PackageId, target::Profile, LanguageFeatures, PackageStore, SourceMap}; |
| 11 | |
| 12 | fn compile(data: &[u8]) { |
| 13 | if let Ok(fuzzed_code) = std::str::from_utf8(data) { |
| 14 | thread_local! { |
| 15 | static STORE_STD: (PackageStore, PackageId) = { |
| 16 | let mut store = PackageStore::new(qsc::compile::core()); |
| 17 | let std = store.insert(qsc::compile::std(&store, Profile::Unrestricted.into())); |
| 18 | (store, std) |
| 19 | }; |
| 20 | } |
| 21 | let sources = SourceMap::new([("fuzzed_code".into(), fuzzed_code.into())], None); |
| 22 | STORE_STD.with(|(store, std)| { |
| 23 | let mut _unit = qsc::compile::compile( |
| 24 | store, |
| 25 | &[(*std, None)], |
| 26 | sources, |
| 27 | qsc::PackageType::Lib, |
| 28 | Profile::Unrestricted.into(), |
| 29 | LanguageFeatures::default(), |
| 30 | ); |
| 31 | }); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | #[cfg(feature = "do_fuzz")] |
| 36 | fuzz_target!(|data: &[u8]| { |
| 37 | compile(data); |
| 38 | }); |
| 39 | |
| 40 | #[cfg(not(feature = "do_fuzz"))] |
| 41 | #[no_mangle] |
| 42 | pub extern "C" fn main() { |
| 43 | compile(&[]); |
| 44 | } |
| 45 | |