diff --git a/Cargo.toml b/Cargo.toml index 9bb5bcb3bf..278ca52bff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -71,6 +71,7 @@ exclude = [ "common/assertions", "common/audio_streams", "common/base", + "common/sys_util_core", "common/cros-fuzz", "common/cros_async", "common/cros_asyncv2", @@ -172,6 +173,7 @@ base = "*" assertions = { path = "common/assertions" } audio_streams = { path = "common/audio_streams" } base = { path = "common/base" } +sys_util_core = { path = "common/sys_util_core" } cros_async = { path = "common/cros_async" } cros_fuzz = { path = "common/cros-fuzz" } # ignored by ebuild data_model = { path = "common/data_model" } diff --git a/common/sys_util/Cargo.toml b/common/sys_util/Cargo.toml index 8baf42a439..29714686ac 100644 --- a/common/sys_util/Cargo.toml +++ b/common/sys_util/Cargo.toml @@ -6,9 +6,10 @@ edition = "2018" include = ["src/**/*", "Cargo.toml"] [dependencies] +sys_util_core = { path = "../sys_util_core" } # provided by ebuild data_model = { path = "../data_model" } # provided by ebuild libc = "*" -poll_token_derive = { version = "*", path = "poll_token_derive" } +poll_token_derive = { path = "../sys_util_core/poll_token_derive" } # provided by ebuild remain = "0.2" thiserror = "*" serde = { version = "1", features = [ "derive" ] } diff --git a/common/sys_util/src/lib.rs b/common/sys_util/src/lib.rs index 28a3755248..609a15db31 100644 --- a/common/sys_util/src/lib.rs +++ b/common/sys_util/src/lib.rs @@ -4,7 +4,6 @@ //! Small system utility modules for usage by other modules. -mod alloc; #[cfg(target_os = "android")] mod android; #[cfg(target_os = "android")] @@ -26,7 +25,6 @@ mod descriptor; mod descriptor_reflection; mod errno; mod eventfd; -mod external_mapping; mod file_flags; pub mod file_traits; mod fork; @@ -53,13 +51,11 @@ pub mod vsock; mod write_zeroes; pub use crate::acpi_event::*; -pub use crate::alloc::LayoutAllocation; pub use crate::capabilities::drop_capabilities; pub use crate::clock::{Clock, FakeClock}; pub use crate::descriptor::*; pub use crate::errno::{errno_result, Error, Result}; pub use crate::eventfd::*; -pub use crate::external_mapping::*; pub use crate::file_flags::*; pub use crate::fork::*; pub use crate::get_filesystem_type::*; @@ -82,9 +78,8 @@ pub use descriptor_reflection::{ SerializeDescriptors, }; pub use poll_token_derive::*; +pub use sys_util_core::*; -pub use crate::external_mapping::Error as ExternalMappingError; -pub use crate::external_mapping::Result as ExternalMappingResult; pub use crate::file_traits::{ AsRawFds, FileAllocate, FileGetLen, FileReadWriteAtVolatile, FileReadWriteVolatile, FileSetLen, FileSync, diff --git a/common/sys_util/src/mmap.rs b/common/sys_util/src/mmap.rs index 3db2f0b715..8b9ca6c319 100644 --- a/common/sys_util/src/mmap.rs +++ b/common/sys_util/src/mmap.rs @@ -13,6 +13,7 @@ use std::ptr::{copy_nonoverlapping, null_mut, read_unaligned, write_unaligned}; use libc::{self, c_int, c_void, read, write}; use remain::sorted; +use sys_util_core::ExternalMapping; use data_model::volatile_memory::*; use data_model::DataInit; @@ -170,6 +171,17 @@ impl dyn MappedRegion { } } +unsafe impl MappedRegion for ExternalMapping { + fn as_ptr(&self) -> *mut u8 { + self.as_ptr() + } + + /// Returns the size of the memory region in bytes. + fn size(&self) -> usize { + self.size() + } +} + /// Wraps an anonymous shared memory mapping in the current process. Provides /// RAII semantics including munmap when no longer needed. #[derive(Debug)] diff --git a/common/sys_util/src/netlink.rs b/common/sys_util/src/netlink.rs index 7daaded905..e6250e749c 100644 --- a/common/sys_util/src/netlink.rs +++ b/common/sys_util/src/netlink.rs @@ -9,7 +9,9 @@ use std::os::unix::io::AsRawFd; use data_model::DataInit; use libc::EINVAL; -use crate::{errno_result, Error, FromRawDescriptor, LayoutAllocation, Result, SafeDescriptor}; +use sys_util_core::LayoutAllocation; + +use crate::{errno_result, Error, FromRawDescriptor, Result, SafeDescriptor}; // Custom nlmsghdr struct that can be declared DataInit. #[repr(C)] diff --git a/common/sys_util/poll_token_derive/.windows_build_test_skip b/common/sys_util_core/.build_test_serial similarity index 100% rename from common/sys_util/poll_token_derive/.windows_build_test_skip rename to common/sys_util_core/.build_test_serial diff --git a/common/sys_util_core/.windows_build_test_skip b/common/sys_util_core/.windows_build_test_skip new file mode 100644 index 0000000000..e69de29bb2 diff --git a/common/sys_util_core/Cargo.toml b/common/sys_util_core/Cargo.toml new file mode 100644 index 0000000000..4645d4dcc5 --- /dev/null +++ b/common/sys_util_core/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "sys_util_core" +version = "0.1.0" +authors = ["The Chromium OS Authors"] +edition = "2018" +include = ["src/**/*", "Cargo.toml"] + +[dependencies] +libc = "*" +remain = "0.2" +thiserror = "*" + +[workspace] diff --git a/common/sys_util_core/poll_token_derive/.windows_build_test_skip b/common/sys_util_core/poll_token_derive/.windows_build_test_skip new file mode 100644 index 0000000000..e69de29bb2 diff --git a/common/sys_util/poll_token_derive/Cargo.toml b/common/sys_util_core/poll_token_derive/Cargo.toml similarity index 95% rename from common/sys_util/poll_token_derive/Cargo.toml rename to common/sys_util_core/poll_token_derive/Cargo.toml index 45290bed07..b4e364bdf5 100644 --- a/common/sys_util/poll_token_derive/Cargo.toml +++ b/common/sys_util_core/poll_token_derive/Cargo.toml @@ -13,3 +13,5 @@ path = "poll_token_derive.rs" proc-macro2 = "^1" quote = "^1" syn = "^1" + +[workspace] diff --git a/common/sys_util/poll_token_derive/poll_token_derive.rs b/common/sys_util_core/poll_token_derive/poll_token_derive.rs similarity index 100% rename from common/sys_util/poll_token_derive/poll_token_derive.rs rename to common/sys_util_core/poll_token_derive/poll_token_derive.rs diff --git a/common/sys_util/poll_token_derive/tests.rs b/common/sys_util_core/poll_token_derive/tests.rs similarity index 100% rename from common/sys_util/poll_token_derive/tests.rs rename to common/sys_util_core/poll_token_derive/tests.rs diff --git a/common/sys_util/src/alloc.rs b/common/sys_util_core/src/alloc.rs similarity index 100% rename from common/sys_util/src/alloc.rs rename to common/sys_util_core/src/alloc.rs diff --git a/common/sys_util/src/external_mapping.rs b/common/sys_util_core/src/external_mapping.rs similarity index 96% rename from common/sys_util/src/external_mapping.rs rename to common/sys_util_core/src/external_mapping.rs index 8499ce447a..22c98c7538 100644 --- a/common/sys_util/src/external_mapping.rs +++ b/common/sys_util_core/src/external_mapping.rs @@ -2,8 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -use crate::MappedRegion; - use remain::sorted; use thiserror::Error; @@ -70,16 +68,14 @@ impl ExternalMapping { unmap, }) } -} -unsafe impl MappedRegion for ExternalMapping { /// used for passing this region to ioctls for setting guest memory. - fn as_ptr(&self) -> *mut u8 { + pub fn as_ptr(&self) -> *mut u8 { self.ptr as *mut u8 } /// Returns the size of the memory region in bytes. - fn size(&self) -> usize { + pub fn size(&self) -> usize { self.size } } diff --git a/common/sys_util_core/src/lib.rs b/common/sys_util_core/src/lib.rs new file mode 100644 index 0000000000..4a71616750 --- /dev/null +++ b/common/sys_util_core/src/lib.rs @@ -0,0 +1,22 @@ +// Copyright 2022 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +//! Small system utility modules for usage by other higher level system +//! utility modules like sys_util(on unix/linux) and win_sys_util(on windows). +//! +//! Crates other than sys_util and win_sys_util should not depend directly on +//! sys_util_core. +//! +//! sys_util_core contains system utilities that are strictly platform/os +//! independent. Platform dependent, conditionally compiled, code should +//! not be added to sys_util_core. +//! + +mod alloc; +mod external_mapping; + +pub use crate::alloc::LayoutAllocation; +pub use crate::external_mapping::Error as ExternalMappingError; +pub use crate::external_mapping::Result as ExternalMappingResult; +pub use crate::external_mapping::*; diff --git a/tools/impl/test_config.py b/tools/impl/test_config.py index 6166b45be1..4333705a3f 100755 --- a/tools/impl/test_config.py +++ b/tools/impl/test_config.py @@ -32,6 +32,7 @@ class TestOption(enum.Enum): # Please add a bug number when restricting a tests. CRATE_OPTIONS: dict[str, list[TestOption]] = { "aarch64": [TestOption.BUILD_ARM_ONLY, TestOption.DO_NOT_BUILD_ARMHF], #b/210015864 + "sys_util_core": [TestOption.SINGLE_THREADED], "crosvm_plugin": [TestOption.BUILD_X86_ONLY], "devices": [TestOption.SINGLE_THREADED, TestOption.DO_NOT_BUILD_ARMHF], "disk": [TestOption.RUN_X86_ONLY], # b/202294155 @@ -49,9 +50,9 @@ CRATE_OPTIONS: dict[str, list[TestOption]] = { "x86_64": [TestOption.BUILD_X86_ONLY], "sys_util": [TestOption.SINGLE_THREADED], "rutabaga_gfx_ffi": [TestOption.DO_NOT_BUILD], # b/206689789 - "rutabaga_gfx": [TestOption.DO_NOT_BUILD_ARMHF], #b/210015864 - "vm_control": [TestOption.DO_NOT_BUILD_ARMHF], #b/210015864 - "libcrosvm_control": [TestOption.DO_NOT_BUILD_ARMHF], #b/210015864 + "rutabaga_gfx": [TestOption.DO_NOT_BUILD_ARMHF], # b/210015864 + "vm_control": [TestOption.DO_NOT_BUILD_ARMHF], # b/210015864 + "libcrosvm_control": [TestOption.DO_NOT_BUILD_ARMHF], # b/210015864 } BUILD_FEATURES: dict[str, str] = {