-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
4,395 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ members = [ | |
"panda-rs", | ||
"panda-sys", | ||
"panda-macros", | ||
"syscall-parser", | ||
] |
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 was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "syscall-parser" | ||
version = "0.1.0" | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
peg = "0.7.0" |
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,7 @@ | ||
# syscall-parser | ||
|
||
Parses `panda/plugins/syscalls2/generated/syscalls_ext_typedefs_*.h` in order to provide Rust bindings to syscalls2. To regenerate `../panda-macros/src/syscalls/*.rs`, run: | ||
|
||
``` | ||
cargo run | ||
``` |
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,134 @@ | ||
use std::io::Write; | ||
|
||
#[derive(Debug)] | ||
struct CallbackType { | ||
name: String, | ||
arguments: Vec<Argument> | ||
} | ||
|
||
#[derive(Debug)] | ||
struct Argument { | ||
name: String, | ||
ty: Type, | ||
} | ||
|
||
#[derive(Debug)] | ||
enum Type { | ||
Ptr(Box<Type>), | ||
Ident(String), | ||
} | ||
|
||
impl Type { | ||
fn as_rust_type(&self) -> String { | ||
match self { | ||
Self::Ptr(ptr) => format!("&mut {}", ptr.as_rust_type()), | ||
Self::Ident(ident) => match &**ident { | ||
"uint8_t" => "u8", | ||
"uint16_t" => "u16", | ||
"uint32_t" => "u32", | ||
"uint64_t" => "u64", | ||
"int8_t" => "i8", | ||
"int16_t" => "i16", | ||
"int32_t" => "i32", | ||
"int64_t" => "i64", | ||
ident @ ("CPUState" | "target_ulong") => ident, | ||
ident => panic!("type {} unrecognized", ident), | ||
}.into(), | ||
} | ||
} | ||
} | ||
|
||
fn sanitize_name(name: &str) -> String { | ||
match name { | ||
"type" => "_type".into(), | ||
_ => name.into(), | ||
} | ||
} | ||
|
||
peg::parser! { | ||
grammar c_parser() for str { | ||
pub(crate) rule callback_types() -> Vec<CallbackType> | ||
= (_? comment())* cb_types:callback_type() ** (_? ";" _?) ";" _ (comment() _?)* { | ||
cb_types | ||
} | ||
|
||
rule comment() | ||
= "//" [^ '\n']* "\n" | ||
|
||
rule callback_type() -> CallbackType | ||
= "typedef" _ "void" _ "(" _? "*" _? name:ident() _? ")" _? | ||
"(" _? arguments:argument() ** ("," _?) _? ")" | ||
{ | ||
CallbackType { name, arguments } | ||
} | ||
|
||
rule argument() -> Argument | ||
= ty:c_type() _ name:ident() { Argument { name, ty } } | ||
|
||
rule c_type() -> Type | ||
= ptr() | ||
/ type_ident() | ||
|
||
rule ptr() -> Type | ||
= ident:type_ident() _? "*" { Type::Ptr(Box::new(ident)) } | ||
|
||
rule type_ident() -> Type | ||
= ident:ident() { Type::Ident(ident.into()) } | ||
|
||
rule ident() -> String | ||
= ident:$( | ||
['a'..='z' | 'A'..='Z' | '_'] ['a'..='z' | 'A'..='Z' | '_' | '0'..='9']* | ||
) { ident.into() } | ||
|
||
rule _() = quiet!{ [' ' | '\n' | '\t']+ } | ||
} | ||
} | ||
|
||
const ARCHES: &[(&str, &str)] = &[ | ||
("x86_64", "x64"), | ||
("i386", "x86"), | ||
("arm", "arm"), | ||
("aarch64", "arm64"), | ||
// no ppc support | ||
//("ppc", "ppc"), | ||
("mips", "mips"), | ||
("mipsel", "mips"), | ||
]; | ||
|
||
fn main() { | ||
for (arch, syscalls_arch) in ARCHES { | ||
println!("Generating for {}...", arch); | ||
let path = std::path::Path::new( | ||
&std::env::var("PANDA_ROOT").expect("Please set 'PANDA_ROOT'") | ||
) | ||
.join(&format!("panda/plugins/syscalls2/generated/syscalls_ext_typedefs_{}.h", syscalls_arch)); | ||
|
||
let contents = std::fs::read_to_string( | ||
path | ||
).unwrap(); | ||
let mut f = std::fs::File::create(format!("../panda-macros/src/syscalls/{}.rs", arch)).unwrap(); | ||
let callback_types = c_parser::callback_types(&contents).unwrap(); | ||
|
||
writeln!(f, "// AUTOGENERATED BY panda-rs/syscall-parser DO NOT EDIT").unwrap(); | ||
writeln!(f, "define_syscalls_callbacks! {{").unwrap(); | ||
for callback_type in callback_types { | ||
let name = callback_type.name.trim_end_matches("_t"); | ||
|
||
// omit BSD syscalls | ||
if name.starts_with("on_sys_") { | ||
let args = callback_type.arguments | ||
.iter() | ||
.map(|arg| format!("{}: {}", sanitize_name(&arg.name), arg.ty.as_rust_type())) | ||
.collect::<Vec<String>>() | ||
.join(", "); | ||
writeln!( | ||
f, | ||
" ({name}, add_callback_{name}, ({args})),", | ||
name=name, | ||
args=args, | ||
).unwrap(); | ||
} | ||
} | ||
writeln!(f, "}}").unwrap(); | ||
} | ||
} |