zkas_compile.rs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2026 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. // See also honggfuzz/zkas_compile.rs
  19. #![no_main]
  20. use libfuzzer_sys::fuzz_target;
  21. use darkfi::zkas::{Lexer, Parser, Compiler, Analyzer};
  22. use std::str;
  23. fuzz_target!(|data: &[u8]| {
  24. // The lex, parse, compile code below is taken from bin/zkas/src/main.rs
  25. // Use only inputs that can be encoded as .chars(), as this is what the
  26. // zkas binary uses
  27. let chars = match str::from_utf8(data) {
  28. Ok(v) => v.chars(),
  29. Err(_e) => return,
  30. };
  31. let filename = "/dev/null";
  32. let lexer = Lexer::new(filename, chars.clone());
  33. let tokens = match lexer.lex() {
  34. Ok(v) => v,
  35. Err(_) => return,
  36. };
  37. // The parser goes over the tokens provided by the lexer and builds
  38. // the initial AST, not caring much about the semantics, just enforcing
  39. // syntax and general structure.
  40. let parser = Parser::new(filename, chars.clone(), tokens);
  41. let (namespace, k, constants, witnesses, statements) = match parser.parse() {
  42. Ok(v) => v,
  43. Err(_) => return,
  44. };
  45. // The analyzer goes through the initial AST provided by the parser and
  46. // converts return and variable types to their correct forms, and also
  47. // checks that the semantics of the ZK script are correct.
  48. let mut analyzer = Analyzer::new(filename, chars.clone(), constants, witnesses, statements);
  49. if analyzer.analyze_types().is_err() {
  50. return
  51. }
  52. // Skip this section because it automatically pauses on output which is probably
  53. // preventing coverage
  54. // if analyzer.analyze_semantic().is_err() {
  55. // return
  56. // }
  57. let compiler = Compiler::new(
  58. filename,
  59. chars.clone(),
  60. namespace,
  61. k,
  62. analyzer.constants,
  63. analyzer.witnesses,
  64. analyzer.statements,
  65. analyzer.literals,
  66. false, // no debug info
  67. );
  68. match compiler.compile() {
  69. Ok(v) => v,
  70. Err(_) => return,
  71. };
  72. });