diff --git a/.github/workflows/windows-bindgen.yml b/.github/workflows/windows-bindgen.yml new file mode 100644 index 0000000000..969377a6b7 --- /dev/null +++ b/.github/workflows/windows-bindgen.yml @@ -0,0 +1,30 @@ +name: windows-bindgen + +on: + pull_request: + push: + paths-ignore: + - '.github/ISSUE_TEMPLATE/**' + branches: + - master + +env: + RUSTFLAGS: -Dwarnings + +jobs: + cargo_sys: + name: Check + strategy: + matrix: + rust: [1.70.0, stable, nightly] + runs-on: + - windows-latest + - ubuntu-latest + runs-on: ${{ matrix.runs-on }} + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Prepare + run: rustup update --no-self-update ${{ matrix.rust }} && rustup default ${{ matrix.rust }} + - name: Check + run: cargo check -p windows-bindgen --all-features diff --git a/.github/workflows/windows-metadata.yml b/.github/workflows/windows-metadata.yml new file mode 100644 index 0000000000..11aa74d040 --- /dev/null +++ b/.github/workflows/windows-metadata.yml @@ -0,0 +1,30 @@ +name: windows-metadata + +on: + pull_request: + push: + paths-ignore: + - '.github/ISSUE_TEMPLATE/**' + branches: + - master + +env: + RUSTFLAGS: -Dwarnings + +jobs: + cargo_sys: + name: Check + strategy: + matrix: + rust: [1.70.0, stable, nightly] + runs-on: + - windows-latest + - ubuntu-latest + runs-on: ${{ matrix.runs-on }} + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Prepare + run: rustup update --no-self-update ${{ matrix.rust }} && rustup default ${{ matrix.rust }} + - name: Check + run: cargo check -p windows-metadata --all-features diff --git a/.github/workflows/windows-version.yml b/.github/workflows/windows-version.yml new file mode 100644 index 0000000000..a64c4a6cf9 --- /dev/null +++ b/.github/workflows/windows-version.yml @@ -0,0 +1,30 @@ +name: windows-version + +on: + pull_request: + push: + paths-ignore: + - '.github/ISSUE_TEMPLATE/**' + branches: + - master + +env: + RUSTFLAGS: -Dwarnings + +jobs: + cargo_sys: + name: Check + strategy: + matrix: + rust: [1.56.0, stable, nightly] + runs-on: + - windows-latest + - ubuntu-latest + runs-on: ${{ matrix.runs-on }} + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Prepare + run: rustup update --no-self-update ${{ matrix.rust }} && rustup default ${{ matrix.rust }} + - name: Check + run: cargo check -p windows-version --all-features diff --git a/crates/libs/bindgen/Cargo.toml b/crates/libs/bindgen/Cargo.toml index 75f26ac96f..aef5561a19 100644 --- a/crates/libs/bindgen/Cargo.toml +++ b/crates/libs/bindgen/Cargo.toml @@ -3,7 +3,7 @@ name = "windows-bindgen" version = "0.52.0" authors = ["Microsoft"] edition = "2021" -rust-version = "1.56" +rust-version = "1.70" license = "MIT OR Apache-2.0" description = "Windows metadata compiler" repository = "https://github.com/microsoft/windows-rs" diff --git a/crates/libs/bindgen/src/rust/constants.rs b/crates/libs/bindgen/src/rust/constants.rs index 3d5e4dd845..1e483fb7ee 100644 --- a/crates/libs/bindgen/src/rust/constants.rs +++ b/crates/libs/bindgen/src/rust/constants.rs @@ -83,10 +83,7 @@ pub fn writer(writer: &Writer, def: metadata::Field) -> TokenStream { } fn initializer(writer: &Writer, def: metadata::Field) -> Option { - let Some(value) = constant(def) else { - return None; - }; - + let value = constant(def)?; let mut input = value.as_str(); let metadata::Type::TypeDef(def, _) = def.ty(None) else { diff --git a/crates/libs/core/src/strings/hstring.rs b/crates/libs/core/src/strings/hstring.rs index 26a53dbf9a..5eac52fd33 100644 --- a/crates/libs/core/src/strings/hstring.rs +++ b/crates/libs/core/src/strings/hstring.rs @@ -83,11 +83,7 @@ impl HSTRING { } fn get_header(&self) -> Option<&Header> { - if let Some(header) = &self.0 { - unsafe { Some(header.as_ref()) } - } else { - None - } + self.0.map(|header| unsafe { header.as_ref() }) } } diff --git a/crates/libs/metadata/Cargo.toml b/crates/libs/metadata/Cargo.toml index c94a8ab624..4cd4c78fb5 100644 --- a/crates/libs/metadata/Cargo.toml +++ b/crates/libs/metadata/Cargo.toml @@ -3,7 +3,7 @@ name = "windows-metadata" version = "0.52.0" authors = ["Microsoft"] edition = "2021" -rust-version = "1.64" +rust-version = "1.70" license = "MIT OR Apache-2.0" description = "Windows metadata reader" repository = "https://github.com/microsoft/windows-rs" diff --git a/crates/libs/metadata/src/file.rs b/crates/libs/metadata/src/file.rs index 31d46c3121..1bc4e10328 100644 --- a/crates/libs/metadata/src/file.rs +++ b/crates/libs/metadata/src/file.rs @@ -1,5 +1,4 @@ use super::*; -type Result = std::result::Result; pub struct File { pub reader: *const Reader, @@ -45,16 +44,12 @@ unsafe impl Sync for File {} impl File { pub fn new(bytes: Vec) -> Option { - Self::ok(bytes).ok() - } - - fn ok(bytes: Vec) -> Result { let mut result = File { bytes, reader: std::ptr::null(), strings: 0, blobs: 0, tables: Default::default() }; let dos = result.bytes.view_as::(0)?; if dos.e_magic != IMAGE_DOS_SIGNATURE || result.bytes.copy_as::(dos.e_lfanew as usize)? != IMAGE_NT_SIGNATURE { - return Err(()); + return None; } let file_offset = dos.e_lfanew as usize + std::mem::size_of::(); @@ -71,20 +66,20 @@ impl File { let optional = result.bytes.view_as::(optional_offset)?; (optional.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR as usize].VirtualAddress, result.bytes.view_as_slice_of::(optional_offset + std::mem::size_of::(), file.NumberOfSections as usize)?) } - _ => return Err(()), + _ => return None, }; let clr = result.bytes.view_as::(offset_from_rva(section_from_rva(sections, com_virtual_address)?, com_virtual_address))?; if clr.cb != std::mem::size_of::() as u32 { - return Err(()); + return None; } let metadata_offset = offset_from_rva(section_from_rva(sections, clr.MetaData.VirtualAddress)?, clr.MetaData.VirtualAddress); let metadata = result.bytes.view_as::(metadata_offset)?; if metadata.signature != METADATA_SIGNATURE { - return Err(()); + return None; } // The METADATA_HEADER struct is not a fixed size so have to offset a little more carefully. @@ -306,7 +301,7 @@ impl File { result.tables[NestedClass::TABLE].set_data(&mut view); result.tables[GenericParam::TABLE].set_data(&mut view); - Ok(result) + Some(result) } pub fn usize(&self, row: usize, table: usize, column: usize) -> usize { @@ -356,8 +351,8 @@ impl File { } } -fn section_from_rva(sections: &[IMAGE_SECTION_HEADER], rva: u32) -> Result<&IMAGE_SECTION_HEADER> { - sections.iter().find(|&s| rva >= s.VirtualAddress && rva < s.VirtualAddress + unsafe { s.Misc.VirtualSize }).ok_or(()) +fn section_from_rva(sections: &[IMAGE_SECTION_HEADER], rva: u32) -> Option<&IMAGE_SECTION_HEADER> { + sections.iter().find(|&s| rva >= s.VirtualAddress && rva < s.VirtualAddress + unsafe { s.Misc.VirtualSize }) } fn offset_from_rva(section: &IMAGE_SECTION_HEADER, rva: u32) -> usize { @@ -365,55 +360,55 @@ fn offset_from_rva(section: &IMAGE_SECTION_HEADER, rva: u32) -> usize { } trait View { - fn view_as(&self, offset: usize) -> Result<&T>; - fn view_as_slice_of(&self, offset: usize, len: usize) -> Result<&[T]>; - fn copy_as(&self, offset: usize) -> Result; - fn view_as_str(&self, offset: usize) -> Result<&[u8]>; - fn is_proper_length(&self, offset: usize) -> Result<()>; - fn is_proper_length_and_alignment(&self, offset: usize, count: usize) -> Result<*const T>; + fn view_as(&self, offset: usize) -> Option<&T>; + fn view_as_slice_of(&self, offset: usize, len: usize) -> Option<&[T]>; + fn copy_as(&self, offset: usize) -> Option; + fn view_as_str(&self, offset: usize) -> Option<&[u8]>; + fn is_proper_length(&self, offset: usize) -> Option<()>; + fn is_proper_length_and_alignment(&self, offset: usize, count: usize) -> Option<*const T>; } impl View for [u8] { - fn view_as(&self, offset: usize) -> Result<&T> { - unsafe { Ok(&*self.is_proper_length_and_alignment(offset, 1)?) } + fn view_as(&self, offset: usize) -> Option<&T> { + unsafe { Some(&*self.is_proper_length_and_alignment(offset, 1)?) } } - fn view_as_slice_of(&self, offset: usize, len: usize) -> Result<&[T]> { - unsafe { Ok(std::slice::from_raw_parts(self.is_proper_length_and_alignment(offset, len)?, len)) } + fn view_as_slice_of(&self, offset: usize, len: usize) -> Option<&[T]> { + unsafe { Some(std::slice::from_raw_parts(self.is_proper_length_and_alignment(offset, len)?, len)) } } - fn copy_as(&self, offset: usize) -> Result { + fn copy_as(&self, offset: usize) -> Option { self.is_proper_length::(offset)?; unsafe { let mut data = std::mem::MaybeUninit::zeroed().assume_init(); std::ptr::copy_nonoverlapping(self[offset..].as_ptr(), &mut data as *mut T as *mut u8, std::mem::size_of::()); - Ok(data) + Some(data) } } - fn view_as_str(&self, offset: usize) -> Result<&[u8]> { + fn view_as_str(&self, offset: usize) -> Option<&[u8]> { let buffer = &self[offset..]; - let index = buffer.iter().position(|c| *c == b'\0').ok_or(())?; - Ok(&self[offset..offset + index]) + let index = buffer.iter().position(|c| *c == b'\0')?; + Some(&self[offset..offset + index]) } - fn is_proper_length(&self, offset: usize) -> Result<()> { + fn is_proper_length(&self, offset: usize) -> Option<()> { if offset + std::mem::size_of::() <= self.len() { - Ok(()) + Some(()) } else { - Err(()) + None } } - fn is_proper_length_and_alignment(&self, offset: usize, count: usize) -> Result<*const T> { + fn is_proper_length_and_alignment(&self, offset: usize, count: usize) -> Option<*const T> { self.is_proper_length::(offset * count)?; let ptr = &self[offset] as *const u8 as *const T; if ptr.align_offset(std::mem::align_of::()) == 0 { - Ok(ptr) + Some(ptr) } else { - Err(()) + None } } }