zkas_compile.rs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 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. // extern crate darkfi_serial;
  19. use honggfuzz::fuzz;
  20. use darkfi::zkas::{Lexer, Parser, Compiler, Analyzer};
  21. use std::str;
  22. fn main() {
  23. loop {
  24. fuzz!(|data: &[u8]| {
  25. // The lex, parse, compile code below is taken from bin/zkas/src/main.rs
  26. // Use only inputs that can be encoded as .chars(), as this is what the
  27. // zkas binary uses
  28. let chars = match str::from_utf8(data) {
  29. Ok(v) => v.chars(),
  30. Err(_e) => return,
  31. };
  32. let filename = "/dev/null";
  33. let lexer = Lexer::new(filename, chars.clone());
  34. let tokens = match lexer.lex() {
  35. Ok(v) => v,
  36. Err(_) => return,
  37. };
  38. // The parser goes over the tokens provided by the lexer and builds
  39. // the initial AST, not caring much about the semantics, just enforcing
  40. // syntax and general structure.
  41. let parser = Parser::new(filename, chars.clone(), tokens);
  42. let (namespace, k, constants, witnesses, statements) = match parser.parse() {
  43. Ok(v) => v,
  44. Err(_) => return,
  45. };
  46. // The analyzer goes through the initial AST provided by the parser and
  47. // converts return and variable types to their correct forms, and also
  48. // checks that the semantics of the ZK script are correct.
  49. let mut analyzer = Analyzer::new(filename, chars.clone(), constants, witnesses, statements);
  50. if analyzer.analyze_types().is_err() {
  51. return
  52. }
  53. // Skip this section because it automatically pauses to prompt
  54. // for user inputwhich is probably preventing coverage.
  55. // if analyzer.analyze_semantic().is_err() {
  56. // return
  57. // }
  58. let compiler = Compiler::new(
  59. filename,
  60. chars.clone(),
  61. namespace,
  62. k,
  63. analyzer.constants,
  64. analyzer.witnesses,
  65. analyzer.statements,
  66. analyzer.literals,
  67. false, // a guess
  68. );
  69. match compiler.compile() {
  70. Ok(v) => v,
  71. Err(_) => return,
  72. };
  73. });
  74. }
  75. }