|
| 1 | +#!/bin/bash -ex |
| 2 | +repo_root=$(readlink -f $(dirname "${BASH_SOURCE[0]}")/..) |
| 3 | + |
| 4 | +function kernel_version { |
| 5 | + kernel=$(uname -r) |
| 6 | + IFS='.' read -ra kernel <<< "${kernel}" |
| 7 | + |
| 8 | + kernel_major=${kernel[0]} |
| 9 | + kernel_minor=${kernel[1]} |
| 10 | +} |
| 11 | + |
| 12 | +function has_vsock_loopback { |
| 13 | + kernel_version |
| 14 | + vsock_loopback=0 |
| 15 | + if [[ 5 -le ${kernel_major} ]]; then |
| 16 | + if [[ 6 -le ${kernel_minor} ]]; then |
| 17 | + if [[ $(lsmod | grep vsock_loopback) ]]; then |
| 18 | + vsock_loopback=1 |
| 19 | + else |
| 20 | + echo "You have an vsock loopback capable kernel, but the vsock_loopback module isn't loaded. Please run \'sudo modprobe vsock_loopback\'" |
| 21 | + exit -1 |
| 22 | + fi |
| 23 | + fi |
| 24 | + fi |
| 25 | +} |
| 26 | + |
| 27 | +function toolchain_version { |
| 28 | + toolchain_version="nightly-2021-09-08-x86_64-unknown-linux-gnu" |
| 29 | +} |
| 30 | + |
| 31 | +function has_tools { |
| 32 | + if [[ $(which musl-gcc) ]]; then |
| 33 | + echo "'musl-gcc' installed correctly" |
| 34 | + else |
| 35 | + echo "'musl-gcc' isn't found. Please run 'sudo apt install musl-tools'" |
| 36 | + exit -1 |
| 37 | + fi |
| 38 | +} |
| 39 | + |
| 40 | +function determine_platform { |
| 41 | + if [[ -z "${NITRO_CLI_BLOBS}" ]]; then |
| 42 | + platform="linux" |
| 43 | + else |
| 44 | + platform="nitro" |
| 45 | + fi |
| 46 | +} |
| 47 | + |
| 48 | +function init { |
| 49 | + kernel_version |
| 50 | + has_vsock_loopback |
| 51 | + toolchain_version |
| 52 | + has_tools |
| 53 | + determine_platform |
| 54 | +} |
| 55 | + |
| 56 | +function compile { |
| 57 | + name=$1 |
| 58 | + VME_TARGET="${TOOLCHAIN_DIR}/rust/rustup/toolchains/${toolchain_version}/lib/rustlib/x86_64-unknown-linux-fortanixvme/x86_64-unknown-linux-fortanixvme.json" |
| 59 | + CC=musl-gcc \ |
| 60 | + RUSTFLAGS="-Clink-self-contained=yes" \ |
| 61 | + cargo +${toolchain_version} build --release --target ${VME_TARGET} -Zbuild-std |
| 62 | + |
| 63 | + # use elf as an output variable |
| 64 | + elf=${repo_root}/fortanix-vme/target/x86_64-unknown-linux-fortanixvme/release/${name} |
| 65 | +} |
| 66 | + |
| 67 | +function cargo_test { |
| 68 | + name=$1 |
| 69 | + pushd ${repo_root}/fortanix-vme/tests/$name |
| 70 | + out=$(mktemp /tmp/$name.out.XXXXX) |
| 71 | + err=$(mktemp /tmp/$name.err.XXXXX) |
| 72 | + |
| 73 | + if [ -f ./test_interaction.sh ]; then |
| 74 | + ./test_interaction.sh & |
| 75 | + test_interaction=$! |
| 76 | + fi |
| 77 | + |
| 78 | + compile ${name} |
| 79 | + |
| 80 | + if [ "${platform}" == "nitro" ]; then |
| 81 | + eif=$(mktemp /tmp/$name.eif.XXXXX) |
| 82 | + elf2eif ${elf} ${eif} |
| 83 | + eif_runner ${eif} ${out} ${err} |
| 84 | + nitro-cli terminate-enclave --all |
| 85 | + |
| 86 | + out=$(tail +12 ${out}) |
| 87 | + err=$(cat ${err} | grep -v "Start.*" || true) |
| 88 | + |
| 89 | + if [ "${out}" != "" ]; then |
| 90 | + echo "Test ${name} Failed" |
| 91 | + echo "Got: ${out}" |
| 92 | + exit -1 |
| 93 | + fi |
| 94 | + |
| 95 | + if [ "${err}" != "" ]; then |
| 96 | + echo "Test ${name} Failed" |
| 97 | + echo "Got: ${err}" |
| 98 | + exit -1 |
| 99 | + else |
| 100 | + echo "Success" |
| 101 | + fi |
| 102 | + else |
| 103 | + ${elf} -- --nocapture |
| 104 | + ${elf} -- --nocapture > ${out} 2> ${err} |
| 105 | + |
| 106 | + out=$(cat ${out} | grep -v "#" || true) |
| 107 | + expected=$(cat ./out.expected) |
| 108 | + |
| 109 | + if [ "${out}" == "${expected}" ]; then |
| 110 | + echo "Test ${name}: Success" |
| 111 | + else |
| 112 | + echo "Test ${name}: Failed" |
| 113 | + echo "Got: ${out}" |
| 114 | + echo "Expected: ${expected}" |
| 115 | + exit -1 |
| 116 | + fi |
| 117 | + fi |
| 118 | + |
| 119 | + if [ -f ./test_interaction.sh ]; then |
| 120 | + kill ${test_interaction} |
| 121 | + fi |
| 122 | + |
| 123 | + popd |
| 124 | +} |
| 125 | + |
| 126 | +function elf2eif { |
| 127 | + enclave_elf=$1 |
| 128 | + enclave_eif=$2 |
| 129 | + |
| 130 | + tmpd=$(mktemp -d) |
| 131 | + echo "FROM alpine" >> ${tmpd}/Dockerfile |
| 132 | + echo "COPY enclave ." >> ${tmpd}/Dockerfile |
| 133 | + echo "CMD ./enclave" >> ${tmpd}/Dockerfile |
| 134 | + |
| 135 | + # Build eif image |
| 136 | + cp ${enclave_elf} ${tmpd}/enclave |
| 137 | + nitro-cli build-enclave --docker-dir ${tmpd} --docker-uri enclave --output-file ${enclave_eif} |
| 138 | +} |
| 139 | + |
| 140 | +function stop_enclaves { |
| 141 | + if [[ ${nitro_platform} -eq 1 ]]; then |
| 142 | + nitro-cli terminate-enclave --all || true |
| 143 | + fi |
| 144 | +} |
| 145 | + |
| 146 | +function eif_runner { |
| 147 | + enclave_eif=$1 |
| 148 | + out=$2 |
| 149 | + err=$3 |
| 150 | + |
| 151 | + # Configure parent, if it hadn't been already |
| 152 | + nitro-cli-config -t 2 -m 512 > /dev/null 2> /dev/null || true |
| 153 | + |
| 154 | + nitro-cli describe-enclaves |
| 155 | + |
| 156 | + echo "running $1" |
| 157 | + # Run enclave |
| 158 | + nitro-cli run-enclave --eif-path ${enclave_eif} --cpu-count 2 --memory 512 --debug-mode > ${out} 2> ${err} |
| 159 | +} |
| 160 | + |
| 161 | +init |
0 commit comments