]>
Commit | Line | Data |
---|---|---|
1 | //! Runtime support needed for testing the stdarch crate. | |
2 | //! | |
3 | //! This basically just disassembles the current executable and then parses the | |
4 | //! output once globally and then provides the `assert` function which makes | |
5 | //! assertions about the disassembly of a function. | |
6 | #![deny(rust_2018_idioms)] | |
7 | #![allow(clippy::missing_docs_in_private_items, clippy::print_stdout)] | |
8 | ||
9 | #[macro_use] | |
10 | extern crate lazy_static; | |
11 | #[macro_use] | |
12 | extern crate cfg_if; | |
13 | ||
14 | pub use assert_instr_macro::*; | |
15 | pub use simd_test_macro::*; | |
16 | use std::{cmp, collections::HashSet, env, hash, hint::black_box, str}; | |
17 | ||
18 | cfg_if! { | |
19 | if #[cfg(target_arch = "wasm32")] { | |
20 | pub mod wasm; | |
21 | use wasm::disassemble_myself; | |
22 | } else { | |
23 | mod disassembly; | |
24 | use crate::disassembly::disassemble_myself; | |
25 | } | |
26 | } | |
27 | ||
28 | lazy_static! { | |
29 | static ref DISASSEMBLY: HashSet<Function> = disassemble_myself(); | |
30 | } | |
31 | ||
32 | #[derive(Debug)] | |
33 | struct Function { | |
34 | name: String, | |
35 | instrs: Vec<String>, | |
36 | } | |
37 | impl Function { | |
38 | fn new(n: &str) -> Self { | |
39 | Self { | |
40 | name: n.to_string(), | |
41 | instrs: Vec::new(), | |
42 | } | |
43 | } | |
44 | } | |
45 | ||
46 | impl cmp::PartialEq for Function { | |
47 | fn eq(&self, other: &Self) -> bool { | |
48 | self.name == other.name | |
49 | } | |
50 | } | |
51 | impl cmp::Eq for Function {} | |
52 | ||
53 | impl hash::Hash for Function { | |
54 | fn hash<H: hash::Hasher>(&self, state: &mut H) { | |
55 | self.name.hash(state) | |
56 | } | |
57 | } | |
58 | ||
59 | /// Main entry point for this crate, called by the `#[assert_instr]` macro. | |
60 | /// | |
61 | /// This asserts that the function at `fnptr` contains the instruction | |
62 | /// `expected` provided. | |
63 | pub fn assert(shim_addr: usize, fnname: &str, expected: &str) { | |
64 | // Make sure that the shim is not removed | |
65 | black_box(shim_addr); | |
66 | ||
67 | //eprintln!("shim name: {fnname}"); | |
68 | let function = &DISASSEMBLY | |
69 | .get(&Function::new(fnname)) | |
70 | .unwrap_or_else(|| panic!("function \"{fnname}\" not found in the disassembly")); | |
71 | //eprintln!(" function: {:?}", function); | |
72 | ||
73 | let mut instrs = &function.instrs[..]; | |
74 | while instrs.last().map_or(false, |s| s == "nop") { | |
75 | instrs = &instrs[..instrs.len() - 1]; | |
76 | } | |
77 | ||
78 | // Look for `expected` as the first part of any instruction in this | |
79 | // function, e.g., tzcntl in tzcntl %rax,%rax. | |
80 | // | |
81 | // There are two cases when the expected instruction is nop: | |
82 | // 1. The expected intrinsic is compiled away so we can't | |
83 | // check for it - aka the intrinsic is not generating any code. | |
84 | // 2. It is a mark, indicating that the instruction will be | |
85 | // compiled into other instructions - mainly because of llvm | |
86 | // optimization. | |
87 | let found = expected == "nop" || instrs.iter().any(|s| s.starts_with(expected)); | |
88 | ||
89 | // Look for subroutine call instructions in the disassembly to detect whether | |
90 | // inlining failed: all intrinsics are `#[inline(always)]`, so calling one | |
91 | // intrinsic from another should not generate subroutine call instructions. | |
92 | let inlining_failed = if cfg!(target_arch = "x86_64") || cfg!(target_arch = "wasm32") { | |
93 | instrs.iter().any(|s| s.starts_with("call ")) | |
94 | } else if cfg!(target_arch = "x86") { | |
95 | instrs.windows(2).any(|s| { | |
96 | // On 32-bit x86 position independent code will call itself and be | |
97 | // immediately followed by a `pop` to learn about the current address. | |
98 | // Let's not take that into account when considering whether a function | |
99 | // failed inlining something. | |
100 | s[0].starts_with("call ") && s[1].starts_with("pop") // FIXME: original logic but does not match comment | |
101 | }) | |
102 | } else if cfg!(any(target_arch = "aarch64", target_arch = "arm64ec")) { | |
103 | instrs.iter().any(|s| s.starts_with("bl ")) | |
104 | } else { | |
105 | // FIXME: Add detection for other archs | |
106 | false | |
107 | }; | |
108 | ||
109 | let instruction_limit = std::env::var("STDARCH_ASSERT_INSTR_LIMIT") | |
110 | .ok() | |
111 | .map_or_else( | |
112 | || match expected { | |
113 | // `cpuid` returns a pretty big aggregate structure, so exempt | |
114 | // it from the slightly more restrictive 22 instructions below. | |
115 | "cpuid" => 30, | |
116 | ||
117 | // Apparently, on Windows, LLVM generates a bunch of | |
118 | // saves/restores of xmm registers around these instructions, | |
119 | // which exceeds the limit of 20 below. As it seems dictated by | |
120 | // Windows's ABI (I believe?), we probably can't do much | |
121 | // about it. | |
122 | "vzeroall" | "vzeroupper" if cfg!(windows) => 30, | |
123 | ||
124 | // Intrinsics using `cvtpi2ps` are typically "composites" and | |
125 | // in some cases exceed the limit. | |
126 | "cvtpi2ps" => 25, | |
127 | // core_arch/src/arm_shared/simd32 | |
128 | // vfmaq_n_f32_vfma : #instructions = 26 >= 22 (limit) | |
129 | "usad8" | "vfma" | "vfms" => 27, | |
130 | "qadd8" | "qsub8" | "sadd8" | "sel" | "shadd8" | "shsub8" | "usub8" | "ssub8" => 29, | |
131 | // core_arch/src/arm_shared/simd32 | |
132 | // vst1q_s64_x4_vst1 : #instructions = 27 >= 22 (limit) | |
133 | "vld3" => 28, | |
134 | // core_arch/src/arm_shared/simd32 | |
135 | // vld4q_lane_u32_vld4 : #instructions = 36 >= 22 (limit) | |
136 | "vld4" => 37, | |
137 | // core_arch/src/arm_shared/simd32 | |
138 | // vst1q_s64_x4_vst1 : #instructions = 40 >= 22 (limit) | |
139 | "vst1" => 41, | |
140 | // core_arch/src/arm_shared/simd32 | |
141 | // vst3q_u32_vst3 : #instructions = 25 >= 22 (limit) | |
142 | "vst3" => 26, | |
143 | // core_arch/src/arm_shared/simd32 | |
144 | // vst4q_u32_vst4 : #instructions = 33 >= 22 (limit) | |
145 | "vst4" => 34, | |
146 | ||
147 | // core_arch/src/arm_shared/simd32 | |
148 | // vst1q_p64_x4_nop : #instructions = 33 >= 22 (limit) | |
149 | "nop" if fnname.contains("vst1q_p64") => 34, | |
150 | ||
151 | // Original limit was 20 instructions, but ARM DSP Intrinsics | |
152 | // are exactly 20 instructions long. So, bump the limit to 22 | |
153 | // instead of adding here a long list of exceptions. | |
154 | _ => 22, | |
155 | }, | |
156 | |v| v.parse().unwrap(), | |
157 | ); | |
158 | let probably_only_one_instruction = instrs.len() < instruction_limit; | |
159 | ||
160 | if found && probably_only_one_instruction && !inlining_failed { | |
161 | return; | |
162 | } | |
163 | ||
164 | // Help debug by printing out the found disassembly, and then panic as we | |
165 | // didn't find the instruction. | |
166 | println!("disassembly for {fnname}: ",); | |
167 | for (i, instr) in instrs.iter().enumerate() { | |
168 | println!("\t{i:2}: {instr}"); | |
169 | } | |
170 | ||
171 | if !found { | |
172 | panic!( | |
173 | "failed to find instruction `{}` in the disassembly", | |
174 | expected | |
175 | ); | |
176 | } else if !probably_only_one_instruction { | |
177 | panic!( | |
178 | "instruction found, but the disassembly contains too many \ | |
179 | instructions: #instructions = {} >= {} (limit)", | |
180 | instrs.len(), | |
181 | instruction_limit | |
182 | ); | |
183 | } else if inlining_failed { | |
184 | panic!( | |
185 | "instruction found, but the disassembly contains subroutine \ | |
186 | call instructions, which hint that inlining failed" | |
187 | ); | |
188 | } | |
189 | } | |
190 | ||
191 | pub fn assert_skip_test_ok(name: &str, missing_features: &[&str]) { | |
192 | println!("Skipping test `{name}` due to missing target features:"); | |
193 | for feature in missing_features { | |
194 | println!(" - {feature}"); | |
195 | } | |
196 | match env::var("STDARCH_TEST_EVERYTHING") { | |
197 | Ok(_) => panic!("skipped test `{name}` when it shouldn't be skipped"), | |
198 | Err(_) => println!("Set STDARCH_TEST_EVERYTHING to make this an error."), | |
199 | } | |
200 | } | |
201 | ||
202 | // See comment in `assert-instr-macro` crate for why this exists | |
203 | pub static mut _DONT_DEDUP: *const u8 = std::ptr::null(); |