Add the Occlum-compatible Rust toolchains and a demo
This commit is contained in:
parent
bc6002f6dd
commit
b29aa1d6d0
@ -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/).
|
||||
|
27
demos/rust/README.md
Normal file
27
demos/rust/README.md
Normal file
@ -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
|
||||
```
|
16
demos/rust/run_rust_demo_on_occlum.sh
Executable file
16
demos/rust/run_rust_demo_on_occlum.sh
Executable file
@ -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
|
11
demos/rust/rust_app/Cargo.toml
Normal file
11
demos/rust/rust_app/Cargo.toml
Normal file
@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "rust_app"
|
||||
version = "1.0.0"
|
||||
authors = ["He Sun <bochang.sh@antfin.com>"]
|
||||
build = "build.rs"
|
||||
|
||||
[dependencies]
|
||||
libc = "0.2"
|
||||
|
||||
[build-dependencies]
|
||||
cc = "1.0"
|
8
demos/rust/rust_app/build.rs
Normal file
8
demos/rust/rust_app/build.rs
Normal file
@ -0,0 +1,8 @@
|
||||
extern crate cc;
|
||||
|
||||
fn main() {
|
||||
cc::Build::new()
|
||||
.file("src/util.cpp")
|
||||
.cpp(true)
|
||||
.compile("libutil.a");
|
||||
}
|
12
demos/rust/rust_app/src/main.rs
Normal file
12
demos/rust/rust_app/src/main.rs
Normal file
@ -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);
|
||||
}
|
4
demos/rust/rust_app/src/util.cpp
Normal file
4
demos/rust/rust_app/src/util.cpp
Normal file
@ -0,0 +1,4 @@
|
||||
extern "C"
|
||||
void increment_by_one(int *input) {
|
||||
*input += 1;
|
||||
}
|
@ -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 && \
|
||||
|
@ -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 && \
|
||||
|
31
tools/toolchains/rust/build.sh
Executable file
31
tools/toolchains/rust/build.sh
Executable file
@ -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 <<EOF
|
||||
#!/bin/bash
|
||||
env CC_x86_64_unknown_linux_musl=x86_64-linux-musl-gcc \
|
||||
CCFLAGS_x86_64_unknown_linux_musl="-fPIC -pie -Wl,-rpath,/opt/occlum/toolchains/gcc/x86_64-linux-musl/lib" \
|
||||
CXX_x86_64_unknown_linux_musl=x86_64-linux-musl-g++ \
|
||||
CXXFLAGS_x86_64_unknown_linux_musl="-fPIC -pie -Wl,-rpath,/opt/occlum/toolchains/gcc/x86_64-linux-musl/lib" \
|
||||
RUSTFLAGS="-C target-feature=-crt-static -C linker=occlum-gcc" \
|
||||
CARGO_BUILD_TARGET=x86_64-unknown-linux-musl \
|
||||
cargo "\$@"
|
||||
EOF
|
||||
|
||||
# Generate the wrapper for rustc
|
||||
cat > ${INSTALL_DIR}/bin/occlum-rustc <<EOF
|
||||
#!/bin/bash
|
||||
rustc -C linker=occlum-gcc -C target-feature=-crt-static "\$@" --target=x86_64-unknown-linux-musl
|
||||
EOF
|
||||
|
||||
chmod +x ${INSTALL_DIR}/bin/occlum-cargo
|
||||
chmod +x ${INSTALL_DIR}/bin/occlum-rustc
|
Loading…
Reference in New Issue
Block a user