Skip to content

Commit

Permalink
Fix alignment computation once and for all
Browse files Browse the repository at this point in the history
  • Loading branch information
gnzlbg committed Jul 3, 2019
1 parent 8e2a831 commit c870733
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 36 deletions.
18 changes: 9 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ matrix:
on:
branch: master
- name: "nightly (x86_64-apple-darwin) + libc-test"
env: TARGET=x86_64-apple-darwin
rust: nightly
os: osx
osx_image: xcode9.4
after_script:
- git clone https://github.com/rust-lang/libc
- sed -i '' 's@ctest = "0.2"@ctest = { path = "../.." }@g' libc/libc-test/Cargo.toml
- cd libc
- cargo generate-lockfile --manifest-path libc-test/Cargo.toml
- |
export TARGET=x86_64-apple-darwin
sh ci/run.sh $TARGET
osx_image: xcode10
after_script: sh ci/run.sh $TARGET
- name: "nightly (i686-apple-darwin) + libc-test"
env: TARGET=i686-apple-darwin
rust: nightly
os: osx
osx_image: xcode10
after_script: sh ci/run.sh $TARGET
- name: "stable (x86_64-unknown-linux-gnu)"
rust: stable
- name: "beta (x86_64-unknown-linux-gnu)"
Expand Down
12 changes: 10 additions & 2 deletions ci/run.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
#!/usr/bin/env sh

# Builds and runs tests for a particular target passed as an argument to this
# script.
Expand All @@ -10,6 +10,14 @@ set -ex
mkdir -p target
git clone https://github.com/rust-lang/libc target/libc
mkdir -p target/libc/target/ctest
sed -i 's@ctest = "0.2"@ctest = { path = "../../.." }@g' target/libc/libc-test/Cargo.toml

case $TARGET in
*linux*)
sed -i 's@ctest = "0.2"@ctest = { path = "../../.." }@g' target/libc/libc-test/Cargo.toml
;;
*apple*)
sed -i '' 's@ctest = "0.2"@ctest = { path = "../.." }@g' target/libc/libc-test/Cargo.toml
;;
esac

cargo test --release --manifest-path target/libc/libc-test/Cargo.toml --target $TARGET
36 changes: 11 additions & 25 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -956,23 +956,6 @@ impl TestGenerator {
for header in &self.headers {
t!(writeln!(gen.c, "#include <{}>", header));
}
t!(gen.c.write_all(
br#"
// Define a proper macro for computing the alignment of a type
#if __cplusplus >= 201103L
// on C++ >= 11 we use the alignof operator
#define libc_test_align_of(x) alignof(x)
#elif __STDC_VERSION__ >= 201112L
// On C >= 11 we just use _Alignof
#define libc_test_align_of(x) _Alignof(x)
#elif defined(_MSC_VER)
#define libc_test_align_of(x) __alignof(x)
#else
#define libc_test_align_of(x) __alignof__(x)
#endif
"#
));

eprintln!("rust version: {}", self.rust_version);
t!(gen.rust.write_all(
if self.rust_version < rustc_version::Version::new(1, 30, 0) {
Expand All @@ -990,7 +973,6 @@ impl TestGenerator {

t!(gen.rust.write_all(
br#"
use std::any::{Any};
use std::mem;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
Expand Down Expand Up @@ -1038,11 +1020,6 @@ impl TestGenerator {
}
}
#[allow(deprecated)] // min_align_of is correct, but deprecated
fn align<T: Any>() -> u64 {
mem::min_align_of::<T>() as u64
}
macro_rules! offset_of {
($ty:ident, $field:ident) => (
(&((*(0 as *const $ty)).$field)) as *const _ as u64
Expand Down Expand Up @@ -1359,7 +1336,16 @@ impl<'a> Generator<'a> {
self.c,
r#"
{linkage} uint64_t __test_size_{ty}(void) {{ return sizeof({cty}); }}
{linkage} uint64_t __test_align_{ty}(void) {{ return libc_test_align_of({cty}); }}
{linkage} uint64_t __test_align_{ty}(void) {{
typedef struct {{
unsigned char c;
{cty} v;
}} type;
type t;
size_t t_addr = (size_t)(unsigned char*)(&t);
size_t v_addr = (size_t)(unsigned char*)(&t.v);
return t_addr >= v_addr? t_addr - v_addr : v_addr - t_addr;
}}
"#,
ty = rust,
cty = c,
Expand All @@ -1380,7 +1366,7 @@ impl<'a> Generator<'a> {
unsafe {{
same(mem::size_of::<{ty}>() as u64,
__test_size_{ty}(), "{ty} size");
same(align::<{ty}>() as u64,
same(mem::align_of::<{ty}>() as u64,
__test_align_{ty}(), "{ty} align");
}}
}}
Expand Down

0 comments on commit c870733

Please sign in to comment.