Skip to content

Commit

Permalink
fix: unaligned read panic on macos
Browse files Browse the repository at this point in the history
  • Loading branch information
anonkey committed Feb 13, 2024
1 parent f34d00d commit 0516a69
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 70 deletions.
146 changes: 80 additions & 66 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,69 +10,95 @@ on:
types: [checks_requested]

permissions:
contents: read
contents: read

env:
env:
MSRV: 1.69.0

jobs:

macos:
runs-on: macos-13
env:
env:
TARGET: x86_64-apple-darwin
steps:
- name: checkout
uses: actions/checkout@v4

- name: setup Rust
uses: dtolnay/rust-toolchain@master
with:
toolchain: '${{ env.MSRV }}'
with:
toolchain: "${{ env.MSRV }}"
components: clippy

- name: build
uses: ./.github/actions/build
with:
TARGET: '${{ env.TARGET }}'
TARGET: "${{ env.TARGET }}"

- name: test
uses: ./.github/actions/test
with:
TARGET: '${{ env.TARGET }}'
TARGET: "${{ env.TARGET }}"

- name: before_cache_script
run: rm -rf $CARGO_HOME/registry/index

macos-silicon:
runs-on: macos-13-arm64
env:
TARGET: aarch64-apple-darwin
steps:
- name: checkout
uses: actions/checkout@v4

- name: setup Rust
uses: dtolnay/rust-toolchain@master
with:
toolchain: "${{ env.MSRV }}"
components: clippy

- name: build
uses: ./.github/actions/build
with:
TARGET: "${{ env.TARGET }}"

- name: test
uses: ./.github/actions/test
with:
TARGET: "${{ env.TARGET }}"

- name: before_cache_script
run: rm -rf $CARGO_HOME/registry/index

# Use cross for QEMU-based testing
# cross needs to execute Docker, GitHub Action already has it installed
cross:
runs-on: ubuntu-20.04
runs-on: ubuntu-20.04
needs: [rustfmt, minver, macos, linux_native_builds, rust_stable]
strategy:
fail-fast: false
matrix:
target: [
arm-unknown-linux-gnueabi,
armv7-unknown-linux-gnueabihf,
i686-unknown-linux-gnu,
i686-unknown-linux-musl,
mips-unknown-linux-gnu,
mips64-unknown-linux-gnuabi64,
mips64el-unknown-linux-gnuabi64,
mipsel-unknown-linux-gnu,
powerpc64le-unknown-linux-gnu,
]
target:
[
arm-unknown-linux-gnueabi,
armv7-unknown-linux-gnueabihf,
i686-unknown-linux-gnu,
i686-unknown-linux-musl,
mips-unknown-linux-gnu,
mips64-unknown-linux-gnuabi64,
mips64el-unknown-linux-gnuabi64,
mipsel-unknown-linux-gnu,
powerpc64le-unknown-linux-gnu,
]

steps:
- name: checkout
uses: actions/checkout@v4

- name: setup Rust
uses: dtolnay/rust-toolchain@master
with:
toolchain: '${{ env.MSRV }}'
with:
toolchain: "${{ env.MSRV }}"
components: clippy

# cross relies on docker or podman, GitHub Acton already has it installed.
Expand All @@ -84,43 +110,38 @@ jobs:
- name: build
uses: ./.github/actions/build
with:
TARGET: '${{ matrix.target }}'
TARGET: "${{ matrix.target }}"
TOOL: cross
RUSTFLAGS: --cfg qemu -D warnings

- name: test
uses: ./.github/actions/test
with:
TARGET: '${{ matrix.target }}'
TARGET: "${{ matrix.target }}"
SUDO: ""
TOOL: cross
RUSTFLAGS: --cfg qemu -D warnings

- name: before_cache_script
run: rm -rf $CARGO_HOME/registry/index



# Tasks for Linux native builds
# Only test x86_64 targets on GitHub Action, leave aarch64 one in Cirrus CI.
linux_native_builds:
runs-on: ubuntu-20.04
strategy:
fail-fast: false
matrix:
target: [
x86_64-unknown-linux-gnu,
x86_64-unknown-linux-musl,
]
target: [x86_64-unknown-linux-gnu, x86_64-unknown-linux-musl]

steps:
- name: checkout
uses: actions/checkout@v4

- name: setup Rust
uses: dtolnay/rust-toolchain@master
with:
toolchain: '${{ env.MSRV }}'
with:
toolchain: "${{ env.MSRV }}"
components: clippy

- name: install targets
Expand All @@ -129,44 +150,42 @@ jobs:
- name: build
uses: ./.github/actions/build
with:
TARGET: '${{ matrix.TARGET }}'
TARGET: "${{ matrix.TARGET }}"

- name: test
uses: ./.github/actions/test
with:
TARGET: '${{ matrix.TARGET }}'
TARGET: "${{ matrix.TARGET }}"

- name: before_cache_script
run: rm -rf $CARGO_HOME/registry/index

rust_stable:
runs-on: ubuntu-20.04
env:
env:
TARGET: x86_64-unknown-linux-gnu
steps:
- name: checkout
uses: actions/checkout@v4

- name: setup Rust
uses: dtolnay/rust-toolchain@stable
with:
with:
components: clippy

- name: build
uses: ./.github/actions/build
with:
TARGET: '${{ env.TARGET }}'
TARGET: "${{ env.TARGET }}"

- name: test
uses: ./.github/actions/test
with:
TARGET: '${{ env.TARGET }}'
TARGET: "${{ env.TARGET }}"

- name: before_cache_script
run: rm -rf $CARGO_HOME/registry/index



# Tasks for cross-compiling, but no testing
cross_compiling:
runs-on: ubuntu-20.04
Expand All @@ -189,8 +208,8 @@ jobs:
- target: x86_64-unknown-illumos
# Cross claims to support running tests on iOS, but it actually doesn't.
# https://github.com/rust-embedded/cross/issues/535
- target: aarch64-apple-ios
# cargo hack tries to invoke the iphonesimulator SDK for iOS
- target: aarch64-apple-ios
# cargo hack tries to invoke the iphonesimulator SDK for iOS
NOHACK: true
# Cross claims to support Linux powerpc64, but it really doesn't.
# https://github.com/rust-embedded/cross/issues/441
Expand All @@ -205,8 +224,8 @@ jobs:

- name: setup Rust
uses: dtolnay/rust-toolchain@master
with:
toolchain: '${{ env.MSRV }}'
with:
toolchain: "${{ env.MSRV }}"
components: clippy

- name: install targets
Expand All @@ -215,18 +234,17 @@ jobs:
- name: build
uses: ./.github/actions/build
with:
TARGET: '${{ matrix.target }}'
BUILD: '${{ env.BUILD }}'
NOHACK: '${{ matrix.NOHACK }}'
TARGET: "${{ matrix.target }}"
BUILD: "${{ env.BUILD }}"
NOHACK: "${{ matrix.NOHACK }}"

- name: before_cache_script
run: rm -rf $CARGO_HOME/registry/index


redox:
runs-on: ubuntu-20.04
needs: [rustfmt, minver, macos, linux_native_builds, rust_stable]
env:
env:
TARGET: x86_64-unknown-redox
CLIPPYFLAGS: -D warnings
BUILD: check
Expand All @@ -237,28 +255,26 @@ jobs:
- name: setup Rust
# Redox's MSRV policy is unclear. Until they define it, use nightly.
uses: dtolnay/rust-toolchain@nightly
with:
with:
components: clippy

- name: install targets
run: rustup target add ${{ env.TARGET }}

