]> granicus.if.org Git - clang/commitdiff
MIPS: Range check __builtin_mips_wrdsp / __builtin_mips_rddsp arguments against the...
authorSimon Atanasyan <satanasyan@mips.com>
Sun, 8 Jul 2012 09:30:00 +0000 (09:30 +0000)
committerSimon Atanasyan <satanasyan@mips.com>
Sun, 8 Jul 2012 09:30:00 +0000 (09:30 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@159911 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Sema/Sema.h
lib/Sema/SemaChecking.cpp
test/CodeGen/builtins-mips-args.c

index 53798217829a4bb2dfcc53a3a83aad3bc03244a7..c1d0d63819bdd1dcba849d379542ae7008e0e60c 100644 (file)
@@ -7061,6 +7061,7 @@ private:
 
   ExprResult CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall);
   bool CheckARMBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall);
+  bool CheckMipsBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall);
 
   bool SemaBuiltinVAStart(CallExpr *TheCall);
   bool SemaBuiltinUnorderedCompare(CallExpr *TheCall);
index 708fd14e49908ead81844233321b30e120056a9a..91f5ce581350f5da3bd825f14db1a88420b18c7a 100644 (file)
@@ -287,6 +287,13 @@ Sema::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
         if (CheckARMBuiltinFunctionCall(BuiltinID, TheCall))
           return ExprError();
         break;
+      case llvm::Triple::mips:
+      case llvm::Triple::mipsel:
+      case llvm::Triple::mips64:
+      case llvm::Triple::mips64el:
+        if (CheckMipsBuiltinFunctionCall(BuiltinID, TheCall))
+          return ExprError();
+        break;
       default:
         break;
     }
@@ -424,6 +431,33 @@ bool Sema::CheckARMBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
   return false;
 }
 
+bool Sema::CheckMipsBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
+  unsigned i = 0, l = 0, u = 0;
+  switch (BuiltinID) {
+  default: return false;
+  case Mips::BI__builtin_mips_wrdsp: i = 1; l = 0; u = 63; break;
+  case Mips::BI__builtin_mips_rddsp: i = 0; l = 0; u = 63; break;
+  };
+
+  // We can't check the value of a dependent argument.
+  if (TheCall->getArg(i)->isTypeDependent() ||
+      TheCall->getArg(i)->isValueDependent())
+    return false;
+
+  // Check that the immediate argument is actually a constant.
+  llvm::APSInt Result;
+  if (SemaBuiltinConstantArg(TheCall, i, Result))
+    return true;
+
+  // Range check against the upper/lower values for this instruction.
+  unsigned Val = Result.getZExtValue();
+  if (Val < l || Val > u)
+    return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range)
+      << l << u << TheCall->getArg(i)->getSourceRange();
+
+  return false;
+}
+
 /// Given a FunctionDecl's FormatAttr, attempts to populate the FomatStringInfo
 /// parameter with the FormatAttr's correct format_idx and firstDataArg.
 /// Returns true when the format fits the function and the FormatStringInfo has
index 7dc73776a780bfffeeab4676e663661021973afe..4bc6f248c18900f6f566cf7279656a40d1f4aec6 100644 (file)
@@ -6,4 +6,8 @@ void foo() {
   int a = 3;
   __builtin_mips_wrdsp(2052, a);  // expected-error{{argument to '__builtin_mips_wrdsp' must be a constant integer}}
   __builtin_mips_rddsp(a);        // expected-error{{argument to '__builtin_mips_rddsp' must be a constant integer}}
+  __builtin_mips_wrdsp(2052, -1); // expected-error{{argument should be a value from 0 to 63}}
+  __builtin_mips_rddsp(-1);       // expected-error{{argument should be a value from 0 to 63}}
+  __builtin_mips_wrdsp(2052, 64); // expected-error{{argument should be a value from 0 to 63}}
+  __builtin_mips_rddsp(64);       // expected-error{{argument should be a value from 0 to 63}}
 }