Skip to content

Commit

Permalink
Setup bazel to build a WASM version of the attestation library
Browse files Browse the repository at this point in the history
Right now the rust code is wholly a placeholder, this CR just sets up all the bazel logic needed.

Bug-Id:
Change-Id: Id451b4163bfbf66f70f12b6c3b9cb0a4bfbb113c
  • Loading branch information
jul-sh committed Jul 15, 2024
1 parent b82c5f4 commit 5811410
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 0 deletions.
28 changes: 28 additions & 0 deletions oak_attestation_explain_wasm/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2020 The Bazel Authors. All rights reserved.
#
# 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("@rules_rust//rust:defs.bzl", "rust_shared_library")

package(
default_visibility = ["//visibility:public"],
licenses = ["notice"],
)

rust_shared_library(
name = "oak_attestation_explain_wasm",
srcs = glob(["src/*.rs"]),
platform = "//:wasm32-unknown-unknown",
)

exports_files(["index.html"])
1 change: 1 addition & 0 deletions oak_attestation_explain_wasm/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Web version of the Oak attestation explaination tool, via WASM.
20 changes: 20 additions & 0 deletions oak_attestation_explain_wasm/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!doctype html>
<html>
<head>
<title>WASM Test</title>
</head>

<body>
<script>
const importObj = {};
(async () => {
const response = await fetch('./oak_attestation_explain_wasm.wasm');
const { instance } = await WebAssembly.instantiateStreaming(
response,
importObj,
);
console.log(instance.exports.nth_prime(10));
})();
</script>
</body>
</html>
79 changes: 79 additions & 0 deletions oak_attestation_explain_wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//
// Copyright 2022 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.
//

// TODO: b/351012006 - Load the attestation library.
// Right now this is just a placeholder for WASM logic without generated
// bindings. From: https://surma.dev/things/rust-to-webassembly/

#![no_std]
extern crate alloc;
use alloc::{alloc::Layout, vec::Vec};
use core::{alloc::GlobalAlloc, cell::UnsafeCell};

const ARENA_SIZE: usize = 128 * 1024;

#[repr(C, align(32))]
struct SimpleAllocator {
arena: UnsafeCell<[u8; ARENA_SIZE]>,
head: UnsafeCell<usize>,
}

impl SimpleAllocator {
const fn new() -> Self {
SimpleAllocator { arena: UnsafeCell::new([0; ARENA_SIZE]), head: UnsafeCell::new(0) }
}
}

unsafe impl Sync for SimpleAllocator {}

#[global_allocator]
static ALLOCATOR: SimpleAllocator = SimpleAllocator::new();

unsafe impl GlobalAlloc for SimpleAllocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let size = layout.size();
let align = layout.align();
let idx = (*self.head.get()).next_multiple_of(align);
*self.head.get() = idx + size;
let arena: &mut [u8; ARENA_SIZE] = &mut (*self.arena.get());
match arena.get_mut(idx) {
Some(item) => item as *mut u8,
_ => core::ptr::null_mut(),
}
}

unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {
/* lol */
}
}

#[panic_handler]
fn panic(_panic: &core::panic::PanicInfo<'_>) -> ! {
core::arch::wasm32::unreachable()
}

#[no_mangle]
pub extern "C" fn nth_prime(n: usize) -> usize {
let mut primes: Vec<usize> = Vec::new();
let mut current = 2;
while primes.len() < n {
if !primes.iter().any(|prime| current % prime == 0) {
primes.push(current);
}
current += 1;
}
primes.into_iter().last().unwrap_or(0)
}

0 comments on commit 5811410

Please sign in to comment.