Skip to content

Commit

Permalink
feat: add a fuzz test for kclvm-parser.parse_expr.
Browse files Browse the repository at this point in the history
Note:
1. "PanicInfo.to_json_string" has been modified.
2. Some problems detected by the fuzz test will be solved soon.

What:
1. Add a fuzz test for kclvm-parser.parse_expr.
2. "PanicInfo.to_json_string" has been modified.
3. Remove some unusd imports.
4. In order to adapt to the new "PanicInfo.to_json_string",
   some code have been changed.

How:
1. Add "fuzz/fuzz_parser" in kclvm/tests.
2. Init kclvm/tests to a cargo project.
3. Add fuzz-parser in Makefile.
4. Remove some unusd imports.
5. "PanicInfo.to_json_string" has been modified by "serde_json::to_string".
6. Change all “$__kcl_PanicInfo__$” to “__kcl_PanicInfo__”.

Why:
1. Add a fuzz test in order to detect more bugs.
2. "serde" provides a better interface for serialization/deserialization of “PanicInfo”.
3. Serialization/Deserialization of “PanicInfo" is helpful for catching panic errors in fuzz.
4. "cargo fuzz" can only be used under a cargo project.
  • Loading branch information
zong-zhe committed May 25, 2022
1 parent 22bf126 commit 4f766e3
Show file tree
Hide file tree
Showing 26 changed files with 1,906 additions and 93 deletions.
1 change: 1 addition & 0 deletions kclvm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions kclvm/ast/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::walker::MutSelfMutWalker;
use crate::AugOp::Mod;
use crate::{ast, build_assign_node, node_ref, Identifier, Module, Node, NodeRef, SchemaStmt};
use std::process::id;

