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

runner script #53

Merged
merged 1 commit into from
Jan 14, 2019
Merged

runner script #53

merged 1 commit into from
Jan 14, 2019

Conversation

akash-fortanix
Copy link
Contributor

This script encapsulates the three steps listed here under Running.

To trigger this script on cargo run, add .cargo/config file within crate with content:

[target.x86_64-fortanix-unknown-sgx]
runner = "ftxsgx-runner-cargo"

And to set custom values for arguments to ftxsgx-elf2sgxs script, add package.metadata section in Cargo.toml

[package.metadata]
threads=15


let args: Vec<String> = env::args().collect();

let mut ftxsgx_elf2sgxs_command = Command::new("ftxsgx-elf2sgxs");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can have a help option, which could mention something on the line of "ftxsgx-elf2sgxs, ftxsgx-runner etc." should be available in $PATH.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that's necessary. This tool will always be installed alongside those.

Copy link
Contributor

@VardhanThigle VardhanThigle Jan 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how command::new works, would that require Command::new("./ftxsgx-elf2sgxs")

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it will look up the commands in your path

@akash-fortanix
Copy link
Contributor Author

Offline Review Comments:
1. Configuration should be under package.metatdata.fortanix-sgx - Done
2. If a subcommand fails, you should forward the exit code - Done
3. Don't run the commands with .output - Done
4. I think .status works here - Done
5. When a subcommand fails, print out its command line - Check?
6. expect("Unable to execute process") which process? - Done
7. Default heap size should be bigger - experiment and decide (one option: 32MB)

@akash-fortanix
Copy link
Contributor Author

  1. When a subcommand fails, print out its command line - Check?
    Done

Copy link
Contributor Author

@akash-fortanix akash-fortanix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe I need to remove Cargo.lock

});
}

process::exit(0);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unnecessary.

}
None => {
println!("{} is not defined in the environment.", key);
process::exit(1);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is incorrect.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was not sure because in this case the sub-commands don't run, so they can't error out, and so parent process shouldn't have non-zero exit status. But I believe the inability to run the process also classifies as a error.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not running anything is definitely an error, yes.

@akash-fortanix
Copy link
Contributor Author

This PR can be reviewed.

Copy link
Member

@jethrogb jethrogb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Improve error handling.

  1. Add at the top:
#[macro_use]
extern crate failure;
#[macro_use]
extern crate failure_derive;

use failure::{Error, ResultExt};

#[derive(Debug, Fail)]
enum CommandFail {
    #[fail(display = "failed to run {}, {}", _0, _1)]
    Io(String, io::Error),
    #[fail(display = "while running {} got {}", _0, _1)]
    Status(String, ExitStatus),
}

fn run_command(cmd: Command) -> Result<(), CommandFail> {
    match cmd.status() {
        Err(e) => Err(CommandFail::Io(format!("{:?}", cmd), e)),
        Ok(status) if status.success() => Ok(()),
        Ok(status) => Err(CommandFail::Status(format!("{:?}", cmd), status)),
    }
}
  1. Move the code in fn main() into a new function:
fn run() -> Result<(), Error> {
    ...

    Ok(())
}
  1. Replace the match env::var_os(key) block with:
let mut filepath = env::var_os(key)
    .ok_or_else(|| format_err!("{} is not defined in the environment.", key))?;
  1. Replace the first three occurences of .expect(...) by .context(...)?.

  2. Replace the command execution blocks by run_command(...)?.

  3. New fn main():

fn main() {
    if let Err(e) = run() {
        eprintln!("ERROR: {}", e);
        process::exit(match e.downcast_ref::<CommandFail>() {
            Some(CommandFail::Status(_, status)) => status.code().unwrap(),
            _ => 1,
        })
    }
}

@@ -26,3 +26,6 @@ sgx-isa = { version = "0.2.0-rc1", path = "../sgx-isa" }
xmas-elf = "0.6.0" # Apache-2.0/MIT
clap = "2.2.5" # MIT
failure = "0.1.1" # MIT/Apache-2.0
serde_derive = "1.0.84"
serde = "1.0.84"
toml = "0.4.10"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add license info like for the other crates above.

use std::io::Read;
use std::process::{self, Command};

const HEAP_SIZE: u32 = 0x20000;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please increase default heap size

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to 32MB as per last discussion.

const HEAP_SIZE: u32 = 0x20000;
const SSAFRAMESIZE: u32 = 1;
const STACK_SIZE: u32 = 0x20000;
const THREADS: u32 = 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we set default number of threads based on num_cpus crate?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, done.

#[derive(Deserialize, Debug)]
#[serde(rename_all = "kebab-case")]
struct Target {
heap_size: Option<u32>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be u64

.expect("Unable to execute ftxsgx-elf2sgxs");

if !ftxsgx_elf2sgxs_status.success() {
println!("{:?}", ftxsgx_elf2sgxs_command);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat!

if debug {
ftxsgx_elf2sgxs_command.arg("--debug");
}
let mut ftxsgx_elf2sgxs_status = ftxsgx_elf2sgxs_command.status()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think mut is necessary here? And you should get a lint for that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it wasn't necessary, but wasn't getting any lint either.


let mut bin_with_ext = String::new();
bin_with_ext.push_str(&args[1]);
bin_with_ext.push_str(".sgxs");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let bin_with_ext = args[1].clone() + ".sgxs";

Copy link
Member

@jethrogb jethrogb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great now. r=me after minor nits

serde_derive = "1.0.84" # MIT/Apache-2.0
serde = "1.0.84" # MIT/Apache-2.0
toml = "0.4.10" # MIT/Apache-2.0
num_cpus = "1.9.0" # MIT/Apache-2.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing newline. Please configure your editor to automatically add a newline at the end of a file

let key = "CARGO_MANIFEST_DIR";
let mut filepath = env::var_os(key)
.ok_or_else(|| format_err!("{} is not defined in the environment.", key))?;
filepath.push(OsString::from("/Cargo.toml"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can just write filepath.push("/Cargo.toml");

@akash-fortanix
Copy link
Contributor Author

bors r=jethrogb

@bors
Copy link
Contributor

bors bot commented Jan 14, 2019

👎 Rejected by code reviews

@jethrogb
Copy link
Member

bors r+

bors bot added a commit that referenced this pull request Jan 14, 2019
53: runner script r=jethrogb a=akash-fortanix

This script encapsulates the three steps listed [here](#49) under `Running`.

To trigger this script on `cargo run`, add .cargo/config file within crate with content:

> [target.x86_64-fortanix-unknown-sgx]
> runner = "ftxsgx-runner-cargo"

And to set custom values for arguments to `ftxsgx-elf2sgxs` script, add `package.metadata` section in Cargo.toml

> [package.metadata]
> threads=15

Co-authored-by: akashfortanix <[email protected]>
@bors
Copy link
Contributor

bors bot commented Jan 14, 2019

Build succeeded

  • continuous-integration/travis-ci/push

@bors bors bot merged commit 96f2038 into fortanix:master Jan 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants