Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bad unsafe is caught by latest rust nightly (rustc 1.70.0-nightly (2eaeb1eee 2023-04-05)) #15

Closed
durin42 opened this issue Apr 6, 2023 · 5 comments · Fixed by #16
Closed
Assignees

Comments

@durin42
Copy link

durin42 commented Apr 6, 2023

Several of the functions in simd_utils.rs are doing UB in unsafe blocks, eg

pub unsafe fn ptr_i16_to_256set1_epi32(buf: &[i16], index: usize) -> __m256i {
    _mm256_set1_epi32(*(buf.get_unchecked(index..).as_ptr() as *const i32))
}

is taking a slice of [i16] which is only going to be 2-byte aligned and casting it to an *i32 which has to be 4-byte aligned. rust-lang/rust@22a7a19 Was recently added to rustc to catch this kind of UB at runtime, and it's firing in the test suite:

---- resize_to_same_width_after_cropping stdout ----
thread 'resize_to_same_width_after_cropping' panicked at 'misaligned pointer dereference: address must be a multiple of 0x4 but is 0x7fca6400346a', src/convolution/vertical_u8/native.rs:49:21
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

---- upscale_u8 stdout ----
thread 'upscale_u8' panicked at 'misaligned pointer dereference: address must be a multiple of 0x4 but is 0x7fca4402ec7e', src/simd_utils.rs:61:20

---- downscale_u8x3 stdout ----
thread 'downscale_u8x3' panicked at 'misaligned pointer dereference: address must be a multiple of 0x4 but is 0x7fca680aeb4d', src/convolution/vertical_u8/native.rs:49:21

---- downscale_u8 stdout ----
thread 'downscale_u8' panicked at 'misaligned pointer dereference: address must be a multiple of 0x4 but is 0x7fca74ffbd7f', src/convolution/vertical_u8/native.rs:49:21

---- downscale_u8x2 stdout ----
thread 'downscale_u8x2' panicked at 'misaligned pointer dereference: address must be a multiple of 0x4 but is 0x7fca6df64d9e', src/convolution/vertical_u8/native.rs:49:21

---- upscale_u8x2 stdout ----
thread 'upscale_u8x2' panicked at 'misaligned pointer dereference: address must be a multiple of 0x8 but is 0x7fca480a404e', src/simd_utils.rs:71:21

---- downscale_u8x4 stdout ----
thread 'downscale_u8x4' panicked at 'misaligned pointer dereference: address must be a multiple of 0x4 but is 0x7fca7001c82a', src/simd_utils.rs:61:20

---- upscale_u8x3 stdout ----
thread 'upscale_u8x3' panicked at 'misaligned pointer dereference: address must be a multiple of 0x4 but is 0x7fca380a100e', src/simd_utils.rs:61:20

---- upscale_u8x4 stdout ----
thread 'upscale_u8x4' panicked at 'misaligned pointer dereference: address must be a multiple of 0x4 but is 0x7fca400a404e', src/simd_utils.rs:61:20

easy to reproduce with cargo +nightly test.

durin42 added a commit to durin42/fast_image_resize that referenced this issue Apr 6, 2023
This was caught by a recent change to Rust (22a7a19f9333bc1fcba97ce444a3515cb5fb33e6)
which proactively detects dereferences of unaligned pointers. Switching from a
dereference to the unaligned read and write methods fixes all the tests on today's nightly.

Fixes Cykooz#15.
@Cykooz Cykooz closed this as completed in #16 Apr 7, 2023
Cykooz added a commit that referenced this issue Apr 7, 2023
…f6d3

Use (read|write)_unaligned for unaligned pointers (#15)
gergo-salyi added a commit to gergo-salyi/multibg-sway that referenced this issue Apr 27, 2023
Current releases of fast_image_resize are broken on rust nightly
Cykooz/fast_image_resize#15

Issue is already fixed on master branch but the new verison is not released yet
So temporarly use a clone of fast_image_resize current master branch
as a local submodule and add corresponding patch field in our Cargo.toml
@schell
Copy link

schell commented May 3, 2023

I'm still seeing this issue on nightly even with 2.7.1. Did this get released or is this only a partial fix?

@Cykooz
Copy link
Owner

Cykooz commented May 3, 2023

I'm not sure that pull request of durin42 fixed all the bugs. I didn't check it on nightly toolchain. I only merge pull request and create 2.7.1 release.

@Cykooz
Copy link
Owner

Cykooz commented May 3, 2023

I have run tests on nightly toolchain and x86_64 architecture. I don't see any issues.

@Cykooz
Copy link
Owner

Cykooz commented May 3, 2023

I have reproduced this issue on arm64 architecture.

@Cykooz Cykooz reopened this May 3, 2023
@Cykooz Cykooz self-assigned this May 3, 2023
Cykooz added a commit that referenced this issue May 4, 2023
@Cykooz
Copy link
Owner

Cykooz commented May 4, 2023

I have released v2.7.2 with fixes for arm64.

@Cykooz Cykooz closed this as completed May 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants