Skip to content

Comments

Raw ptr to slice#26

Merged
ming1 merged 6 commits intomainfrom
raw-ptr-to-slice
Sep 2, 2025
Merged

Raw ptr to slice#26
ming1 merged 6 commits intomainfrom
raw-ptr-to-slice

Conversation

@ming1
Copy link
Collaborator

@ming1 ming1 commented Sep 2, 2025

Convert raw ptr into buffer slice for example/test code.

Add as_slice(), as_mut_slice(), subslice(), and subslice_mut() methods
to IoBuf for safe buffer access without unsafe pointer operations.
These methods leverage existing Deref/DerefMut implementations and
provide automatic bounds checking with Rust's memory safety guarantees.

Signed-off-by: Ming Lei <tom.leiming@gmail.com>
Convert I/O handlers from raw pointer parameters to safe slice references:
- Replace *mut u8 with Option<&[u8]> in handle_io_cmd()
- Use IoBuf's as_slice() method for safe buffer access
- Add educational comments explaining slice safety benefits
- Maintain API compatibility through slice-to-pointer conversion

This demonstrates safer patterns while preserving functionality with
existing libublk APIs that still require raw pointers internally.

Signed-off-by: Ming Lei <tom.leiming@gmail.com>
Replace all unsafe memory operations with safe slice-based alternatives:
- Replace libc::memcpy() calls with copy_from_slice() and copy_to_slice()
- Add comprehensive bounds checking to prevent buffer overflows
- Update function signatures to use slice parameters (&mut [u8])
- Add extensive educational comments explaining slice safety benefits
- Use IoBuf's safe slice access methods throughout

Memory safety improvements:
- Automatic bounds checking prevents buffer overflow vulnerabilities
- Compile-time lifetime verification ensures memory safety
- Eliminates unsafe pointer arithmetic and manual memory management
- Slice operations provide zero-cost abstractions with safety guarantees

This demonstrates how slice-based patterns can replace unsafe memory
operations while maintaining performance and improving code safety.

Signed-off-by: Ming Lei <tom.leiming@gmail.com>
Convert async I/O handler from raw pointer to slice-based patterns:
- Update lo_handle_io_cmd_async() to use &mut [u8] parameter
- Add comprehensive educational comments explaining slice-to-pointer conversion
- Document when pointer conversion is necessary for libublk API compatibility
- Use IoBuf's as_mut_slice() for safe buffer access in async context
- Maintain async/await patterns while improving memory safety

Educational improvements:
- Explain io_uring raw pointer requirements for kernel interface
- Show safe slice access patterns in both sync and async handlers
- Document the conversion pattern: slices for safety, pointers for API calls
- Demonstrate slice bounds checking benefits in async I/O operations

This completes the slice-based conversion for all major example patterns
while preserving the file-backed loop device functionality.

Signed-off-by: Ming Lei <tom.leiming@gmail.com>
- Replace synchronous wait_and_handle_io with async pattern using smol::LocalExecutor
- Convert I/O handling to use async submit_io_cmd with await
- Spawn individual async tasks for each queue tag (0..depth)
- Use ublk_wait_and_handle_ios for async I/O event handling
- Preserve memory copy operations for read/write in async context
- Maintain mlock buffer verification logic for UBLK_DEV_F_MLOCK_IO_BUFFER
- Remove unused rd_handle_io function and UblkError import
- Update console output to indicate async mode

Signed-off-by: Ming Lei <tom.leiming@gmail.com>
…sync ramdisk test

- Update handle_io_cmd signature to accept IO buffer as &mut [u8] slice instead of *mut u8
- Add automatic bounds checking to prevent buffer overruns (bytes > io_buf.len())
- Replace unsafe libc::memcpy with safe slice operations using copy_from_slice()
- Use std::slice::from_raw_parts for safe slice creation from ramdisk memory
- Maintain ramdisk address as usize for device-wide memory access and thread safety
- Improve memory safety while preserving zero-cost abstraction performance
- Add educational comments explaining slice safety benefits over raw pointers

Signed-off-by: Ming Lei <tom.leiming@gmail.com>
@ming1 ming1 merged commit a797702 into main Sep 2, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant