Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(56)

Side by Side Diff: src/PNaClTranslator.cpp

Issue 1363983002: Check that address is i32 for indirect calls. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-subzero.git@master
Patch Set: Fix nits. Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | tests_lit/llvm2ice_tests/Input/no-terminator-inst.tbc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 //===- subzero/src/PNaClTranslator.cpp - ICE from bitcode -----------------===// 1 //===- subzero/src/PNaClTranslator.cpp - ICE from bitcode -----------------===//
2 // 2 //
3 // The Subzero Code Generator 3 // The Subzero Code Generator
4 // 4 //
5 // This file is distributed under the University of Illinois Open Source 5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details. 6 // License. See LICENSE.TXT for details.
7 // 7 //
8 //===----------------------------------------------------------------------===// 8 //===----------------------------------------------------------------------===//
9 /// 9 ///
10 /// \file 10 /// \file
(...skipping 1580 matching lines...) Expand 10 before | Expand all | Expand 10 after
1591 // Checks if the type of operand Op is the valid pointer type, for the given 1591 // Checks if the type of operand Op is the valid pointer type, for the given
1592 // InstructionName. Returns true if valid. Otherwise generates an error 1592 // InstructionName. Returns true if valid. Otherwise generates an error
1593 // message and returns false. 1593 // message and returns false.
1594 bool isValidPointerType(Ice::Operand *Op, const char *InstructionName) { 1594 bool isValidPointerType(Ice::Operand *Op, const char *InstructionName) {
1595 Ice::Type PtrType = Ice::getPointerType(); 1595 Ice::Type PtrType = Ice::getPointerType();
1596 if (Op->getType() == PtrType) 1596 if (Op->getType() == PtrType)
1597 return true; 1597 return true;
1598 std::string Buffer; 1598 std::string Buffer;
1599 raw_string_ostream StrBuf(Buffer); 1599 raw_string_ostream StrBuf(Buffer);
1600 StrBuf << InstructionName << " address not " << PtrType 1600 StrBuf << InstructionName << " address not " << PtrType
1601 << ". Found: " << *Op; 1601 << ". Found: " << Op->getType();
1602 Error(StrBuf.str()); 1602 Error(StrBuf.str());
1603 return false; 1603 return false;
1604 } 1604 }
1605 1605
1606 // Checks if loading/storing a value of type Ty is allowed. Returns true if 1606 // Checks if loading/storing a value of type Ty is allowed. Returns true if
1607 // Valid. Otherwise generates an error message and returns false. 1607 // Valid. Otherwise generates an error message and returns false.
1608 bool isValidLoadStoreType(Ice::Type Ty, const char *InstructionName) { 1608 bool isValidLoadStoreType(Ice::Type Ty, const char *InstructionName) {
1609 if (isLoadStoreType(Ty)) 1609 if (isLoadStoreType(Ty))
1610 return true; 1610 return true;
1611 std::string Buffer; 1611 std::string Buffer;
(...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after
2041 Ice::Operand *reportGetOperandUndefined(NaClBcIndexSize_t Index) { 2041 Ice::Operand *reportGetOperandUndefined(NaClBcIndexSize_t Index) {
2042 std::string Buffer; 2042 std::string Buffer;
2043 raw_string_ostream StrBuf(Buffer); 2043 raw_string_ostream StrBuf(Buffer);
2044 StrBuf << "Value index " << Index << " not defined!"; 2044 StrBuf << "Value index " << Index << " not defined!";
2045 Error(StrBuf.str()); 2045 Error(StrBuf.str());
2046 // Recover and return some value. 2046 // Recover and return some value.
2047 if (!LocalOperands.empty()) 2047 if (!LocalOperands.empty())
2048 return LocalOperands.front(); 2048 return LocalOperands.front();
2049 return Context->getGlobalConstantByID(0); 2049 return Context->getGlobalConstantByID(0);
2050 } 2050 }
2051
2052 void verifyCallArgTypeMatches(Ice::FunctionDeclaration *Fcn,
2053 Ice::SizeT Index, Ice::Type ArgType,
2054 Ice::Type ParamType) {
2055 if (ArgType != ParamType) {
2056 std::string Buffer;
2057 raw_string_ostream StrBuf(Buffer);
2058 StrBuf << "Argument " << (Index + 1) << " of " << printName(Fcn)
2059 << " expects " << ParamType << ". Found: " << ArgType;
2060 Error(StrBuf.str());
2061 }
2062 }
2063
2064 const Ice::IceString printName(Ice::FunctionDeclaration *Fcn) {
2065 if (Fcn)
2066 return Fcn->getName();
2067 return "function";
2068 }
2051 }; 2069 };
2052 2070
2053 void FunctionParser::ExitBlock() { 2071 void FunctionParser::ExitBlock() {
2054 // Check if the last instruction in the function was terminating. 2072 // Check if the last instruction in the function was terminating.
2055 if (!InstIsTerminating) { 2073 if (!InstIsTerminating) {
2056 Error("Last instruction in function not terminator"); 2074 Error("Last instruction in function not terminator");
2057 if (isIRGenerationDisabled()) 2075 if (isIRGenerationDisabled())
2058 return; 2076 return;
2059 // Recover by inserting an unreachable instruction. 2077 // Recover by inserting an unreachable instruction.
2060 CurrentNode->appendInst(Ice::InstUnreachable::create(Func.get())); 2078 CurrentNode->appendInst(Ice::InstUnreachable::create(Func.get()));
(...skipping 592 matching lines...) Expand 10 before | Expand all | Expand 10 after
2653 Ice::SizeT ParamsStartIndex = 2; 2671 Ice::SizeT ParamsStartIndex = 2;
2654 if (Record.GetCode() == naclbitc::FUNC_CODE_INST_CALL) { 2672 if (Record.GetCode() == naclbitc::FUNC_CODE_INST_CALL) {
2655 if (!isValidRecordSizeAtLeast(2, "call")) 2673 if (!isValidRecordSizeAtLeast(2, "call"))
2656 return; 2674 return;
2657 } else { 2675 } else {
2658 if (!isValidRecordSizeAtLeast(3, "call indirect")) 2676 if (!isValidRecordSizeAtLeast(3, "call indirect"))
2659 return; 2677 return;
2660 ParamsStartIndex = 3; 2678 ParamsStartIndex = 3;
2661 } 2679 }
2662 2680
2663 // Extract out the called function and its return type.
2664 uint32_t CalleeIndex = convertRelativeToAbsIndex(Values[1], BaseIndex); 2681 uint32_t CalleeIndex = convertRelativeToAbsIndex(Values[1], BaseIndex);
2665 Ice::Operand *Callee = getOperand(CalleeIndex); 2682 Ice::Operand *Callee = getOperand(CalleeIndex);
2683
2684 // Pull out signature/return type of call (if possible).
2685 Ice::FunctionDeclaration *Fcn = nullptr;
2666 const Ice::FuncSigType *Signature = nullptr; 2686 const Ice::FuncSigType *Signature = nullptr;
2667 Ice::Type ReturnType = Ice::IceType_void; 2687 Ice::Type ReturnType = Ice::IceType_void;
2668 const Ice::Intrinsics::FullIntrinsicInfo *IntrinsicInfo = nullptr; 2688 const Ice::Intrinsics::FullIntrinsicInfo *IntrinsicInfo = nullptr;
2669 // Name of function if a direct call/intrinsic. Null otherwise.
2670 Ice::FunctionDeclaration *Fcn = nullptr;
2671 if (Record.GetCode() == naclbitc::FUNC_CODE_INST_CALL) { 2689 if (Record.GetCode() == naclbitc::FUNC_CODE_INST_CALL) {
2672 Fcn = Context->getFunctionByID(CalleeIndex); 2690 Fcn = Context->getFunctionByID(CalleeIndex);
2673 Signature = &Fcn->getSignature(); 2691 Signature = &Fcn->getSignature();
2674 ReturnType = Signature->getReturnType(); 2692 ReturnType = Signature->getReturnType();
2693 Ice::SizeT NumParams = Values.size() - ParamsStartIndex;
2694 if (NumParams != Signature->getNumArgs()) {
2695 std::string Buffer;
2696 raw_string_ostream StrBuf(Buffer);
2697 StrBuf << "Call to " << printName(Fcn) << " has " << NumParams
2698 << " parameters. Signature expects: " << Signature->getNumArgs();
2699 Error(StrBuf.str());
2700 if (ReturnType != Ice::IceType_void)
2701 setNextLocalInstIndex(nullptr);
2702 return;
2703 }
2675 2704
2676 // Check if this direct call is to an Intrinsic (starts with "llvm.") 2705 // Check if this direct call is to an Intrinsic (starts with "llvm.")
2677 bool BadIntrinsic; 2706 bool BadIntrinsic;
2678 const Ice::IceString &Name = Fcn->getName();
2679 IntrinsicInfo = getTranslator().getContext()->getIntrinsicsInfo().find( 2707 IntrinsicInfo = getTranslator().getContext()->getIntrinsicsInfo().find(
2680 Name, BadIntrinsic); 2708 Fcn->getName(), BadIntrinsic);
2681 if (BadIntrinsic) { 2709 if (BadIntrinsic) {
2682 std::string Buffer; 2710 std::string Buffer;
2683 raw_string_ostream StrBuf(Buffer); 2711 raw_string_ostream StrBuf(Buffer);
2684 StrBuf << "Invalid PNaCl intrinsic call to " << Name; 2712 StrBuf << "Invalid PNaCl intrinsic call to " << Fcn->getName();
2713 Error(StrBuf.str());
2714 IntrinsicInfo = nullptr;
2715 }
2716 if (IntrinsicInfo && IntrinsicInfo->getNumArgs() != NumParams) {
2717 std::string Buffer;
2718 raw_string_ostream StrBuf(Buffer);
2719 StrBuf << "Call to " << printName(Fcn) << " has " << NumParams
2720 << " parameters. Intrinsic expects: " << Signature->getNumArgs();
2685 Error(StrBuf.str()); 2721 Error(StrBuf.str());
2686 if (ReturnType != Ice::IceType_void) 2722 if (ReturnType != Ice::IceType_void)
2687 appendErrorInstruction(ReturnType); 2723 setNextLocalInstIndex(nullptr);
2688 return; 2724 return;
2689 } 2725 }
2690 } else { 2726 } else { // Record.GetCode() == naclbitc::FUNC_CODE_INST_CALL_INDIRECT
2727 // There is no signature. Assume defined by parameter types.
2691 ReturnType = Context->getSimpleTypeByID(Values[2]); 2728 ReturnType = Context->getSimpleTypeByID(Values[2]);
2729 if (!isIRGenerationDisabled() && Callee != nullptr)
2730 isValidPointerType(Callee, "Call indirect");
2731 }
2732
2733 if (Callee == nullptr && !isIRGenerationDisabled())
2734 return;
2735
2736 // Extract out the the call parameters.
2737 SmallVector<Ice::Operand*, 8> Params;
2738 for (Ice::SizeT Index = ParamsStartIndex; Index < Values.size(); ++Index) {
2739 Ice::Operand *Op = getRelativeOperand(Values[Index], BaseIndex);
2740 if (isIRGenerationDisabled())
2741 continue;
2742 if (Op == nullptr) {
2743 std::string Buffer;
2744 raw_string_ostream StrBuf(Buffer);
2745 StrBuf << "Parameter " << (Index - ParamsStartIndex + 1)
2746 << " of " << printName(Fcn) << " is not defined";
2747 Error(StrBuf.str());
2748 if (ReturnType != Ice::IceType_void)
2749 setNextLocalInstIndex(nullptr);
2750 return;
2751 }
2752 Params.push_back(Op);
2692 } 2753 }
2693 2754
2694 // Check return type. 2755 // Check return type.
2695 if (IntrinsicInfo == nullptr && !isCallReturnType(ReturnType)) { 2756 if (IntrinsicInfo == nullptr && !isCallReturnType(ReturnType)) {
2696 std::string Buffer; 2757 std::string Buffer;
2697 raw_string_ostream StrBuf(Buffer); 2758 raw_string_ostream StrBuf(Buffer);
2698 StrBuf << "Return type of called function is invalid: " << ReturnType; 2759 StrBuf << "Return type of " << printName(Fcn) << " is invalid: "
2760 << ReturnType;
2699 Error(StrBuf.str()); 2761 Error(StrBuf.str());
2700 ReturnType = Ice::IceType_i32; 2762 ReturnType = Ice::IceType_i32;
2701 } 2763 }
2702 2764
2765 if (isIRGenerationDisabled()) {
2766 if (ReturnType != Ice::IceType_void)
2767 setNextLocalInstIndex(nullptr);
2768 return;
2769 }
2770
2771 // Type check call parameters.
2772 for (Ice::SizeT Index = 0; Index < Params.size(); ++Index) {
2773 Ice::Operand *Op = Params[Index];
2774 Ice::Type OpType = Op->getType();
2775 if (Signature)
2776 verifyCallArgTypeMatches(
2777 Fcn, Index, OpType, Signature->getArgType(Index));
2778 if (IntrinsicInfo) {
2779 verifyCallArgTypeMatches(Fcn, Index, OpType,
2780 IntrinsicInfo->getArgType(Index));
2781 } else if (!isCallParameterType(OpType)) {
2782 std::string Buffer;
2783 raw_string_ostream StrBuf(Buffer);
2784 StrBuf << "Argument " << *Op << " of " << printName(Fcn)
2785 << " has invalid type: " << Op->getType();
2786 Error(StrBuf.str());
2787 appendErrorInstruction(ReturnType);
2788 }
2789 }
2790
2703 // Extract call information. 2791 // Extract call information.
2704 uint64_t CCInfo = Values[0]; 2792 uint64_t CCInfo = Values[0];
2705 CallingConv::ID CallingConv; 2793 CallingConv::ID CallingConv;
2706 if (!naclbitc::DecodeCallingConv(CCInfo >> 1, CallingConv)) { 2794 if (!naclbitc::DecodeCallingConv(CCInfo >> 1, CallingConv)) {
2707 std::string Buffer; 2795 std::string Buffer;
2708 raw_string_ostream StrBuf(Buffer); 2796 raw_string_ostream StrBuf(Buffer);
2709 StrBuf << "Function call calling convention value " << (CCInfo >> 1) 2797 StrBuf << "Function call calling convention value " << (CCInfo >> 1)
2710 << " not understood."; 2798 << " not understood.";
2711 Error(StrBuf.str()); 2799 Error(StrBuf.str());
2712 if (ReturnType != Ice::IceType_void) 2800 appendErrorInstruction(ReturnType);
2713 appendErrorInstruction(ReturnType);
2714 return; 2801 return;
2715 } 2802 }
2716 bool IsTailCall = static_cast<bool>(CCInfo & 1); 2803 bool IsTailCall = static_cast<bool>(CCInfo & 1);
2717 Ice::SizeT NumParams = Values.size() - ParamsStartIndex; 2804
2718 if (Signature && NumParams != Signature->getNumArgs()) {
2719 std::string Buffer;
2720 raw_string_ostream StrBuf(Buffer);
2721 StrBuf << "Call has " << NumParams
2722 << " parameters. Signature expects: " << Signature->getNumArgs();
2723 Error(StrBuf.str());
2724 // Error recover by only checking parameters in both signature and call.
2725 NumParams = std::min(NumParams, Signature->getNumArgs());
2726 }
2727 if (isIRGenerationDisabled()) {
2728 assert(Callee == nullptr);
2729 // Check that parameters are defined.
2730 for (Ice::SizeT ParamIndex = 0; ParamIndex < NumParams; ++ParamIndex) {
2731 assert(getRelativeOperand(Values[ParamsStartIndex + ParamIndex],
2732 BaseIndex) == nullptr);
2733 }
2734 // Define value slot only if value returned.
2735 if (ReturnType != Ice::IceType_void)
2736 setNextLocalInstIndex(nullptr);
2737 return;
2738 }
2739 // Create the call instruction. 2805 // Create the call instruction.
2740 Ice::Variable *Dest = (ReturnType == Ice::IceType_void) 2806 Ice::Variable *Dest = (ReturnType == Ice::IceType_void)
2741 ? nullptr 2807 ? nullptr
2742 : getNextInstVar(ReturnType); 2808 : getNextInstVar(ReturnType);
2743 std::unique_ptr<Ice::InstCall> Inst; 2809 std::unique_ptr<Ice::InstCall> Inst;
2744 if (IntrinsicInfo) { 2810 if (IntrinsicInfo) {
2745 Inst.reset(Ice::InstIntrinsicCall::create(Func.get(), NumParams, Dest, 2811 Inst.reset(Ice::InstIntrinsicCall::create(Func.get(), Params.size(), Dest,
2746 Callee, IntrinsicInfo->Info)); 2812 Callee, IntrinsicInfo->Info));
2747 } else { 2813 } else {
2748 Inst.reset(Ice::InstCall::create(Func.get(), NumParams, Dest, Callee, 2814 Inst.reset(Ice::InstCall::create(Func.get(), Params.size(), Dest, Callee,
2749 IsTailCall)); 2815 IsTailCall));
2750 } 2816 }
2751 2817 for (Ice::Operand *Param : Params)
2752 // Add parameters. 2818 Inst->addArg(Param);
2753 for (Ice::SizeT ParamIndex = 0; ParamIndex < NumParams; ++ParamIndex) {
2754 Ice::Operand *Op =
2755 getRelativeOperand(Values[ParamsStartIndex + ParamIndex], BaseIndex);
2756 if (Op == nullptr) {
2757 std::string Buffer;
2758 raw_string_ostream StrBuf(Buffer);
2759 StrBuf << "Parameter " << ParamIndex << " of call not defined";
2760 Error(StrBuf.str());
2761 if (ReturnType != Ice::IceType_void)
2762 appendErrorInstruction(ReturnType);
2763 return;
2764 }
2765
2766 // Check that parameter type is valid.
2767 if (Signature) {
2768 if (Op->getType() != Signature->getArgType(ParamIndex)) {
2769 std::string Buffer;
2770 raw_string_ostream StrBuf(Buffer);
2771 StrBuf << "Call argument " << *Op << " expects "
2772 << Signature->getArgType(ParamIndex)
2773 << ". Found: " << Op->getType();
2774 Error(StrBuf.str());
2775 } else if (IntrinsicInfo == nullptr &&
2776 !isCallParameterType(Op->getType())) {
2777 // TODO(kschimpf): Move this check to the function declaration, so
2778 // that it only needs to be checked once.
2779 std::string Buffer;
2780 raw_string_ostream StrBuf(Buffer);
2781 StrBuf << "Call argument " << *Op
2782 << " matches declaration but has invalid type: "
2783 << Op->getType();
2784 Error(StrBuf.str());
2785 }
2786 } else if (!isCallParameterType(Op->getType())) {
2787 std::string Buffer;
2788 raw_string_ostream StrBuf(Buffer);
2789 StrBuf << "Call argument " << *Op
2790 << " has invalid type: " << Op->getType();
2791 Error(StrBuf.str());
2792 }
2793 Inst->addArg(Op);
2794 }
2795
2796 // If intrinsic call, validate call signature.
2797 if (IntrinsicInfo) {
2798 Ice::SizeT ArgIndex = 0;
2799 switch (IntrinsicInfo->validateCall(Inst.get(), ArgIndex)) {
2800 case Ice::Intrinsics::IsValidCall:
2801 break;
2802 case Ice::Intrinsics::BadReturnType: {
2803 std::string Buffer;
2804 raw_string_ostream StrBuf(Buffer);
2805 StrBuf << "Intrinsic " << Fcn->getName() << " expects return type"
2806 << IntrinsicInfo->getReturnType()
2807 << ". Found: " << Inst->getReturnType();
2808 Error(StrBuf.str());
2809 break;
2810 }
2811 case Ice::Intrinsics::WrongNumOfArgs: {
2812 std::string Buffer;
2813 raw_string_ostream StrBuf(Buffer);
2814 StrBuf << "Intrinsic " << Fcn->getName() << " expects "
2815 << IntrinsicInfo->getNumArgs()
2816 << ". Found: " << Inst->getNumArgs();
2817 Error(StrBuf.str());
2818 break;
2819 }
2820 case Ice::Intrinsics::WrongCallArgType: {
2821 std::string Buffer;
2822 raw_string_ostream StrBuf(Buffer);
2823 StrBuf << "Intrinsic " << Fcn->getName() << " expects "
2824 << IntrinsicInfo->getArgType(ArgIndex) << " for argument "
2825 << (ArgIndex + 1)
2826 << ". Found: " << Inst->getArg(ArgIndex)->getType();
2827 Error(StrBuf.str());
2828 break;
2829 }
2830 }
2831 }
2832
2833 CurrentNode->appendInst(Inst.release()); 2819 CurrentNode->appendInst(Inst.release());
2834 return; 2820 return;
2835 } 2821 }
2836 case naclbitc::FUNC_CODE_INST_FORWARDTYPEREF: { 2822 case naclbitc::FUNC_CODE_INST_FORWARDTYPEREF: {
2837 // FORWARDTYPEREF: [opval, ty] 2823 // FORWARDTYPEREF: [opval, ty]
2838 if (!isValidRecordSize(2, "forward type ref")) 2824 if (!isValidRecordSize(2, "forward type ref"))
2839 return; 2825 return;
2840 Ice::Type OpType = Context->getSimpleTypeByID(Values[1]); 2826 Ice::Type OpType = Context->getSimpleTypeByID(Values[1]);
2841 setOperand(Values[0], 2827 setOperand(Values[0],
2842 isIRGenerationDisabled() ? nullptr : createInstVar(OpType)); 2828 isIRGenerationDisabled() ? nullptr : createInstVar(OpType));
(...skipping 453 matching lines...) Expand 10 before | Expand all | Expand 10 after
3296 raw_string_ostream StrBuf(Buffer); 3282 raw_string_ostream StrBuf(Buffer);
3297 StrBuf << IRFilename << ": Does not contain a module!"; 3283 StrBuf << IRFilename << ": Does not contain a module!";
3298 llvm::report_fatal_error(StrBuf.str()); 3284 llvm::report_fatal_error(StrBuf.str());
3299 } 3285 }
3300 if (InputStreamFile.getBitcodeBytes().getExtent() % 4 != 0) { 3286 if (InputStreamFile.getBitcodeBytes().getExtent() % 4 != 0) {
3301 llvm::report_fatal_error("Bitcode stream should be a multiple of 4 bytes"); 3287 llvm::report_fatal_error("Bitcode stream should be a multiple of 4 bytes");
3302 } 3288 }
3303 } 3289 }
3304 3290
3305 } // end of namespace Ice 3291 } // end of namespace Ice
OLDNEW
« no previous file with comments | « no previous file | tests_lit/llvm2ice_tests/Input/no-terminator-inst.tbc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698