Skip to content

examples: add rabe example #42

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

Merged
merged 4 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ jobs:
uses: actions/checkout@v4
with:
submodules: true
- name: "Build Rabe"
run: |
export PATH=$PATH:/root/.cargo/env
rustup default stable
rustup target add wasm32-wasip1
./bin/inv_wrapper.sh rabe
- name: "Build FFmpeg"
run: ./bin/inv_wrapper.sh ffmpeg
- name: "Build ImageMagick"
Expand Down Expand Up @@ -122,7 +128,7 @@ jobs:
shell: bash
env:
FAASM_INI_FILE: ./faasm.ini
FAASM_VERSION: 0.22.0
FAASM_VERSION: 0.27.0
FAASM_WASM_VM: ${{ matrix.faasm_wasm_vm }}
steps:
- uses: csegarragonz/set-compose-version-action@main
Expand Down Expand Up @@ -280,6 +286,12 @@ jobs:
- name: "Run FFmpeg check"
timeout-minutes: 1
run: faasmctl invoke ffmpeg check
# TODO(faasm-bump): this is working, but we need to cut a new faasm release, uncomment
# when we do
# - name: "Run Rabe test"
# if: "contains(env.FAASM_WASM_VM, 'wamr')"
# timeout-minutes: 1
# run: faasmctl invoke rabe test
- name: "Print logs in case of failure"
if: failure()
run: faasmctl logs -s worker
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@
path = examples/Kernels-elastic
url = https://github.com/faasm/Kernels
branch = faasm-elastic
[submodule "examples/rabe"]
path = examples/rabe
url = https://github.com/faasm/rabe.git
branch = faasm
2 changes: 1 addition & 1 deletion cpp
2 changes: 0 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: "3"

services:
build:
image: faasm.azurecr.io/examples-build:${EXAMPLES_BUILD_VERSION}
Expand Down
2 changes: 1 addition & 1 deletion docker/base.dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM ubuntu:22.04 as base
FROM ubuntu:22.04 AS base

RUN apt update \
&& apt install -y \
Expand Down
15 changes: 13 additions & 2 deletions docker/build.dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
ARG CPP_VERSION
ARG EXAMPLES_VERSION
# Base image is not re-built often and tag may lag behind
FROM faasm.azurecr.io/examples-base:0.4.0_0.4.0 as base
FROM faasm.azurecr.io/cpp-sysroot:${CPP_VERSION}
FROM faasm.azurecr.io/examples-base:0.6.0_0.4.0 AS base
FROM faasm.azurecr.io/cpp-sysroot:${CPP_VERSION:-dead}

SHELL ["/bin/bash", "-c"]
ENV IN_DOCKER="on"
Expand All @@ -21,6 +21,12 @@ ARG DEBIAN_FRONTEND=noninteractive
RUN apt update \
&& apt install -y libomp-17-dev

# Install rust
RUN curl --proto '=https' --tlsv1.3 https://sh.rustup.rs -sSf | sh -s -- -y \
&& source ~/.cargo/env \
&& rustup target add wasm32-wasip1


# Fetch the code and update submodules
ARG EXAMPLES_VERSION
RUN mkdir -p code \
Expand All @@ -40,9 +46,11 @@ RUN mkdir -p code \
&& git submodule update --init -f examples/LULESH \
&& git submodule update --init -f examples/libpng \
&& git submodule update --init -f examples/polybench \
&& git submodule update --init -f examples/rabe \
&& git submodule update --init -f examples/tensorflow

# Build the examples and demo functions
ENV PATH=${PATH}:/root/.cargo/bin
RUN cd /code/examples \
&& ./bin/create_venv.sh \
&& source venv/bin/activate \
Expand All @@ -63,14 +71,17 @@ RUN cd /code/examples \
&& inv lammps --migration-net \
&& inv lulesh \
&& inv polybench \
&& inv rabe \
&& inv tensorflow \
# These demo functions link with the cross-compiled static libraries
&& inv func ffmpeg check \
&& inv func lammps chain \
&& inv func mpi migrate \
&& inv func rabe test \
&& inv func tf check

# Prepare bashrc
WORKDIR /code/examples
RUN echo ". /code/examples/bin/workon.sh" >> ~/.bashrc
RUN echo ". $HOME/.cargo/env" >> ~/.bashrc
CMD ["/bin/bash", "-l"]
1 change: 1 addition & 0 deletions examples/rabe
Submodule rabe added at 2c93ec
1 change: 1 addition & 0 deletions func/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ endfunction(faasm_example_func)
add_subdirectory(ffmpeg)
add_subdirectory(lammps)
add_subdirectory(mpi)
add_subdirectory(rabe)
add_subdirectory(tf)
4 changes: 4 additions & 0 deletions func/rabe/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set(FAASM_USER rabe)

