]> git.proxmox.com Git - rustc.git/blob - library/stdarch/crates/stdarch-test/src/lib.rs
New upstream version 1.70.0+dfsg1
[rustc.git] / library / stdarch / crates / stdarch-test / src / lib.rs
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!(target_arch = "aarch64") {
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 = 22 >= 22 (limit)
133 "vld3" => 23,
134 // core_arch/src/arm_shared/simd32
135 // vld4q_lane_u32_vld4 : #instructions = 31 >= 22 (limit)
136 "vld4" => 32,
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 // vst4q_u32_vst4 : #instructions = 26 >= 22 (limit)
142 "vst4" => 27,
143
144 // Temporary, currently the fptosi.sat and fptoui.sat LLVM
145 // intrinsics emit unnecessary code on arm. This can be
146 // removed once it has been addressed in LLVM.
147 "fcvtzu" | "fcvtzs" | "vcvt" => 64,
148
149 // core_arch/src/arm_shared/simd32
150 // vst1q_p64_x4_nop : #instructions = 33 >= 22 (limit)
151 "nop" if fnname.contains("vst1q_p64") => 34,
152
153 // Original limit was 20 instructions, but ARM DSP Intrinsics
154 // are exactly 20 instructions long. So, bump the limit to 22
155 // instead of adding here a long list of exceptions.
156 _ => 22,
157 },
158 |v| v.parse().unwrap(),
159 );
160 let probably_only_one_instruction = instrs.len() < instruction_limit;
161
162 if found && probably_only_one_instruction && !inlining_failed {
163 return;
164 }
165
166 // Help debug by printing out the found disassembly, and then panic as we
167 // didn't find the instruction.
168 println!("disassembly for {fnname}: ",);
169 for (i, instr) in instrs.iter().enumerate() {
170 println!("\t{i:2}: {instr}");
171 }
172
173 if !found {
174 panic!(
175 "failed to find instruction `{}` in the disassembly",
176 expected
177 );
178 } else if !probably_only_one_instruction {
179 panic!(
180 "instruction found, but the disassembly contains too many \
181 instructions: #instructions = {} >= {} (limit)",
182 instrs.len(),
183 instruction_limit
184 );
185 } else if inlining_failed {
186 panic!(
187 "instruction found, but the disassembly contains subroutine \
188 call instructions, which hint that inlining failed"
189 );
190 }
191 }
192
193 pub fn assert_skip_test_ok(name: &str) {
194 if env::var("STDARCH_TEST_EVERYTHING").is_err() {
195 return;
196 }
197 panic!("skipped test `{name}` when it shouldn't be skipped");
198 }
199
200 // See comment in `assert-instr-macro` crate for why this exists
201 pub static mut _DONT_DEDUP: *const u8 = std::ptr::null();