Skip to content

Commit 30014b3

Browse files
authored
Use Rust stubs instead of C (#28)
* Use Rust stubs instead of C Signed-off-by: Jorge Prendes <jorge.prendes@gmail.com> * Address PR comments Signed-off-by: Jorge Prendes <jorge.prendes@gmail.com> --------- Signed-off-by: Jorge Prendes <jorge.prendes@gmail.com>
1 parent 754139c commit 30014b3

File tree

26 files changed

+418
-373
lines changed

26 files changed

+418
-373
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Justfile

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ PWD := replace(justfile_dir(), "\\", "/")
1111
# * include the stubs required by hyperlight-js-runtime
1212
# * define __wasi__ as this disables threading support in quickjs
1313
export HYPERLIGHT_CFLAGS := \
14-
"-I" + PWD + "/src/hyperlight-js-runtime/stubs/include " + \
14+
"-I" + PWD + "/src/hyperlight-js-runtime/include " + \
1515
"-D__wasi__=1 "
1616

1717
# On Windows, use Ninja generator for CMake to avoid aws-lc-sys build issues with Visual Studio generator
1818
export CMAKE_GENERATOR := if os() == "windows" { "Ninja" } else { "" }
1919

2020
ensure-tools:
21-
cargo install cargo-hyperlight --locked --version 0.1.3
21+
cargo install cargo-hyperlight --locked
2222

2323
# Check if npm is installed, install automatically if missing (Linux)
2424
[private]
@@ -39,8 +39,7 @@ check-license-headers:
3939
./dev/check-license-headers.sh
4040

4141
clippy target=default-target features="": (ensure-tools)
42-
cd src/hyperlight-js-runtime && \
43-
cargo hyperlight clippy \
42+
cargo hyperlight clippy -p hyperlight-js-runtime \
4443
--profile={{ if target == "debug" {"dev"} else { target } }} \
4544
-- -D warnings
4645
cargo clippy --all-targets \

src/hyperlight-js-runtime/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,17 @@ tracing = { version = "0.1.44", default-features = false, features = ["log","att
3333
hyperlight-common = { workspace = true, default-features = false }
3434
hyperlight-guest = { workspace = true }
3535
hyperlight-guest-bin = { workspace = true }
36+
chrono = { version = "0.4", default-features = false }
3637

3738
[target.'cfg(not(hyperlight))'.dependencies]
3839
clap = { version = "4.5", features = ["derive"] }
39-
libc = "0.2"
4040

4141
[target.'cfg(not(hyperlight))'.dev-dependencies]
4242
escargot = "0.5"
4343
tempfile = "3.26"
4444

4545
[build-dependencies]
46-
cc = "1.2"
46+
bindgen = "0.72"
4747

4848
[features]
4949
default = []

src/hyperlight-js-runtime/build.rs

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,38 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
16-
fn main() {
17-
if std::env::var_os("CARGO_CFG_HYPERLIGHT").is_none() {
18-
return;
19-
}
2016

21-
let files = ["stubs/clock.c", "stubs/localtime.c"];
17+
use std::env;
18+
use std::path::PathBuf;
2219

23-
for file in files {
24-
println!("cargo:rerun-if-changed={}", file);
25-
}
20+
use bindgen::RustEdition::Edition2024;
2621

27-
cc::Build::new().files(files).compile("stubs");
22+
fn main() -> Result<(), Box<dyn std::error::Error>> {
23+
let mut bindings = bindgen::builder()
24+
.use_core()
25+
.wrap_unsafe_ops(true)
26+
.rust_edition(Edition2024)
27+
.clang_arg("-D_POSIX_MONOTONIC_CLOCK=1")
28+
.clang_arg("-D_POSIX_C_SOURCE=200809L");
29+
30+
bindings = bindings.header_contents(
31+
"libc.h",
32+
"
33+
#pragma once
34+
#include <errno.h>
35+
#include <stdio.h>
36+
#include <time.h>
37+
",
38+
);
39+
40+
println!("cargo:rerun-if-changed=include");
41+
println!("cargo:rerun-if-changed=include/stdio.h");
42+
println!("cargo:rerun-if-changed=include/time.h");
43+
println!("cargo:rerun-if-changed=include/unistd.h");
44+
45+
// Write the generated bindings to an output file.
46+
let out_path = PathBuf::from(env::var("OUT_DIR")?).join("libc.rs");
47+
bindings.generate()?.write_to_file(out_path)?;
48+
49+
Ok(())
2850
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Copyright 2026 The Hyperlight Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
#pragma once
17+
18+
#include "printf.h"
19+
#include_next "stdio.h"
20+
21+
#define stdout NULL
22+
23+
int putchar(int c);
24+
25+
#define vfprintf(f, ...) vprintf(__VA_ARGS__)
26+
#define fprintf(f, ...) printf(__VA_ARGS__)
27+
#define fputc(c, f) putc((char)(c), f)
28+
29+
int fflush(FILE *f);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Copyright 2026 The Hyperlight Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
#pragma once
17+
18+
#include_next "time.h"
19+
20+
#include <sys/time.h>
21+
#include <sys/types.h>
22+
#include <errno.h>
23+
24+
#define CLOCK_REALTIME 0
25+
#define CLOCK_MONOTONIC 1
26+
27+
int clock_gettime(clockid_t clk_id, struct timespec *tp);
28+
struct tm *localtime_r(const time_t *timer, struct tm *tm);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
Copyright 2026 The Hyperlight Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/

src/hyperlight-js-runtime/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ extern crate alloc;
2020
mod globals;
2121
pub mod host;
2222
mod host_fn;
23+
mod libc;
2324
mod modules;
2425
pub(crate) mod utils;
2526

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
Copyright 2026 The Hyperlight Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
mod bindings {
17+
#![allow(
18+
non_camel_case_types,
19+
non_snake_case,
20+
non_upper_case_globals,
21+
dead_code,
22+
unnecessary_transmutes,
23+
clippy::upper_case_acronyms,
24+
clippy::ptr_offset_with_cast
25+
)]
26+
include!(concat!(env!("OUT_DIR"), "/libc.rs"));
27+
}
28+
29+
pub(crate) use core::ffi::*;
30+
31+
pub(crate) use bindings::*;

src/hyperlight-js-runtime/src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ limitations under the License.
1616
#![cfg_attr(hyperlight, no_std)]
1717
#![cfg_attr(hyperlight, no_main)]
1818

19+
#[cfg(hyperlight)]
20+
mod libc;
21+
1922
#[cfg(hyperlight)]
2023
include!("main/hyperlight.rs");
2124

0 commit comments

Comments
 (0)