summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZhiting Zhu <zhitingz@cs.utexas.edu>2017-10-23 22:39:11 -0500
committerZhiting Zhu <zhitingz@cs.utexas.edu>2017-10-23 22:39:11 -0500
commitfa9b5e3c3fa4f81ec2b945d8b8645f538020c934 (patch)
treec74fe31df6e63799c4d552410d28bbcef4ae4e1c
parent17adb13417f5d6cbd718753f0dea4371a4646c5e (diff)
Fix mistakenly derive hash for struct that contains IncompleteArrayField
-rw-r--r--src/ir/analysis/derive_hash.rs5
-rw-r--r--tests/expectations/tests/class.rs90
-rw-r--r--tests/expectations/tests/class_1_0.rs86
-rw-r--r--tests/expectations/tests/derive-hash-struct-with-incomplete-array.rs121
-rw-r--r--tests/headers/class.hpp18
-rw-r--r--tests/headers/class_1_0.hpp18
-rw-r--r--tests/headers/derive-hash-struct-with-incomplete-array.h17
7 files changed, 351 insertions, 4 deletions
diff --git a/src/ir/analysis/derive_hash.rs b/src/ir/analysis/derive_hash.rs
index 569c7246..d2eac677 100644
--- a/src/ir/analysis/derive_hash.rs
+++ b/src/ir/analysis/derive_hash.rs
@@ -187,7 +187,10 @@ impl<'ctx> MonotoneFramework for CannotDeriveHash<'ctx> {
return self.insert(id);
}
- if len <= RUST_DERIVE_IN_ARRAY_LIMIT {
+ if len == 0 {
+ trace!(" cannot derive `Hash` for incomplete arrays");
+ self.insert(id)
+ } else if len <= RUST_DERIVE_IN_ARRAY_LIMIT {
trace!(" array is small enough to derive Hash");
ConstrainResult::Same
} else {
diff --git a/tests/expectations/tests/class.rs b/tests/expectations/tests/class.rs
index 351f041a..eb09d753 100644
--- a/tests/expectations/tests/class.rs
+++ b/tests/expectations/tests/class.rs
@@ -142,6 +142,47 @@ impl Default for C_with_zero_length_array {
}
}
#[repr(C)]
+#[derive(Debug, Default)]
+pub struct C_with_zero_length_array_2 {
+ pub a: ::std::os::raw::c_int,
+ pub zero_length_array: __IncompleteArrayField<::std::os::raw::c_char>,
+}
+#[test]
+fn bindgen_test_layout_C_with_zero_length_array_2() {
+ assert_eq!(
+ ::std::mem::size_of::<C_with_zero_length_array_2>(),
+ 4usize,
+ concat!("Size of: ", stringify!(C_with_zero_length_array_2))
+ );
+ assert_eq!(
+ ::std::mem::align_of::<C_with_zero_length_array_2>(),
+ 4usize,
+ concat!("Alignment of ", stringify!(C_with_zero_length_array_2))
+ );
+ assert_eq!(
+ unsafe { &(*(0 as *const C_with_zero_length_array_2)).a as *const _ as usize },
+ 0usize,
+ concat!(
+ "Alignment of field: ",
+ stringify!(C_with_zero_length_array_2),
+ "::",
+ stringify!(a)
+ )
+ );
+ assert_eq!(
+ unsafe {
+ &(*(0 as *const C_with_zero_length_array_2)).zero_length_array as *const _ as usize
+ },
+ 4usize,
+ concat!(
+ "Alignment of field: ",
+ stringify!(C_with_zero_length_array_2),
+ "::",
+ stringify!(zero_length_array)
+ )
+ );
+}
+#[repr(C)]
pub struct C_with_incomplete_array {
pub a: ::std::os::raw::c_int,
pub big_array: [::std::os::raw::c_char; 33usize],
@@ -166,6 +207,25 @@ impl Default for C_with_incomplete_array {
}
}
#[repr(C)]
+#[derive(Debug, Default)]
+pub struct C_with_incomplete_array_2 {
+ pub a: ::std::os::raw::c_int,
+ pub incomplete_array: __IncompleteArrayField<::std::os::raw::c_char>,
+}
+#[test]
+fn bindgen_test_layout_C_with_incomplete_array_2() {
+ assert_eq!(
+ ::std::mem::size_of::<C_with_incomplete_array_2>(),
+ 4usize,
+ concat!("Size of: ", stringify!(C_with_incomplete_array_2))
+ );
+ assert_eq!(
+ ::std::mem::align_of::<C_with_incomplete_array_2>(),
+ 4usize,
+ concat!("Alignment of ", stringify!(C_with_incomplete_array_2))
+ );
+}
+#[repr(C)]
pub struct C_with_zero_length_array_and_incomplete_array {
pub a: ::std::os::raw::c_int,
pub big_array: [::std::os::raw::c_char; 33usize],
@@ -197,7 +257,33 @@ impl Default for C_with_zero_length_array_and_incomplete_array {
}
}
#[repr(C)]
-#[derive(Debug, Default, Hash, PartialEq, Eq)]
+#[derive(Debug, Default)]
+pub struct C_with_zero_length_array_and_incomplete_array_2 {
+ pub a: ::std::os::raw::c_int,
+ pub zero_length_array: __IncompleteArrayField<::std::os::raw::c_char>,
+ pub incomplete_array: __IncompleteArrayField<::std::os::raw::c_char>,
+}
+#[test]
+fn bindgen_test_layout_C_with_zero_length_array_and_incomplete_array_2() {
+ assert_eq!(
+ ::std::mem::size_of::<C_with_zero_length_array_and_incomplete_array_2>(),
+ 4usize,
+ concat!(
+ "Size of: ",
+ stringify!(C_with_zero_length_array_and_incomplete_array_2)
+ )
+ );
+ assert_eq!(
+ ::std::mem::align_of::<C_with_zero_length_array_and_incomplete_array_2>(),
+ 4usize,
+ concat!(
+ "Alignment of ",
+ stringify!(C_with_zero_length_array_and_incomplete_array_2)
+ )
+ );
+}
+#[repr(C)]
+#[derive(Debug, Default, Hash, PartialOrd, Ord, PartialEq, Eq)]
pub struct WithDtor {
pub b: ::std::os::raw::c_int,
}
@@ -336,7 +422,7 @@ impl Default for WithUnion {
}
}
#[repr(C)]
-#[derive(Debug, Default, Copy, Hash, PartialEq, Eq)]
+#[derive(Debug, Default, Copy, Hash, PartialOrd, Ord, PartialEq, Eq)]
pub struct RealAbstractionWithTonsOfMethods {
pub _address: u8,
}
diff --git a/tests/expectations/tests/class_1_0.rs b/tests/expectations/tests/class_1_0.rs
index 1127c88b..c54edcdb 100644
--- a/tests/expectations/tests/class_1_0.rs
+++ b/tests/expectations/tests/class_1_0.rs
@@ -190,6 +190,47 @@ impl Default for C_with_zero_length_array {
}
}
#[repr(C)]
+#[derive(Debug, Default)]
+pub struct C_with_zero_length_array_2 {
+ pub a: ::std::os::raw::c_int,
+ pub zero_length_array: __IncompleteArrayField<::std::os::raw::c_char>,
+}
+#[test]
+fn bindgen_test_layout_C_with_zero_length_array_2() {
+ assert_eq!(
+ ::std::mem::size_of::<C_with_zero_length_array_2>(),
+ 4usize,
+ concat!("Size of: ", stringify!(C_with_zero_length_array_2))
+ );
+ assert_eq!(
+ ::std::mem::align_of::<C_with_zero_length_array_2>(),
+ 4usize,
+ concat!("Alignment of ", stringify!(C_with_zero_length_array_2))
+ );
+ assert_eq!(
+ unsafe { &(*(0 as *const C_with_zero_length_array_2)).a as *const _ as usize },
+ 0usize,
+ concat!(
+ "Alignment of field: ",
+ stringify!(C_with_zero_length_array_2),
+ "::",
+ stringify!(a)
+ )
+ );
+ assert_eq!(
+ unsafe {
+ &(*(0 as *const C_with_zero_length_array_2)).zero_length_array as *const _ as usize
+ },
+ 4usize,
+ concat!(
+ "Alignment of field: ",
+ stringify!(C_with_zero_length_array_2),
+ "::",
+ stringify!(zero_length_array)
+ )
+ );
+}
+#[repr(C)]
pub struct C_with_incomplete_array {
pub a: ::std::os::raw::c_int,
pub big_array: [::std::os::raw::c_char; 33usize],
@@ -214,6 +255,25 @@ impl Default for C_with_incomplete_array {
}
}
#[repr(C)]
+#[derive(Debug, Default)]
+pub struct C_with_incomplete_array_2 {
+ pub a: ::std::os::raw::c_int,
+ pub incomplete_array: __IncompleteArrayField<::std::os::raw::c_char>,
+}
+#[test]
+fn bindgen_test_layout_C_with_incomplete_array_2() {
+ assert_eq!(
+ ::std::mem::size_of::<C_with_incomplete_array_2>(),
+ 4usize,
+ concat!("Size of: ", stringify!(C_with_incomplete_array_2))
+ );
+ assert_eq!(
+ ::std::mem::align_of::<C_with_incomplete_array_2>(),
+ 4usize,
+ concat!("Alignment of ", stringify!(C_with_incomplete_array_2))
+ );
+}
+#[repr(C)]
pub struct C_with_zero_length_array_and_incomplete_array {
pub a: ::std::os::raw::c_int,
pub big_array: [::std::os::raw::c_char; 33usize],
@@ -245,6 +305,32 @@ impl Default for C_with_zero_length_array_and_incomplete_array {
}
}
#[repr(C)]
+#[derive(Debug, Default)]
+pub struct C_with_zero_length_array_and_incomplete_array_2 {
+ pub a: ::std::os::raw::c_int,
+ pub zero_length_array: __IncompleteArrayField<::std::os::raw::c_char>,
+ pub incomplete_array: __IncompleteArrayField<::std::os::raw::c_char>,
+}
+#[test]
+fn bindgen_test_layout_C_with_zero_length_array_and_incomplete_array_2() {
+ assert_eq!(
+ ::std::mem::size_of::<C_with_zero_length_array_and_incomplete_array_2>(),
+ 4usize,
+ concat!(
+ "Size of: ",
+ stringify!(C_with_zero_length_array_and_incomplete_array_2)
+ )
+ );
+ assert_eq!(
+ ::std::mem::align_of::<C_with_zero_length_array_and_incomplete_array_2>(),
+ 4usize,
+ concat!(
+ "Alignment of ",
+ stringify!(C_with_zero_length_array_and_incomplete_array_2)
+ )
+ );
+}
+#[repr(C)]
#[derive(Debug, Default, Hash, PartialEq, Eq)]
pub struct WithDtor {
pub b: ::std::os::raw::c_int,
diff --git a/tests/expectations/tests/derive-hash-struct-with-incomplete-array.rs b/tests/expectations/tests/derive-hash-struct-with-incomplete-array.rs
new file mode 100644
index 00000000..a2179f41
--- /dev/null
+++ b/tests/expectations/tests/derive-hash-struct-with-incomplete-array.rs
@@ -0,0 +1,121 @@
+/* automatically generated by rust-bindgen */
+
+
+#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
+
+
+#[repr(C)]
+#[derive(Default)]
+pub struct __IncompleteArrayField<T>(::std::marker::PhantomData<T>);
+impl<T> __IncompleteArrayField<T> {
+ #[inline]
+ pub fn new() -> Self {
+ __IncompleteArrayField(::std::marker::PhantomData)
+ }
+ #[inline]
+ pub unsafe fn as_ptr(&self) -> *const T {
+ ::std::mem::transmute(self)
+ }
+ #[inline]
+ pub unsafe fn as_mut_ptr(&mut self) -> *mut T {
+ ::std::mem::transmute(self)
+ }
+ #[inline]
+ pub unsafe fn as_slice(&self, len: usize) -> &[T] {
+ ::std::slice::from_raw_parts(self.as_ptr(), len)
+ }
+ #[inline]
+ pub unsafe fn as_mut_slice(&mut self, len: usize) -> &mut [T] {
+ ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len)
+ }
+}
+impl<T> ::std::fmt::Debug for __IncompleteArrayField<T> {
+ fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
+ fmt.write_str("__IncompleteArrayField")
+ }
+}
+impl<T> ::std::clone::Clone for __IncompleteArrayField<T> {
+ #[inline]
+ fn clone(&self) -> Self {
+ Self::new()
+ }
+}
+impl<T> ::std::marker::Copy for __IncompleteArrayField<T> {}
+#[repr(C)]
+#[derive(Debug, Default)]
+pub struct test {
+ pub a: ::std::os::raw::c_int,
+ pub zero_length_array: __IncompleteArrayField<::std::os::raw::c_char>,
+}
+#[test]
+fn bindgen_test_layout_test() {
+ assert_eq!(
+ ::std::mem::size_of::<test>(),
+ 4usize,
+ concat!("Size of: ", stringify!(test))
+ );
+ assert_eq!(
+ ::std::mem::align_of::<test>(),
+ 4usize,
+ concat!("Alignment of ", stringify!(test))
+ );
+ assert_eq!(
+ unsafe { &(*(0 as *const test)).a as *const _ as usize },
+ 0usize,
+ concat!(
+ "Alignment of field: ",
+ stringify!(test),
+ "::",
+ stringify!(a)
+ )
+ );
+ assert_eq!(
+ unsafe { &(*(0 as *const test)).zero_length_array as *const _ as usize },
+ 4usize,
+ concat!(
+ "Alignment of field: ",
+ stringify!(test),
+ "::",
+ stringify!(zero_length_array)
+ )
+ );
+}
+#[repr(C)]
+#[derive(Debug, Default)]
+pub struct test2 {
+ pub a: ::std::os::raw::c_int,
+ pub incomplete_array: __IncompleteArrayField<::std::os::raw::c_char>,
+}
+#[test]
+fn bindgen_test_layout_test2() {
+ assert_eq!(
+ ::std::mem::size_of::<test2>(),
+ 4usize,
+ concat!("Size of: ", stringify!(test2))
+ );
+ assert_eq!(
+ ::std::mem::align_of::<test2>(),
+ 4usize,
+ concat!("Alignment of ", stringify!(test2))
+ );
+}
+#[repr(C)]
+#[derive(Debug, Default)]
+pub struct test3 {
+ pub a: ::std::os::raw::c_int,
+ pub zero_length_array: __IncompleteArrayField<::std::os::raw::c_char>,
+ pub incomplete_array: __IncompleteArrayField<::std::os::raw::c_char>,
+}
+#[test]
+fn bindgen_test_layout_test3() {
+ assert_eq!(
+ ::std::mem::size_of::<test3>(),
+ 4usize,
+ concat!("Size of: ", stringify!(test3))
+ );
+ assert_eq!(
+ ::std::mem::align_of::<test3>(),
+ 4usize,
+ concat!("Alignment of ", stringify!(test3))
+ );
+}
diff --git a/tests/headers/class.hpp b/tests/headers/class.hpp
index ac2da1a4..f77ac92a 100644
--- a/tests/headers/class.hpp
+++ b/tests/headers/class.hpp
@@ -1,4 +1,4 @@
-// bindgen-flags: --with-derive-hash --with-derive-partialeq --with-derive-eq
+// bindgen-flags: --with-derive-hash --with-derive-partialeq --with-derive-eq --with-derive-partialord --with-derive-ord
//
class C {
int a;
@@ -13,6 +13,11 @@ class C_with_zero_length_array {
char zero_length_array[0];
};
+class C_with_zero_length_array_2 {
+ int a;
+ char zero_length_array[0];
+};
+
class C_with_incomplete_array {
int a;
// More than rust limits (32)
@@ -20,6 +25,11 @@ class C_with_incomplete_array {
char incomplete_array[];
};
+class C_with_incomplete_array_2 {
+ int a;
+ char incomplete_array[];
+};
+
class C_with_zero_length_array_and_incomplete_array {
int a;
// More than rust limits (32)
@@ -28,6 +38,12 @@ class C_with_zero_length_array_and_incomplete_array {
char incomplete_array[];
};
+class C_with_zero_length_array_and_incomplete_array_2 {
+ int a;
+ char zero_length_array[0];
+ char incomplete_array[];
+};
+
class WithDtor {
int b;
diff --git a/tests/headers/class_1_0.hpp b/tests/headers/class_1_0.hpp
index 6fa01e95..e3735eb6 100644
--- a/tests/headers/class_1_0.hpp
+++ b/tests/headers/class_1_0.hpp
@@ -13,6 +13,11 @@ class C_with_zero_length_array {
char zero_length_array[0];
};
+class C_with_zero_length_array_2 {
+ int a;
+ char zero_length_array[0];
+};
+
class C_with_incomplete_array {
int a;
// More than rust limits (32)
@@ -20,6 +25,12 @@ class C_with_incomplete_array {
char incomplete_array[];
};
+class C_with_incomplete_array_2 {
+ int a;
+ char incomplete_array[];
+};
+
+
class C_with_zero_length_array_and_incomplete_array {
int a;
// More than rust limits (32)
@@ -28,6 +39,13 @@ class C_with_zero_length_array_and_incomplete_array {
char incomplete_array[];
};
+class C_with_zero_length_array_and_incomplete_array_2 {
+ int a;
+ char zero_length_array[0];
+ char incomplete_array[];
+};
+
+
class WithDtor {
int b;
diff --git a/tests/headers/derive-hash-struct-with-incomplete-array.h b/tests/headers/derive-hash-struct-with-incomplete-array.h
new file mode 100644
index 00000000..65c009db
--- /dev/null
+++ b/tests/headers/derive-hash-struct-with-incomplete-array.h
@@ -0,0 +1,17 @@
+// bindgen-flags: --with-derive-hash --with-derive-partialeq --with-derive-eq --with-derive-partialord --with-derive-ord
+//
+struct test {
+ int a;
+ char zero_length_array[0];
+};
+
+struct test2 {
+ int a;
+ char incomplete_array[];
+};
+
+struct test3 {
+ int a;
+ char zero_length_array[0];
+ char incomplete_array[];
+};