From 507b6de8e703ed14d4a94f2f63e0811e258ee6ce Mon Sep 17 00:00:00 2001 From: Sewer56 Date: Sun, 23 Jun 2024 23:17:54 +0100 Subject: [PATCH 01/11] Improve: Use revamped Reloaded GitHub Actions --- .github/workflows/deploy-mkdocs.yml | 46 +++---- .github/workflows/rust.yml | 202 +++++++++++++++------------- 2 files changed, 127 insertions(+), 121 deletions(-) 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..dcb4aee 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -1,4 +1,4 @@ -name: Rust Build, Test & Publish +name: Test, Upload Coverage and Release Rust on: push: @@ -9,154 +9,166 @@ on: branches: [ main, master, optimize-for-size ] 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 }} 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 From be4b7f2a954e8d5b16f3babe3cdd12742bf88fac Mon Sep 17 00:00:00 2001 From: Sewer56 Date: Sun, 23 Jun 2024 23:58:22 +0100 Subject: [PATCH 02/11] Style: Fixup use of legacy numeric constants. --- src-rust/src/buffers.rs | 5 ++--- src-rust/src/c/buffers_c_buffers.rs | 8 +++----- src-rust/src/utilities/linux_map_parser.rs | 1 + 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src-rust/src/buffers.rs b/src-rust/src/buffers.rs index b1e5d2f..5129a45 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 {} @@ -242,7 +241,7 @@ mod tests { 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,7 +411,7 @@ 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(); diff --git a/src-rust/src/c/buffers_c_buffers.rs b/src-rust/src/c/buffers_c_buffers.rs index 0e2cf89..ac7c642 100644 --- a/src-rust/src/c/buffers_c_buffers.rs +++ b/src-rust/src/c/buffers_c_buffers.rs @@ -346,14 +346,13 @@ mod tests { 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,14 +429,13 @@ 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); 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(); From a6e4be805e94fa3ba82feb84a7efd57268c0e856 Mon Sep 17 00:00:00 2001 From: Sewer56 Date: Mon, 24 Jun 2024 00:00:24 +0100 Subject: [PATCH 03/11] Fix: Don't build cdylib only, now CI handles this. --- src-rust/Cargo.toml | 3 --- 1 file changed, 3 deletions(-) diff --git a/src-rust/Cargo.toml b/src-rust/Cargo.toml index 28644ba..1955342 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"] From dd77cbb5dbec5dd815a97c0b2ff6500ab338a04e Mon Sep 17 00:00:00 2001 From: Sewer56 Date: Mon, 24 Jun 2024 00:11:43 +0100 Subject: [PATCH 04/11] CI: Only run builds when relevant source directory changes. --- .github/workflows/build-and-publish.yml | 4 ++++ .github/workflows/rust.yml | 4 ++++ 2 files changed, 8 insertions(+) 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/rust.yml b/.github/workflows/rust.yml index dcb4aee..3f4fb64 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -5,8 +5,12 @@ on: branches: [ main, master, optimize-for-size ] tags: - '*' + paths: + - 'src-rust/**' pull_request: branches: [ main, master, optimize-for-size ] + paths: + - 'src-rust/**' workflow_dispatch: env: From d04912307388a04d7944e45b9e6f3ea2c742bf50 Mon Sep 17 00:00:00 2001 From: Sewer56 Date: Mon, 24 Jun 2024 00:09:46 +0100 Subject: [PATCH 05/11] Fixed: Some parts of macOS Build with Enabled Warnings --- src-rust/src/c/buffers_c_buffers.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src-rust/src/c/buffers_c_buffers.rs b/src-rust/src/c/buffers_c_buffers.rs index ac7c642..55d445a 100644 --- a/src-rust/src/c/buffers_c_buffers.rs +++ b/src-rust/src/c/buffers_c_buffers.rs @@ -332,19 +332,22 @@ 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; #[cfg(not(target_os = "macos"))] @@ -436,11 +439,9 @@ mod tests { } 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(); From f04fd9e272526e31071496513de174001a817ca6 Mon Sep 17 00:00:00 2001 From: Sewer56 Date: Mon, 24 Jun 2024 00:28:32 +0100 Subject: [PATCH 06/11] Windows: Fix External Processes Flag --- src-rust/src/internal/buffer_allocator_windows.rs | 8 ++++---- src-rust/src/structs/private_allocation.rs | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src-rust/src/internal/buffer_allocator_windows.rs b/src-rust/src/internal/buffer_allocator_windows.rs index 1f11a5d..e00018f 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, @@ -170,7 +170,7 @@ 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) @@ -179,7 +179,7 @@ pub fn allocate_windows( }; } - #[cfg(not(feature = "external_process"))] + #[cfg(not(feature = "external_processes"))] allocate_fast(&LocalKernel32 {}, max_address, settings) } } diff --git a/src-rust/src/structs/private_allocation.rs b/src-rust/src/structs/private_allocation.rs index f6c6c05..5cf8dcd 100644 --- a/src-rust/src/structs/private_allocation.rs +++ b/src-rust/src/structs/private_allocation.rs @@ -96,7 +96,7 @@ impl PrivateAllocation { use core::ffi::c_void; unsafe { - #[cfg(feature = "external_process")] + #[cfg(feature = "external_processes")] { if self._this_process_id == get_sys_info().this_process_id { let result = @@ -118,7 +118,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 { From 366ca521ef87d7348d3a096b5e781867596c89a1 Mon Sep 17 00:00:00 2001 From: Sewer56 Date: Mon, 24 Jun 2024 01:30:26 +0100 Subject: [PATCH 07/11] Fix: A lot of compile warnings on various platforms. --- src-rust/Cargo.toml | 1 + src-rust/build.rs | 11 ++++++++ src-rust/src/buffers.rs | 3 +-- src-rust/src/internal/buffer_allocator.rs | 2 +- .../src/internal/buffer_allocator_windows.rs | 26 ++++++++++++++----- .../internal/memory_mapped_file_windows.rs | 4 +-- src-rust/src/lib.rs | 3 +++ .../src/structs/internal/locator_header.rs | 2 ++ src-rust/src/structs/private_allocation.rs | 9 ++++--- .../utilities/disable_write_xor_execute.rs | 2 +- .../src/utilities/map_parser_utilities.rs | 1 - 11 files changed, 47 insertions(+), 17 deletions(-) create mode 100644 src-rust/build.rs diff --git a/src-rust/Cargo.toml b/src-rust/Cargo.toml index 1955342..27c05bd 100644 --- a/src-rust/Cargo.toml +++ b/src-rust/Cargo.toml @@ -19,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. +mmap-rs = [] # Uses mmap-rs for memory mapping. This is auto activated during build. [dependencies] concat-string = "1.0.1" diff --git a/src-rust/build.rs b/src-rust/build.rs new file mode 100644 index 0000000..09908c0 --- /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=\"mmap-rs\""); + } +} diff --git a/src-rust/src/buffers.rs b/src-rust/src/buffers.rs index 5129a45..f8983be 100644 --- a/src-rust/src/buffers.rs +++ b/src-rust/src/buffers.rs @@ -234,7 +234,6 @@ mod tests { structs::params::{BufferAllocatorSettings, BufferSearchSettings}, utilities::cached::get_sys_info, }; - use std; #[cfg(not(target_os = "macos"))] #[test] @@ -418,7 +417,7 @@ mod tests { } 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/internal/buffer_allocator.rs b/src-rust/src/internal/buffer_allocator.rs index 0d4b933..4c34a04 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(feature = "mmap-rs")] 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 e00018f..573df6c 100644 --- a/src-rust/src/internal/buffer_allocator_windows.rs +++ b/src-rust/src/internal/buffer_allocator_windows.rs @@ -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 { @@ -172,11 +184,11 @@ pub fn allocate_windows( #[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_processes"))] 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..dbb844f 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", feature = "mmap-rs"))] 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 5cf8dcd..26fcb5a 100644 --- a/src-rust/src/structs/private_allocation.rs +++ b/src-rust/src/structs/private_allocation.rs @@ -98,6 +98,9 @@ impl PrivateAllocation { unsafe { #[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, @@ -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(feature = "mmap-rs")] 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(feature = "mmap-rs")] 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/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; From d6eed89e9a01c911cba43f66f6eb2780557a2b03 Mon Sep 17 00:00:00 2001 From: Sewer56 Date: Mon, 24 Jun 2024 04:01:26 +0100 Subject: [PATCH 08/11] Added: Override for Nightly Toolchain --- src-rust/rust-toolchain.toml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 src-rust/rust-toolchain.toml 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 From ccf10009ffe73da28ef4ea88badf9993ed6170c1 Mon Sep 17 00:00:00 2001 From: Sewer56 Date: Mon, 24 Jun 2024 04:19:01 +0100 Subject: [PATCH 09/11] Fixed: Incorrect drop of memory mapped file due to lack of null terminator. --- src-rust/src/internal/memory_mapped_file_unix.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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()); + } } } } From 97fb34c2d2ffaa08886fe3b3290d8a99b65f5a31 Mon Sep 17 00:00:00 2001 From: Sewer56 Date: Mon, 24 Jun 2024 04:33:33 +0100 Subject: [PATCH 10/11] Code: Change mmap-rs flag to direct-mmap which disables it instead. --- src-rust/Cargo.toml | 2 +- src-rust/build.rs | 4 ++-- src-rust/src/internal/buffer_allocator.rs | 2 +- src-rust/src/lib.rs | 2 +- src-rust/src/structs/private_allocation.rs | 4 ++-- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src-rust/Cargo.toml b/src-rust/Cargo.toml index 27c05bd..e38a1b6 100644 --- a/src-rust/Cargo.toml +++ b/src-rust/Cargo.toml @@ -19,7 +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. -mmap-rs = [] # Uses mmap-rs for memory mapping. This is auto activated during build. +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 index 09908c0..a5dbfa4 100644 --- a/src-rust/build.rs +++ b/src-rust/build.rs @@ -1,11 +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( + if cfg!(any( target_os = "macos", target_os = "windows", target_os = "linux" )) { - println!("cargo:rustc-cfg=feature=\"mmap-rs\""); + println!("cargo:rustc-cfg=feature=\"direct-mmap\""); } } diff --git a/src-rust/src/internal/buffer_allocator.rs b/src-rust/src/internal/buffer_allocator.rs index 4c34a04..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(feature = "mmap-rs")] + #[cfg(not(feature = "direct-mmap"))] crate::internal::buffer_allocator_mmap_rs::allocate_mmap_rs(settings) } diff --git a/src-rust/src/lib.rs b/src-rust/src/lib.rs index dbb844f..ab79807 100644 --- a/src-rust/src/lib.rs +++ b/src-rust/src/lib.rs @@ -133,7 +133,7 @@ pub(crate) mod utilities { pub mod cached; pub mod icache_clear; - #[cfg(any(target_os = "linux", feature = "mmap-rs"))] + #[cfg(any(target_os = "linux", not(feature = "direct-mmap")))] pub mod map_parser_utilities; pub mod mathematics; diff --git a/src-rust/src/structs/private_allocation.rs b/src-rust/src/structs/private_allocation.rs index 26fcb5a..9ccd8d1 100644 --- a/src-rust/src/structs/private_allocation.rs +++ b/src-rust/src/structs/private_allocation.rs @@ -168,7 +168,7 @@ impl PrivateAllocation { } /// Frees the allocated memory when the `PrivateAllocation` instance is dropped. - #[cfg(feature = "mmap-rs")] + #[cfg(not(feature = "direct-mmap"))] pub(crate) fn drop_mmap_rs(&mut self) { use mmap_rs_with_map_from_existing::MmapOptions; let _map = unsafe { @@ -196,7 +196,7 @@ impl Drop for PrivateAllocation { return PrivateAllocation::drop_macos(self); // non-hot-path-os - #[cfg(feature = "mmap-rs")] + #[cfg(not(feature = "direct-mmap"))] return PrivateAllocation::drop_mmap_rs(self); } } From 0585552f96c7df38d497abc921471820215272ee Mon Sep 17 00:00:00 2001 From: Sewer56 Date: Tue, 16 Jul 2024 09:12:46 +0100 Subject: [PATCH 11/11] Added: Run cargo-semver-checks --- .github/workflows/rust.yml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 3f4fb64..f26fe5e 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -138,6 +138,15 @@ jobs: 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: