tests: add tests package

This package serves only to build tests for the kernel, because the
kernel wants to use a different mechanism for spawning processes.

Cargo doesn't support having `#[cfg(test)]` attributes on modules in
order to allow conditional compilation depending on whether we're
running a test or not. To work around this, `main.rs` is included in
another dummy crate.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2020-07-29 21:18:30 +08:00
parent cece761ff1
commit 039975323e
8 changed files with 208 additions and 92 deletions

12
Cargo.lock generated
View file

@ -633,6 +633,18 @@ dependencies = [
"winapi-util",
]
[[package]]
name = "tests"
version = "0.1.0"
dependencies = [
"bitflags",
"hex",
"lazy_static",
"rand",
"sha3",
"xous",
]
[[package]]
name = "textwrap"
version = "0.11.0"

View file

@ -1,6 +1,7 @@
[workspace]
members = [
"kernel",
"tests",
"loader",
"tools",
"xous-rs",

View file

@ -10,24 +10,16 @@ bitflags = "1.2.1"
xous = { path = "../xous-rs", features = ["forget-memory-messages"] }
debug-here = "0.2.2"
stats_alloc = { version = "0.1.8", optional = true }
sha3 = { default-features = false, version = "0.8.2" }
[target.'cfg(target_arch = "riscv32")'.dependencies]
riscv = "0.5.6"
[dependencies.sha3]
default-features = false
version = "0.8.2"
[target.'cfg(windows)'.dependencies]
lazy_static = "1.4"
[target.'cfg(unix)'.dependencies]
lazy_static = "1.4"
[features]
debug-print = []
report-memory = ["stats_alloc"]
[target.'cfg(any(windows, unix))'.dependencies]
lazy_static = "1.4"
rand = "0.7"
hex = "0.4"

View file

@ -212,9 +212,6 @@ impl Process {
let mut process_table = pt.borrow_mut();
let current_pid_idx = process_table.current.get() as usize - 1;
let process = &mut process_table.table[current_pid_idx].as_mut().unwrap();
if !process.threads[thread - 1].allocated {
::debug_here::debug_here!();
}
assert!(
process.threads[thread - 1].allocated,
"tried to switch to thread {} which wasn't allocated",

View file

@ -1239,7 +1239,6 @@ impl SystemServices {
// TODO: Come up with a way to randomize the server ID
let ppid = self.get_process(pid)?.ppid.get();
if ppid != 1 {
debug_here::debug_here!();
panic!("Clients cannot start servers yet");
}

21
tests/Cargo.toml Normal file
View file

@ -0,0 +1,21 @@
[package]
name = "tests"
version = "0.1.0"
authors = ["Sean Cross <sean@xobs.io>"]
edition = "2018"
description = "Core kernel for Xous, including task switching and memory management"
[lib]
name = "kernel"
path = "../kernel/src/main.rs"
test = true
[dependencies]
bitflags = "1.2.1"
sha3 = { default-features = false, version = "0.8.2" }
xous = { path = "../xous-rs", features = ["forget-memory-messages", "processes-as-threads"] }
[target.'cfg(any(windows, unix))'.dependencies]
rand = "0.7"
hex = "0.4"
lazy_static = "1.4"

View file

@ -18,6 +18,10 @@ default-features = false
# will not be implemented. This should only be set by the kernel.
forget-memory-messages = []
# If this is set, create_process() will instead create a thread in
# the current process. This is used by the test suite.
processes-as-threads = []
[target.'cfg(any(windows,unix))'.dependencies]
lazy_static = "1.4"
hex = "0.4"

View file

@ -25,25 +25,180 @@ pub struct ProcessInit {
pub key: ProcessKey,
}
pub struct ProcessArgs<F: FnOnce()> {
main: Option<F>,
name: String,
}
#[cfg(feature = "processes-as-threads")]
mod process {
pub use super::*;
pub struct ProcessArgs<F: FnOnce()> {
main: Option<F>,
name: String,
}
impl<F> ProcessArgs<F>
where
F: FnOnce(),
{
pub fn new(name: &str, main: F) -> ProcessArgs<F> {
ProcessArgs {
main: Some(main),
name: name.to_owned(),
impl<F> ProcessArgs<F>
where
F: FnOnce(),
{
pub fn new(name: &str, main: F) -> ProcessArgs<F> {
ProcessArgs {
main: Some(main),
name: name.to_owned(),
}
}
}
pub struct ProcessHandle(std::thread::JoinHandle<()>);
/// If no connection exists, create a new connection to the server. This means
/// our parent PID will be PID1. Otherwise, reuse the same connection.
pub fn create_process_pre<F>(
_args: &ProcessArgs<F>,
) -> core::result::Result<ProcessInit, crate::Error>
where
F: FnOnce(),
{
ensure_connection()?;
// Ensure there is a connection, because after this function returns
// we'll make a syscall with CreateProcess(). This should only need
// to happen for PID1.
Ok(ProcessInit {
key: PROCESS_KEY
.with(|pk| *pk.borrow())
.unwrap_or_else(default_process_key),
})
}
pub fn create_process_post<F>(
mut args: ProcessArgs<F>,
init: ProcessInit,
pid: PID,
) -> core::result::Result<ProcessHandle, crate::Error>
where
F: FnOnce() + Send + 'static,
{
let server_address = xous_address();
let f = args.main.take().unwrap();
let thread_main = std::thread::Builder::new()
.name(args.name)
.spawn(move || {
set_xous_address(server_address);
THREAD_ID.with(|tid| *tid.borrow_mut() = 1);
PROCESS_ID.with(|p| *p.borrow_mut() = pid);
XOUS_SERVER_CONNECTION.with(|xsc| {
let mut xsc = xsc.borrow_mut();
match xous_connect_impl(server_address, &init.key) {
Ok(a) => {
*xsc = Some(a);
Ok(())
}
Err(_) => Err(crate::Error::InternalError),
}
})?;
crate::create_thread(f)
})
.map_err(|_| crate::Error::InternalError)?
.join()
.unwrap()
.unwrap();
Ok(ProcessHandle(thread_main.0))
}
pub fn wait_process(joiner: ProcessHandle) -> crate::SysCallResult {
joiner.0.join().map(|_| Result::Ok).map_err(|_x| {
// panic!("wait error: {:?}", x);
crate::Error::InternalError
})
}
}
#[cfg(not(feature = "processes-as-threads"))]
mod process {
pub use super::*;
pub struct ProcessArgs<F> {
command: String,
name: String,
_ign: std::marker::PhantomData<F>,
}
impl<F> ProcessArgs<F> {
pub fn new(name: &str, command: String) -> ProcessArgs<F> {
ProcessArgs {
command,
name: name.to_owned(),
_ign: std::marker::PhantomData,
}
}
}
pub struct ProcessHandle(std::process::Child);
/// If no connection exists, create a new connection to the server. This means
/// our parent PID will be PID1. Otherwise, reuse the same connection.
pub fn create_process_pre<F>(
_args: &ProcessArgs<F>,
) -> core::result::Result<ProcessInit, crate::Error> {
ensure_connection()?;
// Ensure there is a connection, because after this function returns
// we'll make a syscall with CreateProcess(). This should only need
// to happen for PID1.
Ok(ProcessInit {
key: PROCESS_KEY
.with(|pk| *pk.borrow())
.unwrap_or_else(default_process_key),
})
}
pub fn create_process_post<F>(
args: ProcessArgs<F>,
init: ProcessInit,
pid: PID,
) -> core::result::Result<ProcessHandle, crate::Error> {
use std::process::Command;
let server_env = format!("{}", xous_address());
let pid_env = format!("{}", pid);
let process_name_env = args.name.to_string();
let process_key_env = hex::encode(&init.key.0);
let (shell, args) = if cfg!(windows) {
("cmd", ["/C", &args.command])
} else if cfg!(unix) {
("sh", ["-c", &args.command])
} else {
panic!("unrecognized platform -- don't know how to shell out");
};
Command::new(shell)
.args(&args)
.env("XOUS_SERVER", server_env)
.env("XOUS_PID", pid_env)
.env("XOUS_PROCESS_NAME", process_name_env)
.env("XOUS_PROCESS_KEY", process_key_env)
.spawn()
.map(ProcessHandle)
.map_err(|e| {
eprintln!("couldn't start command: {}", e);
crate::Error::InternalError
})
}
pub fn wait_process(mut joiner: ProcessHandle) -> crate::SysCallResult {
joiner
.0
.wait()
.or(Err(crate::Error::InternalError))
.and_then(|e| {
if e.success() {
Ok(crate::Result::Ok)
} else {
Err(crate::Error::UnknownError)
}
})
}
}
pub use process::*;
pub struct WaitHandle<T>(std::thread::JoinHandle<T>);
pub struct ProcessHandle(std::thread::JoinHandle<()>);
#[derive(Clone)]
struct ServerConnection {
@ -214,71 +369,6 @@ pub fn ensure_connection() -> core::result::Result<(), crate::Error> {
})
}
/// If no connection exists, create a new connection to the server. This means
/// our parent PID will be PID1. Otherwise, reuse the same connection.
pub fn create_process_pre<F>(
_args: &ProcessArgs<F>,
) -> core::result::Result<ProcessInit, crate::Error>
where
F: FnOnce(),
{
ensure_connection()?;
// Ensure there is a connection, because after this function returns
// we'll make a syscall with CreateProcess(). This should only need
// to happen for PID1.
Ok(ProcessInit {
key: PROCESS_KEY
.with(|pk| *pk.borrow())
.unwrap_or_else(default_process_key),
})
}
pub fn create_process_post<F>(
mut args: ProcessArgs<F>,
init: ProcessInit,
pid: PID,
) -> core::result::Result<ProcessHandle, crate::Error>
where
F: FnOnce() + Send + 'static,
{
let server_address = xous_address();
let f = args.main.take().unwrap();
let thread_main = std::thread::Builder::new()
.name(args.name)
.spawn(move || {
set_xous_address(server_address);
THREAD_ID.with(|tid| *tid.borrow_mut() = 1);
PROCESS_ID.with(|p| *p.borrow_mut() = pid);
XOUS_SERVER_CONNECTION.with(|xsc| {
let mut xsc = xsc.borrow_mut();
match xous_connect_impl(server_address, &init.key) {
Ok(a) => {
*xsc = Some(a);
Ok(())
}
Err(_) => Err(crate::Error::InternalError),
}
})?;
crate::create_thread(f)
})
.map_err(|_| crate::Error::InternalError)?
.join()
.unwrap()
.unwrap();
Ok(ProcessHandle(thread_main.0))
}
pub fn wait_process(joiner: ProcessHandle) -> crate::SysCallResult {
joiner.0.join().map(|_| Result::Ok).map_err(|_x| {
// panic!("wait error: {:?}", x);
crate::Error::InternalError
})
}
fn xous_connect_impl(
addr: SocketAddr,
key: &ProcessKey,