Skip to content

Commit 3da8e2a

Browse files
committed
fix val test
1 parent b7389a5 commit 3da8e2a

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

src/binary.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub mod external {
2626
unsafe {
2727
std::slice::from_raw_parts(
2828
(*self.inner).code.cast(),
29-
(*self.inner).size * std::mem::size_of::<u32>(),
29+
(*self.inner).size * size_of::<u32>(),
3030
)
3131
}
3232
}
@@ -44,7 +44,7 @@ pub mod external {
4444

4545
pub enum Binary {
4646
#[cfg(feature = "use-compiled-tools")]
47-
External(self::external::ExternalBinary),
47+
External(external::ExternalBinary),
4848
OwnedU32(Vec<u32>),
4949
OwnedU8(Vec<u8>),
5050
}
@@ -63,12 +63,12 @@ impl Binary {
6363
}
6464
}
6565

66-
impl std::convert::TryFrom<Vec<u8>> for Binary {
66+
impl TryFrom<Vec<u8>> for Binary {
6767
type Error = crate::Error;
6868

6969
#[inline]
7070
fn try_from(v: Vec<u8>) -> Result<Self, Self::Error> {
71-
if v.len() % std::mem::size_of::<u32>() != 0 {
71+
if v.len() % size_of::<u32>() != 0 {
7272
Err(crate::Error {
7373
inner: spirv_tools_sys::shared::SpirvResult::InvalidBinary,
7474
diagnostic: None,
@@ -125,25 +125,30 @@ impl fmt::Debug for Binary {
125125
/// digestible byte array
126126
#[inline]
127127
pub fn from_binary(bin: &[u32]) -> &[u8] {
128-
unsafe { std::slice::from_raw_parts(bin.as_ptr().cast(), std::mem::size_of_val(bin)) }
128+
unsafe { std::slice::from_raw_parts(bin.as_ptr().cast(), size_of_val(bin)) }
129129
}
130130

131-
/// Transmutes a regular byte array into a SPIRV binary of 32 bit words. This
132-
/// will fail if the input is not `% sizeof(u32)`
131+
/// Transmutes a regular byte array into a SPIRV binary of 32 bit words.
132+
/// Fails if the input is not `input.as_ptr() % 4` and `input.len() % 4`.
133133
#[inline]
134134
pub fn to_binary(bytes: &[u8]) -> Result<&[u32], crate::Error> {
135-
if bytes.len() % std::mem::size_of::<u32>() != 0 {
135+
if bytes.len() % size_of::<u32>() != 0 {
136+
return Err(crate::Error {
137+
inner: spirv_tools_sys::shared::SpirvResult::InvalidBinary,
138+
diagnostic: None,
139+
});
140+
}
141+
if bytes.as_ptr().addr() % size_of::<u32>() != 0 {
136142
return Err(crate::Error {
137143
inner: spirv_tools_sys::shared::SpirvResult::InvalidBinary,
138144
diagnostic: None,
139145
});
140146
}
141147

142148
#[allow(clippy::size_of_in_element_count)]
143-
Ok(unsafe {
144-
std::slice::from_raw_parts(
145-
bytes.as_ptr().cast(),
146-
bytes.len() / std::mem::size_of::<u32>(),
147-
)
148-
})
149+
Ok(
150+
unsafe {
151+
std::slice::from_raw_parts(bytes.as_ptr().cast(), bytes.len() / size_of::<u32>())
152+
},
153+
)
149154
}

tests/val.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22

33
const SPIRV_BIN: &[u8] = include_bytes!("wgpu_example_shader.spv");
44

5-
fn validate_compiled(_input: &[u8]) -> Option<Result<(), spirv_tools::Error>> {
5+
fn validate_compiled(_input: &[u32]) -> Option<Result<(), spirv_tools::Error>> {
66
#[cfg(feature = "use-compiled-tools")]
77
{
88
use spirv_tools::val::{compiled::CompiledValidator, Validator};
99
let cv = CompiledValidator::default();
10-
Some(cv.validate(spirv_tools::binary::to_binary(_input).unwrap(), None))
10+
Some(cv.validate(_input, None))
1111
}
1212
#[cfg(not(feature = "use-compiled-tools"))]
1313
None
1414
}
1515

16-
fn validate_tool(_input: &[u8]) -> Option<Result<(), spirv_tools::Error>> {
16+
fn validate_tool(_input: &[u32]) -> Option<Result<(), spirv_tools::Error>> {
1717
#[cfg(feature = "use-installed-tools")]
1818
{
1919
use spirv_tools::val::{tool::ToolValidator, Validator};
2020
let cv = ToolValidator::default();
21-
Some(cv.validate(spirv_tools::binary::to_binary(_input).unwrap(), None))
21+
Some(cv.validate(_input, None))
2222
}
2323
#[cfg(not(feature = "use-installed-tools"))]
2424
None
@@ -29,9 +29,14 @@ fn gets_error_message() {
2929
let expected_msg = "error:0:0 - Loop header '6[%loop_header]' is targeted by 2 back-edge blocks but the standard requires exactly one";
3030
let expected_notes = " %loop_header = OpLabel\n";
3131

32-
for res in validate_compiled(SPIRV_BIN)
32+
let spirv = SPIRV_BIN
33+
.chunks_exact(size_of::<u32>())
34+
.map(|chunk| u32::from_ne_bytes(chunk.try_into().unwrap()))
35+
.collect::<Vec<_>>();
36+
37+
for res in validate_compiled(&spirv)
3338
.into_iter()
34-
.chain(validate_tool(SPIRV_BIN).into_iter())
39+
.chain(validate_tool(&spirv).into_iter())
3540
{
3641
let err = res.unwrap_err();
3742
let diag = err.diagnostic.as_ref().unwrap();

0 commit comments

Comments
 (0)