From f13bd7fdd1db72491799960f910655ad009b2b5a Mon Sep 17 00:00:00 2001 From: Peter Limkilde Svendsen Date: Wed, 8 May 2019 22:08:13 +0200 Subject: [PATCH 1/4] Add tests for valloc --- tests/stdlib/alloc.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/stdlib/alloc.c b/tests/stdlib/alloc.c index ccb496a1dd..4089262fad 100644 --- a/tests/stdlib/alloc.c +++ b/tests/stdlib/alloc.c @@ -126,4 +126,17 @@ int main(void) { ptr_aligned_alloc_badsize, aligned_alloc_badsize_errno, strerror(aligned_alloc_badsize_errno)); free(ptr_aligned_alloc_badsize); + + errno = 0; + char * ptr_valloc = (char *)valloc(sample_alloc_size); + int valloc_errno = errno; + printf("valloc : %p, errno: %d = %s\n", + ptr_valloc, valloc_errno, strerror(valloc_errno)); + + errno = 0; + char * ptr_valloc_maxsize = (char *)valloc(max_size); + int valloc_maxsize_errno = errno; + printf("valloc (SIZE_MAX) : %p, errno: %d = %s\n", + ptr_valloc_maxsize, valloc_maxsize_errno, + strerror(valloc_maxsize_errno)); } From 8beb10b1bd13d1d5c669a5c1554a06de7ab49ecb Mon Sep 17 00:00:00 2001 From: Peter Limkilde Svendsen Date: Wed, 8 May 2019 23:20:30 +0200 Subject: [PATCH 2/4] Get page size through sysconf() --- src/header/stdlib/mod.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/header/stdlib/mod.rs b/src/header/stdlib/mod.rs index e8002abe69..843ae283ce 100644 --- a/src/header/stdlib/mod.rs +++ b/src/header/stdlib/mod.rs @@ -1,6 +1,7 @@ //! stdlib implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/stdlib.h.html use core::{intrinsics, iter, mem, ptr, slice}; +use core::convert::TryFrom; use rand::distributions::{Alphanumeric, Distribution, Uniform}; use rand::prng::XorShiftRng; use rand::rngs::JitterRng; @@ -14,6 +15,7 @@ use header::limits; use header::string::*; use header::time::constants::CLOCK_MONOTONIC; use header::time::timespec; +use header::unistd::{sysconf, _SC_PAGESIZE}; use header::wchar::*; use header::{ctype, errno, unistd}; use platform; @@ -1008,7 +1010,15 @@ pub unsafe extern "C" fn unsetenv(key: *const c_char) -> c_int { #[no_mangle] pub unsafe extern "C" fn valloc(size: size_t) -> *mut c_void { - memalign(4096, size) + /* _SC_PAGESIZE is a c_long and may in principle not convert + * correctly to a size_t. */ + match size_t::try_from(sysconf(_SC_PAGESIZE)) { + Ok(page_size) => memalign(page_size, size), + Err(_) => { + // A corner case. No errno setting. + ptr::null_mut() + } + } } #[no_mangle] From 54c5423f35b6af0ce6a8e55113d50556c517ce4e Mon Sep 17 00:00:00 2001 From: Peter Limkilde Svendsen Date: Thu, 9 May 2019 23:40:12 +0200 Subject: [PATCH 3/4] Avoid call to memalign() --- src/header/stdlib/mod.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/header/stdlib/mod.rs b/src/header/stdlib/mod.rs index 843ae283ce..149d4dc020 100644 --- a/src/header/stdlib/mod.rs +++ b/src/header/stdlib/mod.rs @@ -1013,7 +1013,15 @@ pub unsafe extern "C" fn valloc(size: size_t) -> *mut c_void { /* _SC_PAGESIZE is a c_long and may in principle not convert * correctly to a size_t. */ match size_t::try_from(sysconf(_SC_PAGESIZE)) { - Ok(page_size) => memalign(page_size, size), + Ok(page_size) => { + /* valloc() is not supposed to be able to set errno to + * EINVAL, hence no call to memalign(). */ + let ptr = platform::alloc_align(size, page_size); + if ptr.is_null() { + platform::errno = errno::ENOMEM; + } + ptr + } Err(_) => { // A corner case. No errno setting. ptr::null_mut() From f8cf25d76c1fa1bcb1f5f4a768760d2ea4613675 Mon Sep 17 00:00:00 2001 From: Peter Limkilde Svendsen Date: Sat, 11 May 2019 11:44:36 +0200 Subject: [PATCH 4/4] Fix inaccurate comment --- src/header/stdlib/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/header/stdlib/mod.rs b/src/header/stdlib/mod.rs index 149d4dc020..5944ad5bc8 100644 --- a/src/header/stdlib/mod.rs +++ b/src/header/stdlib/mod.rs @@ -1010,8 +1010,8 @@ pub unsafe extern "C" fn unsetenv(key: *const c_char) -> c_int { #[no_mangle] pub unsafe extern "C" fn valloc(size: size_t) -> *mut c_void { - /* _SC_PAGESIZE is a c_long and may in principle not convert - * correctly to a size_t. */ + /* sysconf(_SC_PAGESIZE) is a c_long and may in principle not + * convert correctly to a size_t. */ match size_t::try_from(sysconf(_SC_PAGESIZE)) { Ok(page_size) => { /* valloc() is not supposed to be able to set errno to