Skip to content

Commit

Permalink
Merge pull request #25 from JohnTitor/fix-unaligned-references
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnTitor committed May 23, 2021
2 parents 64fc427 + cdd2003 commit aab0ed6
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 18 deletions.
23 changes: 15 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1276,11 +1276,13 @@ impl<'a> Generator<'a> {
fn __test_fsize_{ty}_{field}() -> u64;
}}
unsafe {{
let foo = 0 as *mut {ty};
let zeroed_ty = std::mem::zeroed::<{ty}>();
let ty_ptr = std::ptr::addr_of!((zeroed_ty).{field});
let val = ty_ptr.read_unaligned();
same(offset_of!({ty}, {field}),
__test_offset_{ty}_{field}(),
"field offset {field} of {ty}");
same(mem::size_of_val(&(*foo).{field}) as u64,
same(mem::size_of_val(&val) as u64,
__test_fsize_{ty}_{field}(),
"field size {field} of {ty}");
}}
Expand Down Expand Up @@ -1325,10 +1327,13 @@ impl<'a> Generator<'a> {
-> *mut u8;
}}
unsafe {{
let foo = 0 as *mut {ty};
same(&(*foo).{field} as *const _ as *mut _,
__test_field_type_{ty}_{field}(foo),
let mut zeroed_ty = mem::zeroed::<{ty}>();
let ty_ptr_mut = std::ptr::addr_of_mut!(zeroed_ty);
let field_ptr = std::ptr::addr_of!(zeroed_ty.{field});
same(field_ptr as *mut _,
__test_field_type_{ty}_{field}(ty_ptr_mut),
"field type {field} of {ty}");
mem::forget(zeroed_ty);
}}
"#,
ty = ty,
Expand Down Expand Up @@ -1850,9 +1855,11 @@ impl<'a> Generator<'a> {
self.rust,
r#"
unsafe {{
let size = mem::size_of_val(&(*foo).{field});
let off = offset_of!({ty}, {field}) as usize;
v.push((off, size));
let ty_ptr = std::ptr::addr_of!((*foo).{field});
let val = ty_ptr.read_unaligned();
let size = mem::size_of_val(&val);
let off = offset_of!({ty}, {field}) as usize;
v.push((off, size));
}}
"#,
ty = rust,
Expand Down
1 change: 0 additions & 1 deletion testcrate/src/bin/t1.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![cfg(not(test))]
#![deny(warnings)]
#![allow(unaligned_references)]

use libc::*;
use testcrate::t1::*;
Expand Down
1 change: 0 additions & 1 deletion testcrate/src/bin/t1_cxx.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![cfg(not(test))]
#![deny(warnings)]
#![allow(unaligned_references)]

use libc::*;
use testcrate::t1::*;
Expand Down
1 change: 0 additions & 1 deletion testcrate/src/bin/t2.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![cfg(not(test))]
#![deny(warnings)]
#![allow(unaligned_references)]

use testcrate::t2::*;

Expand Down
1 change: 0 additions & 1 deletion testcrate/src/bin/t2_cxx.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![cfg(not(test))]
#![deny(warnings)]
#![allow(unaligned_references)]

use testcrate::t2::*;

Expand Down
12 changes: 6 additions & 6 deletions testcrate/tests/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@ fn cmd(name: &str) -> Command {
#[test]
fn t1() {
let (o, status) = output(&mut cmd("t1"));
assert!(status.success(), o);
assert!(!o.contains("bad "), o);
assert!(status.success(), "{}", o);
assert!(!o.contains("bad "), "{}", o);
eprintln!("o: {}", o);
}

#[test]
fn t1_cxx() {
let (o, status) = output(&mut cmd("t1_cxx"));
assert!(status.success(), o);
assert!(!o.contains("bad "), o);
assert!(status.success(), "{}", o);
assert!(!o.contains("bad "), "{}", o);
}

#[test]
fn t2() {
let (o, status) = output(&mut cmd("t2"));
assert!(!status.success(), o);
assert!(!status.success(), "{}", o);
let errors = [
"bad T2Foo signed",
"bad T2TypedefFoo signed",
Expand Down Expand Up @@ -74,7 +74,7 @@ fn t2() {
#[test]
fn t2_cxx() {
let (o, status) = output(&mut cmd("t2_cxx"));
assert!(!status.success(), o);
assert!(!status.success(), "{}", o);
let errors = [
"bad T2Foo signed",
"bad T2TypedefFoo signed",
Expand Down

0 comments on commit aab0ed6

Please sign in to comment.