Skip to content
Open
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
97 changes: 97 additions & 0 deletions ffi_utils/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Copyright 2026 Google LLC
#
# 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("@cxx.rs//tools/bazel:rust_cxx_bridge.bzl", "rust_cxx_bridge")
load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")

package(
default_applicable_licenses = [
],
default_visibility = ["//:internal"],
)

cc_library(
name = "cxx_utils",
hdrs = ["cxx_utils.h"],
deps = [
"@abseil-cpp//absl/strings:string_view",
"@cxx.rs//:core",
],
)

rust_library(
name = "status",
srcs = ["status.rs"],
deps = [
":status_cc",
":status_cxx", # fixdeps: keep
"@cxx.rs//:cxx",
],
)

rust_cxx_bridge(
name = "status_cxx",
src = "status.rs",
deps = [":status_cc"],
)

rust_test(
name = "status_test",
crate = ":status",
deps = [
"@crate_index//:googletest",
],
)

cc_library(
name = "status_cc",
srcs = ["status.cc"],
hdrs = ["status.h"],
deps = [
":status_cxx/include",
"@abseil-cpp//absl/status",
"@abseil-cpp//absl/strings:string_view",
"@cxx.rs//:cxx", # fixdeps: keep
"@cxx.rs//:core",
],
)

cc_library(
name = "status_macros",
hdrs = ["status_macros.h"],
)

cc_library(
name = "status_matchers",
testonly = 1,
srcs = ["status_matchers.cc"],
hdrs = ["status_matchers.h"],
deps = [
":status_macros",
"@googletest//:gtest",
"@abseil-cpp//absl/status",
"@abseil-cpp//absl/status:statusor",
],
)

rust_library(
name = "status_matchers_rs",
testonly = 1,
srcs = ["status_matchers.rs"],
deps = [
":status",
"@crate_index//:googletest",
],
)
51 changes: 51 additions & 0 deletions ffi_utils/cxx_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2026 Google LLC
//
// 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 SECURE_AGGREGATION_FFI_UTILS_CXX_UTILS_H_
#define SECURE_AGGREGATION_FFI_UTILS_CXX_UTILS_H_

#include <cstdint>
#include <memory>
#include <string>

#include "absl/strings/string_view.h"
#include "include/cxx.h"

namespace secure_aggregation {

// Clones a std::string behind a unique_ptr, for compatibility with CXX.
inline std::unique_ptr<std::string> CloneString(const std::string& x) {
return std::make_unique<std::string>(x);
}

// Returns a reference to an empty std::string.
inline const std::string& EmptyString() {
static std::string* x = new std::string();
return *x;
}

// Converts a StringView to an absl::string_view.
inline absl::string_view ToAbslStringView(rust::Slice<const uint8_t> sv) {
return absl::string_view(reinterpret_cast<const char*>(sv.data()), sv.size());
}

// Converts an absl::string_view to a Rust u8 slice.
inline rust::Slice<const uint8_t> ToRustSlice(absl::string_view sv) {
return rust::Slice<const uint8_t>(reinterpret_cast<const uint8_t*>(sv.data()),
sv.size());
}

} // namespace secure_aggregation

#endif // SECURE_AGGREGATION_FFI_UTILS_CXX_UTILS_H_
9 changes: 5 additions & 4 deletions shell_wrapper/status.cc → ffi_utils/status.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "shell_wrapper/status.h"
#include "ffi_utils/status.h"

#include <cstdint>
#include <memory>
Expand All @@ -21,6 +21,7 @@

#include "absl/status/status.h"
#include "absl/strings/string_view.h"
#include "ffi_utils/status.rs.h"
#include "include/cxx.h"

namespace secure_aggregation {
Expand All @@ -45,9 +46,9 @@ absl::Status UnwrapFfiStatus(const FfiStatus& status) {
return absl::Status(static_cast<absl::StatusCode>(status.code), message);
}

} // namespace secure_aggregation

FfiStatus MakeFfiStatus(int code, rust::Slice<const uint8_t> message) {
FfiStatus MakeFfiStatus(int32_t code, rust::Slice<const uint8_t> message) {
return secure_aggregation::MakeFfiStatus(
code, std::string(message.begin(), message.end()));
}

} // namespace secure_aggregation
38 changes: 38 additions & 0 deletions ffi_utils/status.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2025 Google LLC
//
// 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 SECURE_AGGREGATION_FFI_UTILS_STATUS_H_
#define SECURE_AGGREGATION_FFI_UTILS_STATUS_H_

#include <memory>
#include <string>

#include "absl/status/status.h"
#include "include/cxx.h"

namespace secure_aggregation {

struct FfiStatus;

FfiStatus MakeFfiStatus();
FfiStatus MakeFfiStatus(absl::Status status);
FfiStatus MakeFfiStatus(int code, std::string message);
absl::Status UnwrapFfiStatus(const FfiStatus& status);

// To be called from Rust.
FfiStatus MakeFfiStatus(int32_t code, rust::Slice<const uint8_t> message);

} // namespace secure_aggregation

#endif // SECURE_AGGREGATION_FFI_UTILS_STATUS_H_
4 changes: 2 additions & 2 deletions shell_wrapper/status.rs → ffi_utils/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

use std::borrow::Cow;

#[cxx::bridge]
#[cxx::bridge(namespace = "secure_aggregation")]
pub mod ffi {
// A simple Status alternative which is cxx-compatible (because it directly uses unique_ptr).
pub struct FfiStatus {
Expand All @@ -31,7 +31,7 @@ pub mod ffi {
}

unsafe extern "C++" {
include!("shell_wrapper/status.h");
include!("ffi_utils/status.h");
pub fn MakeFfiStatus(code: i32, message: &[u8]) -> FfiStatus;
}
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "shell_wrapper/status_matchers.h"
#include "ffi_utils/status_matchers.h"

#include <ostream>
#include <string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@

#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "ffi_utils/status_macros.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "shell_wrapper/status_macros.h"

namespace secure_aggregation {
namespace secagg_internal {
Expand Down
File renamed without changes.
Loading