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 {} has a lane type of {} bits, which doesn't match an expected type of {} bits", 1104 arg, 1105 value_type.lane_bits(), 1106 typ.lane_bits() 1107 ), 1108 )) 1109 } else if typ.bits() != value_type.bits() { 1110 errors.fatal(( 1111 inst, 1112 format!( 1113 "The bitcast argument {} has a type of {} bits, which doesn't match an expected type of {} bits", 1114 arg, 1115 value_type.bits(), 1116 typ.bits() 1117 ), 1118 )) 1119 } else { 1120 Ok(()) 1121 } 1122 } 1123 1124 fn verify_constant_size( 1125 &self, 1126 inst: Inst, 1127 constant: Constant, 1128 errors: &mut VerifierErrors, 1129 ) -> VerifierStepResult<()> { 1130 let type_size = self.func.dfg.ctrl_typevar(inst).bytes() as usize; 1131 let constant_size = self.func.dfg.constants.get(constant).len(); 1132 if type_size != constant_size { 1133 errors.fatal(( 1134 inst, 1135 format!( 1136 "The instruction expects {} to have a size of {} bytes but it has {}", 1137 constant, type_size, constant_size 1138 ), 1139 )) 1140 } else { 1141 Ok(()) 1142 } 1143 } 1144 1145 fn domtree_integrity( 1146 &self, 1147 domtree: &DominatorTree, 1148 errors: &mut VerifierErrors, 1149 ) -> VerifierStepResult<()> { 1150 // We consider two `DominatorTree`s to be equal if they return the same immediate 1151 // dominator for each block. Therefore the current domtree is valid if it matches the freshly 1152 // computed one. 1153 for block in self.func.layout.blocks() { 1154 let expected = self.expected_domtree.idom(block); 1155 let got = domtree.idom(block); 1156 if got != expected { 1157 return errors.fatal(( 1158 block, 1159 format!( 1160 "invalid domtree, expected idom({}) = {:?}, got {:?}", 1161 block, expected, got 1162 ), 1163 )); 1164 } 1165 } 1166 // We also verify if the postorder defined by `DominatorTree` is sane 1167 if domtree.cfg_postorder().len() != self.expected_domtree.cfg_postorder().len() { 1168 return errors.fatal(( 1169 AnyEntity::Function, 1170 "incorrect number of Blocks in postorder traversal", 1171 )); 1172 } 1173 for (index, (&test_block, &true_block)) in domtree 1174 .cfg_postorder() 1175 .iter() 1176 .zip(self.expected_domtree.cfg_postorder().iter()) 1177 .enumerate() 1178 { 1179 if test_block != true_block { 1180 return errors.fatal(( 1181 test_block, 1182 format!( 1183 "invalid domtree, postorder block number {} should be {}, got {}", 1184 index, true_block, test_block 1185 ), 1186 )); 1187 } 1188 } 1189 // We verify rpo_cmp on pairs of adjacent blocks in the postorder 1190 for (&prev_block, &next_block) in domtree.cfg_postorder().iter().adjacent_pairs() { 1191 if self 1192 .expected_domtree 1193 .rpo_cmp(prev_block, next_block, &self.func.layout) 1194 != Ordering::Greater 1195 { 1196 return errors.fatal(( 1197 next_block, 1198 format!( 1199 "invalid domtree, rpo_cmp does not says {} is greater than {}", 1200 prev_block, next_block 1201 ), 1202 )); 1203 } 1204 } 1205 Ok(()) 1206 } 1207 1208 fn typecheck_entry_block_params(&self, errors: &mut VerifierErrors) -> VerifierStepResult<()> { 1209 if let Some(block) = self.func.layout.entry_block() { 1210 let expected_types = &self.func.signature.params; 1211 let block_param_count = self.func.dfg.num_block_params(block); 1212 1213 if block_param_count != expected_types.len() { 1214 return errors.fatal(( 1215 block, 1216 format!( 1217 "entry block parameters ({}) must match function signature ({})", 1218 block_param_count, 1219 expected_types.len() 1220 ), 1221 )); 1222 } 1223 1224 for (i, &arg) in self.func.dfg.block_params(block).iter().enumerate() { 1225 let arg_type = self.func.dfg.value_type(arg); 1226 if arg_type != expected_types[i].value_type { 1227 errors.report(( 1228 block, 1229 format!( 1230 "entry block parameter {} expected to have type {}, got {}", 1231 i, expected_types[i], arg_type 1232 ), 1233 )); 1234 } 1235 } 1236 } 1237 1238 errors.as_result() 1239 } 1240 1241 fn check_entry_not_cold(&self, errors: &mut VerifierErrors) -> VerifierStepResult<()> { 1242 if let Some(entry_block) = self.func.layout.entry_block() { 1243 if self.func.layout.is_cold(entry_block) { 1244 return errors 1245 .fatal((entry_block, format!("entry block cannot be marked as cold"))); 1246 } 1247 } 1248 errors.as_result() 1249 } 1250 1251 fn typecheck(&self, inst: Inst, errors: &mut VerifierErrors) -> VerifierStepResult<()> { 1252 let inst_data = &self.func.dfg[inst]; 1253 let constraints = inst_data.opcode().constraints(); 1254 1255 let ctrl_type = if let Some(value_typeset) = constraints.ctrl_typeset() { 1256 // For polymorphic opcodes, determine the controlling type variable first. 1257 let ctrl_type = self.func.dfg.ctrl_typevar(inst); 1258 1259 if !value_typeset.contains(ctrl_type) { 1260 errors.report(( 1261 inst, 1262 self.context(inst), 1263 format!("has an invalid controlling type {}", ctrl_type), 1264 )); 1265 } 1266 1267 ctrl_type 1268 } else { 1269 // Non-polymorphic instructions don't check the controlling type variable, so `Option` 1270 // is unnecessary and we can just make it `INVALID`. 1271 types::INVALID 1272 }; 1273 1274 // Typechecking instructions is never fatal 1275 let _ = self.typecheck_results(inst, ctrl_type, errors); 1276 let _ = self.typecheck_fixed_args(inst, ctrl_type, errors); 1277 let _ = self.typecheck_variable_args(inst, errors); 1278 let _ = self.typecheck_return(inst, errors); 1279 let _ = self.typecheck_special(inst, ctrl_type, errors); 1280 1281 Ok(()) 1282 } 1283 1284 fn typecheck_results( 1285 &self, 1286 inst: Inst, 1287 ctrl_type: Type, 1288 errors: &mut VerifierErrors, 1289 ) -> VerifierStepResult<()> { 1290 let mut i = 0; 1291 for &result in self.func.dfg.inst_results(inst) { 1292 let result_type = self.func.dfg.value_type(result); 1293 let expected_type = self.func.dfg.compute_result_type(inst, i, ctrl_type); 1294 if let Some(expected_type) = expected_type { 1295 if result_type != expected_type { 1296 errors.report(( 1297 inst, 1298 self.context(inst), 1299 format!( 1300 "expected result {} ({}) to have type {}, found {}", 1301 i, result, expected_type, result_type 1302 ), 1303 )); 1304 } 1305 } else { 1306 return errors.nonfatal(( 1307 inst, 1308 self.context(inst), 1309 "has more result values than expected", 1310 )); 1311 } 1312 i += 1; 1313 } 1314 1315 // There aren't any more result types left. 1316 if self.func.dfg.compute_result_type(inst, i, ctrl_type) != None { 1317 return errors.nonfatal(( 1318 inst, 1319 self.context(inst), 1320 "has fewer result values than expected", 1321 )); 1322 } 1323 Ok(()) 1324 } 1325 1326 fn typecheck_fixed_args( 1327 &self, 1328 inst: Inst, 1329 ctrl_type: Type, 1330 errors: &mut VerifierErrors, 1331 ) -> VerifierStepResult<()> { 1332 let constraints = self.func.dfg[inst].opcode().constraints(); 1333 1334 for (i, &arg) in self.func.dfg.inst_fixed_args(inst).iter().enumerate() { 1335 let arg_type = self.func.dfg.value_type(arg); 1336 match constraints.value_argument_constraint(i, ctrl_type) { 1337 ResolvedConstraint::Bound(expected_type) => { 1338 if arg_type != expected_type { 1339 errors.report(( 1340 inst, 1341 self.context(inst), 1342 format!( 1343 "arg {} ({}) has type {}, expected {}", 1344 i, arg, arg_type, expected_type 1345 ), 1346 )); 1347 } 1348 } 1349 ResolvedConstraint::Free(type_set) => { 1350 if !type_set.contains(arg_type) { 1351 errors.report(( 1352 inst, 1353 self.context(inst), 1354 format!( 1355 "arg {} ({}) with type {} failed to satisfy type set {:?}", 1356 i, arg, arg_type, type_set 1357 ), 1358 )); 1359 } 1360 } 1361 } 1362 } 1363 Ok(()) 1364 } 1365 1366 fn typecheck_variable_args( 1367 &self, 1368 inst: Inst, 1369 errors: &mut VerifierErrors, 1370 ) -> VerifierStepResult<()> { 1371 match self.func.dfg.analyze_branch(inst) { 1372 BranchInfo::SingleDest(block, _) => { 1373 let iter = self 1374 .func 1375 .dfg 1376 .block_params(block) 1377 .iter() 1378 .map(|&v| self.func.dfg.value_type(v)); 1379 self.typecheck_variable_args_iterator(inst, iter, errors)?; 1380 } 1381 BranchInfo::Table(table, block) => { 1382 if let Some(block) = block { 1383 let arg_count = self.func.dfg.num_block_params(block); 1384 if arg_count != 0 { 1385 return errors.nonfatal(( 1386 inst, 1387 self.context(inst), 1388 format!( 1389 "takes no arguments, but had target {} with {} arguments", 1390 block, arg_count, 1391 ), 1392 )); 1393 } 1394 } 1395 for block in self.func.jump_tables[table].iter() { 1396 let arg_count = self.func.dfg.num_block_params(*block); 1397 if arg_count != 0 { 1398 return errors.nonfatal(( 1399 inst, 1400 self.context(inst), 1401 format!( 1402 "takes no arguments, but had target {} with {} arguments", 1403 block, arg_count, 1404 ), 1405 )); 1406 } 1407 } 1408 } 1409 BranchInfo::NotABranch => {} 1410 } 1411 1412 match self.func.dfg[inst].analyze_call(&self.func.dfg.value_lists) { 1413 CallInfo::Direct(func_ref, _) => { 1414 let sig_ref = self.func.dfg.ext_funcs[func_ref].signature; 1415 let arg_types = self.func.dfg.signatures[sig_ref] 1416 .params 1417 .iter() 1418 .map(|a| a.value_type); 1419 self.typecheck_variable_args_iterator(inst, arg_types, errors)?; 1420 } 1421 CallInfo::Indirect(sig_ref, _) => { 1422 let arg_types = self.func.dfg.signatures[sig_ref] 1423 .params 1424 .iter() 1425 .map(|a| a.value_type); 1426 self.typecheck_variable_args_iterator(inst, arg_types, errors)?; 1427 } 1428 CallInfo::NotACall => {} 1429 } 1430 Ok(()) 1431 } 1432 1433 fn typecheck_variable_args_iterator<I: Iterator<Item = Type>>( 1434 &self, 1435 inst: Inst, 1436 iter: I, 1437 errors: &mut VerifierErrors, 1438 ) -> VerifierStepResult<()> { 1439 let variable_args = self.func.dfg.inst_variable_args(inst); 1440 let mut i = 0; 1441 1442 for expected_type in iter { 1443 if i >= variable_args.len() { 1444 // Result count mismatch handled below, we want the full argument count first though 1445 i += 1; 1446 continue; 1447 } 1448 let arg = variable_args[i]; 1449 let arg_type = self.func.dfg.value_type(arg); 1450 if expected_type != arg_type { 1451 errors.report(( 1452 inst, 1453 self.context(inst), 1454 format!( 1455 "arg {} ({}) has type {}, expected {}", 1456 i, variable_args[i], arg_type, expected_type 1457 ), 1458 )); 1459 } 1460 i += 1; 1461 } 1462 if i != variable_args.len() { 1463 return errors.nonfatal(( 1464 inst, 1465 self.context(inst), 1466 format!( 1467 "mismatched argument count for `{}`: got {}, expected {}", 1468 self.func.dfg.display_inst(inst), 1469 variable_args.len(), 1470 i, 1471 ), 1472 )); 1473 } 1474 Ok(()) 1475 } 1476 1477 fn typecheck_return(&self, inst: Inst, errors: &mut VerifierErrors) -> VerifierStepResult<()> { 1478 if self.func.dfg[inst].opcode().is_return() { 1479 let args = self.func.dfg.inst_variable_args(inst); 1480 let expected_types = &self.func.signature.returns; 1481 if args.len() != expected_types.len() { 1482 return errors.nonfatal(( 1483 inst, 1484 self.context(inst), 1485 "arguments of return must match function signature", 1486 )); 1487 } 1488 for (i, (&arg, &expected_type)) in args.iter().zip(expected_types).enumerate() { 1489 let arg_type = self.func.dfg.value_type(arg); 1490 if arg_type != expected_type.value_type { 1491 errors.report(( 1492 inst, 1493 self.context(inst), 1494 format!( 1495 "arg {} ({}) has type {}, must match function signature of {}", 1496 i, arg, arg_type, expected_type 1497 ), 1498 )); 1499 } 1500 } 1501 } 1502 Ok(()) 1503 } 1504 1505 // Check special-purpose type constraints that can't be expressed in the normal opcode 1506 // constraints. 1507 fn typecheck_special( 1508 &self, 1509 inst: Inst, 1510 ctrl_type: Type, 1511 errors: &mut VerifierErrors, 1512 ) -> VerifierStepResult<()> { 1513 match self.func.dfg[inst] { 1514 ir::InstructionData::Unary { opcode, arg } => { 1515 let arg_type = self.func.dfg.value_type(arg); 1516 match opcode { 1517 Opcode::Bextend | Opcode::Uextend | Opcode::Sextend | Opcode::Fpromote => { 1518 if arg_type.lane_count() != ctrl_type.lane_count() { 1519 return errors.nonfatal(( 1520 inst, 1521 self.context(inst), 1522 format!( 1523 "input {} and output {} must have same number of lanes", 1524 arg_type, ctrl_type, 1525 ), 1526 )); 1527 } 1528 if arg_type.lane_bits() >= ctrl_type.lane_bits() { 1529 return errors.nonfatal(( 1530 inst, 1531 self.context(inst), 1532 format!( 1533 "input {} must be smaller than output {}", 1534 arg_type, ctrl_type, 1535 ), 1536 )); 1537 } 1538 } 1539 Opcode::Breduce | Opcode::Ireduce | Opcode::Fdemote => { 1540 if arg_type.lane_count() != ctrl_type.lane_count() { 1541 return errors.nonfatal(( 1542 inst, 1543 self.context(inst), 1544 format!( 1545 "input {} and output {} must have same number of lanes", 1546 arg_type, ctrl_type, 1547 ), 1548 )); 1549 } 1550 if arg_type.lane_bits() <= ctrl_type.lane_bits() { 1551 return errors.nonfatal(( 1552 inst, 1553 self.context(inst), 1554 format!( 1555 "input {} must be larger than output {}", 1556 arg_type, ctrl_type, 1557 ), 1558 )); 1559 } 1560 } 1561 _ => {} 1562 } 1563 } 1564 ir::InstructionData::HeapAddr { heap, arg, .. } => { 1565 let index_type = self.func.dfg.value_type(arg); 1566 let heap_index_type = self.func.heaps[heap].index_type; 1567 if index_type != heap_index_type { 1568 return errors.nonfatal(( 1569 inst, 1570 self.context(inst), 1571 format!( 1572 "index type {} differs from heap index type {}", 1573 index_type, heap_index_type, 1574 ), 1575 )); 1576 } 1577 } 1578 ir::InstructionData::TableAddr { table, arg, .. } => { 1579 let index_type = self.func.dfg.value_type(arg); 1580 let table_index_type = self.func.tables[table].index_type; 1581 if index_type != table_index_type { 1582 return errors.nonfatal(( 1583 inst, 1584 self.context(inst), 1585 format!( 1586 "index type {} differs from table index type {}", 1587 index_type, table_index_type, 1588 ), 1589 )); 1590 } 1591 } 1592 ir::InstructionData::UnaryGlobalValue { global_value, .. } => { 1593 if let Some(isa) = self.isa { 1594 let inst_type = self.func.dfg.value_type(self.func.dfg.first_result(inst)); 1595 let global_type = self.func.global_values[global_value].global_type(isa); 1596 if inst_type != global_type { 1597 return errors.nonfatal(( 1598 inst, self.context(inst), 1599 format!( 1600 "global_value instruction with type {} references global value with type {}", 1601 inst_type, global_type 1602 )), 1603 ); 1604 } 1605 } 1606 } 1607 _ => {} 1608 } 1609 Ok(()) 1610 } 1611 1612 fn cfg_integrity( 1613 &self, 1614 cfg: &ControlFlowGraph, 1615 errors: &mut VerifierErrors, 1616 ) -> VerifierStepResult<()> { 1617 let mut expected_succs = BTreeSet::<Block>::new(); 1618 let mut got_succs = BTreeSet::<Block>::new(); 1619 let mut expected_preds = BTreeSet::<Inst>::new(); 1620 let mut got_preds = BTreeSet::<Inst>::new(); 1621 1622 for block in self.func.layout.blocks() { 1623 expected_succs.extend(self.expected_cfg.succ_iter(block)); 1624 got_succs.extend(cfg.succ_iter(block)); 1625 1626 let missing_succs: Vec<Block> = 1627 expected_succs.difference(&got_succs).cloned().collect(); 1628 if !missing_succs.is_empty() { 1629 errors.report(( 1630 block, 1631 format!("cfg lacked the following successor(s) {:?}", missing_succs), 1632 )); 1633 continue; 1634 } 1635 1636 let excess_succs: Vec<Block> = got_succs.difference(&expected_succs).cloned().collect(); 1637 if !excess_succs.is_empty() { 1638 errors.report(( 1639 block, 1640 format!("cfg had unexpected successor(s) {:?}", excess_succs), 1641 )); 1642 continue; 1643 } 1644 1645 expected_preds.extend( 1646 self.expected_cfg 1647 .pred_iter(block) 1648 .map(|BlockPredecessor { inst, .. }| inst), 1649 ); 1650 got_preds.extend( 1651 cfg.pred_iter(block) 1652 .map(|BlockPredecessor { inst, .. }| inst), 1653 ); 1654 1655 let missing_preds: Vec<Inst> = expected_preds.difference(&got_preds).cloned().collect(); 1656 if !missing_preds.is_empty() { 1657 errors.report(( 1658 block, 1659 format!( 1660 "cfg lacked the following predecessor(s) {:?}", 1661 missing_preds 1662 ), 1663 )); 1664 continue; 1665 } 1666 1667 let excess_preds: Vec<Inst> = got_preds.difference(&expected_preds).cloned().collect(); 1668 if !excess_preds.is_empty() { 1669 errors.report(( 1670 block, 1671 format!("cfg had unexpected predecessor(s) {:?}", excess_preds), 1672 )); 1673 continue; 1674 } 1675 1676 expected_succs.clear(); 1677 got_succs.clear(); 1678 expected_preds.clear(); 1679 got_preds.clear(); 1680 } 1681 errors.as_result() 1682 } 1683 1684 fn immediate_constraints( 1685 &self, 1686 inst: Inst, 1687 errors: &mut VerifierErrors, 1688 ) -> VerifierStepResult<()> { 1689 let inst_data = &self.func.dfg[inst]; 1690 1691 match *inst_data { 1692 ir::InstructionData::Store { flags, .. } => { 1693 if flags.readonly() { 1694 errors.fatal(( 1695 inst, 1696 self.context(inst), 1697 "A store instruction cannot have the `readonly` MemFlag", 1698 )) 1699 } else { 1700 Ok(()) 1701 } 1702 } 1703 ir::InstructionData::BinaryImm8 { 1704 opcode: ir::instructions::Opcode::Extractlane, 1705 imm: lane, 1706 arg, 1707 .. 1708 } 1709 | ir::InstructionData::TernaryImm8 { 1710 opcode: ir::instructions::Opcode::Insertlane, 1711 imm: lane, 1712 args: [arg, _], 1713 .. 1714 } => { 1715 // We must be specific about the opcodes above because other instructions are using 1716 // the same formats. 1717 let ty = self.func.dfg.value_type(arg); 1718 if lane as u32 >= ty.lane_count() { 1719 errors.fatal(( 1720 inst, 1721 self.context(inst), 1722 format!("The lane {} does not index into the type {}", lane, ty,), 1723 )) 1724 } else { 1725 Ok(()) 1726 } 1727 } 1728 _ => Ok(()), 1729 } 1730 } 1731 1732 fn typecheck_function_signature(&self, errors: &mut VerifierErrors) -> VerifierStepResult<()> { 1733 self.func 1734 .signature 1735 .params 1736 .iter() 1737 .enumerate() 1738 .filter(|(_, ¶m)| param.value_type == types::INVALID) 1739 .for_each(|(i, _)| { 1740 errors.report(( 1741 AnyEntity::Function, 1742 format!("Parameter at position {} has an invalid type", i), 1743 )); 1744 }); 1745 1746 self.func 1747 .signature 1748 .returns 1749 .iter() 1750 .enumerate() 1751 .filter(|(_, &ret)| ret.value_type == types::INVALID) 1752 .for_each(|(i, _)| { 1753 errors.report(( 1754 AnyEntity::Function, 1755 format!("Return value at position {} has an invalid type", i), 1756 )) 1757 }); 1758 1759 self.func 1760 .signature 1761 .returns 1762 .iter() 1763 .enumerate() 1764 .for_each(|(i, ret)| { 1765 if let ArgumentPurpose::StructArgument(_) = ret.purpose { 1766 errors.report(( 1767 AnyEntity::Function, 1768 format!("Return value at position {} can't be an struct argument", i), 1769 )) 1770 } 1771 }); 1772 1773 if errors.has_error() { 1774 Err(()) 1775 } else { 1776 Ok(()) 1777 } 1778 } 1779 1780 pub fn run(&self, errors: &mut VerifierErrors) -> VerifierStepResult<()> { 1781 self.verify_global_values(errors)?; 1782 self.verify_heaps(errors)?; 1783 self.verify_tables(errors)?; 1784 self.verify_jump_tables(errors)?; 1785 self.typecheck_entry_block_params(errors)?; 1786 self.check_entry_not_cold(errors)?; 1787 self.typecheck_function_signature(errors)?; 1788 1789 for block in self.func.layout.blocks() { 1790 if self.func.layout.first_inst(block).is_none() { 1791 return errors.fatal((block, format!("{} cannot be empty", block))); 1792 } 1793 for inst in self.func.layout.block_insts(block) { 1794 self.block_integrity(block, inst, errors)?; 1795 self.instruction_integrity(inst, errors)?; 1796 self.typecheck(inst, errors)?; 1797 self.immediate_constraints(inst, errors)?; 1798 } 1799 1800 self.encodable_as_bb(block, errors)?; 1801 } 1802 1803 verify_flags(self.func, &self.expected_cfg, errors)?; 1804 1805 if !errors.is_empty() { 1806 log::warn!( 1807 "Found verifier errors in function:\n{}", 1808 pretty_verifier_error(self.func, None, errors.clone()) 1809 ); 1810 } 1811 1812 Ok(()) 1813 } 1814 } 1815 1816 #[cfg(test)] 1817 mod tests { 1818 use super::{Verifier, VerifierError, VerifierErrors}; 1819 use crate::entity::EntityList; 1820 use crate::ir::instructions::{InstructionData, Opcode}; 1821 use crate::ir::{types, AbiParam, Function}; 1822 use crate::settings; 1823 1824 macro_rules! assert_err_with_msg { 1825 ($e:expr, $msg:expr) => { 1826 match $e.0.get(0) { 1827 None => panic!("Expected an error"), 1828 Some(&VerifierError { ref message, .. }) => { 1829 if !message.contains($msg) { 1830 #[cfg(feature = "std")] 1831 panic!("'{}' did not contain the substring '{}'", message, $msg); 1832 #[cfg(not(feature = "std"))] 1833 panic!("error message did not contain the expected substring"); 1834 } 1835 } 1836 } 1837 }; 1838 } 1839 1840 #[test] 1841 fn empty() { 1842 let func = Function::new(); 1843 let flags = &settings::Flags::new(settings::builder()); 1844 let verifier = Verifier::new(&func, flags.into()); 1845 let mut errors = VerifierErrors::default(); 1846 1847 assert_eq!(verifier.run(&mut errors), Ok(())); 1848 assert!(errors.0.is_empty()); 1849 } 1850 1851 #[test] 1852 fn bad_instruction_format() { 1853 let mut func = Function::new(); 1854 let block0 = func.dfg.make_block(); 1855 func.layout.append_block(block0); 1856 let nullary_with_bad_opcode = func.dfg.make_inst(InstructionData::UnaryImm { 1857 opcode: Opcode::F32const, 1858 imm: 0.into(), 1859 }); 1860 func.layout.append_inst(nullary_with_bad_opcode, block0); 1861 func.stencil.layout.append_inst( 1862 func.stencil.dfg.make_inst(InstructionData::Jump { 1863 opcode: Opcode::Jump, 1864 destination: block0, 1865 args: EntityList::default(), 1866 }), 1867 block0, 1868 ); 1869 let flags = &settings::Flags::new(settings::builder()); 1870 let verifier = Verifier::new(&func, flags.into()); 1871 let mut errors = VerifierErrors::default(); 1872 1873 let _ = verifier.run(&mut errors); 1874 1875 assert_err_with_msg!(errors, "instruction format"); 1876 } 1877 1878 #[test] 1879 fn test_function_invalid_param() { 1880 let mut func = Function::new(); 1881 func.signature.params.push(AbiParam::new(types::INVALID)); 1882 1883 let mut errors = VerifierErrors::default(); 1884 let flags = &settings::Flags::new(settings::builder()); 1885 let verifier = Verifier::new(&func, flags.into()); 1886 1887 let _ = verifier.typecheck_function_signature(&mut errors); 1888 assert_err_with_msg!(errors, "Parameter at position 0 has an invalid type"); 1889 } 1890 1891 #[test] 1892 fn test_function_invalid_return_value() { 1893 let mut func = Function::new(); 1894 func.signature.returns.push(AbiParam::new(types::INVALID)); 1895 1896 let mut errors = VerifierErrors::default(); 1897 let flags = &settings::Flags::new(settings::builder()); 1898 let verifier = Verifier::new(&func, flags.into()); 1899 1900 let _ = verifier.typecheck_function_signature(&mut errors); 1901 assert_err_with_msg!(errors, "Return value at position 0 has an invalid type"); 1902 } 1903 1904 #[test] 1905 fn test_printing_contextual_errors() { 1906 // Build function. 1907 let mut func = Function::new(); 1908 let block0 = func.dfg.make_block(); 1909 func.layout.append_block(block0); 1910 1911 // Build instruction: v0, v1 = iconst 42 1912 let inst = func.dfg.make_inst(InstructionData::UnaryImm { 1913 opcode: Opcode::Iconst, 1914 imm: 42.into(), 1915 }); 1916 func.dfg.append_result(inst, types::I32); 1917 func.dfg.append_result(inst, types::I32); 1918 func.layout.append_inst(inst, block0); 1919 1920 // Setup verifier. 1921 let mut errors = VerifierErrors::default(); 1922 let flags = &settings::Flags::new(settings::builder()); 1923 let verifier = Verifier::new(&func, flags.into()); 1924 1925 // Now the error message, when printed, should contain the instruction sequence causing the 1926 // error (i.e. v0, v1 = iconst.i32 42) and not only its entity value (i.e. inst0) 1927 let _ = verifier.typecheck_results(inst, types::I32, &mut errors); 1928 assert_eq!( 1929 format!("{}", errors.0[0]), 1930 "inst0 (v0, v1 = iconst.i32 42): has more result values than expected" 1931 ) 1932 } 1933 1934 #[test] 1935 fn test_empty_block() { 1936 let mut func = Function::new(); 1937 let block0 = func.dfg.make_block(); 1938 func.layout.append_block(block0); 1939 1940 let flags = &settings::Flags::new(settings::builder()); 1941 let verifier = Verifier::new(&func, flags.into()); 1942 let mut errors = VerifierErrors::default(); 1943 let _ = verifier.run(&mut errors); 1944 1945 assert_err_with_msg!(errors, "block0 cannot be empty"); 1946 } 1947 } 1948