- name: build
uses: ./.github/actions/build
with:
TARGET: '${{ env.TARGET }}'
BUILD: '${{ env.BUILD }}'
CLIPPYFLAGS: '${{ env.CLIPPYFLAGS }}'
TARGET: "${{ env.TARGET }}"
BUILD: "${{ env.BUILD }}"
CLIPPYFLAGS: "${{ env.CLIPPYFLAGS }}"

- name: before_cache_script
run: rm -rf $CARGO_HOME/registry/index



# Rust Tier 3 targets can't use Rustup
tier3:
runs-on: ubuntu-20.04
env:
env:
BUILD: check
ZFLAGS: -Zbuild-std
CLIPPYFLAGS: -D warnings
Expand All @@ -281,7 +297,7 @@ jobs:

- name: setup Rust
uses: dtolnay/rust-toolchain@nightly
with:
with:
components: clippy

- name: install src
Expand All @@ -290,21 +306,20 @@ jobs:
- name: build
uses: ./.github/actions/build
with:
TARGET: '${{ matrix.target }}'
BUILD: '${{ env.BUILD }}'
ZFLAGS: '${{ env.ZFLAGS }}'
CLIPPYFLAGS: '${{ env.CLIPPYFLAGS }}'
TARGET: "${{ matrix.target }}"
BUILD: "${{ env.BUILD }}"
ZFLAGS: "${{ env.ZFLAGS }}"
CLIPPYFLAGS: "${{ env.CLIPPYFLAGS }}"

- name: before_cache_script
run: rm -rf $CARGO_HOME/registry/index


# Test that we can build with the lowest version of all dependencies.
# "cargo test" doesn't work because some of our dev-dependencies, like
# rand, can't build with their own minimal dependencies.
minver:
runs-on: ubuntu-20.04
env:
env:
TARGET: x86_64-unknown-linux-gnu
steps:
- name: checkout
Expand All @@ -313,7 +328,7 @@ jobs:
- name: setup Rust
uses: dtolnay/rust-toolchain@nightly

- name: setup
- name: setup
run: cargo update -Zminimal-versions

- name: check
Expand All @@ -339,4 +354,3 @@ jobs:

- name: before_cache_script
run: rm -rf $CARGO_HOME/registry/index

1 change: 1 addition & 0 deletions changelog/2311.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed `::unistd::Group::members` using read_unaligned to avoid crash on misaligned pointers
7 changes: 3 additions & 4 deletions src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3476,18 +3476,17 @@ impl Group {
let mut ret = Vec::new();

for i in 0.. {
let u = unsafe { mem.offset(i) };
if unsafe { (*u).is_null() } {
let u = unsafe { mem.offset(i).read_unaligned() };
if u.is_null() {
break;
} else {
let s = unsafe {CStr::from_ptr(*u).to_string_lossy().into_owned()};
let s = unsafe {CStr::from_ptr(u).to_string_lossy().into_owned()};
ret.push(s);
}
}

ret
}

/// # Safety
///
/// If `f` writes to its `*mut *mut libc::group` parameter, then it must
Expand Down
22 changes: 22 additions & 0 deletions test/test_unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1372,3 +1372,25 @@ fn test_eaccess_file_exists() {
eaccess(&path, AccessFlags::R_OK | AccessFlags::W_OK)
.expect("assertion failed");
}

#[test]
#[cfg(bsd)]
fn test_group_from() {
let group = Group::from_name("wheel").unwrap().unwrap();
assert!(group.name == "wheel");
let group_id = group.gid;
let group = Group::from_gid(group_id).unwrap().unwrap();
assert_eq!(group.gid, group_id);
assert_eq!(group.name, "wheel");
}

#[test]
#[cfg(target_os = "linux")]
fn test_group_from() {
let group = Group::from_name("root").unwrap().unwrap();
assert!(group.name == "root");
let group_id = group.gid;
let group = Group::from_gid(group_id).unwrap().unwrap();
assert_eq!(group.gid, group_id);
assert_eq!(group.name, "root");
}

0 comments on commit 0516a69

Please sign in to comment.