From 44f343bec8db8fbca649c11764353d21a0fbce6b Mon Sep 17 00:00:00 2001 From: David Carlier Date: Sat, 5 Jun 2021 18:49:25 +0100 Subject: [PATCH 1/2] explicit_bzero implementation proposal --- src/header/strings/mod.rs | 10 +++++++++- tests/strings.c | 4 ++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/header/strings/mod.rs b/src/header/strings/mod.rs index a902c414d4..c27fa32ed2 100644 --- a/src/header/strings/mod.rs +++ b/src/header/strings/mod.rs @@ -1,5 +1,5 @@ //! strings implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/strings.h.html - +#![feature(llvm_asm)] use core::ptr; use crate::{ @@ -22,6 +22,14 @@ pub unsafe extern "C" fn bzero(dst: *mut c_void, n: size_t) { ptr::write_bytes(dst as *mut u8, 0, n); } +#[no_mangle] +pub unsafe extern "C" fn explicit_bzero(s: *mut c_void, n: size_t) { + for i in 0..n { + *(s as *mut u8).add(i) = 0 as u8; + } + llvm_asm!("" :: "r"(s) : "memory") +} + #[no_mangle] pub extern "C" fn ffs(i: c_int) -> c_int { if i == 0 { diff --git a/tests/strings.c b/tests/strings.c index 6d3c4742cb..ede5d6289c 100644 --- a/tests/strings.c +++ b/tests/strings.c @@ -40,4 +40,8 @@ int main(void) { char* str = "hihih"; assert(index(str, 'i') == str + 1); assert(rindex(str, 'i') == str + 3); + + char buf[] = "password"; + explicit_bzero(buf, sizeof(buf)); + assert(buf[0] == 0); } From 63deaec9d851eea7ac183d7af9a0b845a619e628 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Wed, 22 Mar 2023 06:56:06 +0000 Subject: [PATCH 2/2] update inline asm --- src/header/strings/mod.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/header/strings/mod.rs b/src/header/strings/mod.rs index c27fa32ed2..dcf6266685 100644 --- a/src/header/strings/mod.rs +++ b/src/header/strings/mod.rs @@ -1,6 +1,7 @@ //! strings implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/strings.h.html -#![feature(llvm_asm)] + use core::ptr; +use core::arch; use crate::{ header::{ctype, string}, @@ -27,7 +28,7 @@ pub unsafe extern "C" fn explicit_bzero(s: *mut c_void, n: size_t) { for i in 0..n { *(s as *mut u8).add(i) = 0 as u8; } - llvm_asm!("" :: "r"(s) : "memory") + arch::asm!(""); } #[no_mangle]