@@ -16,48 +16,25 @@ limitations under the License.
1616
1717use alloc:: string:: String ;
1818use alloc:: vec;
19- use core:: ffi:: * ;
19+ use core:: ffi:: { c_int , c_long , c_void } ;
2020use core:: sync:: atomic:: { AtomicU64 , Ordering } ;
2121
2222use hyperlight_common:: flatbuffer_wrappers:: function_types:: { ParameterValue , ReturnType } ;
2323
2424use crate :: host_comm:: call_host_function;
25+ use crate :: libc:: {
26+ CLOCK_MONOTONIC , CLOCK_REALTIME , EBADF , EINVAL , EIO , ENOSYS , clockid_t, errno, timespec,
27+ timeval,
28+ } ;
2529
26- unsafe extern "C" {
27- static mut errno: c_int ;
28- }
29-
30- fn set_errno ( val : c_int ) {
30+ fn set_errno ( val : u32 ) {
3131 // SAFETY: single-threaded guest, errno is a global int (__GLOBAL_ERRNO)
32- unsafe { errno = val } ;
32+ // Bindgen uses u32 for the errno definitions, so we convert it to the expected c_int type here
33+ unsafe { errno = val as _ } ;
3334}
3435
35- // POSIX errno values (matching picolibc sys/errno.h)
36- const EINVAL : c_int = 22 ;
37- const EIO : c_int = 5 ;
38- const EBADF : c_int = 9 ;
39- const ENOSYS : c_int = 88 ;
40-
41- // picolibc clock IDs (from time.h)
42- const CLOCK_REALTIME : c_ulong = 1 ;
43- const CLOCK_MONOTONIC : c_ulong = 4 ;
44-
4536static CURRENT_TIME : AtomicU64 = AtomicU64 :: new ( 0 ) ;
4637
47- /// Matches picolibc `struct timespec` layout for x86_64 and aarch64.
48- #[ repr( C ) ]
49- pub ( crate ) struct Timespec {
50- tv_sec : c_long ,
51- tv_nsec : c_long ,
52- }
53-
54- /// Matches picolibc `struct timeval` layout for x86_64 and aarch64.
55- #[ repr( C ) ]
56- pub ( crate ) struct Timeval {
57- tv_sec : c_long ,
58- tv_usec : c_long ,
59- }
60-
6138/// Returns a synthetic monotonically-increasing time starting at Unix epoch
6239/// increasing 1s each call.
6340fn current_time ( ) -> ( u64 , u64 ) {
@@ -66,7 +43,7 @@ fn current_time() -> (u64, u64) {
6643}
6744
6845#[ unsafe( no_mangle) ]
69- pub extern "C" fn read ( fd : c_int , buf : * mut c_void , count : usize ) -> isize {
46+ extern "C" fn read ( fd : c_int , buf : * mut c_void , count : usize ) -> isize {
7047 if buf. is_null ( ) && count > 0 {
7148 set_errno ( EINVAL ) ;
7249 return -1 ;
@@ -81,7 +58,7 @@ pub extern "C" fn read(fd: c_int, buf: *mut c_void, count: usize) -> isize {
8158}
8259
8360#[ unsafe( no_mangle) ]
84- pub extern "C" fn write ( fd : c_int , buf : * const c_void , count : usize ) -> isize {
61+ extern "C" fn write ( fd : c_int , buf : * const c_void , count : usize ) -> isize {
8562 if buf. is_null ( ) && count > 0 {
8663 set_errno ( EINVAL ) ;
8764 return -1 ;
@@ -108,14 +85,19 @@ pub extern "C" fn write(fd: c_int, buf: *const c_void, count: usize) -> isize {
10885}
10986
11087#[ unsafe( no_mangle) ]
111- pub extern "C" fn clock_gettime ( clk_id : c_ulong , tp : * mut Timespec ) -> c_int {
88+ extern "C" fn clock_gettime ( clk_id : clockid_t , tp : * mut timespec ) -> c_int {
89+ // The libc bindings generated by bindgen are u32, but we expect them to be clockid_t,
90+ // so we convert the constants to the expected type here for comparison.
91+ const CLOCK_ID_REALTIME : clockid_t = CLOCK_REALTIME as _ ;
92+ const CLOCK_ID_MONOTONIC : clockid_t = CLOCK_MONOTONIC as _ ;
93+
11294 if tp. is_null ( ) {
11395 set_errno ( EINVAL ) ;
11496 return -1 ;
11597 }
11698
11799 match clk_id {
118- CLOCK_REALTIME | CLOCK_MONOTONIC => {
100+ CLOCK_ID_REALTIME | CLOCK_ID_MONOTONIC => {
119101 let ( secs, nanos) = current_time ( ) ;
120102 unsafe {
121103 ( * tp) . tv_sec = secs as c_long ;
@@ -131,7 +113,7 @@ pub extern "C" fn clock_gettime(clk_id: c_ulong, tp: *mut Timespec) -> c_int {
131113}
132114
133115#[ unsafe( no_mangle) ]
134- pub extern "C" fn gettimeofday ( tv : * mut Timeval , _tz : * mut c_void ) -> c_int {
116+ extern "C" fn gettimeofday ( tv : * mut timeval , _tz : * mut c_void ) -> c_int {
135117 if tv. is_null ( ) {
136118 set_errno ( EINVAL ) ;
137119 return -1 ;
@@ -146,17 +128,17 @@ pub extern "C" fn gettimeofday(tv: *mut Timeval, _tz: *mut c_void) -> c_int {
146128}
147129
148130#[ unsafe( no_mangle) ]
149- pub extern "C" fn _exit ( ec : c_int ) -> ! {
131+ extern "C" fn _exit ( ec : c_int ) -> ! {
150132 hyperlight_guest:: exit:: abort_with_code ( & [ ec as u8 ] ) ;
151133}
152134
153135#[ unsafe( no_mangle) ]
154- pub extern "C" fn lseek ( _fd : c_int , _offset : c_long , _whence : c_int ) -> c_long {
136+ extern "C" fn lseek ( _fd : c_int , _offset : c_long , _whence : c_int ) -> c_long {
155137 set_errno ( ENOSYS ) ;
156138 -1
157139}
158140
159141#[ unsafe( no_mangle) ]
160- pub extern "C" fn close ( _fd : c_int ) -> c_int {
142+ extern "C" fn close ( _fd : c_int ) -> c_int {
161143 0
162144}
0 commit comments