├── examples ├── present.rs └── not_present.rs ├── tests ├── present.rs └── not_present.rs ├── caller ├── src │ └── lib.rs └── Cargo.toml ├── sys ├── src │ └── lib.rs └── Cargo.toml ├── .gitignore ├── Cargo.toml ├── LICENSE ├── src └── lib.rs └── README.md /examples/present.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | if true { 3 | paranoia_caller::mark(); 4 | } 5 | assert!(paranoia::marker_exists()); 6 | } 7 | -------------------------------------------------------------------------------- /examples/not_present.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | if false { 3 | paranoia_caller::mark(); 4 | } 5 | assert!(!paranoia::marker_exists()); 6 | } 7 | -------------------------------------------------------------------------------- /tests/present.rs: -------------------------------------------------------------------------------- 1 | #[test] 2 | fn present() { 3 | if true { 4 | paranoia_caller::mark(); 5 | } 6 | assert!(paranoia::marker_exists()); 7 | } 8 | -------------------------------------------------------------------------------- /tests/not_present.rs: -------------------------------------------------------------------------------- 1 | #[test] 2 | fn not_present() { 3 | if false { 4 | paranoia_caller::mark(); 5 | } 6 | assert!(!paranoia::marker_exists()); 7 | } 8 | -------------------------------------------------------------------------------- /caller/src/lib.rs: -------------------------------------------------------------------------------- 1 | #[link(name = "paranoia_sys")] 2 | extern "C" { 3 | fn ___paranoia_present(); 4 | } 5 | 6 | /// Mark a branch that can be later tested to see if it 7 | /// was compiled away or not. 8 | pub fn mark() { 9 | unsafe { 10 | ___paranoia_present(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /sys/src/lib.rs: -------------------------------------------------------------------------------- 1 | /// This function exists as a marker that will be compiled away 2 | /// if it is never called from a final compiled binary. Its 3 | /// existence is tested using dlsym when running the final 4 | /// binary. 5 | #[no_mangle] 6 | #[inline(never)] 7 | pub extern "C" fn ___paranoia_present() { 8 | #[allow(unsafe_code)] 9 | unsafe { 10 | std::ptr::read_volatile(&()); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Generated by Cargo 2 | # will have compiled files and executables 3 | target 4 | 5 | # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries 6 | # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html 7 | Cargo.lock 8 | 9 | # These are backup files generated by rustfmt 10 | **/*.rs.bk 11 | 12 | 13 | # Added by cargo 14 | 15 | /target 16 | -------------------------------------------------------------------------------- /sys/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "paranoia-sys" 3 | version = "0.1.0" 4 | authors = ["Tyler Neely "] 5 | edition = "2018" 6 | description = "Programmatically determine whether code was optimized away or not" 7 | license = "MIT/Apache-2.0" 8 | homepage = "https://github.com/spacejam/paranoia" 9 | repository = "https://github.com/spacejam/paranoia" 10 | documentation = "https://docs.rs/paranoia/" 11 | 12 | [lib] 13 | crate-type = ["cdylib"] 14 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "paranoia" 3 | authors = ["Tyler Neely "] 4 | description = "Programmatically determine whether code was optimized away or not" 5 | license = "MIT/Apache-2.0" 6 | homepage = "https://github.com/spacejam/paranoia" 7 | repository = "https://github.com/spacejam/paranoia" 8 | documentation = "https://docs.rs/paranoia/" 9 | edition = "2018" 10 | version = "0.1.4" 11 | 12 | [dependencies] 13 | paranoia-caller = { version = "0.1.0" } 14 | paranoia-sys = { version = "0.1.0" } 15 | libc = "0.2.83" 16 | lazy_static = "1.4.0" 17 | -------------------------------------------------------------------------------- /caller/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "paranoia-caller" 3 | authors = ["Tyler Neely "] 4 | description = "Programmatically determine whether code was optimized away or not" 5 | license = "MIT/Apache-2.0" 6 | homepage = "https://github.com/spacejam/paranoia" 7 | repository = "https://github.com/spacejam/paranoia" 8 | documentation = "https://docs.rs/paranoia/" 9 | edition = "2018" 10 | version = "0.1.0" 11 | 12 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 13 | 14 | [dependencies] 15 | paranoia-sys = { version = "0.1.0" } 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Tyler Neely 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | //! Paranoia is a simple hack to see if some code was optimized 2 | //! away (by never being called) or not. This only serves as a 3 | //! hint because sometimes the compiler isn't able to determine 4 | //! whether code is able to be fully eliminated or not. But there 5 | //! are no false negatives - if `marker_exists` returns false, you 6 | //! can be certain that the call to marker was fully optimized away. 7 | //! 8 | //! # Examples 9 | //! 10 | //! `Cargo.toml` 11 | //! 12 | //! ```no_build 13 | //! [dependencies] 14 | //! paranoia-caller = "*" 15 | //! paranoia = "*" 16 | //! ``` 17 | //! 18 | //! verify that it was optimized out: 19 | //! ``` 20 | //! if false { 21 | //! paranoia_caller::mark(); 22 | //! } 23 | //! assert!(!paranoia::marker_exists()); 24 | //! ``` 25 | //! 26 | //! see if it was not able to be optimized out: 27 | //! ``` 28 | //! if true { 29 | //! paranoia_caller::mark(); 30 | //! } 31 | //! assert!(paranoia::marker_exists()); 32 | //! ``` 33 | 34 | fn exists() -> bool { 35 | use libc::{dlsym, RTLD_NEXT}; 36 | use std::ffi::CString; 37 | 38 | let symbol = CString::new(b"___paranoia_present".to_vec()).unwrap(); 39 | 40 | #[allow(unsafe_code)] 41 | let ptr = unsafe { dlsym(RTLD_NEXT, symbol.as_ptr()) }; 42 | 43 | !ptr.is_null() 44 | } 45 | 46 | lazy_static::lazy_static! { 47 | static ref EXISTS: bool = exists(); 48 | } 49 | 50 | /// Check to see if the marker has been optimized away or not. 51 | pub fn marker_exists() -> bool { 52 | *EXISTS 53 | } 54 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # paranoia 2 | 3 | How it works: 4 | 5 | 1. The `paranoia-sys` crate is a cdylib that exports this function using dynamic linking: 6 | ```rust 7 | #[no_mangle] 8 | #[inline(never)] 9 | pub extern "C" fn ___paranoia_present() { 10 | #[allow(unsafe_code)] 11 | unsafe { 12 | std::ptr::read_volatile(&()); 13 | } 14 | } 15 | ``` 16 | 2. The `paranoia-caller` crate dynamically links against that code: 17 | ```rust 18 | #[link(name = "paranoia_sys")] 19 | extern "C" { 20 | fn ___paranoia_present(); 21 | } 22 | 23 | /// Mark a branch that can be later tested to see if it 24 | /// was compiled away or not. 25 | pub fn mark() { 26 | unsafe { 27 | ___paranoia_present(); 28 | } 29 | } 30 | ``` 31 | 3. The `paranoia` crate uses dlsym to check at runtime if the symbol is present: 32 | ```rust 33 | fn exists() -> bool { 34 | let symbol = CString::new(b"___paranoia_present".to_vec()).unwrap(); 35 | 36 | let ptr = unsafe { dlsym(RTLD_NEXT, symbol.as_ptr()) }; 37 | 38 | !ptr.is_null() 39 | } 40 | ``` 41 | 42 | So, we can use this to essentially access information about what compilation did or did not optimize away at runtime. This is handy for avoiding compensatory effort that only needs to be spent when certain functionality is invoked, but you don't want to use cargo features for whatever reason. 43 | 44 | Note that this doesn't work quite as intended for the time being - we need to use a build.rs similar to other `*-sys` crates that build shared objects and uses them to link against, and right now this just serves as a simple proof of concept that you can do this kind of thing at all. 45 | --------------------------------------------------------------------------------