forked from AFLplusplus/LibAFL
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Python Grammar Loader for Nautilus (AFLplusplus#2635)
* add python grammar loader for Nautilus * fmt * fmt toml * add python to macos CI deps * install python * fmt * ci * clippy * fix workflow * fmt * fix baby nautilus * fix nautilus sync * fmt * fmt * clippy * typo * fix miri * remove pyo3 from workspace to packages which need it and make it optional * go back to AsRef<Path> for nautilus grammar loading * replace hardcoded python flags for macos build * typo * taplo fmt * revert formatting of libafl_qemu_arch * ci * typo * remove expects in NautilusContext::from_file and make them Results * remove not(miri) clause in test * try and fix python build fir ios and android * again * android * tmate * fix android build * document load_python_grammar * log if python or json when loading nautilus grammar * make nautilus optional * add nautilus as feature to forkserver_simple_nautilus
- Loading branch information
1 parent
0546df5
commit a5b3ad7
Showing
17 changed files
with
114 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
libafl/src/common/nautilus/grammartec/python_grammar_loader.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use std::{string::String, vec::Vec}; | ||
|
||
use pyo3::{prelude::*, pyclass, types::IntoPyDict}; | ||
|
||
use crate::{nautilus::grammartec::context::Context, Error}; | ||
|
||
#[pyclass] | ||
struct PyContext { | ||
ctx: Context, | ||
} | ||
impl PyContext { | ||
fn get_context(&self) -> Context { | ||
self.ctx.clone() | ||
} | ||
} | ||
|
||
#[pymethods] | ||
impl PyContext { | ||
#[new] | ||
fn new() -> Self { | ||
PyContext { | ||
ctx: Context::new(), | ||
} | ||
} | ||
|
||
fn rule(&mut self, py: Python, nt: &str, format: &Bound<PyAny>) -> PyResult<()> { | ||
if let Ok(s) = format.extract::<&str>() { | ||
self.ctx.add_rule(nt, s.as_bytes()); | ||
} else if let Ok(s) = format.extract::<&[u8]>() { | ||
self.ctx.add_rule(nt, s); | ||
} else { | ||
return Err(pyo3::exceptions::PyValueError::new_err( | ||
"format argument should be string or bytes", | ||
)); | ||
} | ||
Ok(()) | ||
} | ||
|
||
#[allow(clippy::needless_pass_by_value)] | ||
fn script(&mut self, nt: &str, nts: Vec<String>, script: PyObject) { | ||
self.ctx.add_script(nt, &nts, script); | ||
} | ||
|
||
fn regex(&mut self, nt: &str, regex: &str) { | ||
self.ctx.add_regex(nt, regex); | ||
} | ||
} | ||
|
||
fn loader(py: Python, grammar: &str) -> PyResult<Context> { | ||
let py_ctx = Bound::new(py, PyContext::new())?; | ||
let locals = [("ctx", &py_ctx)].into_py_dict_bound(py); | ||
py.run_bound(grammar, None, Some(&locals))?; | ||
Ok(py_ctx.borrow().get_context()) | ||
} | ||
|
||
/// Create a `NautilusContext` from a python grammar file | ||
#[must_use] | ||
pub fn load_python_grammar(grammar: &str) -> Context { | ||
Python::with_gil(|py| { | ||
loader(py, grammar) | ||
.map_err(|e| e.print_and_set_sys_last_vars(py)) | ||
.expect("failed to parse python grammar") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters