microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.23.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

source/fuzz/fuzz_targets/qasm.rs

70lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4#![no_main]
5
6allocator::assign_global!();
7
8#[cfg(feature = "do_fuzz")]
9use libfuzzer_sys::fuzz_target;
10
11use qsc::{
12 PackageStore, PackageType,
13 compile::{compile_ast, package_store_with_stdlib},
14 hir::PackageId,
15 qasm::{
16 CompilerConfig, OutputSemantics, ProgramType, QubitSemantics,
17 compiler::parse_and_compile_to_qsharp_ast_with_config, io::InMemorySourceResolver,
18 },
19 target::Profile,
20};
21
22fn compile(data: &[u8]) {
23 if let Ok(fuzzed_code) = std::str::from_utf8(data) {
24 thread_local! {
25 static STORE_STD: (PackageId, PackageStore) = {
26 package_store_with_stdlib(Profile::Unrestricted.into())
27 };
28 }
29 STORE_STD.with(|(stdid, store)| {
30 let mut resolver = InMemorySourceResolver::from_iter([]);
31 let config = CompilerConfig::new(
32 QubitSemantics::Qiskit,
33 OutputSemantics::OpenQasm,
34 ProgramType::File,
35 Some("Fuzz".into()),
36 None,
37 );
38
39 let unit = parse_and_compile_to_qsharp_ast_with_config(
40 fuzzed_code,
41 "fuzz.qasm",
42 Some(&mut resolver),
43 config,
44 );
45 let (sources, _, package, _, profile) = unit.into_tuple();
46
47 let dependencies = vec![(PackageId::CORE, None), (*stdid, None)];
48
49 let (mut _unit, _errors) = compile_ast(
50 store,
51 &dependencies,
52 package,
53 sources,
54 PackageType::Lib,
55 profile.into(),
56 );
57 });
58 }
59}
60
61#[cfg(feature = "do_fuzz")]
62fuzz_target!(|data: &[u8]| {
63 compile(data);
64});
65
66#[cfg(not(feature = "do_fuzz"))]
67#[unsafe(no_mangle)]
68pub extern "C" fn main() {
69 compile(&[]);
70}
71