Skip to content

Commit 8cbcbec

Browse files
committed
Initial commit
Signed-off-by: Jukka Julku <[email protected]>
0 parents  commit 8cbcbec

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+5437
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/target
2+
Cargo.lock
3+
*~

Cargo.toml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
cargo-features = ["per-package-target"]
2+
3+
[workspace]
4+
members = [
5+
"libedge",
6+
"libeapp",
7+
"libhapp",
8+
]
9+
10+
[workspace.package]
11+
version = "0.2.0"
12+
edition = "2021"
13+
authors = ["Jukka Julku <[email protected]>",
14+
"Markku Kylänpää <[email protected]>"]
15+
repository = "https://github.com/vector-sdk/rust-sdk"
16+
readme = "README.md"
17+
license = "MIT"
18+
description = "Rust SDK for Keystone Enclaves"
19+
20+
[profile.dev]
21+
opt-level = 1
22+
debug = false
23+
24+
[profile.release]
25+
opt-level = 1
26+
debug = false

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 VTT Technical Research Centre of Finland Ltd.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Makefile

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# List of subdirectories. Not used for build, but cleanup!
2+
SUBDIRS := .cargo \
3+
libedge \
4+
libedge/src \
5+
libeapp \
6+
libeapp/.cargo \
7+
libeapp/macros \
8+
libeapp/macros/src \
9+
libeapp/src \
10+
libeapp/src/internal \
11+
libhapp \
12+
libhapp/.cargo \
13+
libhapp/src \
14+
libhapp/src/internal \
15+
16+
# Target architecture for the host application libraries
17+
TARGET_ARCH := riscv64gc-unknown-linux-gnu
18+
x86_64: TARGET_ARCH := x86_64-unknown-linux-gnu
19+
20+
# Common build options. Always build for release to minimize binary size!
21+
CARGO_FLAGS := -v --release
22+
CARGO_DEBUG_FLAGS := --features debug_memory
23+
24+
dir2tgt = $(patsubst %, ./%/$(strip $(2)),$(strip $(1)))
25+
26+
all: riscv64
27+
28+
riscv64:
29+
cargo build --target $(TARGET_ARCH) $(CARGO_FLAGS)
30+
31+
# This target builds the host application libraries for x86_64 architecture.
32+
# Enclave application libraries will still be built for the RISC-V target.
33+
x86_64:
34+
cargo build --target $(TARGET_ARCH) $(CARGO_FLAGS)
35+
36+
# Build with SDK's internal enclave memory debugging support
37+
debug:
38+
cargo build --target $(TARGET_ARCH) $(CARGO_DEBUG_FLAGS) $(CARGO_FLAGS)
39+
40+
# Clean build and temporary files:
41+
clean:
42+
cargo clean
43+
rm -f *~ $(call dir2tgt, $(strip $(SUBDIRS)), *~)

