1 19 20 package edu.umd.cs.findbugs.ba; 21 22 import org.apache.bcel.Constants; 23 import org.apache.bcel.generic.*; 24 25 51 public abstract class AbstractFrameModelingVisitor <Value, FrameType extends Frame<Value>> implements Visitor { 52 private FrameType frame; 53 private Location location; 54 protected ConstantPoolGen cpg; 55 56 61 public AbstractFrameModelingVisitor(ConstantPoolGen cpg) { 62 this.frame = null; 63 this.cpg = cpg; 64 } 65 66 74 public void analyzeInstruction(Instruction ins) throws DataflowAnalysisException { 75 try { 76 ins.accept(this); 77 } catch (InvalidBytecodeException e) { 78 System.out.println("Could not analyze " + ins); 79 e.printStackTrace(System.out); 80 throw new DataflowAnalysisException("Invalid bytecode", e); 81 } 82 } 83 84 87 public ConstantPoolGen getCPG() { 88 return cpg; 89 } 90 91 98 public void setFrameAndLocation(FrameType frame, Location location) { 99 this.frame = frame; 100 this.location = location; 101 } 102 103 108 public FrameType getFrame() { 109 return frame; 110 } 111 112 117 public Location getLocation() { 118 return location; 119 } 120 121 126 public abstract Value getDefaultValue(); 127 128 131 public int getNumWordsConsumed(Instruction ins) { 132 int numWordsConsumed = ins.consumeStack(cpg); 133 if (numWordsConsumed == Constants.UNPREDICTABLE) 134 throw new InvalidBytecodeException("Unpredictable stack consumption"); 135 return numWordsConsumed; 136 } 137 138 141 public int getNumWordsProduced(Instruction ins) { 142 int numWordsProduced = ins.produceStack(cpg); 143 if (numWordsProduced == Constants.UNPREDICTABLE) 144 throw new InvalidBytecodeException("Unpredictable stack productions"); 145 return numWordsProduced; 146 } 147 148 153 private void illegalBytecode(Instruction ins) { 154 throw new InvalidBytecodeException("Illegal bytecode: " + ins); 155 } 156 157 160 161 public void visitStackInstruction(StackInstruction obj) { 162 } 163 164 public void visitLocalVariableInstruction(LocalVariableInstruction obj) { 165 } 166 167 public void visitBranchInstruction(BranchInstruction obj) { 168 } 169 170 public void visitLoadClass(LoadClass obj) { 171 } 172 173 public void visitFieldInstruction(FieldInstruction obj) { 174 } 175 176 public void visitIfInstruction(IfInstruction obj) { 177 } 178 179 public void visitConversionInstruction(ConversionInstruction obj) { 180 } 181 182 public void visitPopInstruction(PopInstruction obj) { 183 } 184 185 public void visitJsrInstruction(JsrInstruction obj) { 186 } 187 188 public void visitGotoInstruction(GotoInstruction obj) { 189 } 190 191 public void visitStoreInstruction(StoreInstruction obj) { 192 } 193 194 public void visitTypedInstruction(TypedInstruction obj) { 195 } 196 197 public void visitSelect(Select obj) { 198 } 199 200 public void visitUnconditionalBranch(UnconditionalBranch obj) { 201 } 202 203 public void visitPushInstruction(PushInstruction obj) { 204 } 205 206 public void visitArithmeticInstruction(ArithmeticInstruction obj) { 207 } 208 209 public void visitCPInstruction(CPInstruction obj) { 210 } 211 212 public void visitInvokeInstruction(InvokeInstruction obj) { 213 } 214 215 public void visitArrayInstruction(ArrayInstruction obj) { 216 } 217 218 public void visitAllocationInstruction(AllocationInstruction obj) { 219 } 220 221 public void visitReturnInstruction(ReturnInstruction obj) { 222 } 223 224 public void visitFieldOrMethod(FieldOrMethod obj) { 225 } 226 227 public void visitConstantPushInstruction(ConstantPushInstruction obj) { 228 } 229 230 public void visitExceptionThrower(ExceptionThrower obj) { 231 } 232 233 public void visitLoadInstruction(LoadInstruction obj) { 234 } 235 236 public void visitVariableLengthInstruction(VariableLengthInstruction obj) { 237 } 238 239 public void visitStackProducer(StackProducer obj) { 240 } 241 242 public void visitStackConsumer(StackConsumer obj) { 243 } 244 245 248 249 254 public void handleStoreInstruction(StoreInstruction obj) { 255 try { 256 int numConsumed = obj.consumeStack(cpg); 257 if (numConsumed == Constants.UNPREDICTABLE) 258 throw new InvalidBytecodeException("Unpredictable stack consumption"); 259 260 int index = obj.getIndex(); 261 262 while (numConsumed-- > 0) { 265 Value value = frame.popValue(); 266 frame.setValue(index++, value); 267 } 268 } catch (DataflowAnalysisException e) { 269 throw new InvalidBytecodeException(e.toString()); 270 } 271 } 272 273 278 public void handleLoadInstruction(LoadInstruction obj) { 279 int numProduced = obj.produceStack(cpg); 280 if (numProduced == Constants.UNPREDICTABLE) 281 throw new InvalidBytecodeException("Unpredictable stack production"); 282 283 int index = obj.getIndex() + numProduced; 284 285 while (numProduced-- > 0) { 289 Value value = frame.getValue(--index); 290 frame.pushValue(value); 291 } 292 } 293 294 299 public void handleNormalInstruction(Instruction ins) { 300 modelNormalInstruction(ins, getNumWordsConsumed(ins), getNumWordsProduced(ins)); 301 } 302 303 311 public void modelNormalInstruction( 312 Instruction ins, 313 int numWordsConsumed, 314 int numWordsProduced) { 315 modelInstruction(ins, numWordsConsumed, numWordsProduced, getDefaultValue()); 316 } 317 318 327 public void modelInstruction( 328 Instruction ins, 329 int numWordsConsumed, 330 int numWordsProduced, 331 Value pushValue) { 332 try { 333 while (numWordsConsumed-- > 0) 334 frame.popValue(); 335 } catch (DataflowAnalysisException e) { 336 throw new InvalidBytecodeException(e.toString()); 337 } 338 339 while (numWordsProduced-- > 0) 340 frame.pushValue(pushValue); 341 } 342 343 346 347 public void visitASTORE(ASTORE obj) { 348 handleStoreInstruction(obj); 349 } 350 351 public void visitDSTORE(DSTORE obj) { 352 handleStoreInstruction(obj); 353 } 354 355 public void visitFSTORE(FSTORE obj) { 356 handleStoreInstruction(obj); 357 } 358 359 public void visitISTORE(ISTORE obj) { 360 handleStoreInstruction(obj); 361 } 362 363 public void visitLSTORE(LSTORE obj) { 364 handleStoreInstruction(obj); 365 } 366 367 370 371 public void visitALOAD(ALOAD obj) { 372 handleLoadInstruction(obj); 373 } 374 375 public void visitDLOAD(DLOAD obj) { 376 handleLoadInstruction(obj); 377 } 378 379 public void visitFLOAD(FLOAD obj) { 380 handleLoadInstruction(obj); 381 } 382 383 public void visitILOAD(ILOAD obj) { 384 handleLoadInstruction(obj); 385 } 386 387 public void visitLLOAD(LLOAD obj) { 388 handleLoadInstruction(obj); 389 } 390 391 394 395 public void visitPOP(POP obj) { 396 handleNormalInstruction(obj); 397 } 398 399 public void visitPOP2(POP2 obj) { 400 handleNormalInstruction(obj); 401 } 402 403 public void visitDUP(DUP obj) { 404 try { 405 Value value = frame.popValue(); 406 frame.pushValue(value); 407 frame.pushValue(value); 408 } catch (DataflowAnalysisException e) { 409 throw new InvalidBytecodeException(e.toString()); 410 } 411 } 412 413 public void visitDUP_X1(DUP_X1 obj) { 414 try { 415 Value value1 = frame.popValue(); 416 Value value2 = frame.popValue(); 417 frame.pushValue(value1); 418 frame.pushValue(value2); 419 frame.pushValue(value1); 420 } catch (DataflowAnalysisException e) { 421 throw new InvalidBytecodeException(e.toString()); 422 } 423 } 424 425 public void visitDUP_X2(DUP_X2 obj) { 426 try { 427 Value value1 = frame.popValue(); 428 Value value2 = frame.popValue(); 429 Value value3 = frame.popValue(); 430 frame.pushValue(value1); 431 frame.pushValue(value3); 432 frame.pushValue(value2); 433 frame.pushValue(value1); 434 } catch (DataflowAnalysisException e) { 435 throw new InvalidBytecodeException(e.toString()); 436 } 437 } 438 439 public void visitDUP2(DUP2 obj) { 440 try { 441 Value value1 = frame.popValue(); 442 Value value2 = frame.popValue(); 443 frame.pushValue(value2); 444 frame.pushValue(value1); 445 frame.pushValue(value2); 446 frame.pushValue(value1); 447 } catch (DataflowAnalysisException e) { 448 throw new InvalidBytecodeException(e.toString()); 449 } 450 } 451 452 public void visitDUP2_X1(DUP2_X1 obj) { 453 try { 454 Value value1 = frame.popValue(); 455 Value value2 = frame.popValue(); 456 Value value3 = frame.popValue(); 457 frame.pushValue(value2); 458 frame.pushValue(value1); 459 frame.pushValue(value3); 460 frame.pushValue(value2); 461 frame.pushValue(value1); 462 } catch (DataflowAnalysisException e) { 463 throw new InvalidBytecodeException(e.toString()); 464 } 465 } 466 467 public void visitDUP2_X2(DUP2_X2 obj) { 468 try { 469 Value value1 = frame.popValue(); 470 Value value2 = frame.popValue(); 471 Value value3 = frame.popValue(); 472 Value value4 = frame.popValue(); 473 frame.pushValue(value2); 474 frame.pushValue(value1); 475 frame.pushValue(value4); 476 frame.pushValue(value3); 477 frame.pushValue(value2); 478 frame.pushValue(value1); 479 } catch (DataflowAnalysisException e) { 480 throw new InvalidBytecodeException(e.toString()); 481 } 482 } 483 484 public void visitSWAP(SWAP obj) { 485 try { 486 Value value1 = frame.popValue(); 487 Value value2 = frame.popValue(); 488 frame.pushValue(value1); 489 frame.pushValue(value2); 490 } catch (DataflowAnalysisException e) { 491 throw new InvalidBytecodeException(e.toString()); 492 } 493 } 494 495 498 499 public void visitIMPDEP1(IMPDEP1 obj) { 500 illegalBytecode(obj); 501 } 502 503 public void visitIMPDEP2(IMPDEP2 obj) { 504 illegalBytecode(obj); 505 } 506 507 public void visitBREAKPOINT(BREAKPOINT obj) { 508 illegalBytecode(obj); 509 } 510 511 514 515 public void visitACONST_NULL(ACONST_NULL obj) { 516 handleNormalInstruction(obj); 517 } 518 519 public void visitGETSTATIC(GETSTATIC obj) { 520 handleNormalInstruction(obj); 521 } 522 523 public void visitIF_ICMPLT(IF_ICMPLT obj) { 524 handleNormalInstruction(obj); 525 } 526 527 public void visitMONITOREXIT(MONITOREXIT obj) { 528 handleNormalInstruction(obj); 529 } 530 531 public void visitIFLT(IFLT obj) { 532 handleNormalInstruction(obj); 533 } 534 535 public void visitBASTORE(BASTORE obj) { 536 handleNormalInstruction(obj); 537 } 538 539 public void visitCHECKCAST(CHECKCAST obj) { 540 handleNormalInstruction(obj); 541 } 542 543 public void visitFCMPG(FCMPG obj) { 544 handleNormalInstruction(obj); 545 } 546 547 public void visitI2F(I2F obj) { 548 handleNormalInstruction(obj); 549 } 550 551 public void visitATHROW(ATHROW obj) { 552 handleNormalInstruction(obj); 553 } 554 555 public void visitDCMPL(DCMPL obj) { 556 handleNormalInstruction(obj); 557 } 558 559 public void visitARRAYLENGTH(ARRAYLENGTH obj) { 560 handleNormalInstruction(obj); 561 } 562 563 public void visitINVOKESTATIC(INVOKESTATIC obj) { 564 handleNormalInstruction(obj); 565 } 566 567 public void visitLCONST(LCONST obj) { 568 handleNormalInstruction(obj); 569 } 570 571 public void visitDREM(DREM obj) { 572 handleNormalInstruction(obj); 573 } 574 575 public void visitIFGE(IFGE obj) { 576 handleNormalInstruction(obj); 577 } 578 579 public void visitCALOAD(CALOAD obj) { 580 handleNormalInstruction(obj); 581 } 582 583 public void visitLASTORE(LASTORE obj) { 584 handleNormalInstruction(obj); 585 } 586 587 public void visitI2D(I2D obj) { 588 handleNormalInstruction(obj); 589 } 590 591 public void visitDADD(DADD obj) { 592 handleNormalInstruction(obj); 593 } 594 595 public void visitINVOKESPECIAL(INVOKESPECIAL obj) { 596 handleNormalInstruction(obj); 597 } 598 599 public void visitIAND(IAND obj) { 600 handleNormalInstruction(obj); 601 } 602 603 public void visitPUTFIELD(PUTFIELD obj) { 604 handleNormalInstruction(obj); 605 } 606 607 public void visitDCONST(DCONST obj) { 608 handleNormalInstruction(obj); 609 } 610 611 public void visitNEW(NEW obj) { 612 handleNormalInstruction(obj); 613 } 614 615 public void visitIFNULL(IFNULL obj) { 616 handleNormalInstruction(obj); 617 } 618 619 public void visitLSUB(LSUB obj) { 620 handleNormalInstruction(obj); 621 } 622 623 public void visitL2I(L2I obj) { 624 handleNormalInstruction(obj); 625 } 626 627 public void visitISHR(ISHR obj) { 628 handleNormalInstruction(obj); 629 } 630 631 public void visitTABLESWITCH(TABLESWITCH obj) { 632 handleNormalInstruction(obj); 633 } 634 635 public void visitIINC(IINC obj) { 636 handleNormalInstruction(obj); 637 } 638 639 public void visitDRETURN(DRETURN obj) { 640 handleNormalInstruction(obj); 641 } 642 643 public void visitDASTORE(DASTORE obj) { 644 handleNormalInstruction(obj); 645 } 646 647 public void visitIALOAD(IALOAD obj) { 648 handleNormalInstruction(obj); 649 } 650 651 public void visitDDIV(DDIV obj) { 652 handleNormalInstruction(obj); 653 } 654 655 public void visitIF_ICMPGE(IF_ICMPGE obj) { 656 handleNormalInstruction(obj); 657 } 658 659 public void visitLAND(LAND obj) { 660 handleNormalInstruction(obj); 661 } 662 663 public void visitIDIV(IDIV obj) { 664 handleNormalInstruction(obj); 665 } 666 667 public void visitLOR(LOR obj) { 668 handleNormalInstruction(obj); 669 } 670 671 public void visitCASTORE(CASTORE obj) { 672 handleNormalInstruction(obj); 673 } 674 675 public void visitFREM(FREM obj) { 676 handleNormalInstruction(obj); 677 } 678 679 public void visitLDC(LDC obj) { 680 handleNormalInstruction(obj); 681 } 682 683 public void visitBIPUSH(BIPUSH obj) { 684 handleNormalInstruction(obj); 685 } 686 687 public void visitF2L(F2L obj) { 688 handleNormalInstruction(obj); 689 } 690 691 public void visitFMUL(FMUL obj) { 692 handleNormalInstruction(obj); 693 } 694 695 public void visitJSR(JSR obj) { 696 handleNormalInstruction(obj); 697 } 698 699 public void visitFSUB(FSUB obj) { 700 handleNormalInstruction(obj); 701 } 702 703 public void visitSASTORE(SASTORE obj) { 704 handleNormalInstruction(obj); 705 } 706 707 public void visitRETURN(RETURN obj) { 708 handleNormalInstruction(obj); 709 } 710 711 public void visitDALOAD(DALOAD obj) { 712 handleNormalInstruction(obj); 713 } 714 715 public void visitSIPUSH(SIPUSH obj) { 716 handleNormalInstruction(obj); 717 } 718 719 public void visitDSUB(DSUB obj) { 720 handleNormalInstruction(obj); 721 } 722 723 public void visitL2F(L2F obj) { 724 handleNormalInstruction(obj); 725 } 726 727 public void visitIF_ICMPGT(IF_ICMPGT obj) { 728 handleNormalInstruction(obj); 729 } 730 731 public void visitF2D(F2D obj) { 732 handleNormalInstruction(obj); 733 } 734 735 public void visitI2L(I2L obj) { 736 handleNormalInstruction(obj); 737 } 738 739 public void visitIF_ACMPNE(IF_ACMPNE obj) { 740 handleNormalInstruction(obj); 741 } 742 743 public void visitI2S(I2S obj) { 744 handleNormalInstruction(obj); 745 } 746 747 public void visitIFEQ(IFEQ obj) { 748 handleNormalInstruction(obj); 749 } 750 751 public void visitIOR(IOR obj) { 752 handleNormalInstruction(obj); 753 } 754 755 public void visitIREM(IREM obj) { 756 handleNormalInstruction(obj); 757 } 758 759 public void visitIASTORE(IASTORE obj) { 760 handleNormalInstruction(obj); 761 } 762 763 public void visitNEWARRAY(NEWARRAY obj) { 764 handleNormalInstruction(obj); 765 } 766 767 public void visitINVOKEINTERFACE(INVOKEINTERFACE obj) { 768 handleNormalInstruction(obj); 769 } 770 771 public void visitINEG(INEG obj) { 772 handleNormalInstruction(obj); 773 } 774 775 public void visitLCMP(LCMP obj) { 776 handleNormalInstruction(obj); 777 } 778 779 public void visitJSR_W(JSR_W obj) { 780 handleNormalInstruction(obj); 781 } 782 783 public void visitMULTIANEWARRAY(MULTIANEWARRAY obj) { 784 handleNormalInstruction(obj); 785 } 786 787 public void visitSALOAD(SALOAD obj) { 788 handleNormalInstruction(obj); 789 } 790 791 public void visitIFNONNULL(IFNONNULL obj) { 792 handleNormalInstruction(obj); 793 } 794 795 public void visitDMUL(DMUL obj) { 796 handleNormalInstruction(obj); 797 } 798 799 public void visitIFNE(IFNE obj) { 800 handleNormalInstruction(obj); 801 } 802 803 public void visitIF_ICMPLE(IF_ICMPLE obj) { 804 handleNormalInstruction(obj); 805 } 806 807 public void visitLDC2_W(LDC2_W obj) { 808 handleNormalInstruction(obj); 809 } 810 811 public void visitGETFIELD(GETFIELD obj) { 812 handleNormalInstruction(obj); 813 } 814 815 public void visitLADD(LADD obj) { 816 handleNormalInstruction(obj); 817 } 818 819 public void visitNOP(NOP obj) { 820 handleNormalInstruction(obj); 821 } 822 823 public void visitFALOAD(FALOAD obj) { 824 handleNormalInstruction(obj); 825 } 826 827 public void visitINSTANCEOF(INSTANCEOF obj) { 828 handleNormalInstruction(obj); 829 } 830 831 public void visitIFLE(IFLE obj) { 832 handleNormalInstruction(obj); 833 } 834 835 public void visitLXOR(LXOR obj) { 836 handleNormalInstruction(obj); 837 } 838 839 public void visitLRETURN(LRETURN obj) { 840 handleNormalInstruction(obj); 841 } 842 843 public void visitFCONST(FCONST obj) { 844 handleNormalInstruction(obj); 845 } 846 847 public void visitIUSHR(IUSHR obj) { 848 handleNormalInstruction(obj); 849 } 850 851 public void visitBALOAD(BALOAD obj) { 852 handleNormalInstruction(obj); 853 } 854 855 public void visitIF_ACMPEQ(IF_ACMPEQ obj) { 856 handleNormalInstruction(obj); 857 } 858 859 public void visitMONITORENTER(MONITORENTER obj) { 860 handleNormalInstruction(obj); 861 } 862 863 public void visitLSHL(LSHL obj) { 864 handleNormalInstruction(obj); 865 } 866 867 public void visitDCMPG(DCMPG obj) { 868 handleNormalInstruction(obj); 869 } 870 871 public void visitD2L(D2L obj) { 872 handleNormalInstruction(obj); 873 } 874 875 public void visitL2D(L2D obj) { 876 handleNormalInstruction(obj); 877 } 878 879 public void visitRET(RET obj) { 880 handleNormalInstruction(obj); 881 } 882 883 public void visitIFGT(IFGT obj) { 884 handleNormalInstruction(obj); 885 } 886 887 public void visitIXOR(IXOR obj) { 888 handleNormalInstruction(obj); 889 } 890 891 public void visitINVOKEVIRTUAL(INVOKEVIRTUAL obj) { 892 handleNormalInstruction(obj); 893 } 894 895 public void visitFASTORE(FASTORE obj) { 896 handleNormalInstruction(obj); 897 } 898 899 public void visitIRETURN(IRETURN obj) { 900 handleNormalInstruction(obj); 901 } 902 903 public void visitIF_ICMPNE(IF_ICMPNE obj) { 904 handleNormalInstruction(obj); 905 } 906 907 public void visitLDIV(LDIV obj) { 908 handleNormalInstruction(obj); 909 } 910 911 public void visitPUTSTATIC(PUTSTATIC obj) { 912 handleNormalInstruction(obj); 913 } 914 915 public void visitAALOAD(AALOAD obj) { 916 handleNormalInstruction(obj); 917 } 918 919 public void visitD2I(D2I obj) { 920 handleNormalInstruction(obj); 921 } 922 923 public void visitIF_ICMPEQ(IF_ICMPEQ obj) { 924 handleNormalInstruction(obj); 925 } 926 927 public void visitAASTORE(AASTORE obj) { 928 handleNormalInstruction(obj); 929 } 930 931 public void visitARETURN(ARETURN obj) { 932 handleNormalInstruction(obj); 933 } 934 935 public void visitFNEG(FNEG obj) { 936 handleNormalInstruction(obj); 937 } 938 939 public void visitGOTO_W(GOTO_W obj) { 940 handleNormalInstruction(obj); 941 } 942 943 public void visitD2F(D2F obj) { 944 handleNormalInstruction(obj); 945 } 946 947 public void visitGOTO(GOTO obj) { 948 handleNormalInstruction(obj); 949 } 950 951 public void visitISUB(ISUB obj) { 952 handleNormalInstruction(obj); 953 } 954 955 public void visitF2I(F2I obj) { 956 handleNormalInstruction(obj); 957 } 958 959 public void visitDNEG(DNEG obj) { 960 handleNormalInstruction(obj); 961 } 962 963 public void visitICONST(ICONST obj) { 964 handleNormalInstruction(obj); 965 } 966 967 public void visitFDIV(FDIV obj) { 968 handleNormalInstruction(obj); 969 } 970 971 public void visitI2B(I2B obj) { 972 handleNormalInstruction(obj); 973 } 974 975 public void visitLNEG(LNEG obj) { 976 handleNormalInstruction(obj); 977 } 978 979 public void visitLREM(LREM obj) { 980 handleNormalInstruction(obj); 981 } 982 983 public void visitIMUL(IMUL obj) { 984 handleNormalInstruction(obj); 985 } 986 987 public void visitIADD(IADD obj) { 988 handleNormalInstruction(obj); 989 } 990 991 public void visitLSHR(LSHR obj) { 992 handleNormalInstruction(obj); 993 } 994 995 public void visitLOOKUPSWITCH(LOOKUPSWITCH obj) { 996 handleNormalInstruction(obj); 997 } 998 999 public void visitFCMPL(FCMPL obj) { 1000 handleNormalInstruction(obj); 1001 } 1002 1003 public void visitI2C(I2C obj) { 1004 handleNormalInstruction(obj); 1005 } 1006 1007 public void visitLMUL(LMUL obj) { 1008 handleNormalInstruction(obj); 1009 } 1010 1011 public void visitLUSHR(LUSHR obj) { 1012 handleNormalInstruction(obj); 1013 } 1014 1015 public void visitISHL(ISHL obj) { 1016 handleNormalInstruction(obj); 1017 } 1018 1019 public void visitLALOAD(LALOAD obj) { 1020 handleNormalInstruction(obj); 1021 } 1022 1023 public void visitANEWARRAY(ANEWARRAY obj) { 1024 handleNormalInstruction(obj); 1025 } 1026 1027 public void visitFRETURN(FRETURN obj) { 1028 handleNormalInstruction(obj); 1029 } 1030 1031 public void visitFADD(FADD obj) { 1032 handleNormalInstruction(obj); 1033 } 1034} 1035 1036 | Popular Tags |