diff --git a/.github/workflows/build-and-publish.yml b/.github/workflows/build-and-publish.yml index 081bcc4..661b43b 100644 --- a/.github/workflows/build-and-publish.yml +++ b/.github/workflows/build-and-publish.yml @@ -4,8 +4,12 @@ on: branches: [ master, refactor-reloaded3-compliance, support-android-and-bsd ] tags: - '*' + paths: + - 'src/**' pull_request: branches: [ master, refactor-reloaded3-compliance, support-android-and-bsd ] + paths: + - 'src/**' workflow_dispatch: jobs: diff --git a/.github/workflows/deploy-mkdocs.yml b/.github/workflows/deploy-mkdocs.yml index f732307..0433caa 100644 --- a/.github/workflows/deploy-mkdocs.yml +++ b/.github/workflows/deploy-mkdocs.yml @@ -1,35 +1,29 @@ -name: Deploy Documentation +name: MkDocs Build and Deploy -# Controls when the action will run. on: - # Triggers the workflow on push on the master branch - push: - branches: [ main, master, refactor-reloaded3-compliance ] - - # Allows you to run this workflow manually from the Actions tab workflow_dispatch: + push: + branches: [ main, master ] + paths: + - "mkdocs.yml" + - "docs/**" + pull_request: + branches: [ main, master ] + paths: + - "mkdocs.yml" + - "docs/**" -# A workflow run is made up of one or more jobs that can run sequentially or in parallel jobs: - # This workflow contains a single job called "build" build: - # The type of runner that the job will run on runs-on: ubuntu-latest - - # Steps represent a sequence of tasks that will be executed as part of the job + permissions: + contents: read + pages: write + id-token: write steps: - - # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it - - name: Checkout Branch - uses: actions/checkout@v2 - with: - submodules: recursive - - # Deploy MkDocs - name: Deploy MkDocs - # You may pin to the exact commit or the version. - # uses: mhausenblas/mkdocs-deploy-gh-pages@66340182cb2a1a63f8a3783e3e2146b7d151a0bb - uses: mhausenblas/mkdocs-deploy-gh-pages@master - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - REQUIREMENTS: ./docs/requirements.txt \ No newline at end of file + uses: Reloaded-Project/devops-mkdocs@v1 + with: + requirements: ./docs/requirements.txt + publish-to-pages: ${{ github.event_name == 'push' }} + checkout-current-repo: true \ No newline at end of file diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 2a9c7d9..f26fe5e 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -1,162 +1,187 @@ -name: Rust Build, Test & Publish +name: Test, Upload Coverage and Release Rust on: push: branches: [ main, master, optimize-for-size ] tags: - '*' + paths: + - 'src-rust/**' pull_request: branches: [ main, master, optimize-for-size ] + paths: + - 'src-rust/**' workflow_dispatch: +env: + build-with-pgo: false + jobs: - test-native: - runs-on: ${{ matrix.os }} + build-and-test-linux: + runs-on: ubuntu-latest strategy: + fail-fast: false matrix: - os: [ubuntu-latest, macos-latest, windows-latest] - - steps: - - uses: actions/checkout@v3 - with: - submodules: recursive - - - id: test-rust - uses: Reloaded-Project/reloaded-project-configurations-rust/.github/actions/test-and-coverage@v1 # upgrade if needed - with: - rust_project_path: src-rust - upload_coverage: true - rust_branch: nightly - - test-wine: - runs-on: ubuntu-latest + target: [x86_64-unknown-linux-gnu, i686-unknown-linux-gnu, aarch64-unknown-linux-gnu] + features: ["c_exports", "c_exports,size_opt", "c_exports,size_opt,no_format", "c_exports,size_opt,no_format,all_private"] steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: submodules: recursive - - id: test-rust - uses: Reloaded-Project/reloaded-project-configurations-rust/.github/actions/test-in-wine@v1 # upgrade if needed + - name: Build C Libraries and Run Tests + uses: Reloaded-Project/devops-rust-lightweight-binary@v1 with: - rust_project_path: src-rust - rust_branch: nightly + rust-project-path: src-rust + crate-name: ${{ github.event.repository.name }} + target: ${{ matrix.target }} + use-pgo: ${{ env.build-with-pgo }} + use-cross: ${{ matrix.target != 'x86_64-unknown-linux-gnu' }} + features: ${{ matrix.features }} + build-library: true + run-tests-and-coverage: true + codecov-token: ${{ secrets.CODECOV_TOKEN }} - build-c-libs-linux: - runs-on: ${{ matrix.os }} + build-and-test-macos-x86: + runs-on: macos-13 strategy: fail-fast: false matrix: - os: [ubuntu-latest] - target: [x86_64-unknown-linux-gnu, i686-unknown-linux-gnu, aarch64-unknown-linux-gnu] features: ["c_exports", "c_exports,size_opt", "c_exports,size_opt,no_format", "c_exports,size_opt,no_format,all_private"] + steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: submodules: recursive - - id: build-libs - uses: Reloaded-Project/reloaded-project-configurations-rust/.github/actions/build-c-library@v1 + + - name: Build C Libraries and Run Tests + uses: Reloaded-Project/devops-rust-lightweight-binary@v1 with: - rust_project_path: src-rust - target: ${{ matrix.target }} + rust-project-path: src-rust + crate-name: ${{ github.event.repository.name }} + target: x86_64-apple-darwin + use-pgo: ${{ env.build-with-pgo }} + use-cross: false features: ${{ matrix.features }} - use_cross: true + build-library: true + run-tests-and-coverage: true + codecov-token: ${{ secrets.CODECOV_TOKEN }} - build-c-libs-macos: - runs-on: ${{ matrix.os }} + build-and-test-macos-arm: + runs-on: macos-14 strategy: fail-fast: false matrix: - os: [macos-latest] - target: [x86_64-apple-darwin, aarch64-apple-darwin] features: ["c_exports", "c_exports,size_opt", "c_exports,size_opt,no_format", "c_exports,size_opt,no_format,all_private"] + steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: submodules: recursive - - id: build-libs - uses: Reloaded-Project/reloaded-project-configurations-rust/.github/actions/build-c-library@v1 + + - name: Build C Libraries and Run Tests + uses: Reloaded-Project/devops-rust-lightweight-binary@v1 with: - rust_project_path: src-rust - target: ${{ matrix.target }} + rust-project-path: src-rust + crate-name: ${{ github.event.repository.name }} + target: aarch64-apple-darwin + use-pgo: ${{ env.build-with-pgo }} + use-cross: false features: ${{ matrix.features }} + build-library: true + run-tests-and-coverage: true + codecov-token: ${{ secrets.CODECOV_TOKEN }} - build-c-libs-windows: - runs-on: ${{ matrix.os }} + build-and-test-windows: strategy: fail-fast: false matrix: - os: [windows-latest] - target: [x86_64-pc-windows-msvc, i686-pc-windows-msvc, aarch64-pc-windows-msvc] + include: + # Native Windows builds with testing + - os: windows-latest + target: x86_64-pc-windows-msvc + run_tests: true + - os: windows-latest + target: i686-pc-windows-msvc + run_tests: true + # aarch64 Windows build without testing + - os: windows-latest + target: aarch64-pc-windows-msvc + run_tests: false # no native runner + # Wine builds (on Linux) + - os: ubuntu-latest + target: x86_64-pc-windows-gnu + run_tests: true + - os: ubuntu-latest + target: i686-pc-windows-gnu + run_tests: true features: ["c_exports,external_processes", "c_exports,size_opt,external_processes", "c_exports,size_opt,no_format,external_processes", "c_exports,size_opt,no_format", "c_exports,size_opt,no_format,all_private"] + + runs-on: ${{ matrix.os }} + steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: submodules: recursive - - id: build-libs - uses: Reloaded-Project/reloaded-project-configurations-rust/.github/actions/build-c-library@v1 + + - name: Build C Libraries and Run Tests + uses: Reloaded-Project/devops-rust-lightweight-binary@v1 with: - rust_project_path: src-rust + rust-project-path: src-rust + crate-name: ${{ github.event.repository.name }} target: ${{ matrix.target }} + use-pgo: ${{ env.build-with-pgo }} + use-cross: ${{ matrix.os == 'ubuntu-latest' || matrix.target == 'aarch64-pc-windows-msvc' }} features: ${{ matrix.features }} + build-library: true + run-tests-and-coverage: ${{ matrix.run_tests }} + codecov-token: ${{ secrets.CODECOV_TOKEN }} + + # Note: The GitHub Runner Images will contain an up to date Rust Stable Toolchain + # thus as per recommendation of cargo-semver-checks, we're using stable here. + - name: Run cargo-semver-checks + if: github.event_name == 'pull_request' || startsWith(github.ref, 'refs/tags/') + run: | + rustup target add ${{ matrix.target }} + cargo install cargo-semver-checks --locked + cargo semver-checks --target ${{ matrix.target }} --features ${{ matrix.features }} build-c-headers: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: submodules: recursive - name: Generate C++ bindings - uses: Reloaded-Project/reloaded-project-configurations-rust/.github/actions/generate-bindings@v1 + uses: Reloaded-Project/devops-rust-cbindgen@v1 with: - rust_project_path: src-rust - config_file: cbindgen_cpp.toml - header_file: bindings_cpp.hpp - + rust-project-path: src-rust + config-file: cbindgen_cpp.toml + output-header-file: bindings_cpp.hpp + - name: Generate C bindings - uses: Reloaded-Project/reloaded-project-configurations-rust/.github/actions/generate-bindings@v1 + uses: Reloaded-Project/devops-rust-cbindgen@v1 with: - rust_project_path: src-rust - config_file: cbindgen_c.toml - header_file: bindings_c.h - - publish-artifacts: - needs: ["build-c-headers", "build-c-libs-windows", "build-c-libs-linux", "build-c-libs-macos", "test-wine", "test-native"] - # Publish only on tags - if: startsWith(github.ref, 'refs/tags/') + rust-project-path: src-rust + config-file: cbindgen_c.toml + output-header-file: bindings_c.h + + publish-crate: + permissions: + contents: write + needs: [build-and-test-linux, build-and-test-macos-x86, build-and-test-macos-arm, build-and-test-windows, build-c-headers] + if: startsWith(github.ref, 'refs/tags/') runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: submodules: recursive - - name: ↙️ Download Artifacts - uses: actions/download-artifact@v3 - with: - path: artifacts - - - name: Compress Artifacts - shell: bash - run: | - dir="artifacts" # Replace with your subdirectory - for subdir in "$dir"/*; do - if [ -d "$subdir" ]; then - base=$(basename "$subdir") - zip -r "$dir/$base.zip" "$subdir" - rm -r "$subdir" - fi - done - ls -A ./artifacts - - - name: GitHub Release Artifacts - uses: softprops/action-gh-release@v1 - with: - files: | - artifacts/* - - - name: Publish to crates.io - uses: Reloaded-Project/reloaded-project-configurations-rust/.github/actions/publish-crate@v1 + - name: Publish Rust Crate and Artifacts + uses: Reloaded-Project/devops-publish-action@v1 with: - rust_project_path: src-rust - token: ${{ secrets.CRATES_IO_TOKEN }} \ No newline at end of file + crates-io-token: ${{ secrets.CRATES_IO_TOKEN }} + rust-project-paths: | + src-rust \ No newline at end of file diff --git a/src-rust/Cargo.toml b/src-rust/Cargo.toml index 28644ba..e38a1b6 100644 --- a/src-rust/Cargo.toml +++ b/src-rust/Cargo.toml @@ -9,9 +9,6 @@ readme = "../README.md" repository = "https://github.com/Reloaded-Project/Reloaded.Memory.Buffers" license = "GPL-3.0" -[lib] -crate-type = ["cdylib"] - # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [features] default = ["std"] @@ -22,6 +19,7 @@ no_format = [] # Removes string formatting (less detailed errors) for binary siz all_private = [] # No memory mapped files, memory is not shared. size_opt = ["nightly"] nightly = [] # Optimizations for nightly builds. +direct-mmap = [] # If set, uses built-in code for memory mapping. This is auto activated in build.rs for supported platforms. [dependencies] concat-string = "1.0.1" diff --git a/src-rust/build.rs b/src-rust/build.rs new file mode 100644 index 0000000..a5dbfa4 --- /dev/null +++ b/src-rust/build.rs @@ -0,0 +1,11 @@ +fn main() { + // This defines a fallback to mmap-rs if one of the explicit memory mapped file implementations + // is not available. + if cfg!(any( + target_os = "macos", + target_os = "windows", + target_os = "linux" + )) { + println!("cargo:rustc-cfg=feature=\"direct-mmap\""); + } +} diff --git a/src-rust/rust-toolchain.toml b/src-rust/rust-toolchain.toml new file mode 100644 index 0000000..271800c --- /dev/null +++ b/src-rust/rust-toolchain.toml @@ -0,0 +1,2 @@ +[toolchain] +channel = "nightly" \ No newline at end of file diff --git a/src-rust/src/buffers.rs b/src-rust/src/buffers.rs index b1e5d2f..f8983be 100644 --- a/src-rust/src/buffers.rs +++ b/src-rust/src/buffers.rs @@ -10,7 +10,6 @@ use crate::utilities::disable_write_xor_execute::{ use crate::utilities::icache_clear::clear_instruction_cache; use crate::utilities::mathematics::round_up; use core::ptr::{copy_nonoverlapping, NonNull}; -use core::u8; pub struct Buffers {} @@ -235,14 +234,13 @@ mod tests { structs::params::{BufferAllocatorSettings, BufferSearchSettings}, utilities::cached::get_sys_info, }; - use std; #[cfg(not(target_os = "macos"))] #[test] fn allocate_private_memory_in_2gib() { let mut settings = BufferAllocatorSettings::new(); settings.min_address = 0; - settings.max_address = std::i32::MAX as usize; + settings.max_address = i32::MAX as usize; let result = Buffers::allocate_private_memory(&mut settings); assert!(result.is_ok()); @@ -412,14 +410,14 @@ mod tests { #[test] fn get_buffer_with_proximity() { const SIZE: usize = 4096; - let base_address = get_sys_info().max_address - (std::i32::MAX as usize); + let base_address = get_sys_info().max_address - (i32::MAX as usize); unsafe { LocatorHeaderFinder::reset(); } let item = Buffers::get_buffer(&BufferSearchSettings::from_proximity( - std::i32::MAX as usize, + i32::MAX as usize, base_address, SIZE, )); diff --git a/src-rust/src/c/buffers_c_buffers.rs b/src-rust/src/c/buffers_c_buffers.rs index 0e2cf89..55d445a 100644 --- a/src-rust/src/c/buffers_c_buffers.rs +++ b/src-rust/src/c/buffers_c_buffers.rs @@ -332,28 +332,30 @@ pub extern "C" fn overwrite_allocated_code_ex( } #[cfg(test)] +#[allow(unused_imports)] mod tests { - use crate::c::buffers_c_buffers::{ - buffers_allocate_private_memory, buffers_get_buffer, buffersearchsettings_from_proximity, - }; - use crate::c::buffers_c_buffers::{free_allocation_result, free_get_buffer_result}; - use crate::c::buffers_c_locatoritem::{ - locatoritem_append_bytes, locatoritem_bytes_left, locatoritem_min_address, + use crate::c::{ + buffers_c_buffers::{ + buffers_allocate_private_memory, buffers_get_buffer, + buffersearchsettings_from_proximity, free_allocation_result, free_get_buffer_result, + }, + buffers_c_locatoritem::locatoritem_append_bytes, }; use crate::{ + c::buffers_c_locatoritem::{locatoritem_bytes_left, locatoritem_min_address}, internal::locator_header_finder::LocatorHeaderFinder, structs::params::{BufferAllocatorSettings, BufferSearchSettings}, utilities::cached::get_sys_info, }; + use rstest::rstest; - use std; #[cfg(not(target_os = "macos"))] #[test] fn allocate_private_memory_in_2gib() { let mut settings = BufferAllocatorSettings::new(); settings.min_address = 0; - settings.max_address = std::i32::MAX as usize; + settings.max_address = i32::MAX as usize; let result = buffers_allocate_private_memory(&mut settings); assert!(result.is_ok); @@ -430,19 +432,16 @@ mod tests { #[test] fn get_buffer_with_proximity() { const SIZE: usize = 4096; - let base_address = get_sys_info().max_address - (std::i32::MAX as usize); + let base_address = get_sys_info().max_address - (i32::MAX as usize); unsafe { LocatorHeaderFinder::reset(); } - let settings = - buffersearchsettings_from_proximity(std::i32::MAX as usize, base_address, SIZE); - + let settings = buffersearchsettings_from_proximity(i32::MAX as usize, base_address, SIZE); let result = buffers_get_buffer(&settings); assert!(result.is_ok); - assert!(locatoritem_bytes_left(result.ok) >= SIZE as u32); let offset = (locatoritem_min_address(result.ok) as i64 - base_address as i64).abs(); diff --git a/src-rust/src/internal/buffer_allocator.rs b/src-rust/src/internal/buffer_allocator.rs index 0d4b933..802bca2 100644 --- a/src-rust/src/internal/buffer_allocator.rs +++ b/src-rust/src/internal/buffer_allocator.rs @@ -22,7 +22,7 @@ pub fn allocate( return crate::internal::buffer_allocator_osx::allocate_osx(settings); // Fallback for non-hot-path OSes. - #[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "linux")))] + #[cfg(not(feature = "direct-mmap"))] crate::internal::buffer_allocator_mmap_rs::allocate_mmap_rs(settings) } diff --git a/src-rust/src/internal/buffer_allocator_windows.rs b/src-rust/src/internal/buffer_allocator_windows.rs index 1f11a5d..573df6c 100644 --- a/src-rust/src/internal/buffer_allocator_windows.rs +++ b/src-rust/src/internal/buffer_allocator_windows.rs @@ -56,12 +56,12 @@ impl Kernel32 for LocalKernel32 { } } -#[cfg(feature = "external_process")] +#[cfg(feature = "external_processes")] pub struct RemoteKernel32 { handle: HANDLE, } -#[cfg(feature = "external_process")] +#[cfg(feature = "external_processes")] impl Kernel32 for RemoteKernel32 { fn virtual_query( &self, @@ -69,7 +69,7 @@ impl Kernel32 for RemoteKernel32 { lp_buffer: &mut MEMORY_BASIC_INFORMATION, ) -> usize { unsafe { - VirtualQueryEx( + windows_sys::Win32::System::Memory::VirtualQueryEx( self.handle, lp_address, lp_buffer, @@ -80,7 +80,7 @@ impl Kernel32 for RemoteKernel32 { fn virtual_alloc(&self, lp_address: *const c_void, dw_size: usize) -> *mut c_void { unsafe { - VirtualAllocEx( + windows_sys::Win32::System::Memory::VirtualAllocEx( self.handle, lp_address, dw_size, @@ -91,7 +91,14 @@ impl Kernel32 for RemoteKernel32 { } fn virtual_free(&self, lp_address: *mut c_void, dw_size: usize) -> bool { - unsafe { VirtualFreeEx(self.handle, lp_address, dw_size, MEM_RELEASE) != 0 } + unsafe { + windows_sys::Win32::System::Memory::VirtualFreeEx( + self.handle, + lp_address, + dw_size, + MEM_RELEASE, + ) != 0 + } } } @@ -111,6 +118,11 @@ impl ProcessHandle { pub fn is_valid(&self) -> bool { self.handle != 0 } + + #[cfg(feature = "external_processes")] + pub fn get_raw_handle(&self) -> HANDLE { + self.handle + } } impl Drop for ProcessHandle { @@ -170,16 +182,16 @@ pub fn allocate_windows( let handle = process_handle.handle; let max_address = get_max_windows_address(settings.target_process_id, handle); - #[cfg(feature = "external_process")] + #[cfg(feature = "external_processes")] { - return if get_sys_info().this_process_id == settings.target_process_id { - allocate_fast(&LocalKernel32 {}, max_address, &settings) + if get_sys_info().this_process_id == settings.target_process_id { + allocate_fast(&LocalKernel32 {}, max_address, settings) } else { - allocate_fast(&RemoteKernel32 { handle }, max_address, &settings) - }; + allocate_fast(&RemoteKernel32 { handle }, max_address, settings) + } } - #[cfg(not(feature = "external_process"))] + #[cfg(not(feature = "external_processes"))] allocate_fast(&LocalKernel32 {}, max_address, settings) } } diff --git a/src-rust/src/internal/memory_mapped_file_unix.rs b/src-rust/src/internal/memory_mapped_file_unix.rs index 432fdda..278d3a0 100644 --- a/src-rust/src/internal/memory_mapped_file_unix.rs +++ b/src-rust/src/internal/memory_mapped_file_unix.rs @@ -10,7 +10,6 @@ use alloc::string::String; #[cfg(not(feature = "no_format"))] use errno::errno; -use libc::c_char; use libc::mkdir; use libc::stat; use libc::S_IFDIR; @@ -156,8 +155,11 @@ impl Drop for UnixMemoryMappedFile { let _ = unsafe { munmap(self.data as *mut c_void, self.length) }; unsafe { close(self.file_descriptor) }; if !self.already_existed { - unsafe { - libc::unlink(self.file_path.as_ptr() as *const c_char); + // Create a null-terminated C string + if let Ok(file_path_cstr) = CString::new(self.file_path.as_str()) { + unsafe { + libc::unlink(file_path_cstr.as_ptr()); + } } } } diff --git a/src-rust/src/internal/memory_mapped_file_windows.rs b/src-rust/src/internal/memory_mapped_file_windows.rs index 19289ad..567e954 100644 --- a/src-rust/src/internal/memory_mapped_file_windows.rs +++ b/src-rust/src/internal/memory_mapped_file_windows.rs @@ -92,12 +92,12 @@ mod tests { let file_length = get_sys_info().allocation_granularity as usize; let mmf = WindowsMemoryMappedFile::new(&file_name, file_length); - assert_eq!(mmf.already_existed, false); + assert!(!mmf.already_existed); assert_eq!(mmf.length, file_length); // Assert the file can be opened again (i.e., it exists) let mmf_existing = WindowsMemoryMappedFile::new(&file_name, file_length); - assert_eq!(mmf_existing.already_existed, true); + assert!(mmf_existing.already_existed); } #[test] diff --git a/src-rust/src/lib.rs b/src-rust/src/lib.rs index bae3de9..ab79807 100644 --- a/src-rust/src/lib.rs +++ b/src-rust/src/lib.rs @@ -132,7 +132,10 @@ pub(crate) mod utilities { pub mod address_range; pub mod cached; pub mod icache_clear; + + #[cfg(any(target_os = "linux", not(feature = "direct-mmap")))] pub mod map_parser_utilities; + pub mod mathematics; pub mod wrappers; diff --git a/src-rust/src/structs/internal/locator_header.rs b/src-rust/src/structs/internal/locator_header.rs index 7ba8873..26cc083 100644 --- a/src-rust/src/structs/internal/locator_header.rs +++ b/src-rust/src/structs/internal/locator_header.rs @@ -8,6 +8,7 @@ use crate::utilities::cached::get_sys_info; use crate::utilities::wrappers::Unaligned; use core::alloc::Layout; use core::cell::Cell; +#[cfg(not(all(target_os = "macos", target_arch = "aarch64")))] use core::cmp::min; use core::mem::size_of; use core::ptr::null_mut; @@ -88,6 +89,7 @@ impl LocatorHeader { self.num_items = 0; } + #[cfg(not(all(target_os = "macos", target_arch = "aarch64")))] fn initialize_remaining_space_as_buffers(&mut self, mut remaining_bytes: u32) { let mut num_items = 0u8; unsafe { diff --git a/src-rust/src/structs/private_allocation.rs b/src-rust/src/structs/private_allocation.rs index f6c6c05..9ccd8d1 100644 --- a/src-rust/src/structs/private_allocation.rs +++ b/src-rust/src/structs/private_allocation.rs @@ -96,8 +96,11 @@ impl PrivateAllocation { use core::ffi::c_void; unsafe { - #[cfg(feature = "external_process")] + #[cfg(feature = "external_processes")] { + use crate::internal::buffer_allocator_windows::ProcessHandle; + use windows_sys::Win32::System::Memory::VirtualFreeEx; + if self._this_process_id == get_sys_info().this_process_id { let result = VirtualFree(self.base_address.as_ptr() as *mut c_void, 0, MEM_RELEASE); @@ -107,7 +110,7 @@ impl PrivateAllocation { } else { let process_handle = ProcessHandle::open_process(self._this_process_id); let result = VirtualFreeEx( - process_handle.get_handle(), + process_handle.get_raw_handle(), self.base_address.as_ptr() as *mut c_void, 0, MEM_RELEASE, @@ -118,7 +121,7 @@ impl PrivateAllocation { }; } - #[cfg(not(feature = "external_process"))] + #[cfg(not(feature = "external_processes"))] { let result = VirtualFree(self.base_address.as_ptr() as *mut c_void, 0, MEM_RELEASE); if result == 0 { @@ -165,7 +168,7 @@ impl PrivateAllocation { } /// Frees the allocated memory when the `PrivateAllocation` instance is dropped. - #[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "linux")))] + #[cfg(not(feature = "direct-mmap"))] pub(crate) fn drop_mmap_rs(&mut self) { use mmap_rs_with_map_from_existing::MmapOptions; let _map = unsafe { @@ -193,7 +196,7 @@ impl Drop for PrivateAllocation { return PrivateAllocation::drop_macos(self); // non-hot-path-os - #[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "linux")))] + #[cfg(not(feature = "direct-mmap"))] return PrivateAllocation::drop_mmap_rs(self); } } diff --git a/src-rust/src/utilities/disable_write_xor_execute.rs b/src-rust/src/utilities/disable_write_xor_execute.rs index 1d890c2..02a9e16 100644 --- a/src-rust/src/utilities/disable_write_xor_execute.rs +++ b/src-rust/src/utilities/disable_write_xor_execute.rs @@ -1,7 +1,7 @@ // An utility to disable write xor execute protection on a memory region. // This method contains the code to disable W^X on platforms where it's enforced. -#[cfg(target_os = "macos")] +#[cfg(all(target_os = "macos", target_arch = "aarch64"))] use { libc::mach_task_self, mach::vm::mach_vm_protect, mach::vm_prot::VM_PROT_EXECUTE, mach::vm_prot::VM_PROT_READ, mach::vm_prot::VM_PROT_WRITE, mach::vm_types::mach_vm_size_t, diff --git a/src-rust/src/utilities/linux_map_parser.rs b/src-rust/src/utilities/linux_map_parser.rs index b48dfbd..553caff 100644 --- a/src-rust/src/utilities/linux_map_parser.rs +++ b/src-rust/src/utilities/linux_map_parser.rs @@ -112,6 +112,7 @@ pub fn get_free_regions_from_process_id(process_id: i32) -> Vec get_free_regions(®ions) } +#[allow(clippy::comparison_chain)] unsafe fn read_to_string(path: &str) -> String { const BUFFER_SIZE: usize = 131_072; // 128 KB let c_path = CString::new(path).unwrap(); diff --git a/src-rust/src/utilities/map_parser_utilities.rs b/src-rust/src/utilities/map_parser_utilities.rs index cbfd949..f7d7b53 100644 --- a/src-rust/src/utilities/map_parser_utilities.rs +++ b/src-rust/src/utilities/map_parser_utilities.rs @@ -44,7 +44,6 @@ impl MemoryMapEntryTrait for MemoryMapEntry { /// # Arguments /// /// * `regions` - A slice of MemoryMapEntry that contains the regions. - #[cfg_attr(feature = "size_opt", optimize(size))] pub fn get_free_regions(regions: &[T]) -> Vec { let mut last_end_address: usize = 0;