Skip to content
Merged
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
20 changes: 13 additions & 7 deletions score/mw/log/rust/score_log_bridge/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,13 @@ impl From<LogLevel> for score_log::LevelFilter {
}
}

const CONTEXT_MAX_SIZE: usize = 4;

/// Name of the context.
/// Max 4 bytes containing ASCII characters.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Context {
data: [c_char; 4],
data: [c_char; CONTEXT_MAX_SIZE],
size: usize,
}

Expand All @@ -95,10 +97,10 @@ impl From<&str> for Context {
);

// Get number of characters.
let size = min(value.len(), 4);
let size = min(value.len(), CONTEXT_MAX_SIZE);

// Copy data into array.
let mut data = [0; _];
let mut data = [0; CONTEXT_MAX_SIZE];
// SAFETY:
// Copying is safe:
// - source is a `&str`.
Expand All @@ -122,12 +124,12 @@ impl From<&Context> for &str {
// Characters are reinterpreted from `c_char` (`i8`) to `u8`.
let data = value.data.as_ptr().cast();
// Number of bytes is always bound to provided or max allowed size.
let size = min(value.size, 4);
let size = min(value.size, CONTEXT_MAX_SIZE);
unsafe {
// Create a slice from pointer and size.
let slice = from_raw_parts(data, size);
// Create a UTF-8 string from a slice.
str::from_utf8_unchecked(slice)
core::str::from_utf8_unchecked(slice)
}
}
}
Expand Down Expand Up @@ -173,6 +175,8 @@ unsafe impl Send for Recorder {}
// Refer to `runtime.h` for more details.
unsafe impl Sync for Recorder {}

const SLOT_HANDLE_STORAGE_SIZE: usize = 24;

/// Opaque storage type representing `SlotHandle`.
///
/// `SlotHandle` is expected to be allocated on stack to reduce performance overhead.
Expand All @@ -183,7 +187,7 @@ unsafe impl Sync for Recorder {}
#[cfg(any(feature = "x86_64_linux", feature = "arm64_qnx", feature = "x86_64_qnx"))]
#[repr(C, align(8))]
pub struct SlotHandleStorage {
_private: [u8; 24],
_private: [u8; SLOT_HANDLE_STORAGE_SIZE],
}

#[cfg(not(any(feature = "x86_64_linux", feature = "arm64_qnx", feature = "x86_64_qnx")))]
Expand Down Expand Up @@ -212,7 +216,9 @@ impl SlotHandleStorage {
impl Default for SlotHandleStorage {
/// Create storage for `SlotHandle`.
fn default() -> Self {
Self { _private: [0; _] }
Self {
_private: [0; SLOT_HANDLE_STORAGE_SIZE],
}
}
}

Expand Down
Loading