faasm_example_func(test test.cpp)
target_link_libraries(rabe_test librabe-cpp.a librabe.a)
29 changes: 29 additions & 0 deletions func/rabe/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <rabe_bindings.hpp>
#include <iostream>
#include <string>

int main()
{
// Create temporary encryption context for this test
auto& ctx = rabe::CpAbeContextWrapper::get(rabe::ContextFetchMode::Create);

// Prepare encryption
std::string plainText = "dance like no one's watching, encrypt like everyone is!";
std::string policy = "\"A\" and \"B\"";
auto cipherText = ctx.cpAbeEncrypt(policy, plainText);

// Prepare decryption
std::vector<std::string> attributes = {"A", "B"};
auto actualPlainText = ctx.cpAbeDecrypt(attributes, cipherText);

// Compare
std::string actualPlainTextStr;
actualPlainTextStr.assign(reinterpret_cast<char*>(actualPlainText.data()), actualPlainText.size());
if (plainText != actualPlainTextStr) {
std::cerr << "Encryption/decryption test failed!" << std::endl;
return -1;
}

std::cout << "Encryption worked!" << std::endl;
return 0;
}
2 changes: 2 additions & 0 deletions tasks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from . import libpng
from . import lulesh
from . import polybench
from . import rabe
from . import tensorflow
from . import wasm

Expand All @@ -30,6 +31,7 @@
libpng,
lulesh,
polybench,
rabe,
tensorflow,
wasm,
)
4 changes: 2 additions & 2 deletions tasks/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ def cli(ctx, clean=False):
"""
service = "build"

if clean:
# Clean existing build
if not exists(DEV_FAASM_LOCAL) or clean:
if exists(DEV_FAASM_LOCAL):
rmtree(DEV_FAASM_LOCAL)

makedirs(DEV_FAASM_LOCAL)

# Populate the local mounts with the existing content
Expand Down
1 change: 1 addition & 0 deletions tasks/func.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def tests(ctx, clean=False):
"""
funcs = [
["ffmpeg", "check"],
["rabe", "test"],
["tf", "check"],
# TODO: this two functions are not used in the tests, as they are used
# to exercise migration, for what we need a distributed test setting
Expand Down
65 changes: 65 additions & 0 deletions tasks/rabe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from faasmtools.build import CMAKE_TOOLCHAIN_FILE, get_faasm_build_env_dict
from invoke import task
from os import environ, makedirs
from os.path import exists, join
from shutil import copy, rmtree
from subprocess import run
from tasks.env import EXAMPLES_DIR


@task(default=True)
def build(ctx, clean=False):
"""
Compile rabe library (in Rust) and C++ bindings into a WASM library
"""
rabe_dir = join(EXAMPLES_DIR, "rabe")

if clean:
rmtree(join(rabe_dir, "target"))

# First, cross-compile the rust library to WASM
cargo_cmd = "cargo build --release --target=wasm32-wasip1"
run(cargo_cmd, shell=True, check=True, cwd=rabe_dir)

# Install it in the WASM sysroot
build_env = get_faasm_build_env_dict()
src_lib = join(rabe_dir, "target", "wasm32-wasip1", "release", "librabe.a")
dst_lib = join(build_env["FAASM_WASM_LIB_INSTALL_DIR"], "librabe.a")
copy(src_lib, dst_lib)

# Build the CPP bindings library, and cross-compile it to WASM
rabe_cpp_dir = join(rabe_dir, "cpp-bindings")
build_dir = join(rabe_cpp_dir, "build")

if clean and exists(build_dir):
rmtree(build_dir)
if not exists(build_dir):
makedirs(build_dir)

cmake_cmd = [
"cmake",
"-GNinja",
"-DCMAKE_BUILD_TYPE=Release",
"-DCMAKE_TOOLCHAIN_FILE={}".format(CMAKE_TOOLCHAIN_FILE),
rabe_cpp_dir,
]
cmake_cmd = " ".join(cmake_cmd)
print(cmake_cmd)

work_env = environ.copy()
work_env.update(get_faasm_build_env_dict())
print(build_dir)
run(cmake_cmd, shell=True, check=True, cwd=build_dir, env=work_env)
run("ninja", shell=True, check=True, cwd=build_dir)

# Install the library in the WASM sysroot
src_lib = join(build_dir, "librabe-cpp.a")
dst_lib = join(build_env["FAASM_WASM_LIB_INSTALL_DIR"], "librabe-cpp.a")
copy(src_lib, dst_lib)

# Install the header in the WASM sysroot too
src_header = join(rabe_cpp_dir, "rabe_bindings.hpp")
dst_header = join(
build_env["FAASM_WASM_HEADER_INSTALL_DIR"], "rabe_bindings.hpp"
)
copy(src_header, dst_header)