Add std to trampoline

This commit is contained in:
Micha Reiser 2024-02-22 13:12:19 +01:00
parent ad59171c8c
commit 8f62126d1f
No known key found for this signature in database
11 changed files with 39 additions and 121 deletions

View File

@ -0,0 +1,4 @@
[unstable]
build-std = ["std", "panic_abort"]
build-std-features = ["compiler-builtins-mem", "panic_immediate_abort"]

View File

@ -9,12 +9,7 @@ autotests = false
# Need to optimize etc. or else build fails
[profile.dev]
lto = true
codegen-units = 1
opt-level = 1
panic = "abort"
debug-assertions = false
overflow-checks = false
debug = true
[profile.release]
@ -28,32 +23,26 @@ debug = false
[dependencies]
windows-sys = { version = "0.52.0", features = [
"Win32_Foundation",
"Win32_Security",
"Win32_Storage_FileSystem",
"Win32_System_Console",
"Win32_System_Diagnostics_Debug",
"Win32_System_Environment",
"Win32_System_IO",
"Win32_System_JobObjects",
"Win32_System_JobObjects",
"Win32_System_LibraryLoader",
"Win32_System_Memory",
"Win32_System_Threading",
"Win32_System_WindowsProgramming",
"Win32_UI_WindowsAndMessaging",
"Win32_Foundation",
"Win32_Security",
"Win32_Storage_FileSystem",
"Win32_System_Console",
"Win32_System_Diagnostics_Debug",
"Win32_System_Environment",
"Win32_System_IO",
"Win32_System_JobObjects",
"Win32_System_JobObjects",
"Win32_System_LibraryLoader",
"Win32_System_Memory",
"Win32_System_Threading",
"Win32_System_WindowsProgramming",
"Win32_UI_WindowsAndMessaging",
] }
# This provides implementations of memcpy, memset, etc., which the compiler assumes
# are available. But there's also a hidden copy of this crate inside `core`/`alloc`,
# and they may or may not conflict depending on how clever the linker is feeling.
# The issue is that the hidden copy doesn't have the "mem" feature enabled, and we
# need it. So two options:
# - Uncomment this, and cross fingers that it doesn't cause conflicts
# - Use -Zbuild-std=... -Zbuild-std-features=compiler-builtins-mem, which enables
# the mem feature on the built-in builtins.
#compiler_builtins = { version = "*", features = ["mem"]}
ufmt-write = "0.1.0"
ufmt = "0.2.0"
ufmt = { version = "0.2.0", features = ["std"] }
[build-dependencies]
embed-manifest = "1.4.0"

View File

@ -9,8 +9,7 @@ fn main() {
let manifest =
new_manifest("uv.Trampoline").remove_dependency("Microsoft.Windows.Common-Controls");
embed_manifest(manifest).expect("unable to embed manifest");
println!("cargo:rustc-link-arg=/ENTRY:entry");
println!("cargo:rustc-link-arg=/LTCG");
println!("cargo:rustc-link-arg-bins=/ENTRY:entry");
println!("cargo:rerun-if-changed=build.rs");
}
}

View File

@ -1,9 +1,8 @@
#![no_std]
#![no_main]
#![windows_subsystem = "console"]
// build.rs passes a custom linker flag to make this the entrypoint to the executable
#[no_mangle]
pub extern "C" fn entry() -> ! {
pub extern "C" fn entry() {
uv_trampoline::bounce::bounce(false)
}

View File

@ -1,4 +1,3 @@
#![no_std]
#![no_main]
#![windows_subsystem = "windows"]

View File

@ -1,7 +1,6 @@
use alloc::string::String;
use alloc::{ffi::CString, vec, vec::Vec};
use core::mem::MaybeUninit;
use core::{
use std::ffi::CString;
use std::mem::MaybeUninit;
use std::{
ffi::CStr,
mem,
ptr::{addr_of, addr_of_mut, null, null_mut},
@ -69,8 +68,8 @@ fn make_child_cmdline() -> CString {
// Helpful when debugging trampline issues
// eprintln!(
// "executable_name: '{}'\nnew_cmdline: {}",
// core::str::from_utf8(executable_name.to_bytes()).unwrap(),
// core::str::from_utf8(child_cmdline.as_slice()).unwrap()
// std::str::from_utf8(executable_name.to_bytes()).unwrap(),
// std::str::from_utf8(child_cmdline.as_slice()).unwrap()
// );
CString::from_vec_with_nul(child_cmdline).unwrap_or_else(|_| {
@ -507,9 +506,9 @@ pub fn bounce(is_gui: bool) -> ! {
/// Prints the passed error message if the `actual_result` is equal to `error_code` and exits the process with status 1.
#[inline]
fn expect_result<T, F>(actual_result: T, error_code: T, error_message: F) -> T
where
T: Eq,
F: FnOnce() -> String,
where
T: Eq,
F: FnOnce() -> String,
{
if actual_result == error_code {
print_last_error_and_exit(&error_message());
@ -530,7 +529,7 @@ fn print_last_error_and_exit(message: &str) -> ! {
let err = unsafe { GetLastError() };
eprintln!("Received error code: {}", err);
let mut msg_ptr: *mut u8 = core::ptr::null_mut();
let mut msg_ptr: *mut u8 = std::ptr::null_mut();
let size = unsafe {
FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER
@ -543,9 +542,9 @@ fn print_last_error_and_exit(message: &str) -> ! {
// but if you pass FORMAT_MESSAGE_ALLOCATE_BUFFER then you have to
// *actually* pass in a *mut *mut u16 and just lie about the type.
// Getting Rust to do this requires some convincing.
core::ptr::addr_of_mut!(msg_ptr) as *mut _ as _,
std::ptr::addr_of_mut!(msg_ptr) as *mut _ as _,
0,
core::ptr::null(),
std::ptr::null(),
)
};
@ -556,7 +555,7 @@ fn print_last_error_and_exit(message: &str) -> ! {
);
} else {
let reason = unsafe {
let reason = core::slice::from_raw_parts(msg_ptr, size as usize + 1);
let reason = std::slice::from_raw_parts(msg_ptr, size as usize + 1);
CStr::from_bytes_with_nul_unchecked(reason)
};
eprintln!(

View File

@ -1,7 +1,5 @@
extern crate alloc;
use alloc::{ffi::CString, string::String};
use core::{
use std::ffi::CString;
use std::{
convert::Infallible,
ptr::{addr_of_mut, null, null_mut},
};

View File

@ -14,6 +14,6 @@ impl<T: Sized> SizeOf for T {
#[macro_export]
macro_rules! c {
($s:literal) => {
core::ffi::CStr::from_bytes_with_nul_unchecked(concat!($s, "\0").as_bytes())
std::ffi::CStr::from_bytes_with_nul_unchecked(concat!($s, "\0").as_bytes())
};
}

View File

@ -1,9 +1,3 @@
#![feature(panic_info_message)]
#![no_std]
extern crate alloc;
pub mod bounce;
mod diagnostics;
mod helpers;
mod runtime;

View File

@ -1,63 +0,0 @@
// Nothing in this file is directly imported anywhere else; it just fills in
// some of the no_std gaps.
extern crate alloc;
use alloc::alloc::{GlobalAlloc, Layout};
use core::ffi::c_void;
use windows_sys::Win32::System::{
Memory::{GetProcessHeap, HeapAlloc, HeapFree, HeapReAlloc, HEAP_ZERO_MEMORY},
Threading::ExitProcess,
};
use crate::eprintln;
// Windows wants this symbol. It has something to do with floating point usage?
// idk, defining it gets rid of link errors.
#[no_mangle]
#[used]
static _fltused: i32 = 0;
struct SystemAlloc;
#[global_allocator]
static SYSTEM_ALLOC: SystemAlloc = SystemAlloc;
unsafe impl Sync for SystemAlloc {}
unsafe impl GlobalAlloc for SystemAlloc {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
HeapAlloc(GetProcessHeap(), 0, layout.size()) as *mut u8
}
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
HeapFree(GetProcessHeap(), 0, ptr as *const c_void);
}
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, layout.size()) as *mut u8
}
unsafe fn realloc(&self, ptr: *mut u8, _layout: Layout, new_size: usize) -> *mut u8 {
HeapReAlloc(GetProcessHeap(), 0, ptr as *const c_void, new_size) as *mut u8
}
}
#[panic_handler]
fn panic(info: &core::panic::PanicInfo) -> ! {
if let Some(location) = info.location() {
let mut msg = "(couldn't format message)";
if let Some(msg_args) = info.message() {
if let Some(msg_str) = msg_args.as_str() {
msg = msg_str;
}
}
eprintln!(
"panic at {}:{} (column {}): {}",
location.file(),
location.line(),
location.column(),
msg,
);
}
unsafe {
ExitProcess(128);
}
}