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

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: Clean up code some more. Created 5 years, 3 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 const Ice::IceString UnknownName("function");
Jim Stichnoth 2015/09/25 22:13:20 Can you just do: return "function"; ?
Karl 2015/09/28 18:23:11 Done.
2068 return UnknownName;
2069 }
2051 }; 2070 };
2052 2071
2053 void FunctionParser::ExitBlock() { 2072 void FunctionParser::ExitBlock() {
2054 // Check if the last instruction in the function was terminating. 2073 // Check if the last instruction in the function was terminating.
2055 if (!InstIsTerminating) { 2074 if (!InstIsTerminating) {
2056 Error("Last instruction in function not terminator"); 2075 Error("Last instruction in function not terminator");
2057 if (isIRGenerationDisabled()) 2076 if (isIRGenerationDisabled())
2058 return; 2077 return;
2059 // Recover by inserting an unreachable instruction. 2078 // Recover by inserting an unreachable instruction.
2060 CurrentNode->appendInst(Ice::InstUnreachable::create(Func.get())); 2079 CurrentNode->appendInst(Ice::InstUnreachable::create(Func.get()));
(...skipping 592 matching lines...) Expand 10 before | Expand all | Expand 10 after
2653 Ice::SizeT ParamsStartIndex = 2; 2672 Ice::SizeT ParamsStartIndex = 2;
2654 if (Record.GetCode() == naclbitc::FUNC_CODE_INST_CALL) { 2673 if (Record.GetCode() == naclbitc::FUNC_CODE_INST_CALL) {
2655 if (!isValidRecordSizeAtLeast(2, "call")) 2674 if (!isValidRecordSizeAtLeast(2, "call"))
2656 return; 2675 return;
2657 } else { 2676 } else {
2658 if (!isValidRecordSizeAtLeast(3, "call indirect")) 2677 if (!isValidRecordSizeAtLeast(3, "call indirect"))
2659 return; 2678 return;
2660 ParamsStartIndex = 3; 2679 ParamsStartIndex = 3;
2661 } 2680 }
2662 2681
2663 // Extract out the called function and its return type.
2664 uint32_t CalleeIndex = convertRelativeToAbsIndex(Values[1], BaseIndex); 2682 uint32_t CalleeIndex = convertRelativeToAbsIndex(Values[1], BaseIndex);
2665 Ice::Operand *Callee = getOperand(CalleeIndex); 2683 Ice::Operand *Callee = getOperand(CalleeIndex);
2684
2685 // Pull out signature/return type of call (if possible).
2686 Ice::FunctionDeclaration *Fcn = nullptr;
2666 const Ice::FuncSigType *Signature = nullptr; 2687 const Ice::FuncSigType *Signature = nullptr;
2667 Ice::Type ReturnType = Ice::IceType_void; 2688 Ice::Type ReturnType = Ice::IceType_void;
2668 const Ice::Intrinsics::FullIntrinsicInfo *IntrinsicInfo = nullptr; 2689 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) { 2690 if (Record.GetCode() == naclbitc::FUNC_CODE_INST_CALL) {
2672 Fcn = Context->getFunctionByID(CalleeIndex); 2691 Fcn = Context->getFunctionByID(CalleeIndex);
2673 Signature = &Fcn->getSignature(); 2692 Signature = &Fcn->getSignature();
2674 ReturnType = Signature->getReturnType(); 2693 ReturnType = Signature->getReturnType();
2694 Ice::SizeT NumParams = Values.size() - ParamsStartIndex;
2695 if (NumParams != Signature->getNumArgs()) {
2696 std::string Buffer;
2697 raw_string_ostream StrBuf(Buffer);
2698 StrBuf << "Call to " << printName(Fcn) << " has " << NumParams
2699 << " parameters. Signature expects: " << Signature->getNumArgs();
2700 Error(StrBuf.str());
2701 if (ReturnType != Ice::IceType_void)
2702 setNextLocalInstIndex(nullptr);
2703 return;
2704 }
2675 2705
2676 // Check if this direct call is to an Intrinsic (starts with "llvm.") 2706 // Check if this direct call is to an Intrinsic (starts with "llvm.")
2677 bool BadIntrinsic; 2707 bool BadIntrinsic;
2678 const Ice::IceString &Name = Fcn->getName();
2679 IntrinsicInfo = getTranslator().getContext()->getIntrinsicsInfo().find( 2708 IntrinsicInfo = getTranslator().getContext()->getIntrinsicsInfo().find(
2680 Name, BadIntrinsic); 2709 Fcn->getName(), BadIntrinsic);
2681 if (BadIntrinsic) { 2710 if (BadIntrinsic) {
2682 std::string Buffer; 2711 std::string Buffer;
2683 raw_string_ostream StrBuf(Buffer); 2712 raw_string_ostream StrBuf(Buffer);
2684 StrBuf << "Invalid PNaCl intrinsic call to " << Name; 2713 StrBuf << "Invalid PNaCl intrinsic call to " << Fcn->getName();
2714 Error(StrBuf.str());
2715 IntrinsicInfo = nullptr;
2716 }
2717 if (IntrinsicInfo && IntrinsicInfo->getNumArgs() != NumParams) {
2718 std::string Buffer;
2719 raw_string_ostream StrBuf(Buffer);
2720 StrBuf << "Call to " << printName(Fcn) << " has " << NumParams
2721 << " parameters. Intrinsic expects: " << Signature->getNumArgs();
2685 Error(StrBuf.str()); 2722 Error(StrBuf.str());
2686 if (ReturnType != Ice::IceType_void) 2723 if (ReturnType != Ice::IceType_void)
2687 appendErrorInstruction(ReturnType); 2724 setNextLocalInstIndex(nullptr);
2688 return; 2725 return;
2689 } 2726 }
2690 } else { 2727 } else { // Record.GetCode() == naclbitc::FUNC_CODE_INST_CALL_INDIRECT
2728 // There is no signature. Assume defined by parameter types.
2691 ReturnType = Context->getSimpleTypeByID(Values[2]); 2729 ReturnType = Context->getSimpleTypeByID(Values[2]);
2730 if (!isIRGenerationDisabled() && Callee != nullptr)
2731 isValidPointerType(Callee, "Call indirect");
2732 }
2733
2734 if (Callee == nullptr && !isIRGenerationDisabled())
2735 return;
2736
2737 // Extract out the the call parameters.
2738 SmallVector<Ice::Operand*, 8> Params;
2739 for (Ice::SizeT Index = ParamsStartIndex; Index < Values.size(); ++Index) {
2740 Ice::Operand *Op = getRelativeOperand(Values[Index], BaseIndex);
2741 if (isIRGenerationDisabled())
2742 continue;
2743 if (Op == nullptr) {
2744 std::string Buffer;
2745 raw_string_ostream StrBuf(Buffer);
2746 StrBuf << "Parameter " << (Index - ParamsStartIndex + 1)
2747 << " of " << printName(Fcn) << " is not defined";
2748 Error(StrBuf.str());
2749 if (ReturnType != Ice::IceType_void)
2750 setNextLocalInstIndex(nullptr);
2751 return;
2752 }
2753 Params.push_back(Op);
2692 } 2754 }
2693 2755
2694 // Check return type. 2756 // Check return type.
2695 if (IntrinsicInfo == nullptr && !isCallReturnType(ReturnType)) { 2757 if (IntrinsicInfo == nullptr && !isCallReturnType(ReturnType)) {
2696 std::string Buffer; 2758 std::string Buffer;
2697 raw_string_ostream StrBuf(Buffer); 2759 raw_string_ostream StrBuf(Buffer);
2698 StrBuf << "Return type of called function is invalid: " << ReturnType; 2760 StrBuf << "Return type of " << printName(Fcn) << " is invalid: "
2761 << ReturnType;
2699 Error(StrBuf.str()); 2762 Error(StrBuf.str());
2700 ReturnType = Ice::IceType_i32; 2763 ReturnType = Ice::IceType_i32;
2701 } 2764 }
2702 2765
2766 if (isIRGenerationDisabled()) {
2767 if (ReturnType != Ice::IceType_void)
2768 setNextLocalInstIndex(nullptr);
2769 return;
2770 }
2771
2772 // Type check call parameters.
2773 for (Ice::SizeT Index = 0; Index < Params.size(); ++Index) {
2774 Ice::Operand *Op = Params[Index];
2775 Ice::Type OpType = Op->getType();
2776 if (Signature)
2777 verifyCallArgTypeMatches(
2778 Fcn, Index, OpType, Signature->getArgType(Index));
2779 if (IntrinsicInfo) {
2780 verifyCallArgTypeMatches(Fcn, Index, OpType,
2781 IntrinsicInfo->getArgType(Index));
2782 } else if (!isCallParameterType(OpType)) {
2783 std::string Buffer;
2784 raw_string_ostream StrBuf(Buffer);
2785 StrBuf << "Argument " << *Op << " of " << printName(Fcn)
2786 << " has invalid type: " << Op->getType();
2787 Error(StrBuf.str());
2788 appendErrorInstruction(ReturnType);
2789 }
2790 }
2791
2703 // Extract call information. 2792 // Extract call information.
2704 uint64_t CCInfo = Values[0]; 2793 uint64_t CCInfo = Values[0];
2705 CallingConv::ID CallingConv; 2794 CallingConv::ID CallingConv;
2706 if (!naclbitc::DecodeCallingConv(CCInfo >> 1, CallingConv)) { 2795 if (!naclbitc::DecodeCallingConv(CCInfo >> 1, CallingConv)) {
2707 std::string Buffer; 2796 std::string Buffer;
2708 raw_string_ostream StrBuf(Buffer); 2797 raw_string_ostream StrBuf(Buffer);
2709 StrBuf << "Function call calling convention value " << (CCInfo >> 1) 2798 StrBuf << "Function call calling convention value " << (CCInfo >> 1)
2710 << " not understood."; 2799 << " not understood.";
2711 Error(StrBuf.str()); 2800 Error(StrBuf.str());
2712 if (ReturnType != Ice::IceType_void) 2801 appendErrorInstruction(ReturnType);
2713 appendErrorInstruction(ReturnType);
2714 return; 2802 return;
2715 } 2803 }
2716 bool IsTailCall = static_cast<bool>(CCInfo & 1); 2804 bool IsTailCall = static_cast<bool>(CCInfo & 1);
2717 Ice::SizeT NumParams = Values.size() - ParamsStartIndex; 2805
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. 2806 // Create the call instruction.
2740 Ice::Variable *Dest = (ReturnType == Ice::IceType_void) 2807 Ice::Variable *Dest = (ReturnType == Ice::IceType_void)
2741 ? nullptr 2808 ? nullptr
2742 : getNextInstVar(ReturnType); 2809 : getNextInstVar(ReturnType);
2743 std::unique_ptr<Ice::InstCall> Inst; 2810 std::unique_ptr<Ice::InstCall> Inst;
2744 if (IntrinsicInfo) { 2811 if (IntrinsicInfo) {
2745 Inst.reset(Ice::InstIntrinsicCall::create(Func.get(), NumParams, Dest, 2812 Inst.reset(Ice::InstIntrinsicCall::create(Func.get(), Params.size(), Dest,
2746 Callee, IntrinsicInfo->Info)); 2813 Callee, IntrinsicInfo->Info));
2747 } else { 2814 } else {
2748 Inst.reset(Ice::InstCall::create(Func.get(), NumParams, Dest, Callee, 2815 Inst.reset(Ice::InstCall::create(Func.get(), Params.size(), Dest, Callee,
2749 IsTailCall)); 2816 IsTailCall));
2750 } 2817 }
2751 2818 for (auto Param : Params)
Jim Stichnoth 2015/09/25 22:13:20 auto *Param But actually, I'd prefer "Ice::Operan
Karl 2015/09/28 18:23:11 Done.
2752 // Add parameters. 2819 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()); 2820 CurrentNode->appendInst(Inst.release());
2834 return; 2821 return;
2835 } 2822 }
2836 case naclbitc::FUNC_CODE_INST_FORWARDTYPEREF: { 2823 case naclbitc::FUNC_CODE_INST_FORWARDTYPEREF: {
2837 // FORWARDTYPEREF: [opval, ty] 2824 // FORWARDTYPEREF: [opval, ty]
2838 if (!isValidRecordSize(2, "forward type ref")) 2825 if (!isValidRecordSize(2, "forward type ref"))
2839 return; 2826 return;
2840 Ice::Type OpType = Context->getSimpleTypeByID(Values[1]); 2827 Ice::Type OpType = Context->getSimpleTypeByID(Values[1]);
2841 setOperand(Values[0], 2828 setOperand(Values[0],
2842 isIRGenerationDisabled() ? nullptr : createInstVar(OpType)); 2829 isIRGenerationDisabled() ? nullptr : createInstVar(OpType));
(...skipping 453 matching lines...) Expand 10 before | Expand all | Expand 10 after
3296 raw_string_ostream StrBuf(Buffer); 3283 raw_string_ostream StrBuf(Buffer);
3297 StrBuf << IRFilename << ": Does not contain a module!"; 3284 StrBuf << IRFilename << ": Does not contain a module!";
3298 llvm::report_fatal_error(StrBuf.str()); 3285 llvm::report_fatal_error(StrBuf.str());
3299 } 3286 }
3300 if (InputStreamFile.getBitcodeBytes().getExtent() % 4 != 0) { 3287 if (InputStreamFile.getBitcodeBytes().getExtent() % 4 != 0) {
3301 llvm::report_fatal_error("Bitcode stream should be a multiple of 4 bytes"); 3288 llvm::report_fatal_error("Bitcode stream should be a multiple of 4 bytes");
3302 } 3289 }
3303 } 3290 }
3304 3291
3305 } // end of namespace Ice 3292 } // 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