[llvm] 247ecc9 - [AsmParser] Check that addrspace fits within 24 bits

Alex Bradbury via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 9 01:54:03 PST 2022


Author: Luke Lau
Date: 2022-12-09T09:53:42Z
New Revision: 247ecc99e43130adbcca6fb604da3f336c839d16

URL: https://github.com/llvm/llvm-project/commit/247ecc99e43130adbcca6fb604da3f336c839d16
DIFF: https://github.com/llvm/llvm-project/commit/247ecc99e43130adbcca6fb604da3f336c839d16.diff

LOG: [AsmParser] Check that addrspace fits within 24 bits

Address spaces equal or larger than 1 << 24 don't fit and produce an
assertion during debug builds, or worse in release. This causes an error
to be reported during parsing instead.

Differential Revision: https://reviews.llvm.org/D139528

Added: 
    llvm/test/Assembler/invalid-addrspace.ll

Modified: 
    llvm/lib/AsmParser/LLParser.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp
index 866243c75864e..36ebdb09c8268 100644
--- a/llvm/lib/AsmParser/LLParser.cpp
+++ b/llvm/lib/AsmParser/LLParser.cpp
@@ -1802,10 +1802,15 @@ bool LLParser::parseOptionalAddrSpace(unsigned &AddrSpace, unsigned DefaultAS) {
       }
       Lex.Lex();
       return false;
-    } else if (Lex.getKind() != lltok::APSInt) {
-      return tokError("expected integer or string constant");
     }
-    return parseUInt32(AddrSpace);
+    if (Lex.getKind() != lltok::APSInt)
+      return tokError("expected integer or string constant");
+    SMLoc Loc = Lex.getLoc();
+    if (parseUInt32(AddrSpace))
+      return true;
+    if (!isUInt<24>(AddrSpace))
+      return error(Loc, "invalid address space, must be a 24-bit integer");
+    return false;
   };
 
   return parseToken(lltok::lparen, "expected '(' in address space") ||

diff  --git a/llvm/test/Assembler/invalid-addrspace.ll b/llvm/test/Assembler/invalid-addrspace.ll
new file mode 100644
index 0000000000000..fb8fe340678b8
--- /dev/null
+++ b/llvm/test/Assembler/invalid-addrspace.ll
@@ -0,0 +1,8 @@
+; RUN: not llvm-as < %s 2>&1 | FileCheck %s
+; Check that parser rejects address spaces that are too large to fit in the 24 bits
+
+define void @f() {
+; CHECK: invalid address space, must be a 24-bit integer
+  %y = alloca i32, addrspace(16777216)
+  ret void
+}


        


More information about the llvm-commits mailing list