Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read_addrs() can return fewer bytes than requested #115

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions example_no_std/src/gdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ impl MultiThreadBase for DummyTarget {
_start_addr: u32,
data: &mut [u8],
_tid: Tid, // same address space for each core
) -> TargetResult<(), Self> {
) -> TargetResult<usize, Self> {
print_str("> read_addrs");
data.iter_mut().for_each(|b| *b = 0x55);
Ok(())
Ok(data.len())
}

#[inline(never)]
Expand Down
17 changes: 14 additions & 3 deletions examples/armv4t/gdb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,22 @@ impl SingleThreadBase for Emu {
Some(self)
}

fn read_addrs(&mut self, start_addr: u32, data: &mut [u8]) -> TargetResult<(), Self> {
for (addr, val) in (start_addr..).zip(data.iter_mut()) {
fn read_addrs(&mut self, start_addr: u32, data: &mut [u8]) -> TargetResult<usize, Self> {
// These values are taken from the link script.
const MEMORY_ORIGIN: u32 = 0x5555_0000;
const MEMORY_LENGTH: u32 = 0x1000_0000;
const MEMORY_END: u32 = MEMORY_ORIGIN + MEMORY_LENGTH;

if !(MEMORY_ORIGIN..MEMORY_END).contains(&start_addr) {
return Err(TargetError::NonFatal);
}

let end_addr = std::cmp::min(start_addr + data.len() as u32, MEMORY_END);

for (addr, val) in (start_addr..end_addr).zip(data.iter_mut()) {
*val = self.mem.r8(addr)
}
Ok(())
Ok((end_addr - start_addr) as usize)
}

fn write_addrs(&mut self, start_addr: u32, data: &[u8]) -> TargetResult<(), Self> {
Expand Down
4 changes: 2 additions & 2 deletions examples/armv4t_multicore/gdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ impl MultiThreadBase for Emu {
start_addr: u32,
data: &mut [u8],
_tid: Tid, // same address space for each core
) -> TargetResult<(), Self> {
) -> TargetResult<usize, Self> {
for (addr, val) in (start_addr..).zip(data.iter_mut()) {
*val = self.mem.r8(addr)
}
Ok(())
Ok(data.len())
}

fn write_addrs(
Expand Down
4 changes: 3 additions & 1 deletion src/stub/core_impl/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ impl<T: Target, C: Connection> GdbStubImpl<T, C> {

let addr = addr + NumCast::from(i).ok_or(Error::TargetMismatch)?;
let data = &mut buf[..chunk_size];
match target.base_ops() {
let data_len = match target.base_ops() {
BaseOps::SingleThread(ops) => ops.read_addrs(addr, data),
BaseOps::MultiThread(ops) => {
ops.read_addrs(addr, data, self.current_mem_tid)
Expand All @@ -249,6 +249,8 @@ impl<T: Target, C: Connection> GdbStubImpl<T, C> {
n -= chunk_size;
i += chunk_size;

// TODO: add more specific error variant?
let data = data.get(..data_len).ok_or(Error::PacketBufferOverflow)?;
res.write_hex_buf(data)?;
}
HandlerStatus::Handled
Expand Down
18 changes: 13 additions & 5 deletions src/target/ext/base/multithread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,25 @@ pub trait MultiThreadBase: Target {
None
}

/// Read bytes from the specified address range.
/// Read bytes from the specified address range and return the number of
/// bytes that were read.
///
/// If the requested address range could not be accessed (e.g: due to
/// MMU protection, unhanded page fault, etc...), an appropriate non-fatal
/// error should be returned.
/// Implementations may return a number `n` that is less than `data.len()`
/// to indicate that memory starting at `start_addr + n` cannot be
/// accessed.
///
/// Implemenations may also return an appropriate non-fatal error if the
/// requested address range could not be accessed (e.g: due to MMU
/// protection, unhanded page fault, etc...).
///
/// Implementations must guarantee that the returned number is less than or
/// equal `data.len()`.
fn read_addrs(
&mut self,
start_addr: <Self::Arch as Arch>::Usize,
data: &mut [u8],
tid: Tid,
) -> TargetResult<(), Self>;
) -> TargetResult<usize, Self>;

/// Write bytes to the specified address range.
///
Expand Down
18 changes: 13 additions & 5 deletions src/target/ext/base/singlethread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,24 @@ pub trait SingleThreadBase: Target {
None
}

/// Read bytes from the specified address range.
/// Read bytes from the specified address range and return the number of
/// bytes that were read.
///
/// If the requested address range could not be accessed (e.g: due to
/// MMU protection, unhanded page fault, etc...), an appropriate
/// non-fatal error should be returned.
/// Implementations may return a number `n` that is less than `data.len()`
/// to indicate that memory starting at `start_addr + n` cannot be
/// accessed.
///
/// Implemenations may also return an appropriate non-fatal error if the
/// requested address range could not be accessed (e.g: due to MMU
/// protection, unhanded page fault, etc...).
///
/// Implementations must guarantee that the returned number is less than or
/// equal `data.len()`.
fn read_addrs(
&mut self,
start_addr: <Self::Arch as Arch>::Usize,
data: &mut [u8],
) -> TargetResult<(), Self>;
) -> TargetResult<usize, Self>;

/// Write bytes to the specified address range.
///
Expand Down
4 changes: 2 additions & 2 deletions src/target/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
//! &mut self,
//! start_addr: u32,
//! data: &mut [u8],
//! ) -> TargetResult<(), Self> { todo!() }
//! ) -> TargetResult<usize, Self> { todo!() }
//!
//! fn write_addrs(
//! &mut self,
Expand Down Expand Up @@ -409,7 +409,7 @@ pub trait Target {
/// # &mut self,
/// # start_addr: u32,
/// # data: &mut [u8],
/// # ) -> TargetResult<(), Self> { todo!() }
/// # ) -> TargetResult<usize, Self> { todo!() }
/// #
/// # fn write_addrs(
/// # &mut self,
Expand Down