Skip to content

Commit 081593b

Browse files
committed
vexos: improve unsafe hygiene in alloc and PAL, fix fs issues
1 parent 1f31032 commit 081593b

File tree

3 files changed

+21
-19
lines changed

3 files changed

+21
-19
lines changed

library/std/src/sys/alloc/vexos.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::alloc::{GlobalAlloc, Layout, System};
55
use crate::ptr;
66
use crate::sync::atomic::{AtomicBool, Ordering};
77

8-
// symbols defined in the target linkerscript
8+
// Symbols for heap section boundaries defined in the target's linkerscript
99
unsafe extern "C" {
1010
static mut __heap_start: u8;
1111
static mut __heap_end: u8;
@@ -21,10 +21,12 @@ unsafe impl dlmalloc::Allocator for Vexos {
2121
static INIT: AtomicBool = AtomicBool::new(false);
2222

2323
if !INIT.swap(true, Ordering::Relaxed) {
24+
// This target has no growable heap, as user memory has a fixed
25+
// size/location and VEXos does not manage allocation for us.
2426
unsafe {
2527
(
26-
(&raw mut __heap_start).cast(),
27-
(&raw const __heap_end).byte_offset_from(ptr::addr_of!(__heap_start)) as _,
28+
(&raw mut __heap_start).cast::<u8>(),
29+
(&raw const __heap_end).offset_from_unsigned(&raw const __heap_start),
2830
0,
2931
)
3032
}
@@ -63,31 +65,31 @@ unsafe impl GlobalAlloc for System {
6365
#[inline]
6466
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
6567
// SAFETY: DLMALLOC access is guaranteed to be safe because we are a single-threaded target, which
66-
// guarantees unique and non-reentrant access to the allocator.
68+
// guarantees unique and non-reentrant access to the allocator. As such, no allocator lock is used.
6769
// Calling malloc() is safe because preconditions on this function match the trait method preconditions.
6870
unsafe { DLMALLOC.malloc(layout.size(), layout.align()) }
6971
}
7072

7173
#[inline]
7274
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
7375
// SAFETY: DLMALLOC access is guaranteed to be safe because we are a single-threaded target, which
74-
// guarantees unique and non-reentrant access to the allocator.
76+
// guarantees unique and non-reentrant access to the allocator. As such, no allocator lock is used.
7577
// Calling calloc() is safe because preconditions on this function match the trait method preconditions.
7678
unsafe { DLMALLOC.calloc(layout.size(), layout.align()) }
7779
}
7880

7981
#[inline]
8082
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
8183
// SAFETY: DLMALLOC access is guaranteed to be safe because we are a single-threaded target, which
82-
// guarantees unique and non-reentrant access to the allocator.
84+
// guarantees unique and non-reentrant access to the allocator. As such, no allocator lock is used.
8385
// Calling free() is safe because preconditions on this function match the trait method preconditions.
8486
unsafe { DLMALLOC.free(ptr, layout.size(), layout.align()) }
8587
}
8688

8789
#[inline]
8890
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
8991
// SAFETY: DLMALLOC access is guaranteed to be safe because we are a single-threaded target, which
90-
// guarantees unique and non-reentrant access to the allocator.
92+
// guarantees unique and non-reentrant access to the allocator. As such, no allocator lock is used.
9193
// Calling realloc() is safe because preconditions on this function match the trait method preconditions.
9294
unsafe { DLMALLOC.realloc(ptr, layout.size(), layout.align(), new_size) }
9395
}

library/std/src/sys/fs/vexos.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl FileAttr {
5555
/// Creates a FileAttr by getting data from an opened file.
5656
fn from_fd(fd: *mut vex_sdk::FIL) -> io::Result<Self> {
5757
// `vexFileSize` returns -1 upon error, so u64::try_from will fail on error.
58-
if let Some(size) = u64::try_from(unsafe { vex_sdk::vexFileSize(fd) }) {
58+
if let Ok(size) = u64::try_from(unsafe { vex_sdk::vexFileSize(fd) }) {
5959
Ok(Self::File { size })
6060
} else {
6161
Err(io::Error::new(io::ErrorKind::InvalidData, "Failed to get file size"))
@@ -64,7 +64,7 @@ impl FileAttr {
6464

6565
fn from_path(path: &Path) -> io::Result<Self> {
6666
// vexFileStatus returns 3 if the given path is a directory.
67-
const FILE_STATUS_DIR: i32 = 3;
67+
const FILE_STATUS_DIR: u32 = 3;
6868

6969
run_path_with_cstr(path, &|c_path| {
7070
let file_type = unsafe { vex_sdk::vexFileStatus(c_path.as_ptr()) };
@@ -84,7 +84,7 @@ impl FileAttr {
8484

8585
pub fn size(&self) -> u64 {
8686
match self {
87-
Self::File { size } => size,
87+
Self::File { size } => *size,
8888
Self::Dir => 0,
8989
}
9090
}
@@ -94,7 +94,7 @@ impl FileAttr {
9494
}
9595

9696
pub fn file_type(&self) -> FileType {
97-
self == FileAttr::Dir
97+
FileType { is_dir: matches!(self, FileAttr::Dir) }
9898
}
9999

100100
pub fn modified(&self) -> io::Result<SystemTime> {
@@ -160,7 +160,7 @@ impl DirEntry {
160160
}
161161

162162
pub fn file_name(&self) -> OsString {
163-
self.path.file_name().unwrap_or_default()
163+
self.path.file_name().unwrap_or_default().into()
164164
}
165165

166166
pub fn metadata(&self) -> io::Result<FileAttr> {
@@ -235,14 +235,14 @@ impl File {
235235
create,
236236
create_new,
237237
} => unsafe {
238-
if create_new {
238+
if *create_new {
239239
if vex_sdk::vexFileStatus(path.as_ptr()) != 0 {
240240
return Err(io::Error::new(
241241
io::ErrorKind::AlreadyExists,
242242
"File exists",
243243
));
244244
}
245-
} else if !create {
245+
} else if !*create {
246246
if vex_sdk::vexFileStatus(path.as_ptr()) == 0 {
247247
return Err(io::Error::new(
248248
io::ErrorKind::NotFound,
@@ -263,14 +263,14 @@ impl File {
263263
create,
264264
create_new,
265265
} => unsafe {
266-
if create_new {
266+
if *create_new {
267267
if vex_sdk::vexFileStatus(path.as_ptr()) != 0 {
268268
return Err(io::Error::new(
269269
io::ErrorKind::AlreadyExists,
270270
"File exists",
271271
));
272272
}
273-
} else if !create {
273+
} else if !*create {
274274
if vex_sdk::vexFileStatus(path.as_ptr()) == 0 {
275275
return Err(io::Error::new(
276276
io::ErrorKind::NotFound,
@@ -279,7 +279,7 @@ impl File {
279279
}
280280
}
281281

282-
if truncate {
282+
if *truncate {
283283
unsafe { vex_sdk::vexFileOpenCreate(path.as_ptr()) }
284284
} else {
285285
// Open in append, but jump to the start of the file.
@@ -434,7 +434,7 @@ impl File {
434434
// we have to calculate the offset from the end of the file ourselves.
435435
map_fresult(vex_sdk::vexFileSeek(
436436
self.fd.0,
437-
try_convert_offset(self.file_attr()?.size as i64 + offset)?,
437+
try_convert_offset(self.file_attr()?.size() as i64 + offset)?,
438438
SEEK_SET,
439439
))?
440440
}

library/std/src/sys/pal/vexos/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub unsafe extern "C" fn _start() -> ! {
3636
ptr::write_bytes(
3737
&raw mut __bss_start,
3838
0,
39-
(&raw mut __bss_end).offset_from(&raw mut __bss_start) as usize,
39+
(&raw mut __bss_end).offset_from_unsigned(&raw mut __bss_start),
4040
);
4141

4242
main();

0 commit comments

Comments
 (0)