README.md

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# A Rust SDK for building Keystone enclave applications
2+
3+
This project aims to provide a Rust programming language SDK for building
4+
[Keystone](https://keystone-enclave.org/) enclave and host applications for the
5+
RISC-V architecture.
6+
7+
The SDK is designed to be compatible with the
8+
[Keystone Eyrie Modular Runtime](https://github.com/keystone-enclave/keystone-runtime).
9+
10+
**NOTE**: This work is experimental and on a very early stage. The security of
11+
the API or its implementation has not been properly verified yet.
12+
Therefore, **Do not use in production!**
13+
14+
# Prequisites
15+
16+
### Bulding Keystone
17+
18+
Download and build [Keystone](https://github.com/keystone-enclave/keystone) for QEMU
19+
environment using [instructions](http://docs.keystone-enclave.org/en/latest/Getting-Started/Install-Dependencies.html)
20+
given in [Keystone documentation](http://docs.keystone-enclave.org).
21+
22+
**NOTE:** The code in this repository has only been tested using QEMU!
23+
24+
### Install Rust
25+
26+
This project uses experimental Rust features currently only available in Rust's
27+
nightly build.
28+
29+
Use [rustup](https://www.rust-lang.org/tools/install) to install the Rust
30+
environment and add required RISC-V targets:
31+
32+
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
33+
rustup default nightly
34+
rustup target add riscv64gc-unknown-none-elf
35+
rustup target add riscv64gc-unknown-linux-gnu
36+
37+
# Build SDK crates
38+
39+
cargo build --release
40+
41+
# Test
42+
43+
Test with Rust SDK [demo application](https://github.com/vector-sdk/rust-sdk-demo)!

libeapp/.cargo/config

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[build]
2+
target = "riscv64gc-unknown-none-elf"

libeapp/.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/target
2+
Cargo.lock
3+
*~

libeapp/Cargo.toml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
cargo-features = ["per-package-target"]
2+
3+
[package]
4+
name = "eapp"
5+
version.workspace = true
6+
edition.workspace = true
7+
authors.workspace = true
8+
repository.workspace = true
9+
readme.workspace = true
10+
license.workspace = true
11+
description = """
12+
A library for enclave application development
13+
"""
14+
forced-target = "riscv64gc-unknown-none-elf"
15+
16+
[dependencies]
17+
buddy_system_allocator = "0.8"
18+
spin = {version = "0.9.3" }
19+
20+
[dependencies.eapp-macros]
21+
path = "./macros"
22+
23+
[dependencies.edge]
24+
path = "../libedge"
25+
26+
[features]
27+
default = ["heap", "heap_rt"]
28+
heap = [] # Application has heap available
29+
heap_rt = ["heap"] # Runtime has heap available
30+
debug_memory = ["heap", "edge/debug_memory"]

libeapp/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# libeapp
2+
3+
A Rust library for building enclave applications

libeapp/build.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//! Build options
2+
//
3+
// SPDX-License-Identifier: MIT
4+
// Copyright (C) 2022 VTT Technical Research Centre of Finland Ltd
5+
6+
fn main() {
7+
// Always link statically as no libraries are available in the enclave
8+
println!("cargo:rustc-link-arg=-static");
9+
// No standard libraries available in the enclave
10+
println!("cargo:rustc-link-arg=-nostdlib");
11+
}

libeapp/eapp.lds

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/* An example linker script for Rust Keystone enclave application
2+
*
3+
* SPDX-License-Identifier: MIT
4+
* Copyright (C) 2022 VTT Technical Research Centre of Finland Ltd
5+
*/
6+
OUTPUT_ARCH( "riscv" )
7+
8+
/* Use linker argument '--defsym HEAP_SIZE=<size>' to alter heap size. The size
9+
* must a multiple of page size (4096 bytes). Default value is 0. */
10+
EXTERN(HEAP_SIZE)
11+
12+
ASSERT(DEFINED(HEAP_SIZE) ? HEAP_SIZE % 4096 == 0 : 1,
13+
"HEAP_SIZE must be multiple of page size (4096 bytes)")
14+
15+
/* Use linker argument '--defsym ECALL_INPUT_SIZE=<size>' to alter the amount of
16+
* space reserved for ecall input buffer. This option is only needed if feature
17+
* "heap_rt" is NOT used. Default value is 0.
18+
*/
19+
EXTERN(ECALL_INPUT_SIZE)
20+
21+
/* Use linker argument '--defsym ECALL_OUTPUT_SIZE=<size>' to alter the amount
22+
* of space reserved for ecall output buffer. This option is only needed if
23+
* feature "heap_rt" is NOT used. Default value is 0.
24+
*/
25+
EXTERN(ECALL_OUTPUT_SIZE)
26+
27+
/* ECall header is always 20 bytes */
28+
ECALL_HEADER_LENGTH = 24;
29+
/* Length of the ecall input buffer in bytes */
30+
ECALL_IBUF_SIZE = DEFINED(ECALL_INPUT_SIZE)
31+
? ECALL_INPUT_SIZE + ECALL_HEADER_LENGTH
32+
: 0;
33+
/* Length of the ecall output buffer in bytes */
34+
ECALL_OBUF_SIZE = DEFINED(ECALL_OUTPUT_SIZE)
35+
? ECALL_OUTPUT_SIZE + ECALL_HEADER_LENGTH
36+
: 0;
37+
38+
/* Program's actual entry point before eapp_entry. Defined in libeapp */
39+
ENTRY(_start)
40+
41+
PHDRS
42+
{
43+
/* phdrs PT_PHDR PHDRS; */
44+
text PT_LOAD FILEHDR PHDRS FLAGS(5);
45+
data PT_LOAD FLAGS (6);
46+
alloc PT_LOAD FLAGS (6);
47+
bss PT_LOAD;
48+
}
49+
50+
SECTIONS
51+
{
52+
/* The text section must be aligned to page boundary. Keystone
53+
* runtime will check it.
54+
*/
55+
. = 0x00001000;
56+
.text : {
57+
/* The _start function should always be at address 0x00001000 */
58+
*(.text._start)
59+
*(.text)
60+
*(.text.*)
61+
} : text
62+
.rodata :
63+
{
64+
*(.rdata)
65+
*(.rodata)
66+
*(.rodata.*)
67+
} /* defaults to .text */
68+
. = ALIGN(0x1000);
69+
.data : { *(.data) } : data
70+
.debug : { *(.debug) }
71+
72+
/* Ecall buffers, in case feature "heap_rt" is not used */
73+
. = ALIGN(0x1000);
74+
.ecall_zone (NOLOAD):
75+
{
76+
*(.ecall_zone);
77+
PROVIDE(__ecall_inbuf_end = DEFINED(__ecall_inbuf_start)
78+
? . + ECALL_IBUF_SIZE
79+
: . );
80+
PROVIDE(__ecall_outbuf_start = DEFINED(__ecall_inbuf_start)
81+
? . + ECALL_IBUF_SIZE
82+
: . );
83+
PROVIDE(__ecall_outbuf_end = DEFINED(__ecall_inbuf_start)
84+
? . + ECALL_IBUF_SIZE + ECALL_OBUF_SIZE
85+
: . );
86+
/* This statement enforces correct section size since the location counter
87+
* is not moved otherwise (only symbols are defined). However, if none of
88+
* the symbols are needed and .ecall_zone is effectively empty, this
89+
* statement also causes the linker not to remove the section (TODO?)
90+
*/
91+
. = DEFINED(__ecall_inbuf_start)
92+
? . + ECALL_IBUF_SIZE + ECALL_OBUF_SIZE
93+
: . ;
94+
} : alloc
95+
96+
/* Section .malloc_zone and its symbols are used by the tiny malloc
97+
* included in the Keystone enclave application libraries.
98+
*/
99+
. = ALIGN(0x1000);
100+
.malloc_zone (NOLOAD):
101+
{
102+
__malloc_zone_start = .;
103+
/* '__malloc_start' defined in libeapp will be placed here: */
104+
*(.malloc_zone);
105+
PROVIDE(__malloc_start = .);
106+
. = DEFINED(HEAP_SIZE) ? (. + HEAP_SIZE) : . ;
107+
PROVIDE(__malloc_zone_stop = .);
108+
} : alloc
109+
110+
. = ALIGN(0x4);
111+
.bss : { *(.bss)
112+
*(.bss.*) } : bss
113+
114+
/* Unless specified, the linker by default outputs the .eh_frame section
115+
* before the text section, which moves the text section from the page
116+
* boundary, causing trouble with Eyrie, as the _start function will shift
117+
* from its intended address 0x00001000.
118+
*/
119+
/DISCARD/ : {
120+
*(.eh_frame)
121+
}
122+
_end = .;
123+
}

libeapp/macros/Cargo.toml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "eapp-macros"
3+
version.workspace = true
4+
edition.workspace = true
5+
authors.workspace = true
6+
repository.workspace = true
7+
readme.workspace = true
8+
license.workspace = true
9+
description = """
10+
A macro for defining enclave application entry points
11+
"""
12+
13+
[lib]
14+
proc-macro = true
15+
16+
[dependencies]
17+
syn = { version = "1.0.86", features = ["full", "extra-traits"] }
18+
quote = "1.0.15"

0 commit comments

Comments
 (0)