fn get_dummy_assign_ast() -> ast::Node<ast::AssignStmt> {
let filename = "main.k";
Expand Down Expand Up @@ -149,8 +147,7 @@ fn test_try_from_for_stringlit() {
let json_str = serde_json::to_string(&str_lit).unwrap();

let str_expected =
r#"{"is_long_string":false,"raw_value":"\"test_str\"","value":"test_str"}"#
.to_string();
r#"{"is_long_string":false,"raw_value":"\"test_str\"","value":"test_str"}"#.to_string();
assert_eq!(str_expected, json_str);
}

Expand Down
1 change: 1 addition & 0 deletions kclvm/compiler/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions kclvm/error/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions kclvm/lexer/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions kclvm/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ test-runtime:
test-grammar: install-pytest
cd tests/integration/grammar && kclvm -m pytest -v -n 5 --junitxml $(COVER_TEST_FILE_PATH) --html=$(COVER_REPORT_FILE_PATH)

fuzz-parser:
cd tests && cargo fuzz run fuzz_parser

install-pytest:
kclvm -mpip install pytest-html

Expand Down
1 change: 1 addition & 0 deletions kclvm/parser/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion kclvm/parser/src/parser/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::parser::Parser;
use crate::session::ParseSession;
use expect_test::{expect, Expect};
use kclvm_ast::ast::*;
use kclvm_span::{create_session_globals_then, BytePos, FilePathMapping, SourceMap, Symbol};
use kclvm_span::{create_session_globals_then, BytePos, FilePathMapping, SourceMap};
use rustc_span::Pos;
use std::path::PathBuf;
use std::sync::Arc;
Expand Down
2 changes: 0 additions & 2 deletions kclvm/parser/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::fmt::format;

use crate::*;

use expect_test::{expect, Expect};
Expand Down
2 changes: 1 addition & 1 deletion kclvm/plugin/kclvm_plugin.i
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class AppContext(_kclvm_plugin_AppContextBase):
try:
return self._call_py_method_unsafe(name, args_json, kwargs_json)
except Exception as e:
return json.dumps({ "$__kcl_PanicInfo__$": f"{e}" })
return json.dumps({ "__kcl_PanicInfo__": f"{e}" })

def _get_plugin(self, plugin_name:str) -> typing.Optional[any]:
if plugin_name in self._plugin_dict:
Expand Down
2 changes: 1 addition & 1 deletion kclvm/plugin/kclvm_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ def _call_py_method(self, name: str, args_json: str, kwargs_json: str) -> str:
try:
return self._call_py_method_unsafe(name, args_json, kwargs_json)
except Exception as e:
return json.dumps({"$__kcl_PanicInfo__$": f"{e}"})
return json.dumps({"__kcl_PanicInfo__": f"{e}"})

def _get_plugin(self, plugin_name: str) -> typing.Optional[any]:
if plugin_name in self._plugin_dict:
Expand Down
1 change: 1 addition & 0 deletions kclvm/runner/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions kclvm/runtime/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions kclvm/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ path = "src/lib.rs"
[dependencies]
kclvm_runtime_internal_macros = { path = "./internal_macros" }
json_minimal = {path = "./src/3rdparty/json_minimal", version = "0.1.0"}
serde = { version = "1", features = ["derive"] }
serde_yaml = "0.8.23"

base64 = "0.13.0"
Expand Down
6 changes: 3 additions & 3 deletions kclvm/runtime/src/api/kclvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

#[allow(non_camel_case_types)]
type kclvm_value_ref_t = crate::ValueRef;

use crate::IndexMap;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::{
cell::RefCell,
Expand Down Expand Up @@ -273,9 +273,9 @@ pub struct OptionHelp {
}

#[allow(non_snake_case)]
#[derive(PartialEq, Clone, Default, Debug)]
#[derive(PartialEq, Clone, Default, Debug, Serialize, Deserialize)]
pub struct PanicInfo {
pub __kcl_PanicInfo__: bool, // "$__kcl_PanicInfo__$"
pub __kcl_PanicInfo__: bool, // "__kcl_PanicInfo__"

pub rust_file: String,
pub rust_line: i32,
Expand Down
86 changes: 6 additions & 80 deletions kclvm/runtime/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

pub mod api;
pub use api::*;
use json_minimal::*;

#[allow(non_camel_case_types)]
type kclvm_value_ref_t = crate::ValueRef;
Expand All @@ -13,86 +12,13 @@ impl crate::PanicInfo {
}

pub fn to_json_string(&self) -> String {
let mut json = Json::new();

// PanicInfo
json.add(Json::OBJECT {
name: "$__kcl_PanicInfo__$".to_string(),
value: Box::new(Json::BOOL(self.__kcl_PanicInfo__)),
});

// is warnning?
json.add(Json::OBJECT {
name: "is_warning".to_string(),
value: Box::new(Json::BOOL(self.is_warning)),
});

// message
json.add(Json::OBJECT {
name: "message".to_string(),
value: Box::new(Json::STRING(self.message.to_string())),
});

// err_type
json.add(Json::OBJECT {
name: "err_type_code".to_string(),
value: Box::new(Json::NUMBER(self.err_type_code as f64)),
});

// kcl info
if !self.kcl_arg_msg.is_empty() {
json.add(Json::OBJECT {
name: "kcl_arg_msg".to_string(),
value: Box::new(Json::STRING(self.kcl_arg_msg.to_string())),
});
}
json.add(Json::OBJECT {
name: "kcl_file".to_string(),
value: Box::new(Json::STRING(self.kcl_file.to_string())),
});
json.add(Json::OBJECT {
name: "kcl_line".to_string(),
value: Box::new(Json::NUMBER(self.kcl_line as f64)),
});
json.add(Json::OBJECT {
name: "kcl_col".to_string(),
value: Box::new(Json::NUMBER(self.kcl_col as f64)),
});

if !self.kcl_config_meta_arg_msg.is_empty() {
json.add(Json::OBJECT {
name: "kcl_config_meta_arg_msg".to_string(),
value: Box::new(Json::STRING(self.kcl_config_meta_arg_msg.to_string())),
});
json.add(Json::OBJECT {
name: "kcl_config_meta_file".to_string(),
value: Box::new(Json::STRING(self.kcl_config_meta_file.to_string())),
});
json.add(Json::OBJECT {
name: "kcl_config_meta_line".to_string(),
value: Box::new(Json::NUMBER(self.kcl_config_meta_line as f64)),
});
json.add(Json::OBJECT {
name: "kcl_config_meta_col".to_string(),
value: Box::new(Json::NUMBER(self.kcl_config_meta_col as f64)),
});
let result = serde_json::to_string(&self);
match result {
Ok(res) => res,
_ => {
panic!("PanicInfo Deserialize Failed")
}
}

// rust info
json.add(Json::OBJECT {
name: "rust_file".to_string(),
value: Box::new(Json::STRING(self.rust_file.to_string())),
});
json.add(Json::OBJECT {
name: "rust_line".to_string(),
value: Box::new(Json::NUMBER(self.rust_line as f64)),
});
json.add(Json::OBJECT {
name: "rust_col".to_string(),
value: Box::new(Json::NUMBER(self.rust_col as f64)),
});

json.print()
}
}

Expand Down
2 changes: 1 addition & 1 deletion kclvm/runtime/src/stdlib/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub extern "C" fn kclvm_plugin_invoke(

let ptr = kclvm_value_from_json(result_json);
{
if let Some(msg) = ptr_as_ref(ptr).dict_get_value("$__kcl_PanicInfo__$") {
if let Some(msg) = ptr_as_ref(ptr).dict_get_value("__kcl_PanicInfo__") {
let ctx = Context::current_context_mut();
ctx.set_err_type(&ErrType::EvaluationError_TYPE);

Expand Down
1 change: 1 addition & 0 deletions kclvm/sema/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions kclvm/tests/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions kclvm/tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "tests"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
3 changes: 3 additions & 0 deletions kclvm/tests/fuzz/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
target
corpus
artifacts
Loading

0 comments on commit 4f766e3

Please sign in to comment.