1 //! A verifier for ensuring that functions are well formed.
2 //! It verifies:
3 //!
4 //! block integrity
5 //!
6 //! - All instructions reached from the `block_insts` iterator must belong to
7 //!   the block as reported by `inst_block()`.
8 //! - Every block must end in a terminator instruction, and no other instruction
9 //!   can be a terminator.
10 //! - Every value in the `block_params` iterator belongs to the block as reported by `value_block`.
11 //!
12 //! Instruction integrity
13 //!
14 //! - The instruction format must match the opcode.
15 //! - All result values must be created for multi-valued instructions.
16 //! - All referenced entities must exist. (Values, blocks, stack slots, ...)
17 //! - Instructions must not reference (eg. branch to) the entry block.
18 //!
19 //! SSA form
20 //!
21 //! - Values must be defined by an instruction that exists and that is inserted in
22 //!   a block, or be an argument of an existing block.
23 //! - Values used by an instruction must dominate the instruction.
24 //!
25 //! Control flow graph and dominator tree integrity:
26 //!
27 //! - All predecessors in the CFG must be branches to the block.
28 //! - All branches to a block must be present in the CFG.
29 //! - A recomputed dominator tree is identical to the existing one.
30 //! - The entry block must not be a cold block.
31 //!
32 //! Type checking
33 //!
34 //! - Compare input and output values against the opcode's type constraints.
35 //!   For polymorphic opcodes, determine the controlling type variable first.
36 //! - Branches and jumps must pass arguments to destination blocks that match the
37 //!   expected types exactly. The number of arguments must match.
38 //! - All blocks in a jump table must take no arguments.
39 //! - Function calls are type checked against their signature.
40 //! - The entry block must take arguments that match the signature of the current
41 //!   function.
42 //! - All return instructions must have return value operands matching the current
43 //!   function signature.
44 //!
45 //! Global values
46 //!
47 //! - Detect cycles in global values.
48 //! - Detect use of 'vmctx' global value when no corresponding parameter is defined.
49 //!
50 //! TODO:
51 //! Ad hoc checking
52 //!
53 //! - Stack slot loads and stores must be in-bounds.
54 //! - Immediate constraints for certain opcodes, like `udiv_imm v3, 0`.
55 //! - `Insertlane` and `extractlane` instructions have immediate lane numbers that must be in
56 //!   range for their polymorphic type.
57 //! - Swizzle and shuffle instructions take a variable number of lane arguments. The number
58 //!   of arguments must match the destination type, and the lane indexes must be in range.
59 
60 use self::flags::verify_flags;
61 use crate::dbg::DisplayList;
62 use crate::dominator_tree::DominatorTree;
63 use crate::entity::SparseSet;
64 use crate::flowgraph::{BlockPredecessor, ControlFlowGraph};
65 use crate::ir;
66 use crate::ir::entities::AnyEntity;
67 use crate::ir::instructions::{BranchInfo, CallInfo, InstructionFormat, ResolvedConstraint};
68 use crate::ir::{
69     types, ArgumentPurpose, Block, Constant, DynamicStackSlot, FuncRef, Function, GlobalValue,
70     Inst, JumpTable, Opcode, SigRef, StackSlot, Type, Value, ValueDef, ValueList,
71 };
72 use crate::isa::TargetIsa;
73 use crate::iterators::IteratorExtras;
74 use crate::print_errors::pretty_verifier_error;
75 use crate::settings::FlagsOrIsa;
76 use crate::timing;
77 use alloc::collections::BTreeSet;
78 use alloc::string::{String, ToString};
79 use alloc::vec::Vec;
80 use core::cmp::Ordering;
81 use core::fmt::{self, Display, Formatter};
82 
83 mod flags;
84 
85 /// A verifier error.
86 #[derive(Debug, PartialEq, Eq, Clone)]
87 pub struct VerifierError {
88     /// The entity causing the verifier error.
89     pub location: AnyEntity,
90     /// Optionally provide some context for the given location; e.g., for `inst42` provide
91     /// `Some("v3 = iconst.i32 0")` for more comprehensible errors.
92     pub context: Option<String>,
93     /// The error message.
94     pub message: String,
95 }
96 
97 // This is manually implementing Error and Display instead of using thiserror to reduce the amount
98 // of dependencies used by Cranelift.
99 impl std::error::Error for VerifierError {}
100 
101 impl Display for VerifierError {
102     fn fmt(&self, f: &mut Formatter) -> fmt::Result {
103         match &self.context {
104             None => write!(f, "{}: {}", self.location, self.message),
105             Some(context) => write!(f, "{} ({}): {}", self.location, context, self.message),
106         }
107     }
108 }
109 
110 /// Convenience converter for making error-reporting less verbose.
111 ///
112 /// Converts a tuple of `(location, context, message)` to a `VerifierError`.
113 /// ```
114 /// use cranelift_codegen::verifier::VerifierErrors;
115 /// use cranelift_codegen::ir::Inst;
116 /// let mut errors = VerifierErrors::new();
117 /// errors.report((Inst::from_u32(42), "v3 = iadd v1, v2", "iadd cannot be used with values of this type"));
118 /// // note the double parenthenses to use this syntax
119 /// ```
120 impl<L, C, M> From<(L, C, M)> for VerifierError
121 where
122     L: Into<AnyEntity>,
123     C: Into<String>,
124     M: Into<String>,
125 {
126     fn from(items: (L, C, M)) -> Self {
127         let (location, context, message) = items;
128         Self {
129             location: location.into(),
130             context: Some(context.into()),
131             message: message.into(),
132         }
133     }
134 }
135 
136 /// Convenience converter for making error-reporting less verbose.
137 ///
138 /// Same as above but without `context`.
139 impl<L, M> From<(L, M)> for VerifierError
140 where
141     L: Into<AnyEntity>,
142     M: Into<String>,
143 {
144     fn from(items: (L, M)) -> Self {
145         let (location, message) = items;
146         Self {
147             location: location.into(),
148             context: None,
149             message: message.into(),
150         }
151     }
152 }
153 
154 /// Result of a step in the verification process.
155 ///
156 /// Functions that return `VerifierStepResult<()>` should also take a
157 /// mutable reference to `VerifierErrors` as argument in order to report
158 /// errors.
159 ///
160 /// Here, `Ok` represents a step that **did not lead to a fatal error**,
161 /// meaning that the verification process may continue. However, other (non-fatal)
162 /// errors might have been reported through the previously mentioned `VerifierErrors`
163 /// argument.
164 pub type VerifierStepResult<T> = Result<T, ()>;
165 
166 /// Result of a verification operation.
167 ///
168 /// Unlike `VerifierStepResult<()>` which may be `Ok` while still having reported
169 /// errors, this type always returns `Err` if an error (fatal or not) was reported.
170 pub type VerifierResult<T> = Result<T, VerifierErrors>;
171 
172 /// List of verifier errors.
173 #[derive(Debug, Default, PartialEq, Eq, Clone)]
174 pub struct VerifierErrors(pub Vec<VerifierError>);
175 
176 // This is manually implementing Error and Display instead of using thiserror to reduce the amount
177 // of dependencies used by Cranelift.
178 impl std::error::Error for VerifierErrors {}
179 
180 impl VerifierErrors {
181     /// Return a new `VerifierErrors` struct.
182     #[inline]
183     pub fn new() -> Self {
184         Self(Vec::new())
185     }
186 
187     /// Return whether no errors were reported.
188     #[inline]
189     pub fn is_empty(&self) -> bool {
190         self.0.is_empty()
191     }
192 
193     /// Return whether one or more errors were reported.
194     #[inline]
195     pub fn has_error(&self) -> bool {
196         !self.0.is_empty()
197     }
198 
199     /// Return a `VerifierStepResult` that is fatal if at least one error was reported,
200     /// and non-fatal otherwise.
201     #[inline]
202     pub fn as_result(&self) -> VerifierStepResult<()> {
203         if self.is_empty() {
204             Ok(())
205         } else {
206             Err(())
207         }
208     }
209 
210     /// Report an error, adding it to the list of errors.
211     pub fn report(&mut self, error: impl Into<VerifierError>) {
212         self.0.push(error.into());
213     }
214 
215     /// Report a fatal error and return `Err`.
216     pub fn fatal(&mut self, error: impl Into<VerifierError>) -> VerifierStepResult<()> {
217         self.report(error);
218         Err(())
219     }
220 
221     /// Report a non-fatal error and return `Ok`.
222     pub fn nonfatal(&mut self, error: impl Into<VerifierError>) -> VerifierStepResult<()> {
223         self.report(error);
224         Ok(())
225     }
226 }
227 
228 impl From<Vec<VerifierError>> for VerifierErrors {
229     fn from(v: Vec<VerifierError>) -> Self {
230         Self(v)
231     }
232 }
233 
234 impl Into<Vec<VerifierError>> for VerifierErrors {
235     fn into(self) -> Vec<VerifierError> {
236         self.0
237     }
238 }
239 
240 impl Into<VerifierResult<()>> for VerifierErrors {
241     fn into(self) -> VerifierResult<()> {
242         if self.is_empty() {
243             Ok(())
244         } else {
245             Err(self)
246         }
247     }
248 }
249 
250 impl Display for VerifierErrors {
251     fn fmt(&self, f: &mut Formatter) -> fmt::Result {
252         for err in &self.0 {
253             writeln!(f, "- {}", err)?;
254         }
255         Ok(())
256     }
257 }
258 
259 /// Verify `func`.
260 pub fn verify_function<'a, FOI: Into<FlagsOrIsa<'a>>>(
261     func: &Function,
262     fisa: FOI,
263 ) -> VerifierResult<()> {
264     let _tt = timing::verifier();
265     let mut errors = VerifierErrors::default();
266     let verifier = Verifier::new(func, fisa.into());
267     let result = verifier.run(&mut errors);
268     if errors.is_empty() {
269         result.unwrap();
270         Ok(())
271     } else {
272         Err(errors)
273     }
274 }
275 
276 /// Verify `func` after checking the integrity of associated context data structures `cfg` and
277 /// `domtree`.
278 pub fn verify_context<'a, FOI: Into<FlagsOrIsa<'a>>>(
279     func: &Function,
280     cfg: &ControlFlowGraph,
281     domtree: &DominatorTree,
282     fisa: FOI,
283     errors: &mut VerifierErrors,
284 ) -> VerifierStepResult<()> {
285     let _tt = timing::verifier();
286     let verifier = Verifier::new(func, fisa.into());
287     if cfg.is_valid() {
288         verifier.cfg_integrity(cfg, errors)?;
289     }
290     if domtree.is_valid() {
291         verifier.domtree_integrity(domtree, errors)?;
292     }
293     verifier.run(errors)
294 }
295 
296 struct Verifier<'a> {
297     func: &'a Function,
298     expected_cfg: ControlFlowGraph,
299     expected_domtree: DominatorTree,
300     isa: Option<&'a dyn TargetIsa>,
301 }
302 
303 impl<'a> Verifier<'a> {
304     pub fn new(func: &'a Function, fisa: FlagsOrIsa<'a>) -> Self {
305         let expected_cfg = ControlFlowGraph::with_function(func);
306         let expected_domtree = DominatorTree::with_function(func, &expected_cfg);
307         Self {
308             func,
309             expected_cfg,
310             expected_domtree,
311             isa: fisa.isa,
312         }
313     }
314 
315     /// Determine a contextual error string for an instruction.
316     #[inline]
317     fn context(&self, inst: Inst) -> String {
318         self.func.dfg.display_inst(inst).to_string()
319     }
320 
321     // Check for:
322     //  - cycles in the global value declarations.
323     //  - use of 'vmctx' when no special parameter declares it.
324     fn verify_global_values(&self, errors: &mut VerifierErrors) -> VerifierStepResult<()> {
325         let mut cycle_seen = false;
326         let mut seen = SparseSet::new();
327 
328         'gvs: for gv in self.func.global_values.keys() {
329             seen.clear();
330             seen.insert(gv);
331 
332             let mut cur = gv;
333             loop {
334                 match self.func.global_values[cur] {
335                     ir::GlobalValueData::Load { base, .. }
336                     | ir::GlobalValueData::IAddImm { base, .. } => {
337                         if seen.insert(base).is_some() {
338                             if !cycle_seen {
339                                 errors.report((
340                                     gv,
341                                     format!("global value cycle: {}", DisplayList(seen.as_slice())),
342                                 ));
343                                 // ensures we don't report the cycle multiple times
344                                 cycle_seen = true;
345                             }
346                             continue 'gvs;
347                         }
348 
349                         cur = base;
350                     }
351                     _ => break,
352                 }
353             }
354 
355             match self.func.global_values[gv] {
356                 ir::GlobalValueData::VMContext { .. } => {
357                     if self
358                         .func
359                         .special_param(ir::ArgumentPurpose::VMContext)
360                         .is_none()
361                     {
362                         errors.report((gv, format!("undeclared vmctx reference {}", gv)));
363                     }
364                 }
365                 ir::GlobalValueData::IAddImm {
366                     base, global_type, ..
367                 } => {
368                     if !global_type.is_int() {
369                         errors.report((
370                             gv,
371                             format!("iadd_imm global value with non-int type {}", global_type),
372                         ));
373                     } else if let Some(isa) = self.isa {
374                         let base_type = self.func.global_values[base].global_type(isa);
375                         if global_type != base_type {
376                             errors.report((
377                                 gv,
378                                 format!(
379                                     "iadd_imm type {} differs from operand type {}",
380                                     global_type, base_type
381                                 ),
382                             ));
383                         }
384                     }
385                 }
386                 ir::GlobalValueData::Load { base, .. } => {
387                     if let Some(isa) = self.isa {
388                         let base_type = self.func.global_values[base].global_type(isa);
389                         let pointer_type = isa.pointer_type();
390                         if base_type != pointer_type {
391                             errors.report((
392                                 gv,
393                                 format!(
394                                     "base {} has type {}, which is not the pointer type {}",
395                                     base, base_type, pointer_type
396                                 ),
397                             ));
398                         }
399                     }
400                 }
401                 _ => {}
402             }
403         }
404 
405         // Invalid global values shouldn't stop us from verifying the rest of the function
406         Ok(())
407     }
408 
409     fn verify_heaps(&self, errors: &mut VerifierErrors) -> VerifierStepResult<()> {
410         if let Some(isa) = self.isa {
411             for (heap, heap_data) in &self.func.heaps {
412                 let base = heap_data.base;
413                 if !self.func.global_values.is_valid(base) {
414                     return errors.nonfatal((heap, format!("invalid base global value {}", base)));
415                 }
416 
417                 let pointer_type = isa.pointer_type();
418                 let base_type = self.func.global_values[base].global_type(isa);
419                 if base_type != pointer_type {
420                     errors.report((
421                         heap,
422                         format!(
423                             "heap base has type {}, which is not the pointer type {}",
424                             base_type, pointer_type
425                         ),
426                     ));
427                 }
428 
429                 if let ir::HeapStyle::Dynamic { bound_gv, .. } = heap_data.style {
430                     if !self.func.global_values.is_valid(bound_gv) {
431                         return errors
432                             .nonfatal((heap, format!("invalid bound global value {}", bound_gv)));
433                     }
434 
435                     let bound_type = self.func.global_values[bound_gv].global_type(isa);
436                     if pointer_type != bound_type {
437                         errors.report((
438                             heap,
439                             format!(
440                                 "heap pointer type {} differs from the type of its bound, {}",
441                                 pointer_type, bound_type
442                             ),
443                         ));
444                     }
445                 }
446             }
447         }
448 
449         Ok(())
450     }
451 
452     fn verify_tables(&self, errors: &mut VerifierErrors) -> VerifierStepResult<()> {
453         if let Some(isa) = self.isa {
454             for (table, table_data) in &self.func.tables {
455                 let base = table_data.base_gv;
456                 if !self.func.global_values.is_valid(base) {
457                     return errors.nonfatal((table, format!("invalid base global value {}", base)));
458                 }
459 
460                 let pointer_type = isa.pointer_type();
461                 let base_type = self.func.global_values[base].global_type(isa);
462                 if base_type != pointer_type {
463                     errors.report((
464                         table,
465                         format!(
466                             "table base has type {}, which is not the pointer type {}",
467                             base_type, pointer_type
468                         ),
469                     ));
470                 }
471 
472                 let bound_gv = table_data.bound_gv;
473                 if !self.func.global_values.is_valid(bound_gv) {
474                     return errors
475                         .nonfatal((table, format!("invalid bound global value {}", bound_gv)));
476                 }
477 
478                 let index_type = table_data.index_type;
479                 let bound_type = self.func.global_values[bound_gv].global_type(isa);
480                 if index_type != bound_type {
481                     errors.report((
482                         table,
483                         format!(
484                             "table index type {} differs from the type of its bound, {}",
485                             index_type, bound_type
486                         ),
487                     ));
488                 }
489             }
490         }
491 
492         Ok(())
493     }
494 
495     fn verify_jump_tables(&self, errors: &mut VerifierErrors) -> VerifierStepResult<()> {
496         for (jt, jt_data) in &self.func.jump_tables {
497             for &block in jt_data.iter() {
498                 self.verify_block(jt, block, errors)?;
499             }
500         }
501         Ok(())
502     }
503 
504     /// Check that the given block can be encoded as a BB, by checking that only
505     /// branching instructions are ending the block.
506     fn encodable_as_bb(&self, block: Block, errors: &mut VerifierErrors) -> VerifierStepResult<()> {
507         match self.func.is_block_basic(block) {
508             Ok(()) => Ok(()),
509             Err((inst, message)) => errors.fatal((inst, self.context(inst), message)),
510         }
511     }
512 
513     fn block_integrity(
514         &self,
515         block: Block,
516         inst: Inst,
517         errors: &mut VerifierErrors,
518     ) -> VerifierStepResult<()> {
519         let is_terminator = self.func.dfg[inst].opcode().is_terminator();
520         let is_last_inst = self.func.layout.last_inst(block) == Some(inst);
521 
522         if is_terminator && !is_last_inst {
523             // Terminating instructions only occur at the end of blocks.
524             return errors.fatal((
525                 inst,
526                 self.context(inst),
527                 format!(
528                     "a terminator instruction was encountered before the end of {}",
529                     block
530                 ),
531             ));
532         }
533         if is_last_inst && !is_terminator {
534             return errors.fatal((block, "block does not end in a terminator instruction"));
535         }
536 
537         // Instructions belong to the correct block.
538         let inst_block = self.func.layout.inst_block(inst);
539         if inst_block != Some(block) {
540             return errors.fatal((
541                 inst,
542                 self.context(inst),
543                 format!("should belong to {} not {:?}", block, inst_block),
544             ));
545         }
546 
547         // Parameters belong to the correct block.
548         for &arg in self.func.dfg.block_params(block) {
549             match self.func.dfg.value_def(arg) {
550                 ValueDef::Param(arg_block, _) => {
551                     if block != arg_block {
552                         return errors.fatal((arg, format!("does not belong to {}", block)));
553                     }
554                 }
555                 _ => {
556                     return errors.fatal((arg, "expected an argument, found a result"));
557                 }
558             }
559         }
560 
561         Ok(())
562     }
563 
564     fn instruction_integrity(
565         &self,
566         inst: Inst,
567         errors: &mut VerifierErrors,
568     ) -> VerifierStepResult<()> {
569         let inst_data = &self.func.dfg[inst];
570         let dfg = &self.func.dfg;
571 
572         // The instruction format matches the opcode
573         if inst_data.opcode().format() != InstructionFormat::from(inst_data) {
574             return errors.fatal((
575                 inst,
576                 self.context(inst),
577                 "instruction opcode doesn't match instruction format",
578             ));
579         }
580 
581         let num_fixed_results = inst_data.opcode().constraints().num_fixed_results();
582         // var_results is 0 if we aren't a call instruction
583         let var_results = dfg
584             .call_signature(inst)
585             .map_or(0, |sig| dfg.signatures[sig].returns.len());
586         let total_results = num_fixed_results + var_results;
587 
588         // All result values for multi-valued instructions are created
589         let got_results = dfg.inst_results(inst).len();
590         if got_results != total_results {
591             return errors.fatal((
592                 inst,
593                 self.context(inst),
594                 format!(
595                     "expected {} result values, found {}",
596                     total_results, got_results,
597                 ),
598             ));
599         }
600 
601         self.verify_entity_references(inst, errors)
602     }
603 
604     fn verify_entity_references(
605         &self,
606         inst: Inst,
607         errors: &mut VerifierErrors,
608     ) -> VerifierStepResult<()> {
609         use crate::ir::instructions::InstructionData::*;
610 
611         for &arg in self.func.dfg.inst_args(inst) {
612             self.verify_inst_arg(inst, arg, errors)?;
613 
614             // All used values must be attached to something.
615             let original = self.func.dfg.resolve_aliases(arg);
616             if !self.func.dfg.value_is_attached(original) {
617                 errors.report((
618                     inst,
619                     self.context(inst),
620                     format!("argument {} -> {} is not attached", arg, original),
621                 ));
622             }
623         }
624 
625         for &res in self.func.dfg.inst_results(inst) {
626             self.verify_inst_result(inst, res, errors)?;
627         }
628 
629         match self.func.dfg[inst] {
630             MultiAry { ref args, .. } => {
631                 self.verify_value_list(inst, args, errors)?;
632             }
633             Jump {
634                 destination,
635                 ref args,
636                 ..
637             }
638             | Branch {
639                 destination,
640                 ref args,
641                 ..
642             }
643             | BranchInt {
644                 destination,
645                 ref args,
646                 ..
647             }
648             | BranchFloat {
649                 destination,
650                 ref args,
651                 ..
652             }
653             | BranchIcmp {
654                 destination,
655                 ref args,
656                 ..
657             } => {
658                 self.verify_block(inst, destination, errors)?;
659                 self.verify_value_list(inst, args, errors)?;
660             }
661             BranchTable {
662                 table, destination, ..
663             } => {
664                 self.verify_block(inst, destination, errors)?;
665                 self.verify_jump_table(inst, table, errors)?;
666             }
667             Call {
668                 func_ref, ref args, ..
669             } => {
670                 self.verify_func_ref(inst, func_ref, errors)?;
671                 self.verify_value_list(inst, args, errors)?;
672             }
673             CallIndirect {
674                 sig_ref, ref args, ..
675             } => {
676                 self.verify_sig_ref(inst, sig_ref, errors)?;
677                 self.verify_value_list(inst, args, errors)?;
678             }
679             FuncAddr { func_ref, .. } => {
680                 self.verify_func_ref(inst, func_ref, errors)?;
681             }
682             StackLoad { stack_slot, .. } | StackStore { stack_slot, .. } => {
683                 self.verify_stack_slot(inst, stack_slot, errors)?;
684             }
685             DynamicStackLoad {
686                 dynamic_stack_slot, ..
687             }
688             | DynamicStackStore {
689                 dynamic_stack_slot, ..
690             } => {
691                 self.verify_dynamic_stack_slot(inst, dynamic_stack_slot, errors)?;
692             }
693             UnaryGlobalValue { global_value, .. } => {
694                 self.verify_global_value(inst, global_value, errors)?;
695             }
696             HeapAddr { heap, .. } => {
697                 self.verify_heap(inst, heap, errors)?;
698             }
699             TableAddr { table, .. } => {
700                 self.verify_table(inst, table, errors)?;
701             }
702             NullAry {
703                 opcode: Opcode::GetPinnedReg,
704             }
705             | Unary {
706                 opcode: Opcode::SetPinnedReg,
707                 ..
708             } => {
709                 if let Some(isa) = &self.isa {
710                     if !isa.flags().enable_pinned_reg() {
711                         return errors.fatal((
712                             inst,
713                             self.context(inst),
714                             "GetPinnedReg/SetPinnedReg cannot be used without enable_pinned_reg",
715                         ));
716                     }
717                 } else {
718                     return errors.fatal((
719                         inst,
720                         self.context(inst),
721                         "GetPinnedReg/SetPinnedReg need an ISA!",
722                     ));
723                 }
724             }
725             NullAry {
726                 opcode: Opcode::GetFramePointer | Opcode::GetReturnAddress,
727             } => {
728                 if let Some(isa) = &self.isa {
729                     // Backends may already rely on this check implicitly, so do
730                     // not relax it without verifying that it is safe to do so.
731                     if !isa.flags().preserve_frame_pointers() {
732                         return errors.fatal((
733                             inst,
734                             self.context(inst),
735                             "`get_frame_pointer`/`get_return_address` cannot be used without \
736                              enabling `preserve_frame_pointers`",
737                         ));
738                     }
739                 } else {
740                     return errors.fatal((
741                         inst,
742                         self.context(inst),
743                         "`get_frame_pointer`/`get_return_address` require an ISA!",
744                     ));
745                 }
746             }
747             Unary {
748                 opcode: Opcode::Bitcast,
749                 arg,
750             } => {
751                 self.verify_bitcast(inst, arg, errors)?;
752             }
753             UnaryConst {
754                 opcode: Opcode::Vconst,
755                 constant_handle,
756                 ..
757             } => {
758                 self.verify_constant_size(inst, constant_handle, errors)?;
759             }
760 
761             // Exhaustive list so we can't forget to add new formats
762             AtomicCas { .. }
763             | AtomicRmw { .. }
764             | LoadNoOffset { .. }
765             | StoreNoOffset { .. }
766             | Unary { .. }
767             | UnaryConst { .. }
768             | UnaryImm { .. }
769             | UnaryIeee32 { .. }
770             | UnaryIeee64 { .. }
771             | UnaryBool { .. }
772             | Binary { .. }
773             | BinaryImm8 { .. }
774             | BinaryImm64 { .. }
775             | Ternary { .. }
776             | TernaryImm8 { .. }
777             | Shuffle { .. }
778             | IntCompare { .. }
779             | IntCompareImm { .. }
780             | IntCond { .. }
781             | FloatCompare { .. }
782             | FloatCond { .. }
783             | IntSelect { .. }
784             | Load { .. }
785             | Store { .. }
786             | Trap { .. }
787             | CondTrap { .. }
788             | IntCondTrap { .. }
789             | FloatCondTrap { .. }
790             | NullAry { .. } => {}
791         }
792 
793         Ok(())
794     }
795 
796     fn verify_block(
797         &self,
798         loc: impl Into<AnyEntity>,
799         e: Block,
800         errors: &mut VerifierErrors,
801     ) -> VerifierStepResult<()> {
802         if !self.func.dfg.block_is_valid(e) || !self.func.layout.is_block_inserted(e) {
803             return errors.fatal((loc, format!("invalid block reference {}", e)));
804         }
805         if let Some(entry_block) = self.func.layout.entry_block() {
806             if e == entry_block {
807                 return errors.fatal((loc, format!("invalid reference to entry block {}", e)));
808             }
809         }
810         Ok(())
811     }
812 
813     fn verify_sig_ref(
814         &self,
815         inst: Inst,
816         s: SigRef,
817         errors: &mut VerifierErrors,
818     ) -> VerifierStepResult<()> {
819         if !self.func.dfg.signatures.is_valid(s) {
820             errors.fatal((
821                 inst,
822                 self.context(inst),
823                 format!("invalid signature reference {}", s),
824             ))
825         } else {
826             Ok(())
827         }
828     }
829 
830     fn verify_func_ref(
831         &self,
832         inst: Inst,
833         f: FuncRef,
834         errors: &mut VerifierErrors,
835     ) -> VerifierStepResult<()> {
836         if !self.func.dfg.ext_funcs.is_valid(f) {
837             errors.nonfatal((
838                 inst,
839                 self.context(inst),
840                 format!("invalid function reference {}", f),
841             ))
842         } else {
843             Ok(())
844         }
845     }
846 
847     fn verify_stack_slot(
848         &self,
849         inst: Inst,
850         ss: StackSlot,
851         errors: &mut VerifierErrors,
852     ) -> VerifierStepResult<()> {
853         if !self.func.sized_stack_slots.is_valid(ss) {
854             errors.nonfatal((
855                 inst,
856                 self.context(inst),
857                 format!("invalid stack slot {}", ss),
858             ))
859         } else {
860             Ok(())
861         }
862     }
863 
864     fn verify_dynamic_stack_slot(
865         &self,
866         inst: Inst,
867         ss: DynamicStackSlot,
868         errors: &mut VerifierErrors,
869     ) -> VerifierStepResult<()> {
870         if !self.func.dynamic_stack_slots.is_valid(ss) {
871             errors.nonfatal((
872                 inst,
873                 self.context(inst),
874                 format!("invalid dynamic stack slot {}", ss),
875             ))
876         } else {
877             Ok(())
878         }
879     }
880 
881     fn verify_global_value(
882         &self,
883         inst: Inst,
884         gv: GlobalValue,
885         errors: &mut VerifierErrors,
886     ) -> VerifierStepResult<()> {
887         if !self.func.global_values.is_valid(gv) {
888             errors.nonfatal((
889                 inst,
890                 self.context(inst),
891                 format!("invalid global value {}", gv),
892             ))
893         } else {
894             Ok(())
895         }
896     }
897 
898     fn verify_heap(
899         &self,
900         inst: Inst,
901         heap: ir::Heap,
902         errors: &mut VerifierErrors,
903     ) -> VerifierStepResult<()> {
904         if !self.func.heaps.is_valid(heap) {
905             errors.nonfatal((inst, self.context(inst), format!("invalid heap {}", heap)))
906         } else {
907             Ok(())
908         }
909     }
910 
911     fn verify_table(
912         &self,
913         inst: Inst,
914         table: ir::Table,
915         errors: &mut VerifierErrors,
916     ) -> VerifierStepResult<()> {
917         if !self.func.tables.is_valid(table) {
918             errors.nonfatal((inst, self.context(inst), format!("invalid table {}", table)))
919         } else {
920             Ok(())
921         }
922     }
923 
924     fn verify_value_list(
925         &self,
926         inst: Inst,
927         l: &ValueList,
928         errors: &mut VerifierErrors,
929     ) -> VerifierStepResult<()> {
930         if !l.is_valid(&self.func.dfg.value_lists) {
931             errors.nonfatal((
932                 inst,
933                 self.context(inst),
934                 format!("invalid value list reference {:?}", l),
935             ))
936         } else {
937             Ok(())
938         }
939     }
940 
941     fn verify_jump_table(
942         &self,
943         inst: Inst,
944         j: JumpTable,
945         errors: &mut VerifierErrors,
946     ) -> VerifierStepResult<()> {
947         if !self.func.jump_tables.is_valid(j) {
948             errors.nonfatal((
949                 inst,
950                 self.context(inst),
951                 format!("invalid jump table reference {}", j),
952             ))
953         } else {
954             Ok(())
955         }
956     }
957 
958     fn verify_value(
959         &self,
960         loc_inst: Inst,
961         v: Value,
962         errors: &mut VerifierErrors,
963     ) -> VerifierStepResult<()> {
964         let dfg = &self.func.dfg;
965         if !dfg.value_is_valid(v) {
966             errors.nonfatal((
967                 loc_inst,
968                 self.context(loc_inst),
969                 format!("invalid value reference {}", v),
970             ))
971         } else {
972             Ok(())
973         }
974     }
975 
976     fn verify_inst_arg(
977         &self,
978         loc_inst: Inst,
979         v: Value,
980         errors: &mut VerifierErrors,
981     ) -> VerifierStepResult<()> {
982         self.verify_value(loc_inst, v, errors)?;
983 
984         let dfg = &self.func.dfg;
985         let loc_block = self.func.layout.pp_block(loc_inst);
986         let is_reachable = self.expected_domtree.is_reachable(loc_block);
987 
988         // SSA form
989         match dfg.value_def(v) {
990             ValueDef::Result(def_inst, _) => {
991                 // Value is defined by an instruction that exists.
992                 if !dfg.inst_is_valid(def_inst) {
993                     return errors.fatal((
994                         loc_inst,
995                         self.context(loc_inst),
996                         format!("{} is defined by invalid instruction {}", v, def_inst),
997                     ));
998                 }
999                 // Defining instruction is inserted in a block.
1000                 if self.func.layout.inst_block(def_inst) == None {
1001                     return errors.fatal((
1002                         loc_inst,
1003                         self.context(loc_inst),
1004                         format!("{} is defined by {} which has no block", v, def_inst),
1005                     ));
1006                 }
1007                 // Defining instruction dominates the instruction that uses the value.
1008                 if is_reachable {
1009                     if !self
1010                         .expected_domtree
1011                         .dominates(def_inst, loc_inst, &self.func.layout)
1012                     {
1013                         return errors.fatal((
1014                             loc_inst,
1015                             self.context(loc_inst),
1016                             format!("uses value {} from non-dominating {}", v, def_inst),
1017                         ));
1018                     }
1019                     if def_inst == loc_inst {
1020                         return errors.fatal((
1021                             loc_inst,
1022                             self.context(loc_inst),
1023                             format!("uses value {} from itself", v),
1024                         ));
1025                     }
1026                 }
1027             }
1028             ValueDef::Param(block, _) => {
1029                 // Value is defined by an existing block.
1030                 if !dfg.block_is_valid(block) {
1031                     return errors.fatal((
1032                         loc_inst,
1033                         self.context(loc_inst),
1034                         format!("{} is defined by invalid block {}", v, block),
1035                     ));
1036                 }
1037                 // Defining block is inserted in the layout
1038                 if !self.func.layout.is_block_inserted(block) {
1039                     return errors.fatal((
1040                         loc_inst,
1041                         self.context(loc_inst),
1042                         format!("{} is defined by {} which is not in the layout", v, block),
1043                     ));
1044                 }
1045                 // The defining block dominates the instruction using this value.
1046                 if is_reachable
1047                     && !self
1048                         .expected_domtree
1049                         .dominates(block, loc_inst, &self.func.layout)
1050                 {
1051                     return errors.fatal((
1052                         loc_inst,
1053                         self.context(loc_inst),
1054                         format!("uses value arg from non-dominating {}", block),
1055                     ));
1056                 }
1057             }
1058         }
1059         Ok(())
1060     }
1061 
1062     fn verify_inst_result(
1063         &self,
1064         loc_inst: Inst,
1065         v: Value,
1066         errors: &mut VerifierErrors,
1067     ) -> VerifierStepResult<()> {
1068         self.verify_value(loc_inst, v, errors)?;
1069 
1070         match self.func.dfg.value_def(v) {
1071             ValueDef::Result(def_inst, _) => {
1072                 if def_inst != loc_inst {
1073                     errors.fatal((
1074                         loc_inst,
1075                         self.context(loc_inst),
1076                         format!("instruction result {} is not defined by the instruction", v),
1077                     ))
1078                 } else {
1079                     Ok(())
1080                 }
1081             }
1082             ValueDef::Param(_, _) => errors.fatal((
1083                 loc_inst,
1084                 self.context(loc_inst),
1085                 format!("instruction result {} is not defined by the instruction", v),
1086             )),
1087         }
1088     }
1089 
1090     fn verify_bitcast(
1091         &self,
1092         inst: Inst,
1093         arg: Value,
1094         errors: &mut VerifierErrors,
1095     ) -> VerifierStepResult<()> {
1096         let typ = self.func.dfg.ctrl_typevar(inst);
1097         let value_type = self.func.dfg.value_type(arg);
1098 
1099         if typ.lane_bits() < value_type.lane_bits() {
1100             errors.fatal((
1101                 inst,
1102                 format!(
1103                     "The bitcast argument {} doesn't fit in a type of {} bits",
1104                     arg,
1105                     typ.lane_bits()
1106                 ),
1107             ))
1108         } else {
1109             Ok(())
1110         }
1111     }
1112 
1113     fn verify_constant_size(
1114         &self,
1115         inst: Inst,
1116         constant: Constant,
1117         errors: &mut VerifierErrors,
1118     ) -> VerifierStepResult<()> {
1119         let type_size = self.func.dfg.ctrl_typevar(inst).bytes() as usize;
1120         let constant_size = self.func.dfg.constants.get(constant).len();
1121         if type_size != constant_size {
1122             errors.fatal((
1123                 inst,
1124                 format!(
1125                     "The instruction expects {} to have a size of {} bytes but it has {}",
1126                     constant, type_size, constant_size
1127                 ),
1128             ))
1129         } else {
1130             Ok(())
1131         }
1132     }
1133 
1134     fn domtree_integrity(
1135         &self,
1136         domtree: &DominatorTree,
1137         errors: &mut VerifierErrors,
1138     ) -> VerifierStepResult<()> {
1139         // We consider two `DominatorTree`s to be equal if they return the same immediate
1140         // dominator for each block. Therefore the current domtree is valid if it matches the freshly
1141         // computed one.
1142         for block in self.func.layout.blocks() {
1143             let expected = self.expected_domtree.idom(block);
1144             let got = domtree.idom(block);
1145             if got != expected {
1146                 return errors.fatal((
1147                     block,
1148                     format!(
1149                         "invalid domtree, expected idom({}) = {:?}, got {:?}",
1150                         block, expected, got
1151                     ),
1152                 ));
1153             }
1154         }
1155         // We also verify if the postorder defined by `DominatorTree` is sane
1156         if domtree.cfg_postorder().len() != self.expected_domtree.cfg_postorder().len() {
1157             return errors.fatal((
1158                 AnyEntity::Function,
1159                 "incorrect number of Blocks in postorder traversal",
1160             ));
1161         }
1162         for (index, (&test_block, &true_block)) in domtree
1163             .cfg_postorder()
1164             .iter()
1165             .zip(self.expected_domtree.cfg_postorder().iter())
1166             .enumerate()
1167         {
1168             if test_block != true_block {
1169                 return errors.fatal((
1170                     test_block,
1171                     format!(
1172                         "invalid domtree, postorder block number {} should be {}, got {}",
1173                         index, true_block, test_block
1174                     ),
1175                 ));
1176             }
1177         }
1178         // We verify rpo_cmp on pairs of adjacent blocks in the postorder
1179         for (&prev_block, &next_block) in domtree.cfg_postorder().iter().adjacent_pairs() {
1180             if self
1181                 .expected_domtree
1182                 .rpo_cmp(prev_block, next_block, &self.func.layout)
1183                 != Ordering::Greater
1184             {
1185                 return errors.fatal((
1186                     next_block,
1187                     format!(
1188                         "invalid domtree, rpo_cmp does not says {} is greater than {}",
1189                         prev_block, next_block
1190                     ),
1191                 ));
1192             }
1193         }
1194         Ok(())
1195     }
1196 
1197     fn typecheck_entry_block_params(&self, errors: &mut VerifierErrors) -> VerifierStepResult<()> {
1198         if let Some(block) = self.func.layout.entry_block() {
1199             let expected_types = &self.func.signature.params;
1200             let block_param_count = self.func.dfg.num_block_params(block);
1201 
1202             if block_param_count != expected_types.len() {
1203                 return errors.fatal((
1204                     block,
1205                     format!(
1206                         "entry block parameters ({}) must match function signature ({})",
1207                         block_param_count,
1208                         expected_types.len()
1209                     ),
1210                 ));
1211             }
1212 
1213             for (i, &arg) in self.func.dfg.block_params(block).iter().enumerate() {
1214                 let arg_type = self.func.dfg.value_type(arg);
1215                 if arg_type != expected_types[i].value_type {
1216                     errors.report((
1217                         block,
1218                         format!(
1219                             "entry block parameter {} expected to have type {}, got {}",
1220                             i, expected_types[i], arg_type
1221                         ),
1222                     ));
1223                 }
1224             }
1225         }
1226 
1227         errors.as_result()
1228     }
1229 
1230     fn check_entry_not_cold(&self, errors: &mut VerifierErrors) -> VerifierStepResult<()> {
1231         if let Some(entry_block) = self.func.layout.entry_block() {
1232             if self.func.layout.is_cold(entry_block) {
1233                 return errors
1234                     .fatal((entry_block, format!("entry block cannot be marked as cold")));
1235             }
1236         }
1237         errors.as_result()
1238     }
1239 
1240     fn typecheck(&self, inst: Inst, errors: &mut VerifierErrors) -> VerifierStepResult<()> {
1241         let inst_data = &self.func.dfg[inst];
1242         let constraints = inst_data.opcode().constraints();
1243 
1244         let ctrl_type = if let Some(value_typeset) = constraints.ctrl_typeset() {
1245             // For polymorphic opcodes, determine the controlling type variable first.
1246             let ctrl_type = self.func.dfg.ctrl_typevar(inst);
1247 
1248             if !value_typeset.contains(ctrl_type) {
1249                 errors.report((
1250                     inst,
1251                     self.context(inst),
1252                     format!("has an invalid controlling type {}", ctrl_type),
1253                 ));
1254             }
1255 
1256             ctrl_type
1257         } else {
1258             // Non-polymorphic instructions don't check the controlling type variable, so `Option`
1259             // is unnecessary and we can just make it `INVALID`.
1260             types::INVALID
1261         };
1262 
1263         // Typechecking instructions is never fatal
1264         let _ = self.typecheck_results(inst, ctrl_type, errors);
1265         let _ = self.typecheck_fixed_args(inst, ctrl_type, errors);
1266         let _ = self.typecheck_variable_args(inst, errors);
1267         let _ = self.typecheck_return(inst, errors);
1268         let _ = self.typecheck_special(inst, ctrl_type, errors);
1269 
1270         Ok(())
1271     }
1272 
1273     fn typecheck_results(
1274         &self,
1275         inst: Inst,
1276         ctrl_type: Type,
1277         errors: &mut VerifierErrors,
1278     ) -> VerifierStepResult<()> {
1279         let mut i = 0;
1280         for &result in self.func.dfg.inst_results(inst) {
1281             let result_type = self.func.dfg.value_type(result);
1282             let expected_type = self.func.dfg.compute_result_type(inst, i, ctrl_type);
1283             if let Some(expected_type) = expected_type {
1284                 if result_type != expected_type {
1285                     errors.report((
1286                         inst,
1287                         self.context(inst),
1288                         format!(
1289                             "expected result {} ({}) to have type {}, found {}",
1290                             i, result, expected_type, result_type
1291                         ),
1292                     ));
1293                 }
1294             } else {
1295                 return errors.nonfatal((
1296                     inst,
1297                     self.context(inst),
1298                     "has more result values than expected",
1299                 ));
1300             }
1301             i += 1;
1302         }
1303 
1304         // There aren't any more result types left.
1305         if self.func.dfg.compute_result_type(inst, i, ctrl_type) != None {
1306             return errors.nonfatal((
1307                 inst,
1308                 self.context(inst),
1309                 "has fewer result values than expected",
1310             ));
1311         }
1312         Ok(())
1313     }
1314 
1315     fn typecheck_fixed_args(
1316         &self,
1317         inst: Inst,
1318         ctrl_type: Type,
1319         errors: &mut VerifierErrors,
1320     ) -> VerifierStepResult<()> {
1321         let constraints = self.func.dfg[inst].opcode().constraints();
1322 
1323         for (i, &arg) in self.func.dfg.inst_fixed_args(inst).iter().enumerate() {
1324             let arg_type = self.func.dfg.value_type(arg);
1325             match constraints.value_argument_constraint(i, ctrl_type) {
1326                 ResolvedConstraint::Bound(expected_type) => {
1327                     if arg_type != expected_type {
1328                         errors.report((
1329                             inst,
1330                             self.context(inst),
1331                             format!(
1332                                 "arg {} ({}) has type {}, expected {}",
1333                                 i, arg, arg_type, expected_type
1334                             ),
1335                         ));
1336                     }
1337                 }
1338                 ResolvedConstraint::Free(type_set) => {
1339                     if !type_set.contains(arg_type) {
1340                         errors.report((
1341                             inst,
1342                             self.context(inst),
1343                             format!(
1344                                 "arg {} ({}) with type {} failed to satisfy type set {:?}",
1345                                 i, arg, arg_type, type_set
1346                             ),
1347                         ));
1348                     }
1349                 }
1350             }
1351         }
1352         Ok(())
1353     }
1354 
1355     fn typecheck_variable_args(
1356         &self,
1357         inst: Inst,
1358         errors: &mut VerifierErrors,
1359     ) -> VerifierStepResult<()> {
1360         match self.func.dfg.analyze_branch(inst) {
1361             BranchInfo::SingleDest(block, _) => {
1362                 let iter = self
1363                     .func
1364                     .dfg
1365                     .block_params(block)
1366                     .iter()
1367                     .map(|&v| self.func.dfg.value_type(v));
1368                 self.typecheck_variable_args_iterator(inst, iter, errors)?;
1369             }
1370             BranchInfo::Table(table, block) => {
1371                 if let Some(block) = block {
1372                     let arg_count = self.func.dfg.num_block_params(block);
1373                     if arg_count != 0 {
1374                         return errors.nonfatal((
1375                             inst,
1376                             self.context(inst),
1377                             format!(
1378                                 "takes no arguments, but had target {} with {} arguments",
1379                                 block, arg_count,
1380                             ),
1381                         ));
1382                     }
1383                 }
1384                 for block in self.func.jump_tables[table].iter() {
1385                     let arg_count = self.func.dfg.num_block_params(*block);
1386                     if arg_count != 0 {
1387                         return errors.nonfatal((
1388                             inst,
1389                             self.context(inst),
1390                             format!(
1391                                 "takes no arguments, but had target {} with {} arguments",
1392                                 block, arg_count,
1393                             ),
1394                         ));
1395                     }
1396                 }
1397             }
1398             BranchInfo::NotABranch => {}
1399         }
1400 
1401         match self.func.dfg[inst].analyze_call(&self.func.dfg.value_lists) {
1402             CallInfo::Direct(func_ref, _) => {
1403                 let sig_ref = self.func.dfg.ext_funcs[func_ref].signature;
1404                 let arg_types = self.func.dfg.signatures[sig_ref]
1405                     .params
1406                     .iter()
1407                     .map(|a| a.value_type);
1408                 self.typecheck_variable_args_iterator(inst, arg_types, errors)?;
1409             }
1410             CallInfo::Indirect(sig_ref, _) => {
1411                 let arg_types = self.func.dfg.signatures[sig_ref]
1412                     .params
1413                     .iter()
1414                     .map(|a| a.value_type);
1415                 self.typecheck_variable_args_iterator(inst, arg_types, errors)?;
1416             }
1417             CallInfo::NotACall => {}
1418         }
1419         Ok(())
1420     }
1421 
1422     fn typecheck_variable_args_iterator<I: Iterator<Item = Type>>(
1423         &self,
1424         inst: Inst,
1425         iter: I,
1426         errors: &mut VerifierErrors,
1427     ) -> VerifierStepResult<()> {
1428         let variable_args = self.func.dfg.inst_variable_args(inst);
1429         let mut i = 0;
1430 
1431         for expected_type in iter {
1432             if i >= variable_args.len() {
1433                 // Result count mismatch handled below, we want the full argument count first though
1434                 i += 1;
1435                 continue;
1436             }
1437             let arg = variable_args[i];
1438             let arg_type = self.func.dfg.value_type(arg);
1439             if expected_type != arg_type {
1440                 errors.report((
1441                     inst,
1442                     self.context(inst),
1443                     format!(
1444                         "arg {} ({}) has type {}, expected {}",
1445                         i, variable_args[i], arg_type, expected_type
1446                     ),
1447                 ));
1448             }
1449             i += 1;
1450         }
1451         if i != variable_args.len() {
1452             return errors.nonfatal((
1453                 inst,
1454                 self.context(inst),
1455                 format!(
1456                     "mismatched argument count for `{}`: got {}, expected {}",
1457                     self.func.dfg.display_inst(inst),
1458                     variable_args.len(),
1459                     i,
1460                 ),
1461             ));
1462         }
1463         Ok(())
1464     }
1465 
1466     fn typecheck_return(&self, inst: Inst, errors: &mut VerifierErrors) -> VerifierStepResult<()> {
1467         if self.func.dfg[inst].opcode().is_return() {
1468             let args = self.func.dfg.inst_variable_args(inst);
1469             let expected_types = &self.func.signature.returns;
1470             if args.len() != expected_types.len() {
1471                 return errors.nonfatal((
1472                     inst,
1473                     self.context(inst),
1474                     "arguments of return must match function signature",
1475                 ));
1476             }
1477             for (i, (&arg, &expected_type)) in args.iter().zip(expected_types).enumerate() {
1478                 let arg_type = self.func.dfg.value_type(arg);
1479                 if arg_type != expected_type.value_type {
1480                     errors.report((
1481                         inst,
1482                         self.context(inst),
1483                         format!(
1484                             "arg {} ({}) has type {}, must match function signature of {}",
1485                             i, arg, arg_type, expected_type
1486                         ),
1487                     ));
1488                 }
1489             }
1490         }
1491         Ok(())
1492     }
1493 
1494     // Check special-purpose type constraints that can't be expressed in the normal opcode
1495     // constraints.
1496     fn typecheck_special(
1497         &self,
1498         inst: Inst,
1499         ctrl_type: Type,
1500         errors: &mut VerifierErrors,
1501     ) -> VerifierStepResult<()> {
1502         match self.func.dfg[inst] {
1503             ir::InstructionData::Unary { opcode, arg } => {
1504                 let arg_type = self.func.dfg.value_type(arg);
1505                 match opcode {
1506                     Opcode::Bextend | Opcode::Uextend | Opcode::Sextend | Opcode::Fpromote => {
1507                         if arg_type.lane_count() != ctrl_type.lane_count() {
1508                             return errors.nonfatal((
1509                                 inst,
1510                                 self.context(inst),
1511                                 format!(
1512                                     "input {} and output {} must have same number of lanes",
1513                                     arg_type, ctrl_type,
1514                                 ),
1515                             ));
1516                         }
1517                         if arg_type.lane_bits() >= ctrl_type.lane_bits() {
1518                             return errors.nonfatal((
1519                                 inst,
1520                                 self.context(inst),
1521                                 format!(
1522                                     "input {} must be smaller than output {}",
1523                                     arg_type, ctrl_type,
1524                                 ),
1525                             ));
1526                         }
1527                     }
1528                     Opcode::Breduce | Opcode::Ireduce | Opcode::Fdemote => {
1529                         if arg_type.lane_count() != ctrl_type.lane_count() {
1530                             return errors.nonfatal((
1531                                 inst,
1532                                 self.context(inst),
1533                                 format!(
1534                                     "input {} and output {} must have same number of lanes",
1535                                     arg_type, ctrl_type,
1536                                 ),
1537                             ));
1538                         }
1539                         if arg_type.lane_bits() <= ctrl_type.lane_bits() {
1540                             return errors.nonfatal((
1541                                 inst,
1542                                 self.context(inst),
1543                                 format!(
1544                                     "input {} must be larger than output {}",
1545                                     arg_type, ctrl_type,
1546                                 ),
1547                             ));
1548                         }
1549                     }
1550                     _ => {}
1551                 }
1552             }
1553             ir::InstructionData::HeapAddr { heap, arg, .. } => {
1554                 let index_type = self.func.dfg.value_type(arg);
1555                 let heap_index_type = self.func.heaps[heap].index_type;
1556                 if index_type != heap_index_type {
1557                     return errors.nonfatal((
1558                         inst,
1559                         self.context(inst),
1560                         format!(
1561                             "index type {} differs from heap index type {}",
1562                             index_type, heap_index_type,
1563                         ),
1564                     ));
1565                 }
1566             }
1567             ir::InstructionData::TableAddr { table, arg, .. } => {
1568                 let index_type = self.func.dfg.value_type(arg);
1569                 let table_index_type = self.func.tables[table].index_type;
1570                 if index_type != table_index_type {
1571                     return errors.nonfatal((
1572                         inst,
1573                         self.context(inst),
1574                         format!(
1575                             "index type {} differs from table index type {}",
1576                             index_type, table_index_type,
1577                         ),
1578                     ));
1579                 }
1580             }
1581             ir::InstructionData::UnaryGlobalValue { global_value, .. } => {
1582                 if let Some(isa) = self.isa {
1583                     let inst_type = self.func.dfg.value_type(self.func.dfg.first_result(inst));
1584                     let global_type = self.func.global_values[global_value].global_type(isa);
1585                     if inst_type != global_type {
1586                         return errors.nonfatal((
1587                             inst, self.context(inst),
1588                             format!(
1589                                 "global_value instruction with type {} references global value with type {}",
1590                                 inst_type, global_type
1591                             )),
1592                         );
1593                     }
1594                 }
1595             }
1596             _ => {}
1597         }
1598         Ok(())
1599     }
1600 
1601     fn cfg_integrity(
1602         &self,
1603         cfg: &ControlFlowGraph,
1604         errors: &mut VerifierErrors,
1605     ) -> VerifierStepResult<()> {
1606         let mut expected_succs = BTreeSet::<Block>::new();
1607         let mut got_succs = BTreeSet::<Block>::new();
1608         let mut expected_preds = BTreeSet::<Inst>::new();
1609         let mut got_preds = BTreeSet::<Inst>::new();
1610 
1611         for block in self.func.layout.blocks() {
1612             expected_succs.extend(self.expected_cfg.succ_iter(block));
1613             got_succs.extend(cfg.succ_iter(block));
1614 
1615             let missing_succs: Vec<Block> =
1616                 expected_succs.difference(&got_succs).cloned().collect();
1617             if !missing_succs.is_empty() {
1618                 errors.report((
1619                     block,
1620                     format!("cfg lacked the following successor(s) {:?}", missing_succs),
1621                 ));
1622                 continue;
1623             }
1624 
1625             let excess_succs: Vec<Block> = got_succs.difference(&expected_succs).cloned().collect();
1626             if !excess_succs.is_empty() {
1627                 errors.report((
1628                     block,
1629                     format!("cfg had unexpected successor(s) {:?}", excess_succs),
1630                 ));
1631                 continue;
1632             }
1633 
1634             expected_preds.extend(
1635                 self.expected_cfg
1636                     .pred_iter(block)
1637                     .map(|BlockPredecessor { inst, .. }| inst),
1638             );
1639             got_preds.extend(
1640                 cfg.pred_iter(block)
1641                     .map(|BlockPredecessor { inst, .. }| inst),
1642             );
1643 
1644             let missing_preds: Vec<Inst> = expected_preds.difference(&got_preds).cloned().collect();
1645             if !missing_preds.is_empty() {
1646                 errors.report((
1647                     block,
1648                     format!(
1649                         "cfg lacked the following predecessor(s) {:?}",
1650                         missing_preds
1651                     ),
1652                 ));
1653                 continue;
1654             }
1655 
1656             let excess_preds: Vec<Inst> = got_preds.difference(&expected_preds).cloned().collect();
1657             if !excess_preds.is_empty() {
1658                 errors.report((
1659                     block,
1660                     format!("cfg had unexpected predecessor(s) {:?}", excess_preds),
1661                 ));
1662                 continue;
1663             }
1664 
1665             expected_succs.clear();
1666             got_succs.clear();
1667             expected_preds.clear();
1668             got_preds.clear();
1669         }
1670         errors.as_result()
1671     }
1672 
1673     fn immediate_constraints(
1674         &self,
1675         inst: Inst,
1676         errors: &mut VerifierErrors,
1677     ) -> VerifierStepResult<()> {
1678         let inst_data = &self.func.dfg[inst];
1679 
1680         match *inst_data {
1681             ir::InstructionData::Store { flags, .. } => {
1682                 if flags.readonly() {
1683                     errors.fatal((
1684                         inst,
1685                         self.context(inst),
1686                         "A store instruction cannot have the `readonly` MemFlag",
1687                     ))
1688                 } else {
1689                     Ok(())
1690                 }
1691             }
1692             ir::InstructionData::BinaryImm8 {
1693                 opcode: ir::instructions::Opcode::Extractlane,
1694                 imm: lane,
1695                 arg,
1696                 ..
1697             }
1698             | ir::InstructionData::TernaryImm8 {
1699                 opcode: ir::instructions::Opcode::Insertlane,
1700                 imm: lane,
1701                 args: [arg, _],
1702                 ..
1703             } => {
1704                 // We must be specific about the opcodes above because other instructions are using
1705                 // the same formats.
1706                 let ty = self.func.dfg.value_type(arg);
1707                 if lane as u32 >= ty.lane_count() {
1708                     errors.fatal((
1709                         inst,
1710                         self.context(inst),
1711                         format!("The lane {} does not index into the type {}", lane, ty,),
1712                     ))
1713                 } else {
1714                     Ok(())
1715                 }
1716             }
1717             _ => Ok(()),
1718         }
1719     }
1720 
1721     fn typecheck_function_signature(&self, errors: &mut VerifierErrors) -> VerifierStepResult<()> {
1722         self.func
1723             .signature
1724             .params
1725             .iter()
1726             .enumerate()
1727             .filter(|(_, &param)| param.value_type == types::INVALID)
1728             .for_each(|(i, _)| {
1729                 errors.report((
1730                     AnyEntity::Function,
1731                     format!("Parameter at position {} has an invalid type", i),
1732                 ));
1733             });
1734 
1735         self.func
1736             .signature
1737             .returns
1738             .iter()
1739             .enumerate()
1740             .filter(|(_, &ret)| ret.value_type == types::INVALID)
1741             .for_each(|(i, _)| {
1742                 errors.report((
1743                     AnyEntity::Function,
1744                     format!("Return value at position {} has an invalid type", i),
1745                 ))
1746             });
1747 
1748         self.func
1749             .signature
1750             .returns
1751             .iter()
1752             .enumerate()
1753             .for_each(|(i, ret)| {
1754                 if let ArgumentPurpose::StructArgument(_) = ret.purpose {
1755                     errors.report((
1756                         AnyEntity::Function,
1757                         format!("Return value at position {} can't be an struct argument", i),
1758                     ))
1759                 }
1760             });
1761 
1762         if errors.has_error() {
1763             Err(())
1764         } else {
1765             Ok(())
1766         }
1767     }
1768 
1769     pub fn run(&self, errors: &mut VerifierErrors) -> VerifierStepResult<()> {
1770         self.verify_global_values(errors)?;
1771         self.verify_heaps(errors)?;
1772         self.verify_tables(errors)?;
1773         self.verify_jump_tables(errors)?;
1774         self.typecheck_entry_block_params(errors)?;
1775         self.check_entry_not_cold(errors)?;
1776         self.typecheck_function_signature(errors)?;
1777 
1778         for block in self.func.layout.blocks() {
1779             if self.func.layout.first_inst(block).is_none() {
1780                 return errors.fatal((block, format!("{} cannot be empty", block)));
1781             }
1782             for inst in self.func.layout.block_insts(block) {
1783                 self.block_integrity(block, inst, errors)?;
1784                 self.instruction_integrity(inst, errors)?;
1785                 self.typecheck(inst, errors)?;
1786                 self.immediate_constraints(inst, errors)?;
1787             }
1788 
1789             self.encodable_as_bb(block, errors)?;
1790         }
1791 
1792         verify_flags(self.func, &self.expected_cfg, errors)?;
1793 
1794         if !errors.is_empty() {
1795             log::warn!(
1796                 "Found verifier errors in function:\n{}",
1797                 pretty_verifier_error(self.func, None, errors.clone())
1798             );
1799         }
1800 
1801         Ok(())
1802     }
1803 }
1804 
1805 #[cfg(test)]
1806 mod tests {
1807     use super::{Verifier, VerifierError, VerifierErrors};
1808     use crate::entity::EntityList;
1809     use crate::ir::instructions::{InstructionData, Opcode};
1810     use crate::ir::{types, AbiParam, Function};
1811     use crate::settings;
1812 
1813     macro_rules! assert_err_with_msg {
1814         ($e:expr, $msg:expr) => {
1815             match $e.0.get(0) {
1816                 None => panic!("Expected an error"),
1817                 Some(&VerifierError { ref message, .. }) => {
1818                     if !message.contains($msg) {
1819                         #[cfg(feature = "std")]
1820                         panic!("'{}' did not contain the substring '{}'", message, $msg);
1821                         #[cfg(not(feature = "std"))]
1822                         panic!("error message did not contain the expected substring");
1823                     }
1824                 }
1825             }
1826         };
1827     }
1828 
1829     #[test]
1830     fn empty() {
1831         let func = Function::new();
1832         let flags = &settings::Flags::new(settings::builder());
1833         let verifier = Verifier::new(&func, flags.into());
1834         let mut errors = VerifierErrors::default();
1835 
1836         assert_eq!(verifier.run(&mut errors), Ok(()));
1837         assert!(errors.0.is_empty());
1838     }
1839 
1840     #[test]
1841     fn bad_instruction_format() {
1842         let mut func = Function::new();
1843         let block0 = func.dfg.make_block();
1844         func.layout.append_block(block0);
1845         let nullary_with_bad_opcode = func.dfg.make_inst(InstructionData::UnaryImm {
1846             opcode: Opcode::F32const,
1847             imm: 0.into(),
1848         });
1849         func.layout.append_inst(nullary_with_bad_opcode, block0);
1850         func.stencil.layout.append_inst(
1851             func.stencil.dfg.make_inst(InstructionData::Jump {
1852                 opcode: Opcode::Jump,
1853                 destination: block0,
1854                 args: EntityList::default(),
1855             }),
1856             block0,
1857         );
1858         let flags = &settings::Flags::new(settings::builder());
1859         let verifier = Verifier::new(&func, flags.into());
1860         let mut errors = VerifierErrors::default();
1861 
1862         let _ = verifier.run(&mut errors);
1863 
1864         assert_err_with_msg!(errors, "instruction format");
1865     }
1866 
1867     #[test]
1868     fn test_function_invalid_param() {
1869         let mut func = Function::new();
1870         func.signature.params.push(AbiParam::new(types::INVALID));
1871 
1872         let mut errors = VerifierErrors::default();
1873         let flags = &settings::Flags::new(settings::builder());
1874         let verifier = Verifier::new(&func, flags.into());
1875 
1876         let _ = verifier.typecheck_function_signature(&mut errors);
1877         assert_err_with_msg!(errors, "Parameter at position 0 has an invalid type");
1878     }
1879 
1880     #[test]
1881     fn test_function_invalid_return_value() {
1882         let mut func = Function::new();
1883         func.signature.returns.push(AbiParam::new(types::INVALID));
1884 
1885         let mut errors = VerifierErrors::default();
1886         let flags = &settings::Flags::new(settings::builder());
1887         let verifier = Verifier::new(&func, flags.into());
1888 
1889         let _ = verifier.typecheck_function_signature(&mut errors);
1890         assert_err_with_msg!(errors, "Return value at position 0 has an invalid type");
1891     }
1892 
1893     #[test]
1894     fn test_printing_contextual_errors() {
1895         // Build function.
1896         let mut func = Function::new();
1897         let block0 = func.dfg.make_block();
1898         func.layout.append_block(block0);
1899 
1900         // Build instruction: v0, v1 = iconst 42
1901         let inst = func.dfg.make_inst(InstructionData::UnaryImm {
1902             opcode: Opcode::Iconst,
1903             imm: 42.into(),
1904         });
1905         func.dfg.append_result(inst, types::I32);
1906         func.dfg.append_result(inst, types::I32);
1907         func.layout.append_inst(inst, block0);
1908 
1909         // Setup verifier.
1910         let mut errors = VerifierErrors::default();
1911         let flags = &settings::Flags::new(settings::builder());
1912         let verifier = Verifier::new(&func, flags.into());
1913 
1914         // Now the error message, when printed, should contain the instruction sequence causing the
1915         // error (i.e. v0, v1 = iconst.i32 42) and not only its entity value (i.e. inst0)
1916         let _ = verifier.typecheck_results(inst, types::I32, &mut errors);
1917         assert_eq!(
1918             format!("{}", errors.0[0]),
1919             "inst0 (v0, v1 = iconst.i32 42): has more result values than expected"
1920         )
1921     }
1922 
1923     #[test]
1924     fn test_empty_block() {
1925         let mut func = Function::new();
1926         let block0 = func.dfg.make_block();
1927         func.layout.append_block(block0);
1928 
1929         let flags = &settings::Flags::new(settings::builder());
1930         let verifier = Verifier::new(&func, flags.into());
1931         let mut errors = VerifierErrors::default();
1932         let _ = verifier.run(&mut errors);
1933 
1934         assert_err_with_msg!(errors, "block0 cannot be empty");
1935     }
1936 }
1937