Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assert platform-minimum requirements at build time #3797

Merged
merged 4 commits into from Oct 19, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions tokio/build.rs
Expand Up @@ -19,4 +19,29 @@ fn main() {
);
}
}

assert_platform_minimums();
}

/// Assert platform-minimum requirements for Tokio to work correctly. This might
/// be feasible to do as a constant fn, once MSRC is bumped a bit more.
fn assert_platform_minimums() {
use std::mem;
use std::sync::atomic::{AtomicIsize, AtomicUsize};

if mem::size_of::<usize>() < 4 {
panic!("Tokio only works correctly if usize is at least 4 bytes.");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about cross-compilation? Then the build script is not built in the target architecture. Another option is this:

#[cfg_attr(target_pointer_width = "16", compile_error("Pointer width must be at least 32"))]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. While strictly speaking not exhaustive, that's probably good enough until an MSRV bump. If someone designs a 31-bit hobby platform today they only have themselves to blame. :D

Copy link
Contributor Author

@udoprog udoprog May 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about this until we have sufficient const eval?

#[cfg(not(any(target_pointer_width = "32", target_pointer_width = "64", target_pointer_width = "128")))]
compile_error! {
    "Tokio requires the platform pointer width to be 32, 64, or 128 bits"
}


if mem::size_of::<isize>() < 4 {
panic!("Tokio only works correctly if isize is at least 4 bytes.");
}

if mem::size_of::<AtomicUsize>() < 4 {
panic!("Tokio only works correctly if AtomicUsize is at least 4 bytes.");
}

if mem::size_of::<AtomicIsize>() < 4 {
panic!("Tokio only works correctly if AtomicIsize is at least 4 bytes.");
}
}