Skip to content

Commit

Permalink
runtime/glue: add C++->Rust FFI glue code
Browse files Browse the repository at this point in the history
Use a mutable global to hold references to the Rust runtime, and to the
C++ pseudo-Node factory function that's registered.

Use a local handle mapping to allow opaque Rust types to be
recovered.
  • Loading branch information
daviddrysdale committed Apr 7, 2020
1 parent 10d346d commit 39fe05a
Show file tree
Hide file tree
Showing 7 changed files with 484 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ members = [
"experimental/split_grpc/proxy",
"experimental/split_grpc/server",
"oak/server/rust/oak_abi",
"oak/server/rust/oak_glue",
"oak/server/rust/oak_runtime",
"runner",
"sdk/rust/oak",
Expand Down
58 changes: 58 additions & 0 deletions oak/server/rust/oak_glue/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#
# Copyright 2019 The Project Oak Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

load("@io_bazel_rules_rust//rust:rust.bzl", "rust_library")
load("@rules_cc//cc:defs.bzl", "cc_library")

package(
default_visibility = ["//oak/server:__subpackages__"],
licenses = ["notice"],
)

rust_library(
name = "oak_glue",
srcs = glob(["src/**/*.rs"]),
crate_type = "staticlib",
edition = "2018",
deps = [
"//cargo:byteorder",
"//cargo:lazy_static",
"//cargo:log",
"//cargo:prost",
"//cargo:simple_logger",
"//oak/server/rust/oak_abi",
"//oak/server/rust/oak_runtime",
],
)

# Wrapper rule to expose the resulting static library as a statically linked cc_library and
# corresponding header so that it can be depended on by other cc_library and cc_binary rules.
#
# TODO: There seems to be something wrong with this rule related to caching in Bazel.
# To reproduce:
# - change src/lib.rs and introduce a syntax error
# - bazel build :oak_glue_wrapper
# - this should produce a compile error, instead Bazel is still caching the old artifact
#
# However, building a cc_binary target (e.g. //oak/server/dev:dev_oak_runner) does force
# a rebuild of the Rust code.
cc_library(
name = "oak_glue_wrapper",
srcs = [":oak_glue"],
hdrs = ["oak_glue.h"],
linkopts = ["-ldl"],
linkstatic = True,
)
21 changes: 21 additions & 0 deletions oak/server/rust/oak_glue/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "oak_glue"
version = "0.1.0"
authors = ["David Drysdale <[email protected]>"]
edition = "2018"
license = "Apache-2.0"

[lib]
name = "oak_glue"

[dependencies]
# Note that if new dependencies are added here:
# - they need to be synced to //cargo/Cargo.toml
# - `cd cargo && cargo raze` needs re-running, and the results checked in
byteorder = "*"
lazy_static = "*"
log = { version = "*", features = ["std"] }
oak_abi = { version = "=0.1.0" }
oak_runtime = { version = "=0.1.0" }
prost = "*"
simple_logger = "*"
56 changes: 56 additions & 0 deletions oak/server/rust/oak_glue/oak_glue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2019 The Project Oak Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef OAK_SERVER_RUST_OAK_GLUE_H_
#define OAK_SERVER_RUST_OAK_GLUE_H_

#include <cstdint>

extern "C" {

// Perform start of day initialization. Must only be called once, before
// any other functions.
void glue_init();

// Function pointer used for up-calling from Rust to C++ to create and run a pseudo-Node.
// - The data parameter has the factory_data value registered at glue_start.
// - [name, name+name_len) gives the config name for the Node to be started.
// - node_id should be used for all glue_<fn>() invocations by the new Node.
// - handle is a read half of an initial channel.
typedef void (*node_factory)(uintptr_t data, const char* name, uint32_t name_len, uint64_t node_id,
uint64_t handle);

// Start the Rust runtime, passing a serialized ApplicationConfiguration
// protobuf message.
uint64_t glue_start(const uint8_t* config_buf, uint32_t config_len, node_factory factory,
uintptr_t factory_data);
// Stop the Rust runtime.
void glue_stop();

// The following functions are analogous to those on the Oak ABI, with the
// addition of an initial node_id parameter that identifies the Node performing
// the operation.
uint32_t glue_wait_on_channels(uint64_t node_id, uint8_t* buf, uint32_t count);
uint32_t glue_channel_read(uint64_t node_id, uint64_t handle, uint8_t* buf, uint32_t size,
uint32_t* actual_size, uint8_t* handle_buf, uint32_t handle_count,
uint32_t* actual_handle_count);
uint32_t glue_channel_write(uint64_t node_id, uint64_t handle, const uint8_t* buf, uint32_t size,
const uint8_t* handle_buf, uint32_t handle_count);
uint32_t glue_channel_create(uint64_t node_id, uint64_t* write, uint64_t* read);
uint32_t glue_channel_close(uint64_t node_id, uint64_t handle);
};

#endif // OAK_SERVER_RUST_OAK_GLUE_H_
Loading

0 comments on commit 39fe05a

Please sign in to comment.