Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't generate syscalls in build.rs #5

Merged
merged 1 commit into from
Feb 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/target
target/
**/*.rs.bk
Cargo.lock
src/nr.rs
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ default = ["use_std"]
use_std = []

[build-dependencies]
tempfile = "3.0"
cc = "1.0"

[dev-dependencies]
libc = "0.2"

[workspace]
members = [
"syscalls-gen",
]
151 changes: 0 additions & 151 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,154 +1,3 @@
use std::env;
use std::fs::File;
use std::io::{Error, ErrorKind, Read, Result, Write};
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};

use tempfile::NamedTempFile;

static CPP: &str = "cpp";

#[cfg(not(target_os = "linux"))]
compile_error!("syscalls only supports Linux");

#[cfg(not(any(target_arch = "x86_64", target_arch = "x86")))]
compile_error!("syscalls only supports x86_64");

#[cfg(all(target_arch = "x86_64", target_pointer_width = "32"))]
static UNISTD_H: &str = "asm/unistd_x32.h";
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
static UNISTD_H: &str = "asm/unistd_64.h";
#[cfg(target_arch = "x86")]
static UNISTD_H: &str = "asm/unistd_32.h";

fn get_asm_unistd_h() -> Result<String> {
let mut file = NamedTempFile::new()?;
writeln!(file.as_file_mut(), "#include <sys/syscall.h>")?;
let subp = Command::new(CPP)
.arg("-xc")
.arg(file.path())
.stdout(Stdio::piped())
.spawn()?;
let output = subp.wait_with_output()?;
let cpp_out: String = unsafe { String::from_utf8_unchecked(output.stdout) };
let asm_unistd: Option<&str> = cpp_out
.lines()
.filter(|x| x.contains(UNISTD_H))
.next()
.map(|line| {
line.split_whitespace()
.filter(|x| x.contains(UNISTD_H))
.next()
})
.unwrap_or(None);
if let Some(unistd) = asm_unistd {
Ok(unistd.chars().filter(|x| *x != '"').collect())
} else {
Err(Error::new(
ErrorKind::Other,
"cpp returned expected result.",
))
}
}

fn gen_syscalls_from(unistd: String) -> Result<Vec<(String, i32)>> {
let mut file = File::open(unistd)?;
let mut buff = String::new();
let mut ret: Vec<(String, i32)> = Vec::new();
file.read_to_string(&mut buff)?;
for candidate in buff
.lines()
.filter(|x| x.starts_with("#define") && x.contains("__NR_"))
{
let words = candidate.split_whitespace();
let mut it = words.skip_while(|x| !x.starts_with("__NR_"));
let name_ = it.next();
let nr_ = it.filter(|x| x.parse::<i32>().is_ok()).next();
if let Some((name, nr)) = name_.and_then(|x| {
nr_.and_then(|y| {
y.parse::<i32>().ok().and_then(|z| Some((x.to_string(), z)))
})
}) {
ret.push((name, nr));
}
}

Ok(ret)
}

fn gen_syscalls() -> Result<Vec<(String, i32)>> {
get_asm_unistd_h().and_then(|x| gen_syscalls_from(x))
}

fn gen_syscall_nrs(dest: &Path) -> Result<()> {
let mut f = File::create(dest)?;
writeln!(f, "// AUTOMATICALLY GENERATED BY build.rs. DO NOT EDIT.\n")?;
writeln!(f, "pub use self::SyscallNo::*;")?;
writeln!(f, "use core::fmt;")?;

writeln!(
f,
"#[allow(non_snake_case, non_camel_case_types, non_upper_case_globals)]"
)?;
writeln!(f, "#[derive(PartialEq, Eq, Clone, Copy)]")?;
writeln!(f, "pub enum SyscallNo {{")?;

let syscalls = gen_syscalls()?;
assert!(syscalls.len() > 100);

for (name, nr) in &syscalls {
writeln!(
f,
" SYS{} = {},",
name.chars().skip(4).collect::<String>(),
nr
)?;
}
writeln!(f, "}}")?;

writeln!(f, "static SYSCALL_NAMES: [&str; {}] = [", syscalls.len())?;
for (name, _) in &syscalls {
writeln!(
f,
" \"{}\",",
name.chars().skip(5).collect::<String>().as_str()
)?;
}
writeln!(f, "];")?;

writeln!(f, "impl fmt::Debug for SyscallNo {{")?;
writeln!(
f,
" fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {{"
)?;
writeln!(
f,
" write!(f, \"{{}}\", SYSCALL_NAMES[self.clone() as usize])"
)?;
writeln!(f, " }}")?;
writeln!(f, "}}")?;

writeln!(f, "static SYSCALL_IDS: [SyscallNo; {}] = [", syscalls.len())?;
for (name, _) in &syscalls {
writeln!(f, " SYS{},", name.chars().skip(4).collect::<String>())?;
}
writeln!(f, "];")?;

writeln!(f, "impl From<i32> for SyscallNo {{")?;
writeln!(f, " fn from(item: i32) -> Self {{")?;
writeln!(f, " if item as usize > SYSCALL_IDS.len() {{")?;
writeln!(f, " panic!(\"invalid syscall: {{}}\", item)")?;
writeln!(f, " }} else {{")?;
writeln!(f, " SYSCALL_IDS[item as usize]")?;
writeln!(f, " }}")?;
writeln!(f, " }}")?;
writeln!(f, "}}")?;

Ok(())
}

fn main() {
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
gen_syscall_nrs(&out_path.join("nr.rs")).unwrap();
cc::Build::new().file("src/syscall.c").compile("syscall");
}
2 changes: 1 addition & 1 deletion src/helper.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::SyscallNo;
use crate::nr::SyscallNo;

extern "C" {
fn internal_syscall0(nr: i64) -> i64;
Expand Down
6 changes: 2 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

#[macro_use]

mod nr;
pub mod helper;
pub mod macros;

// Include the generated system calls.
include!(concat!(env!("OUT_DIR"), "/nr.rs"));

pub use self::helper::*;
pub use SyscallNo::*;
pub use self::nr::SyscallNo::*;

#[cfg(test)]
mod tests {
Expand Down
Loading