Skip to content

Commit

Permalink
examples: add rabe example
Browse files Browse the repository at this point in the history
  • Loading branch information
csegarragonz committed Oct 3, 2024
1 parent dfd6987 commit 8cc1869
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 7 deletions.
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
9 changes: 8 additions & 1 deletion docker/build.dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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/examples-base:0.6.0_0.4.0 AS base
FROM faasm.azurecr.io/cpp-sysroot:${CPP_VERSION}

SHELL ["/bin/bash", "-c"]
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-wasi


# Fetch the code and update submodules
ARG EXAMPLES_VERSION
RUN mkdir -p code \
Expand Down Expand Up @@ -73,4 +79,5 @@ RUN cd /code/examples \
# 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
64 changes: 64 additions & 0 deletions tasks/rabe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
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
# TODO: rename to wasm32-wasip1
cargo_cmd = "cargo build --release --target=wasm32-wasi"
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-wasi", "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)

0 comments on commit 8cc1869

Please sign in to comment.