stub some things

This commit is contained in:
nora 2023-10-01 16:42:44 +02:00
parent 609c5e6e3d
commit 3a98a2b822
7 changed files with 84 additions and 11 deletions

View file

@ -14,11 +14,11 @@ pub const STDERR: i32 = 2;
pub const EOF: i32 = -1;
#[doc(hidden)]
pub struct Printer;
pub struct Printer(pub i32);
impl core::fmt::Write for Printer {
fn write_str(&mut self, s: &str) -> core::fmt::Result {
unsafe { write_all(STDOUT, s.as_bytes()).map_err(|_| core::fmt::Error) }
unsafe { write_all(self.0, s.as_bytes()).map_err(|_| core::fmt::Error) }
}
}
@ -27,7 +27,7 @@ macro_rules! println {
($($tt:tt)*) => {
{
use ::core::fmt::Write;
::core::writeln!($crate::io::Printer, $($tt)*).unwrap();
::core::writeln!($crate::io::Printer($crate::io::STDOUT), $($tt)*).unwrap();
}
};
}

View file

@ -1,7 +1,6 @@
#![no_std]
#![feature(c_variadic)]
#![feature(thread_local)]
#![warn(unreachable_pub)]
#![warn(rust_2018_idioms)]
#![allow(clippy::missing_safety_doc)]
@ -15,6 +14,7 @@ pub mod error;
pub mod fmt;
pub mod io;
pub mod mem;
pub mod misc;
pub mod start;
mod stubs;
mod sys;

18
libuwuc/src/misc.rs Normal file
View file

@ -0,0 +1,18 @@
use crate::utils::SharedThinCstr;
use core::fmt::Write;
pub fn assert_failed(
assertion: SharedThinCstr<'_>,
file: SharedThinCstr<'_>,
line: u32,
_function: Option<SharedThinCstr<'_>>,
) -> ! {
let _ = writeln!(
crate::io::Printer(crate::io::STDERR),
"assertion failed: '{:?}' at {:?}:{}",
assertion,
file,
line
);
crate::start::abort();
}

View file

@ -15,7 +15,9 @@ macro_rules! cstr {
($value:literal) => {{
let s = concat!($value, "\0");
#[allow(unused_unsafe)]
unsafe { $crate::utils::SharedThinCstr::from_raw(s.as_ptr().cast()) }
unsafe {
$crate::utils::SharedThinCstr::from_raw(s.as_ptr().cast())
}
}};
}
@ -26,6 +28,10 @@ impl<'a> SharedThinCstr<'a> {
Self(NonNull::new_unchecked(ptr as *mut u8), PhantomData)
}
pub unsafe fn from_nullable(ptr: *const u8) -> Option<Self> {
NonNull::new(ptr as *mut u8).map(|ptr| Self(ptr, PhantomData))
}
pub fn as_ptr(self) -> NonNull<u8> {
self.0
}

View file

@ -1,3 +1,7 @@
use core::ffi::c_uint;
use libuwuc::utils::SharedThinCstr;
#[no_mangle]
pub extern "C" fn __stack_chk_fail() -> ! {
unsafe {
@ -9,4 +13,19 @@ pub extern "C" fn __stack_chk_fail() -> ! {
#[no_mangle]
pub extern "C" fn __errno_location() -> *const i32 {
libuwuc::error::errno_location()
}
}
#[no_mangle]
pub unsafe extern "C" fn __assert_fail(
assertion: *const u8,
file: *const u8,
line: c_uint,
function: *const u8,
) -> ! {
libuwuc::misc::assert_failed(
SharedThinCstr::from_raw(assertion),
SharedThinCstr::from_raw(file),
line,
SharedThinCstr::from_nullable(function),
)
}

View file

@ -44,9 +44,13 @@ pub unsafe extern "C" fn printf(format: *const u8, mut args: ...) -> c_int {
}
}
#[no_mangle]
pub unsafe extern "C" fn __fprintf_chk(_flag: c_int, file: &FileStream, format: *const u8, mut args: ...) -> c_int {
pub unsafe extern "C" fn __fprintf_chk(
_flag: c_int,
file: &FileStream,
format: *const u8,
mut args: ...
) -> c_int {
let mut sink = WriteCounter(file, 0);
let result = libuwuc::fmt::printf::printf_generic(
@ -77,7 +81,6 @@ pub unsafe extern "C" fn fprintf(file: &FileStream, format: *const u8, mut args:
}
}
// STREAMS:
#[no_mangle]
@ -87,17 +90,37 @@ pub static stdout: &FileStream = &FileStream::from_raw_fd(STDOUT);
#[no_mangle]
pub static stderr: &FileStream = &FileStream::from_raw_fd(STDERR);
#[no_mangle]
pub unsafe extern "C" fn fgetc(_stream: *mut FileStream) -> c_int {
todo!()
}
#[no_mangle]
pub unsafe extern "C" fn ungetc(_c: c_int, _stream: *mut FileStream) -> c_int {
todo!()
}
#[no_mangle]
pub unsafe extern "C" fn fputc(c: c_int, stream: *mut FileStream) -> c_int {
libuwuc::io::stream::fputc(c as u8, &*stream)
}
#[no_mangle]
pub unsafe extern "C" fn fread(
_ptr: *const u8,
_size: usize,
_nmemb: usize,
_stream: *mut FileStream,
) -> usize {
todo!()
}
#[no_mangle]
pub unsafe extern "C" fn fwrite(
ptr: *const u8,
size: usize,
nitems: usize,
nmemb: usize,
stream: &FileStream,
) -> usize {
libuwuc::io::stream::fwrite(ptr, size, nitems, stream)
libuwuc::io::stream::fwrite(ptr, size, nmemb, stream)
}

7
tests/c/mem.c Normal file
View file

@ -0,0 +1,7 @@
#include<string.h>
int main(void) {
char buf[10];
memset(buf, 0, sizeof(buf));
}