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

Cheeky Unsoundness Now Asserts in New Rust #327

Open
cwfitzgerald opened this issue Nov 16, 2024 · 0 comments
Open

Cheeky Unsoundness Now Asserts in New Rust #327

cwfitzgerald opened this issue Nov 16, 2024 · 0 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@cwfitzgerald
Copy link
Contributor

cwfitzgerald commented Nov 16, 2024

glow/src/web_sys.rs

Lines 6165 to 6215 in cf08bc4

unsafe fn texture_data_view(ty: u32, bytes: &[u8]) -> js_sys::Object {
use std::mem::size_of;
use std::slice::from_raw_parts;
match ty {
BYTE => {
let data = from_raw_parts(bytes.as_ptr() as *const i8, bytes.len() / size_of::<i8>());
js_sys::Int8Array::view(data).into()
}
SHORT => {
#[allow(clippy::cast_ptr_alignment)]
let data = from_raw_parts(bytes.as_ptr() as *const i16, bytes.len() / size_of::<i16>());
js_sys::Int16Array::view(data).into()
}
UNSIGNED_SHORT
| UNSIGNED_SHORT_5_6_5
| UNSIGNED_SHORT_5_5_5_1
| UNSIGNED_SHORT_4_4_4_4
| HALF_FLOAT
| HALF_FLOAT_OES => {
#[allow(clippy::cast_ptr_alignment)]
let data = from_raw_parts(bytes.as_ptr() as *const u16, bytes.len() / size_of::<u16>());
js_sys::Uint16Array::view(data).into()
}
INT => {
#[allow(clippy::cast_ptr_alignment)]
let data = from_raw_parts(bytes.as_ptr() as *const i32, bytes.len() / size_of::<i32>());
js_sys::Int32Array::view(data).into()
}
UNSIGNED_INT
| UNSIGNED_INT_5_9_9_9_REV
| UNSIGNED_INT_2_10_10_10_REV
| UNSIGNED_INT_10F_11F_11F_REV
| UNSIGNED_INT_24_8 => {
#[allow(clippy::cast_ptr_alignment)]
let data = from_raw_parts(bytes.as_ptr() as *const u32, bytes.len() / size_of::<u32>());
js_sys::Uint32Array::view(data).into()
}
FLOAT => {
#[allow(clippy::cast_ptr_alignment)]
let data = from_raw_parts(bytes.as_ptr() as *const f32, bytes.len() / size_of::<f32>());
js_sys::Float32Array::view(data).into()
}
UNSIGNED_BYTE | _ => js_sys::Uint8Array::view(bytes).into(),
}

The cheeky casts here are causing slice::from_raw_parts to error out due to the lack of alignment. I don't know if the views themselves are required to be aligned, but if you don't put aligned data into glow, it gets mad. Probably needs view_mut_raw

panicked at library/core/src/panicking.rs:221:5:
unsafe precondition(s) violated: slice::from_raw_parts requires the pointer to be aligned and non-null, and the total size of the slice not to exceed `isize::MAX`

Stack:

__wbg_get_imports/imports.wbg.__wbg_new_abda76e883ba8a5f/<@http://localhost:8888/assets/index-B7R0YP-3.js:7482:19
logError@http://localhost:8888/assets/index-B7R0YP-3.js:7192:15
__wbg_get_imports/imports.wbg.__wbg_new_abda76e883ba8a5f@http://localhost:8888/assets/index-B7R0YP-3.js:7481:12
client.wasm.console_error_panic_hook::Error::new::hc69b5fa529afea52@http://localhost:8888/assets/client_bg-PjZmLhTd.wasm:wasm-function[9801]:0x4af724
client.wasm.console_error_panic_hook::hook_impl::hf10585ca8c9ba013@http://localhost:8888/assets/client_bg-PjZmLhTd.wasm:wasm-function[2694]:0x367907
client.wasm.console_error_panic_hook::hook::h4ee3fc12c0aca055@http://localhost:8888/assets/client_bg-PjZmLhTd.wasm:wasm-function[14330]:0x4f99cd
client.wasm.core::ops::function::Fn::call::h0d9ba157495fc0df@http://localhost:8888/assets/client_bg-PjZmLhTd.wasm:wasm-function[11813]:0x4d5cbe
client.wasm.std::panicking::rust_panic_with_hook::h33fe77d38d305ca3@http://localhost:8888/assets/client_bg-PjZmLhTd.wasm:wasm-function[5025]:0x40a9e1
client.wasm.std::panicking::begin_panic_handler::{{closure}}::h98de848d678bad07@http://localhost:8888/assets/client_bg-PjZmLhTd.wasm:wasm-function[6866]:0x45b7ee
client.wasm.std::sys::backtrace::__rust_end_short_backtrace::h2bcfc60c3cf0a312@http://localhost:8888/assets/client_bg-PjZmLhTd.wasm:wasm-function[15938]:0x50976e
client.wasm.rust_begin_unwind@http://localhost:8888/assets/client_bg-PjZmLhTd.wasm:wasm-function[10697]:0x4c1beb
client.wasm.core::panicking::panic_nounwind_fmt::hd2fee749e9eab1f3@http://localhost:8888/assets/client_bg-PjZmLhTd.wasm:wasm-function[10698]:0x4c1c3e
client.wasm.core::panicking::panic_nounwind::h1c2f45581e84758f@http://localhost:8888/assets/client_bg-PjZmLhTd.wasm:wasm-function[12064]:0x4d9c8e
client.wasm.core::slice::raw::from_raw_parts::precondition_check::hb66aef015918d8d0@http://localhost:8888/assets/client_bg-PjZmLhTd.wasm:wasm-function[2271]:0x33b088
client.wasm.core::slice::raw::from_raw_parts::h5e5801e03f4aea99@http://localhost:8888/assets/client_bg-PjZmLhTd.wasm:wasm-function[10334]:0x4ba800
client.wasm.glow::web::texture_data_view::hd5077806e43852fd@http://localhost:8888/assets/client_bg-PjZmLhTd.wasm:wasm-function[1007]:0x26619c
client.wasm.<glow::web::Context as glow::HasContext>::tex_image_2d::{{closure}}::haa471d6d7e5a6efd@http://localhost:8888/assets/client_bg-PjZmLhTd.wasm:wasm-function[9844]:0x4b05a8
client.wasm.core::option::Option<T>::map::h30ad24a51c9ae128@http://localhost:8888/assets/client_bg-PjZmLhTd.wasm:wasm-function[5728]:0x42cf1c
client.wasm.<glow::web::Context as glow::HasContext>::tex_image_2d::h72e31b182d00508e@http://localhost:8888/assets/client_bg-PjZmLhTd.wasm:wasm-function[1803]:0x2ffc6a
@grovesNL grovesNL added bug Something isn't working help wanted Extra attention is needed labels Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants