Skip to content
This repository has been archived by the owner on Jun 29, 2024. It is now read-only.

Commit

Permalink
chore(stdless): Add testing actions to cover stdless variants
Browse files Browse the repository at this point in the history
Added testing for stdless with ntdll and without ntdll

Also added
`require-feature=["alloc"]` on both crates.

I currently do not see a
way, to make this way alloc free, because of the many dynamically sized
buffers required for the Windows api and converting strings to be
compatible.

Closes #22
  • Loading branch information
C0D3-M4513R committed Jul 26, 2022
1 parent db66a81 commit 29a6d8a
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 78 deletions.
48 changes: 15 additions & 33 deletions .github/workflows/build-dll.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
branches: [staging,trying,dev,main]
# Remove the line above to run when pushing to master
pull_request:
branches: [ main ]
branches: [ main, dev]
workflow_dispatch:
env:
CARGO_TERM_COLOR: always
Expand All @@ -22,6 +22,8 @@ jobs:
runs-on: windows-latest
strategy:
matrix:
profile: ["dev","release"]
feature: ["none","std"]
target: [i686-pc-windows-msvc, i686-pc-windows-gnu ,x86_64-pc-windows-msvc, x86_64-pc-windows-gnu]
steps:
- uses: actions/checkout@v3
Expand All @@ -36,53 +38,33 @@ jobs:
- uses: actions-rs/cargo@v1
with:
command: build
args: --target ${{ matrix.target }} --profile release --package dll-inject
args: --target ${{ matrix.target }} --profile ${{ matrix.profile }} --feature ${{ matrix.feature }} --package dll-inject
- name: generate bindings
working-directory: ./dll-inject
run: cbindgen --config cbindgen.toml --crate dll-inject --output dll-inject.h

- name: list dependencies
working-directory: ./dll-inject/src
run: rustc empty.rs --crate-type staticlib --print native-static-libs
uses: actions-rs/cargo@v1
working-directory: ./dll-inject/
with:
command: rustc
args: -p dll-inject --features ${{ matrix.feature }} -- --print native-static-libs

- uses: actions/upload-artifact@v3
name: upload release artifact
if: ${{ matrix.profile == 'release' }}
with:
name: dll-inject-${{ matrix.target}}-release
name: dll-inject-${{ matrix.target}}-release-${{ matrix.feature }}
path: |
dll-inject/dll-inject.h
target/${{ matrix.target }}/release/libdll_inject.*
target/${{ matrix.target }}/release/dll_inject.*
build-dev:
runs-on: windows-latest
strategy:
matrix:
target: [i686-pc-windows-msvc, i686-pc-windows-gnu ,x86_64-pc-windows-msvc, x86_64-pc-windows-gnu]
steps:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
target: ${{ matrix.target }}
- uses: actions-rs/cargo@v1
with:
command: install
args: cbindgen
- uses: actions-rs/cargo@v1
with:
command: build
args: ${{ matrix.args }} --target ${{ matrix.target }} --profile dev --package dll-inject
- name: generate bindings
working-directory: ./dll-inject
run: cbindgen --config cbindgen.toml --crate dll-inject --output dll-inject.h

- name: list dependencies
working-directory: ./dll-inject/src
run: rustc empty.rs --crate-type staticlib --print native-static-libs

- uses: actions/upload-artifact@v3
name: upload dev artifact
if: ${{ matrix.profile == 'dev' }}
with:
name: dll-inject-${{ matrix.target}}-dev
name: dll-inject-${{ matrix.target}}-dev-${{ matrix.feature }}
path: |
dll-inject/dll-inject.h
target/${{ matrix.target }}/debug/dll_inject.*
Expand Down
38 changes: 10 additions & 28 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ on:
branches: [staging,trying,dev,main]
# Remove the line above to run when pushing to master
pull_request:
branches: [ main ]
branches: [ main, dev ]
workflow_dispatch:
env:
CARGO_TERM_COLOR: always

jobs:
ci:
name: "build-ci"
needs: ["build", "build-ubuntu", "build-beta"]
needs: ["build", "build-ubuntu"]
runs-on: ubuntu-latest
steps:
- name: Done
Expand All @@ -24,21 +24,26 @@ jobs:
strategy:
matrix:
args: [-v] #--release -v,
toolchain: ["beta","stable"]
features: ["","std","x86tox64","x86tox64 std"]
target: [i686-pc-windows-msvc,x86_64-pc-windows-msvc]
steps:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
profile: minimal
toolchain: ${{ matrix.toolchain }}
override: true
target: ${{ matrix.target }}
- uses: actions-rs/cargo@v1
with:
command: build
args: ${{ matrix.args }} --target ${{ matrix.target }} --package inject-lib
args: ${{ matrix.args }} --features ${{ matrix.features }} --target ${{ matrix.target }} --package inject-lib
- uses: actions-rs/cargo@v1
with:
command: test
args: ${{ matrix.args }} --target ${{ matrix.target }} --package inject-lib
args: ${{ matrix.args }} --features ${{ matrix.features }} --target ${{ matrix.target }} --package inject-lib

build-ubuntu:
name: "build-ubuntu"
runs-on: ubuntu-latest
Expand All @@ -59,26 +64,3 @@ jobs:
with:
command: test
args: ${{ matrix.args }} --features ntdll --package inject-lib

build-beta:
name: "build-beta"
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ windows-latest ] #ubuntu-latest
args: [-v] #--release -v
target: [ i686-pc-windows-msvc,x86_64-pc-windows-msvc ]
steps:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
toolchain: beta
target: ${{ matrix.target }}
- uses: actions-rs/cargo@v1
with:
command: build
args: ${{ matrix.args }} --all-features --target ${{ matrix.target }} --package inject-lib
- uses: actions-rs/cargo@v1
with:
command: test
args: ${{ matrix.args }} --all-features --target ${{ matrix.target }} --package inject-lib
6 changes: 6 additions & 0 deletions dll-inject/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ publish = false

[lib]
crate-type = ["staticlib"]
required-features = ["alloc"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
inject-lib = {path="../inject-lib",features=["x86tox64"]}

[features]
default=["alloc"]
alloc=["inject-lib/alloc"]
std=["inject-lib/std"]
none=[]
Empty file removed dll-inject/src/empty.rs
Empty file.
38 changes: 21 additions & 17 deletions dll-inject/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
#![cfg(target_family = "windows")]
extern crate inject_lib;
use inject_lib::error::Error;
#![cfg_attr(not(feature = "std"),no_std)]
#![cfg(not(feature = "alloc"))]
compile_error!("Cannot work without alloc");

extern crate alloc;
use alloc::vec::Vec;
use inject_lib::{Data, Inject, Injector};
use std::ffi::{c_void, CStr, CString};
use std::ops::Add;
use std::os::raw::c_char;
use std::ptr::null_mut;
use core::ptr::null_mut;

#[repr(C)]
pub enum Result<T> {
Ok(T),
Err(CString),
}
///Reads len bytes from ptr, and returns it as a vec
fn read(ptr: *mut u8, len: usize) -> Vec<u8> {
#[cfg(feature = "std")]
println!("reading {} bytes from {:x?}", len, ptr);
let mut v = Vec::with_capacity(len);
for i in 0..len {
Expand All @@ -27,15 +24,17 @@ fn read(ptr: *mut u8, len: usize) -> Vec<u8> {
///A return code of 0 means success.
pub extern "C" fn inject(pid: u32, dll: *mut u8, len: usize, wait: bool) -> i16 {
let v = read(dll, len);
let dll = String::from_utf8(v);
let dll = core::str::from_utf8(v);
if dll.is_err() {
#[cfg(feature = "std")]
eprintln!("inject-lib: ERROR: Non UTF-8 String found.");
return -2;
}
let dll = unsafe { dll.unwrap_unchecked() }; //Safety: checked and handled above;
let i = Injector::new(Data::Str(dll.as_str()), pid);
let i = Injector::new(Data::Str(dll), pid);
let r = i.inject(wait).inject();
if let Err(e) = r {
#[cfg(feature = "std")]
eprintln!("inject-lib: ERROR: {}", e);
return -1;
}
Expand All @@ -47,15 +46,17 @@ pub extern "C" fn inject(pid: u32, dll: *mut u8, len: usize, wait: bool) -> i16
///A return code of 0 means success.
pub extern "C" fn eject(pid: u32, dll: *mut u8, len: usize, wait: bool) -> i16 {
let v = read(dll, len);
let dll = String::from_utf8(v);
let dll = core::str::from_utf8(v);
if dll.is_err() {
#[cfg(feature = "std")]
eprintln!("inject-lib: ERROR: Non UTF-8 String found.");
return -2;
}
let dll = unsafe { dll.unwrap_unchecked() }; //Safety: checked and handled above;
let i = Injector::new(Data::Str(dll.as_str()), pid);
let i = Injector::new(Data::Str(dll), pid);
let r = i.inject(wait).eject();
if let Err(e) = r {
#[cfg(feature = "std")]
eprintln!("inject-lib: ERROR: {}", e);
return -1;
}
Expand All @@ -74,8 +75,9 @@ pub struct FindPid {
///Returns a array of pids, if exitcode=0.
pub extern "C" fn find_pid(name: *mut u8, len: usize) -> FindPid {
let v = read(name, len);
let name = String::from_utf8(v);
let name = core::str::from_utf8(v);
if name.is_err() {
#[cfg(feature = "std")]
eprintln!("inject-lib: ERROR: Non UTF-8 String found.");
return FindPid {
len: 0,
Expand All @@ -84,16 +86,18 @@ pub extern "C" fn find_pid(name: *mut u8, len: usize) -> FindPid {
};
}
let name = unsafe { name.unwrap_unchecked() }; //Safety: checked and handled above;
let vec = Injector::find_pid(Data::Str(name.as_str()));
let vec = Injector::find_pid(Data::Str(name));
if let Ok(vec) = vec {
let v = vec.leak();
#[cfg(feature = "std")]
eprintln!("{:x?}", v.as_mut_ptr());
return FindPid {
len: v.len(),
arr: v.as_mut_ptr(),
exitcode: 0,
};
} else {
#[cfg(feature = "std")]
eprintln!("inject-lib: ERROR: {}", vec.unwrap_err());
return FindPid {
len: 0,
Expand Down
3 changes: 3 additions & 0 deletions inject-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ widestring = {version="1",default-features=false}
simple_logger = {version = "2.1.0",default-features=false}
thread_local = "1.1.4"

[lib]
required-features=["alloc"]

[features]
default=["alloc"]
nightly=[]
Expand Down

0 comments on commit 29a6d8a

Please sign in to comment.