-
Notifications
You must be signed in to change notification settings - Fork 1
WIP: harm runtime #50
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,3 +4,5 @@ | |
| */ | ||
|
|
||
| pub mod labels; | ||
| pub mod memory; | ||
| pub mod runtime; | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,72 @@ | ||||||||||||||||||||||||||||||||||||||||||
| /* Copyright (C) 2026 Ivan Boldyrev | ||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||
| * This document is licensed under the BSD 3-clause license. | ||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| #[cfg(feature = "memmap2")] | ||||||||||||||||||||||||||||||||||||||||||
| mod memmap2; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| #[cfg(feature = "memmap2")] | ||||||||||||||||||||||||||||||||||||||||||
| pub use self::memmap2::{Mmap2Buffer, Mmap2FixedMemory}; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| pub trait Memory<FM: FixedMemory> { | ||||||||||||||||||||||||||||||||||||||||||
| type ExtendError; | ||||||||||||||||||||||||||||||||||||||||||
| type FixedMemoryError; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| /// Current writing position. | ||||||||||||||||||||||||||||||||||||||||||
| fn pos(&self) -> usize; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| /// If memory has fixed capacity, return it. | ||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||
| /// A `Vec` is not considered a memory of fixed capacity because it can grow indefinitely. | ||||||||||||||||||||||||||||||||||||||||||
| fn capacity(&self) -> Option<usize>; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| /// Append data to the memory. Should fail when it reaches memory's capacity. | ||||||||||||||||||||||||||||||||||||||||||
| fn try_extend<I: Iterator<Item = u8>>(&mut self, bytes: I) -> Result<(), Self::ExtendError>; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| /// Transform into fixed-location memory. | ||||||||||||||||||||||||||||||||||||||||||
| fn into_fixed_memory(self) -> Result<FM, Self::FixedMemoryError>; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| /// Align position. | ||||||||||||||||||||||||||||||||||||||||||
| fn align(&mut self, alignment: usize) -> Result<(), Self::ExtendError> { | ||||||||||||||||||||||||||||||||||||||||||
| let pos = self.pos(); | ||||||||||||||||||||||||||||||||||||||||||
| let remn = pos % alignment; | ||||||||||||||||||||||||||||||||||||||||||
| if remn != 0 { | ||||||||||||||||||||||||||||||||||||||||||
| self.try_extend(core::iter::repeat(0).take(alignment - remn))?; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| Ok(()) | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+30
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Guard against alignment == 0 to avoid panic.
Suggested fix fn align(&mut self, alignment: usize) -> Result<(), Self::ExtendError> {
- let pos = self.pos();
- let remn = pos % alignment;
+ if alignment <= 1 {
+ return Ok(());
+ }
+ let pos = self.pos();
+ let remn = pos % alignment;
if remn != 0 {
self.try_extend(core::iter::repeat(0).take(alignment - remn))?;
}
Ok(())
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| /// Memory with fixed location that can be transformed to an executable one after relocations are applied. | ||||||||||||||||||||||||||||||||||||||||||
| pub trait FixedMemory: AsMut<[u8]> { | ||||||||||||||||||||||||||||||||||||||||||
| type ExecutableMemory; | ||||||||||||||||||||||||||||||||||||||||||
| type ExecutableMemoryError; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| fn into_executable_memory(self) -> Result<Self::ExecutableMemory, Self::ExecutableMemoryError>; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| #[cfg(test)] | ||||||||||||||||||||||||||||||||||||||||||
| mod tests { | ||||||||||||||||||||||||||||||||||||||||||
| #[cfg(feature = "memmap2")] | ||||||||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||||||||
| fn test_align() { | ||||||||||||||||||||||||||||||||||||||||||
| use super::*; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| let mut data = Vec::<u8>::new(); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| Memory::align(&mut data, 8); | ||||||||||||||||||||||||||||||||||||||||||
| assert!(data.is_empty()); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| data.push(1); | ||||||||||||||||||||||||||||||||||||||||||
| Memory::align(&mut data, 8); | ||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(data.len(), 8); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| Memory::align(&mut data, 8); | ||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(data.len(), 8); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| data.extend_from_slice(&[1, 2, 3, 4, 5, 6, 7]); | ||||||||||||||||||||||||||||||||||||||||||
| Memory::align(&mut data, 8); | ||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(data.len(), 16); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid panics and silent redefinitions in label definition APIs.
define_labelusestodo!/panic!,define_named_labeloverwrites existing definitions, andname_labelcan rebind names. For a public API this should returnResultwith explicit errors to prevent accidental label corruption and runtime panics. This will also require updatingAssemblercall sites.Suggested fix (return Result and guard redefinitions)
📝 Committable suggestion
🤖 Prompt for AI Agents