nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: bitshifter/glam-rs
Pull request #606 (2025-02-10 12:33:21)

Remove `bytecheck` conditional testing in `rkyv`
Fixes https://github.com/bitshifter/glam-rs/pull/605#discussion_r1942707852



Patch:
diff --git a/src/features/impl_rkyv.rs b/src/features/impl_rkyv.rs
index 1172301..3e2c5db 100644
--- a/src/features/impl_rkyv.rs
+++ b/src/features/impl_rkyv.rs
@@ -1,6 +1,8 @@
 #[cfg(feature = "bytecheck")]
 macro_rules! impl_rkyv {
     (@bytecheck $type:ty) => {
+        // SAFETY: All bit patterns are valid for these primitive types.
+        // https://docs.rs/bytecheck/0.8.1/src/bytecheck/lib.rs.html#352
         unsafe impl<C: Fallible +?Sized> rkyv::bytecheck::CheckBytes<C> for $type {
             #[inline]
             unsafe fn check_bytes(
@@ -175,7 +177,6 @@ mod test {
     >;
     /// The deserializer type expected by [`rkyv::deserialize()`].
     pub type TestDeserializer = rkyv::api::high::HighDeserializer<rkyv::rancor::Panic>;
-    pub type TestValidator<'a> = rkyv::api::high::HighValidator<'a, rkyv::rancor::Panic>;
     pub fn test_archive<T>(value: &T)
     where
         T: core::fmt::Debug
@@ -183,16 +184,12 @@ mod test {
             + rkyv::Portable
             + for<'a> rkyv::Serialize<TestSerializer<'a>>,
         T::Archived: core::fmt::Debug + PartialEq<T> + rkyv::Deserialize<T, TestDeserializer>,
-        // #[cfg(feature = "bytecheck")]
-        T::Archived: for<'a> rkyv::bytecheck::CheckBytes<TestValidator<'a>>,
     {
         let buffer = rkyv::to_bytes(value).unwrap();
 
-        #[cfg(feature = "bytecheck")]
-        {
-            let archived_value = rkyv::access::<T::Archived, rkyv::rancor::Panic>(&buffer).unwrap();
-            assert_eq!(archived_value, value);
-        }
+        // SAFETY: all bit patterns are valid for the primitive types used by glam.  There is
+        // no need to write special-cased conditional tests that rely on bytecheck for the safe
+        // rkyv::access() wrapper.
         let archived_value = unsafe { rkyv::access_unchecked::<T::Archived>(&buffer) };
         assert_eq!(archived_value, value);
         assert_eq!(


Test patch:
diff --git a/build_and_test_features.sh b/build_and_test_features.sh
index 30f1d98..2f22314 100755
--- a/build_and_test_features.sh
+++ b/build_and_test_features.sh
@@ -10,6 +10,7 @@ FEATURE_SETS=(
   # std
   "std"
   "std $DEPENDENCIES"
+  "std $DEPENDENCIES bytecheck"
   "std scalar-math $DEPENDENCIES"
   "std cuda"
   "std scalar-math cuda"
Repository: bytecodealliance/wasmtime
Pull request #7945 (2024-02-15 10:40:39)

Fix UB in cranelift-jit when trying to define a zero sized data object
And also fix UB when the allocation fails.

Fixes https://github.com/bytecodealliance/wasmtime/issues/7918


Patch:
diff --git a/cranelift/jit/src/backend.rs b/cranelift/jit/src/backend.rs
index f04b89d84..631a646bb 100644
--- a/cranelift/jit/src/backend.rs
+++ b/cranelift/jit/src/backend.rs
@@ -851,7 +851,11 @@ impl Module for JITModule {
         } = data;
 
         let size = init.size();
-        let ptr = if decl.writable {
+        let ptr = if size == 0 {
+            // Return a correctly aligned non-null pointer to avoid UB in write_bytes and
+            // copy_nonoverlapping.
+            usize::try_from(align.unwrap_or(WRITABLE_DATA_ALIGNMENT)).unwrap() as *mut u8
+        } else if decl.writable {
             self.memory
                 .writable
                 .allocate(size, align.unwrap_or(WRITABLE_DATA_ALIGNMENT))
@@ -869,6 +873,17 @@ impl Module for JITModule {
                 })?
         };
 
+        if ptr.is_null() {
+            // FIXME pass a Layout to allocate and only compute the layout once.
+            std::alloc::handle_alloc_error(
+                std::alloc::Layout::from_size_align(
+                    size,
+                    align.unwrap_or(READONLY_DATA_ALIGNMENT).try_into().unwrap(),
+                )
+                .unwrap(),
+            );
+        }
+
         match *init {
             Init::Uninitialized => {
                 panic!("data is not initialized yet");


Test patch:
diff --git a/cranelift/jit/tests/basic.rs b/cranelift/jit/tests/basic.rs
index a6e5a0464..4969b2cca 100644
--- a/cranelift/jit/tests/basic.rs
+++ b/cranelift/jit/tests/basic.rs
@@ -213,3 +213,27 @@ fn libcall_function() {
 
     module.finalize_definitions().unwrap();
 }
+
+// This used to cause UB. See https://github.com/bytecodealliance/wasmtime/issues/7918.
+#[test]
+fn empty_data_object() {
+    let mut flag_builder = settings::builder();
+    flag_builder.set("use_colocated_libcalls", "false").unwrap();
+    // FIXME set back to true once the x64 backend supports it.
+    flag_builder.set("is_pic", "false").unwrap();
+    let isa_builder = cranelift_native::builder().unwrap_or_else(|msg| {
+        panic!("host machine is not supported: {}", msg);
+    });
+    let isa = isa_builder
+        .finish(settings::Flags::new(flag_builder))
+        .unwrap();
+    let mut module = JITModule::new(JITBuilder::with_isa(isa, default_libcall_names()));
+
+    let data_id = module
+        .declare_data("empty", Linkage::Export, false, false)
+        .unwrap();
+
+    let mut data = DataDescription::new();
+    data.define(Box::new([]));
+    module.define_data(data_id, &data).unwrap();
+}
Repository: quantumlib/OpenFermion
Pull request #709 (2021-02-18 19:45:13)

Name change on RDM reconstruction function.
Correcting the spelling on C. Valdemoro's name.

Mea Culpa.


Patch:
diff --git a/src/openfermion/__init__.py b/src/openfermion/__init__.py
index fc8cfc8..952902c 100644
--- a/src/openfermion/__init__.py
+++ b/src/openfermion/__init__.py
@@ -136,7 +136,7 @@ from openfermion.linalg import (
     fit_known_frequencies,
     prony,
     wedge,
-    valdemaro_reconstruction,
+    valdemoro_reconstruction,
 )
 
 from openfermion.measurements import (
diff --git a/src/openfermion/linalg/__init__.py b/src/openfermion/linalg/__init__.py
index 0e47e3e..ba0700d 100644
--- a/src/openfermion/linalg/__init__.py
+++ b/src/openfermion/linalg/__init__.py
@@ -44,7 +44,7 @@ from .linear_qubit_operator import (
 )
 
 from .rdm_reconstruction import (
-    valdemaro_reconstruction,)
+    valdemoro_reconstruction,)
 
 from .sparse_tools import (
     wrapped_kronecker,
diff --git a/src/openfermion/linalg/rdm_reconstruction.py b/src/openfermion/linalg/rdm_reconstruction.py
index 60ff640..da3a2a4 100644
--- a/src/openfermion/linalg/rdm_reconstruction.py
+++ b/src/openfermion/linalg/rdm_reconstruction.py
@@ -2,7 +2,7 @@ import numpy as np
 from openfermion.linalg.wedge_product import wedge
 
 
-def valdemaro_reconstruction(tpdm, n_electrons):
+def valdemoro_reconstruction(tpdm, n_electrons):
     """
     Build a 3-RDM by cumulant expansion and setting 3rd cumulant to zero
 


Test patch:
diff --git a/src/openfermion/linalg/rdm_reconstruction_test.py b/src/openfermion/linalg/rdm_reconstruction_test.py
index 6ef79dc..d1a4f18 100644
--- a/src/openfermion/linalg/rdm_reconstruction_test.py
+++ b/src/openfermion/linalg/rdm_reconstruction_test.py
@@ -1,5 +1,5 @@
 import numpy as np
-from openfermion.linalg.rdm_reconstruction import valdemaro_reconstruction
+from openfermion.linalg.rdm_reconstruction import valdemoro_reconstruction
 from openfermion.linalg.wedge_product import wedge
 
 
@@ -7,7 +7,7 @@ def test_valdemoro_correct_size():
     m = 4
     opdm = np.random.random((m, m))
     tpdm = wedge(opdm, opdm, (1, 1), (1, 1))
-    d3v = valdemaro_reconstruction(tpdm, 4)
+    d3v = valdemoro_reconstruction(tpdm, 4)
     assert d3v.shape == (m, m, m, m, m, m)
 
 
@@ -15,12 +15,12 @@ def test_correct_trace():
     opdm = np.diag([1] * 2 + [0] * 2)
     tpdm = wedge(opdm, opdm, (1, 1), (1, 1))
     assert np.isclose(np.einsum('ijji', tpdm), 2 * (2 - 1) / 2)
-    d3v = valdemaro_reconstruction(tpdm, 2)
+    d3v = valdemoro_reconstruction(tpdm, 2)
     # because only 2 particles in this system
     assert np.isclose(np.einsum('ijkkji', d3v), 0)
 
     opdm = np.diag([1] * 5 + [0] * 3)
     tpdm = wedge(opdm, opdm, (1, 1), (1, 1))
     assert np.isclose(np.einsum('ijji', tpdm), 5 * (5 - 1) / 2)
-    d3v = valdemaro_reconstruction(tpdm, 5)
+    d3v = valdemoro_reconstruction(tpdm, 5)
     assert np.isclose(np.einsum('ijkkji', d3v), 5 * (5 - 1) * (5 - 2) / 6)
\ No newline at end of file