diff --git a/Cargo.lock b/Cargo.lock index 441d5502179b0eeff8f16688538bcffbd73fc2c2..df1972092b3051ae8f95aa903812b6d2d4ce9ba3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,9 +4,9 @@ version = 3 [[package]] name = "ansi_term" -version = "0.11.0" +version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" +checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" dependencies = [ "winapi", ] @@ -24,15 +24,15 @@ dependencies = [ [[package]] name = "autocfg" -version = "1.0.1" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] name = "bitflags" -version = "1.2.1" +version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "cbindgen" @@ -42,11 +42,11 @@ checksum = "2db2df1ebc842c41fd2c4ae5b5a577faf63bd5151b953db752fc686812bee318" dependencies = [ "clap", "log", - "proc-macro2 1.0.28", - "quote 1.0.9", + "proc-macro2 1.0.36", + "quote 1.0.15", "serde", "serde_json", - "syn 1.0.74", + "syn 1.0.86", "tempfile", "toml", ] @@ -62,9 +62,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.0.69" +version = "1.0.73" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e70cc2f62c6ce1868963827bd677764c62d07c3d9a3e1fb1177ee1a9ab199eb2" +checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11" [[package]] name = "cfg-if" @@ -74,9 +74,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clap" -version = "2.33.3" +version = "2.34.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002" +checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c" dependencies = [ "ansi_term", "atty", @@ -107,14 +107,12 @@ name = "crtn" version = "0.1.0" [[package]] -name = "getrandom" -version = "0.2.3" +name = "fastrand" +version = "1.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753" +checksum = "c3fcf0cee53519c866c09b5de1f6c56ff9d647101f81c1964fa632e148896cdf" dependencies = [ - "cfg-if", - "libc", - "wasi", + "instant", ] [[package]] @@ -137,11 +135,20 @@ dependencies = [ "libc", ] +[[package]] +name = "instant" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" +dependencies = [ + "cfg-if", +] + [[package]] name = "itoa" -version = "0.4.7" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736" +checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35" [[package]] name = "lazy_static" @@ -158,15 +165,15 @@ version = "0.1.0" [[package]] name = "libc" -version = "0.2.98" +version = "0.2.119" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "320cfe77175da3a483efed4bc0adc1968ca050b098ce4f2f1c13a56626128790" +checksum = "1bf2e165bb3457c8e098ea76f3e3bc9db55f87aa90d52d0e6be741470916aaa4" [[package]] name = "lock_api" -version = "0.4.4" +version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0382880606dff6d15c9476c416d18690b72742aa7b605bb6dd6ec9030fbf07eb" +checksum = "88943dd7ef4a2e5a4bfa2753aaab3013e34ce2533d1996fb18ef591e315e2b3b" dependencies = [ "scopeguard", ] @@ -182,9 +189,9 @@ dependencies = [ [[package]] name = "memchr" -version = "2.4.0" +version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b16bd47d9e329435e309c58469fe0791c2d0d1ba96ec0954152a5ae2b04387dc" +checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" [[package]] name = "memoffset" @@ -214,12 +221,6 @@ checksum = "b4596b6d070b27117e987119b4dac604f3c58cfb0b191112e24771b2faeac1a6" name = "posix-regex" version = "0.1.0" -[[package]] -name = "ppv-lite86" -version = "0.2.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" - [[package]] name = "proc-macro2" version = "0.4.30" @@ -231,9 +232,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.28" +version = "1.0.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c7ed8b8c7b886ea3ed7dde405212185f423ab44682667c8c6dd14aa1d9f6612" +checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029" dependencies = [ "unicode-xid 0.2.2", ] @@ -249,11 +250,11 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.9" +version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" +checksum = "864d3e96a899863136fc6e99f3d7cae289dafe43bf2c5ac19b70df7210c0a145" dependencies = [ - "proc-macro2 1.0.28", + "proc-macro2 1.0.36", ] [[package]] @@ -281,28 +282,6 @@ dependencies = [ "rand_core 0.3.1", ] -[[package]] -name = "rand" -version = "0.8.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8" -dependencies = [ - "libc", - "rand_chacha", - "rand_core 0.6.3", - "rand_hc", -] - -[[package]] -name = "rand_chacha" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" -dependencies = [ - "ppv-lite86", - "rand_core 0.6.3", -] - [[package]] name = "rand_core" version = "0.3.1" @@ -318,24 +297,6 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" -[[package]] -name = "rand_core" -version = "0.6.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7" -dependencies = [ - "getrandom", -] - -[[package]] -name = "rand_hc" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d51e9f596de227fda2ea6c84607f5558e196eeaf43c986b724ba4fb8fdf497e7" -dependencies = [ - "rand_core 0.6.3", -] - [[package]] name = "redox_syscall" version = "0.1.57" @@ -351,6 +312,14 @@ dependencies = [ "bitflags", ] +[[package]] +name = "redox_syscall" +version = "0.2.11" +source = "git+https://gitlab.redox-os.org/4lDO2/syscall.git?branch=update-toolchain-2022#938d1c1cb6ebfae84b449cbd7dbe860f0e8a5ea5" +dependencies = [ + "bitflags", +] + [[package]] name = "relibc" version = "0.2.5" @@ -365,8 +334,8 @@ dependencies = [ "memoffset", "posix-regex", "ralloc", - "rand 0.5.6", - "redox_syscall 0.2.10", + "rand", + "redox_syscall 0.2.11", "sc", "spin 0.9.2", ] @@ -400,15 +369,15 @@ dependencies = [ [[package]] name = "ryu" -version = "1.0.5" +version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" +checksum = "73b4b750c782965c211b42f022f59af1fbceabdd026623714f104152f1ec149f" [[package]] name = "sc" -version = "0.2.4" +version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d6c362b5ec9cd8c12544e0ad4a610855204a0f127163d122e1d2f51f249d8404" +checksum = "7502841977ed89ca99709ad39e29f026a2f2924fbd5377fed019ba90d9dc0f6b" [[package]] name = "scopeguard" @@ -460,29 +429,29 @@ checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" [[package]] name = "serde" -version = "1.0.127" +version = "1.0.136" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f03b9878abf6d14e6779d3f24f07b2cfa90352cfec4acc5aab8f1ac7f146fae8" +checksum = "ce31e24b01e1e524df96f1c2fdd054405f8d7376249a5110886fb4b658484789" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.127" +version = "1.0.136" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a024926d3432516606328597e0f224a51355a493b49fdd67e9209187cbe55ecc" +checksum = "08597e7152fcd306f41838ed3e37be9eaeed2b61c42e2117266a554fab4662f9" dependencies = [ - "proc-macro2 1.0.28", - "quote 1.0.9", - "syn 1.0.74", + "proc-macro2 1.0.36", + "quote 1.0.15", + "syn 1.0.86", ] [[package]] name = "serde_json" -version = "1.0.66" +version = "1.0.79" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "336b10da19a12ad094b59d870ebde26a45402e5b470add4b5fd03c5048a32127" +checksum = "8e8d9fa5c3b304765ce1fd9c4c8a3de2c8db365a5b91be52f186efc675681d95" dependencies = [ "itoa", "ryu", @@ -523,24 +492,24 @@ dependencies = [ [[package]] name = "syn" -version = "1.0.74" +version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1873d832550d4588c3dbc20f01361ab00bfe741048f71e3fecf145a7cc18b29c" +checksum = "8a65b3f4ffa0092e9887669db0eae07941f023991ab58ea44da8fe8e2d511c6b" dependencies = [ - "proc-macro2 1.0.28", - "quote 1.0.9", + "proc-macro2 1.0.36", + "quote 1.0.15", "unicode-xid 0.2.2", ] [[package]] name = "tempfile" -version = "3.2.0" +version = "3.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dac1c663cfc93810f88aed9b8941d48cabf856a1b111c29a40439018d870eb22" +checksum = "5cdb1ef4eaeeaddc8fbd371e5017057064af0911902ef36b39801f67cc6d79e4" dependencies = [ "cfg-if", + "fastrand", "libc", - "rand 0.8.4", "redox_syscall 0.2.10", "remove_dir_all", "winapi", @@ -572,9 +541,9 @@ checksum = "e92e959f029e4f8ee25d70d15ab58d2b46f98a17bc238b9265ff0c26f6f3d67f" [[package]] name = "unicode-width" -version = "0.1.8" +version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" +checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973" [[package]] name = "unicode-xid" @@ -594,12 +563,6 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" -[[package]] -name = "wasi" -version = "0.10.2+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" - [[package]] name = "winapi" version = "0.3.9" diff --git a/Cargo.toml b/Cargo.toml index 903b14285aa960478f3fb06a6065225a06e91aff..359bb420c36714aadecd85701e2af96fc9dc98fd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,7 +39,7 @@ optional = true sc = "0.2.3" [target.'cfg(target_os = "redox")'.dependencies] -redox_syscall = "0.2.10" +redox_syscall = { git = "https://gitlab.redox-os.org/4lDO2/syscall.git", branch = "update-toolchain-2022" } spin = "0.9.0" [features] diff --git a/Makefile b/Makefile index d1cfc3395136f08dbf2275f0ba0085ab3695c656..828e9d31fab2dc8717f8b50c985911132628550a 100644 --- a/Makefile +++ b/Makefile @@ -56,7 +56,9 @@ WEAKEN_SYMBOLS=\ -W __rust_alloc \ -W __rust_alloc_zeroed \ -W __rust_dealloc \ - -W __rust_realloc + -W __rust_realloc \ + -W __rdl_oom \ + -W __rg_oom .PHONY: all clean fmt install install-headers libs submodules test diff --git a/src/crt0/src/lib.rs b/src/crt0/src/lib.rs index 3146fd61144641d640a93b512897d5371c19d1af..c92a897c0cc1da8eeca22a4a72eb9b7ad800fb48 100644 --- a/src/crt0/src/lib.rs +++ b/src/crt0/src/lib.rs @@ -1,32 +1,25 @@ //! crt0 #![no_std] -#![feature(asm)] #![feature(linkage)] -#![feature(llvm_asm)] -#![feature(naked_functions)] -#[no_mangle] -#[naked] -pub unsafe extern "C" fn _start() { - #[cfg(target_arch = "x86_64")] - llvm_asm!("mov rdi, rsp - and rsp, 0xFFFFFFFFFFFFFFF0 - call relibc_start" - : - : - : - : "intel", "volatile" - ); - #[cfg(target_arch = "aarch64")] - llvm_asm!("mov x0, sp - bl relibc_start" - : - : - : - : "volatile" - ); -} +use core::arch::global_asm; + +#[cfg(target_arch = "x86_64")] +global_asm!(" + .globl _start +_start: + mov rdi, rsp + and rsp, 0xFFFFFFFFFFFFFFF0 + call relibc_start +"); +#[cfg(target_arch = "aarch64")] +global_asm!(" + .globl _start +_start: + mov x0, sp + bl relibc_start +"); #[linkage = "weak"] #[no_mangle] diff --git a/src/crti/src/lib.rs b/src/crti/src/lib.rs index 9ec19e013f6fc04cd214cd58639c568e26355ce2..8e50d1d0bfc1da71fffab45c1f66534df6ac92e2 100644 --- a/src/crti/src/lib.rs +++ b/src/crti/src/lib.rs @@ -1,9 +1,10 @@ //! crti #![no_std] -#![feature(global_asm)] #![feature(linkage)] +use core::arch::global_asm; + // https://wiki.osdev.org/Creating_a_C_Library#crtbegin.o.2C_crtend.o.2C_crti.o.2C_and_crtn.o #[cfg(target_arch = "x86_64")] global_asm!( diff --git a/src/crtn/src/lib.rs b/src/crtn/src/lib.rs index 8d21fd4406e95b8f4f0767d9f7b12190cfee0cad..e2af095320f3ee56fcb9795db35df67526222031 100644 --- a/src/crtn/src/lib.rs +++ b/src/crtn/src/lib.rs @@ -1,9 +1,10 @@ //! crti #![no_std] -#![feature(global_asm)] #![feature(linkage)] +use core::arch::global_asm; + // https://wiki.osdev.org/Creating_a_C_Library#crtbegin.o.2C_crtend.o.2C_crti.o.2C_and_crtn.o #[cfg(target_arch = "x86_64")] global_asm!( diff --git a/src/header/setjmp/mod.rs b/src/header/setjmp/mod.rs index 6371c9a58b4ab0f46f280a8bb938f2bb24d30bc5..372e5676127639d0c2d88e7e0c8746fd3f7189d0 100644 --- a/src/header/setjmp/mod.rs +++ b/src/header/setjmp/mod.rs @@ -1,5 +1,7 @@ //! setjmp implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/setjmp.h.html +use core::arch::global_asm; + macro_rules! platform_specific { ($($arch:expr,$ext:expr;)+) => { $( diff --git a/src/header/signal/redox.rs b/src/header/signal/redox.rs index c74893457e70fa54f24939deeab59d39a09d4a8b..7c8036faf3a19ef565f9a11f4023211c35d14769 100644 --- a/src/header/signal/redox.rs +++ b/src/header/signal/redox.rs @@ -1,3 +1,5 @@ +use core::arch::global_asm; + // Needs to be defined in assembly because it can't have a function prologue // rax is register, 119 is SIGRETURN #[cfg(target_arch = "x86_64")] diff --git a/src/header/unistd/mod.rs b/src/header/unistd/mod.rs index 22a68767bc0d51fafceca8e8f0dc8a5a77de1c0a..35a13471a226b8f1275e4700304fd416820634b3 100644 --- a/src/header/unistd/mod.rs +++ b/src/header/unistd/mod.rs @@ -427,9 +427,10 @@ pub extern "C" fn isatty(fd: c_int) -> c_int { } } -// #[no_mangle] -pub extern "C" fn lchown(path: *const c_char, owner: uid_t, group: gid_t) -> c_int { - unimplemented!(); +#[no_mangle] +pub unsafe extern "C" fn lchown(path: *const c_char, owner: uid_t, group: gid_t) -> c_int { + let path = CStr::from_ptr(path); + Sys::lchown(path, owner, group) } #[no_mangle] diff --git a/src/ld_so/src/lib.rs b/src/ld_so/src/lib.rs index 254b2adbdd9b8dfc0dca53e28087559e4fb5a8b7..1dd2e438c1720984210eaa902bbe22458ff57b45 100644 --- a/src/ld_so/src/lib.rs +++ b/src/ld_so/src/lib.rs @@ -1,59 +1,48 @@ #![no_std] -#![feature(asm)] #![feature(linkage)] -#![feature(llvm_asm)] -#![feature(naked_functions)] -#[naked] -#[no_mangle] -pub unsafe extern "C" fn _start() { - #[cfg(target_arch = "aarch64")] - llvm_asm!(" - mov x0, sp - bl relibc_ld_so_start - # TODO: aarch64 - " - : - : - : - : "volatile" - ); - #[cfg(target_arch = "x86_64")] - llvm_asm!(" - # rsi = _start + 5 - call next -next: pop rsi +use core::arch::global_asm; - # Save original stack and align stack to 16 bytes - mov rbp, rsp - and rsp, 0xFFFFFFFFFFFFFFF0 +#[cfg(target_arch = "aarch64")] +global_asm!(" +.globl _start +_start: + mov x0, sp + bl relibc_ld_so_start + # TODO: aarch64 +"); - # Call ld_so_start(stack, entry) - mov rdi, rbp - sub rsi, 5 - call relibc_ld_so_start +#[cfg(target_arch = "x86_64")] +global_asm!(" +.globl _start +_start: + # rsi = _start + 5 + call 2f +2: pop rsi - # Restore original stack, clear registers, and jump to new start function - mov rsp, rbp - xor rcx, rcx - xor rdx, rdx - xor rdi, rdi - xor rsi, rsi - xor r8, r8 - xor r9, r9 - xor r10, r10 - xor r11, r11 - fninit - jmp rax - " - : - : - : - : "intel", "volatile" - ); -} + # Save original stack and align stack to 16 bytes + mov rbp, rsp + and rsp, 0xFFFFFFFFFFFFFFF0 + + # Call ld_so_start(stack, entry) + mov rdi, rbp + sub rsi, 5 + call relibc_ld_so_start + + # Restore original stack, clear registers, and jump to new start function + mov rsp, rbp + xor rcx, rcx + xor rdx, rdx + xor rdi, rdi + xor rsi, rsi + xor r8, r8 + xor r9, r9 + xor r10, r10 + xor r11, r11 + fninit + jmp rax +"); -#[naked] #[no_mangle] pub unsafe extern "C" fn main(_argc: isize, _argv: *const *const i8) -> usize { // LD diff --git a/src/ld_so/tcb.rs b/src/ld_so/tcb.rs index 620c10a9a4c0dc0c8d0cf308a3463e81c017b9c4..3714a811f54e801a84961236fa6a3941c2bd5f95 100644 --- a/src/ld_so/tcb.rs +++ b/src/ld_so/tcb.rs @@ -1,5 +1,6 @@ -use alloc::vec::Vec; use core::{mem, ptr, slice}; +use core::arch::asm; +use alloc::vec::Vec; use goblin::error::{Error, Result}; use crate::{ @@ -198,6 +199,7 @@ impl Tcb { #[inline(always)] #[cfg(target_arch = "aarch64")] unsafe fn arch_read(offset: usize) -> usize { + // TODO: s/llvm_asm/asm/g let tp: usize; llvm_asm!("mrs $0, tpidr_el0" : "=r"(tp) diff --git a/src/lib.rs b/src/lib.rs index 42560052e08e47ab9b6ae21e38dcbaab61c54fee..f3883d827d224d93a6cb1523fd69a76f74f1915b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,17 +3,13 @@ #![allow(non_upper_case_globals)] #![allow(unused_variables)] #![feature(allocator_api)] -#![feature(asm)] +#![feature(asm_const)] #![feature(box_into_pin)] #![feature(c_variadic)] #![feature(const_btree_new)] -#![feature(const_raw_ptr_deref)] #![feature(core_intrinsics)] -#![feature(global_asm)] #![feature(lang_items)] #![feature(linkage)] -#![feature(llvm_asm)] -#![feature(maybe_uninit_extra)] #![feature(stmt_expr_attributes)] #![feature(str_internals)] #![feature(thread_local)] diff --git a/src/platform/linux/mod.rs b/src/platform/linux/mod.rs index c196be4aa6009d13273bd24f92cf88722a390a0b..4b92fc2c4b7894262bf500161ac9e6d0a00cf125 100644 --- a/src/platform/linux/mod.rs +++ b/src/platform/linux/mod.rs @@ -279,6 +279,10 @@ impl Pal for Sys { e(unsafe { syscall!(GETUID) }) as uid_t } + fn lchown(path: &CStr, owner: uid_t, group: gid_t) -> c_int { + e(unsafe { syscall!(LCHOWN, path.as_ptr(), owner, group) }) + } + fn link(path1: &CStr, path2: &CStr) -> c_int { e(unsafe { syscall!( diff --git a/src/platform/pal/mod.rs b/src/platform/pal/mod.rs index 8317d2e5cbd099988ed515fac6d6ae3377b278b8..73f32b1a1566521ce82bc84aa4fabdc97009c426 100644 --- a/src/platform/pal/mod.rs +++ b/src/platform/pal/mod.rs @@ -106,6 +106,8 @@ pub trait Pal { fn getuid() -> uid_t; + fn lchown(path: &CStr, owner: uid_t, group: gid_t) -> c_int; + fn link(path1: &CStr, path2: &CStr) -> c_int; fn lseek(fildes: c_int, offset: off_t, whence: c_int) -> off_t; diff --git a/src/platform/redox/extra.rs b/src/platform/redox/extra.rs index 19138aa39436c90e7bddab6c6e9262803425ce89..9953ec2c0eb19b699a2cf8a1d5add434c096ba85 100644 --- a/src/platform/redox/extra.rs +++ b/src/platform/redox/extra.rs @@ -1,4 +1,5 @@ use core::{ptr, slice}; +use core::arch::global_asm; use crate::platform::{sys::e, types::*}; @@ -47,3 +48,62 @@ pub unsafe extern "C" fn redox_physmap( pub unsafe extern "C" fn redox_physunmap(virtual_address: *mut c_void) -> c_int { e(syscall::physunmap(virtual_address as usize)) as c_int } + +extern "C" { + pub fn pte_clone_inner(stack: usize) -> usize; +} + +global_asm!(" + .globl pte_clone_inner + .type pte_clone_inner, @function + +pte_clone_inner: + # Move the 1st argument `stack` of this function into the second argument to clone. + mov rsi, rdi + mov rax, {SYS_CLONE} + mov rdi, {flags} + + # Call clone syscall + syscall + + # Check if child or parent + test rax, rax + jnz 2f + + # Load registers + pop rax + pop rdi + pop rsi + pop rdx + pop rcx + pop r8 + pop r9 + + # Call entry point + call rax + + # Exit + mov rax, 1 + xor rdi, rdi + syscall + + # Invalid instruction on failure to exit + ud2 + + # Return PID if parent +2: + ret + + .size pte_clone_inner, . - pte_clone_inner + + ", + + flags = const( + syscall::CLONE_VM.bits() + | syscall::CLONE_FS.bits() + | syscall::CLONE_FILES.bits() + | syscall::CLONE_SIGHAND.bits() + | syscall::CLONE_STACK.bits() + ), + SYS_CLONE = const(syscall::SYS_CLONE), +); diff --git a/src/platform/redox/mod.rs b/src/platform/redox/mod.rs index 1028478d461d3130a953979f26204d76b48a7f42..59b137a1390538fcc44870c30b355d35c5adec0c 100644 --- a/src/platform/redox/mod.rs +++ b/src/platform/redox/mod.rs @@ -1,4 +1,6 @@ use core::{mem, ptr, result::Result as CoreResult, slice, str}; +use core::arch::asm; + use syscall::{ self, data::{Map, Stat as redox_stat, StatVfs as redox_statvfs, TimeSpec as redox_timespec}, @@ -686,6 +688,17 @@ impl Pal for Sys { e(syscall::getuid()) as pid_t } + fn lchown(path: &CStr, owner: uid_t, group: gid_t) -> c_int { + // TODO: Is it correct for regular chown to use O_PATH? On Linux the meaning of that flag + // is to forbid file operations, including fchown. + + // unlike chown, never follow symbolic links + match File::open(path, fcntl::O_CLOEXEC | fcntl::O_NOFOLLOW) { + Ok(file) => Self::fchown(*file, owner, group), + Err(_) => -1, + } + } + fn link(path1: &CStr, path2: &CStr) -> c_int { e(unsafe { syscall::link(path1.as_ptr() as *const u8, path2.as_ptr() as *const u8) }) as c_int @@ -839,50 +852,7 @@ impl Pal for Sys { #[cfg(target_arch = "x86_64")] unsafe fn pte_clone(stack: *mut usize) -> pid_t { - let flags = syscall::CLONE_VM - | syscall::CLONE_FS - | syscall::CLONE_FILES - | syscall::CLONE_SIGHAND - | syscall::CLONE_STACK; - let pid; - llvm_asm!(" - # Call clone syscall - syscall - - # Check if child or parent - test rax, rax - jnz .parent - - # Load registers - pop rax - pop rdi - pop rsi - pop rdx - pop rcx - pop r8 - pop r9 - - # Call entry point - call rax - - # Exit - mov rax, 1 - xor rdi, rdi - syscall - - # Invalid instruction on failure to exit - ud2 - - # Return PID if parent - .parent: - " - : "={rax}"(pid) - : "{rax}"(syscall::SYS_CLONE), "{rdi}"(flags), "{rsi}"(stack) - : "memory", "rbx", "rcx", "rdx", "rsi", "rdi", "r8", - "r9", "r10", "r11", "r12", "r13", "r14", "r15" - : "intel", "volatile" - ); - e(syscall::Error::demux(pid)) as pid_t + e(syscall::Error::demux(extra::pte_clone_inner(stack as usize))) as pid_t } fn read(fd: c_int, buf: &mut [u8]) -> ssize_t {