From b29aa1d6d080c6f364d160f59620294794d3823b Mon Sep 17 00:00:00 2001 From: He Sun Date: Tue, 19 May 2020 15:13:10 +0800 Subject: [PATCH] Add the Occlum-compatible Rust toolchains and a demo --- demos/README.md | 1 + demos/rust/README.md | 27 +++++++++++++++++++++++ demos/rust/run_rust_demo_on_occlum.sh | 16 ++++++++++++++ demos/rust/rust_app/Cargo.toml | 11 ++++++++++ demos/rust/rust_app/build.rs | 8 +++++++ demos/rust/rust_app/src/main.rs | 12 +++++++++++ demos/rust/rust_app/src/util.cpp | 4 ++++ tools/docker/Dockerfile.centos7.5 | 6 ++++++ tools/docker/Dockerfile.ubuntu18.04 | 6 ++++++ tools/toolchains/rust/build.sh | 31 +++++++++++++++++++++++++++ 10 files changed, 122 insertions(+) create mode 100644 demos/rust/README.md create mode 100755 demos/rust/run_rust_demo_on_occlum.sh create mode 100644 demos/rust/rust_app/Cargo.toml create mode 100644 demos/rust/rust_app/build.rs create mode 100644 demos/rust/rust_app/src/main.rs create mode 100644 demos/rust/rust_app/src/util.cpp create mode 100755 tools/toolchains/rust/build.sh diff --git a/demos/README.md b/demos/README.md index 24ec7398..c5635f95 100644 --- a/demos/README.md +++ b/demos/README.md @@ -19,6 +19,7 @@ This set of demos shows how real-world apps can be easily run inside SGX enclave * [grpc](grpc/): A client and server communicating through [gRPC](https://grpc.io/). * [openvino](openvino/) A benchmark of [OpenVINO Inference Engine](https://docs.openvinotoolkit.org/2019_R3/_docs_IE_DG_inference_engine_intro.html). * [python](python/) A demo of [Python](https://www.python.org). +* [rust](rust/) A demo of [Rust](https://www.rust-lang.org/). * [sqlite](sqlite/) A demo of [SQLite](https://www.sqlite.org) SQL database engine. * [tensorflow_lite](tensorflow_lite/): A demo and benchmark of [Tensorflow Lite](https://www.tensorflow.org/lite) inference engine. * [xgboost](xgboost/): A demo of [XGBoost](https://xgboost.readthedocs.io/en/latest/). diff --git a/demos/rust/README.md b/demos/rust/README.md new file mode 100644 index 00000000..69051136 --- /dev/null +++ b/demos/rust/README.md @@ -0,0 +1,27 @@ +# Use Rust with Occlum + +This directory contains scripts and source code to demonstrate how to +compile and run Rust programs on Occlum. + +## occlum-cargo and occlum-rustc + +We introduce cargo and rustc wrappers called occlum-cargo and occlum-rustc +respectively. They wrap the original commands with options specific to occlum. +Refer to tools/toolchains/rust/build.sh for more information. + +## rust\_app + +This directory contains source code of a Rust program with a cpp FFI. The cpp +interface increments the input by one. Rust code calls the function and +displays the result on the terminal. + +One can use occlum-cargo in the way cargo is used. In the rust\_app directory, +calling ```occlum-cargo build``` will build the demo and ```occlum-cargo run``` +will run the demo on host. To run the demo in occlum, run: +``` +run_rust_demo_on_occlum.sh +``` +The output will be displayed on the terminal: +``` +5 + 1 = 6 +``` diff --git a/demos/rust/run_rust_demo_on_occlum.sh b/demos/rust/run_rust_demo_on_occlum.sh new file mode 100755 index 00000000..116132d0 --- /dev/null +++ b/demos/rust/run_rust_demo_on_occlum.sh @@ -0,0 +1,16 @@ +#!/bin/bash +set -e + +# compile rust_app +pushd rust_app +occlum-cargo build +popd + +# initialize occlum workspace +rm -rf occlum_context && mkdir occlum_context && cd occlum_context + +occlum init +cp ../rust_app/target/x86_64-unknown-linux-musl/debug/rust_app image/bin + +occlum build +occlum run /bin/rust_app diff --git a/demos/rust/rust_app/Cargo.toml b/demos/rust/rust_app/Cargo.toml new file mode 100644 index 00000000..7473eaaf --- /dev/null +++ b/demos/rust/rust_app/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "rust_app" +version = "1.0.0" +authors = ["He Sun "] +build = "build.rs" + +[dependencies] +libc = "0.2" + +[build-dependencies] +cc = "1.0" diff --git a/demos/rust/rust_app/build.rs b/demos/rust/rust_app/build.rs new file mode 100644 index 00000000..30a10bfe --- /dev/null +++ b/demos/rust/rust_app/build.rs @@ -0,0 +1,8 @@ +extern crate cc; + +fn main() { + cc::Build::new() + .file("src/util.cpp") + .cpp(true) + .compile("libutil.a"); +} diff --git a/demos/rust/rust_app/src/main.rs b/demos/rust/rust_app/src/main.rs new file mode 100644 index 00000000..c55841cb --- /dev/null +++ b/demos/rust/rust_app/src/main.rs @@ -0,0 +1,12 @@ +extern crate libc; + +extern "C" { + fn increment_by_one(input: *mut libc::c_int); +} + +fn main() { + let mut input = 5; + let old = input; + unsafe { increment_by_one(&mut input) }; + println!("{} + 1 = {}", old, input); +} diff --git a/demos/rust/rust_app/src/util.cpp b/demos/rust/rust_app/src/util.cpp new file mode 100644 index 00000000..6955d542 --- /dev/null +++ b/demos/rust/rust_app/src/util.cpp @@ -0,0 +1,4 @@ +extern "C" +void increment_by_one(int *input) { + *input += 1; +} diff --git a/tools/docker/Dockerfile.centos7.5 b/tools/docker/Dockerfile.centos7.5 index 9138ed15..adfa5220 100644 --- a/tools/docker/Dockerfile.centos7.5 +++ b/tools/docker/Dockerfile.centos7.5 @@ -100,6 +100,12 @@ WORKDIR /tmp RUN ./build.sh ENV PATH="/usr/local/occlum/golang/bin:$PATH" +# Install Occlum Rust toolchain +COPY toolchains/rust/* /tmp/ +WORKDIR /tmp +RUN ./build.sh +ENV PATH="/opt/occlum/toolchains/rust/bin:$PATH" + # Install the latest version of Occlum WORKDIR /root RUN git clone https://github.com/occlum/occlum && \ diff --git a/tools/docker/Dockerfile.ubuntu18.04 b/tools/docker/Dockerfile.ubuntu18.04 index ede544ac..792ee9f0 100644 --- a/tools/docker/Dockerfile.ubuntu18.04 +++ b/tools/docker/Dockerfile.ubuntu18.04 @@ -86,6 +86,12 @@ WORKDIR /tmp RUN ./build.sh ENV PATH="/usr/local/occlum/golang/bin:$PATH" +# Install Occlum Rust toolchain +COPY toolchains/rust/* /tmp/ +WORKDIR /tmp +RUN ./build.sh +ENV PATH="/opt/occlum/toolchains/rust/bin:$PATH" + # Install the latest version of Occlum WORKDIR /root RUN git clone https://github.com/occlum/occlum && \ diff --git a/tools/toolchains/rust/build.sh b/tools/toolchains/rust/build.sh new file mode 100755 index 00000000..2d790703 --- /dev/null +++ b/tools/toolchains/rust/build.sh @@ -0,0 +1,31 @@ +#!/bin/bash +THIS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" +INSTALL_DIR=/opt/occlum/toolchains/rust + +mkdir -p ${INSTALL_DIR}/bin + +rustup target add x86_64-unknown-linux-musl + +# Generate the wrapper for Cargo +# Use -crt-static to dynamically link the application to musl libc library. +# Refer to https://github.com/rust-lang/rfcs/blob/master/text/1721-crt-static.md +# for more information about crt-static +cat > ${INSTALL_DIR}/bin/occlum-cargo < ${INSTALL_DIR}/bin/occlum-rustc <