[lld] r326431 - [ELF] - Don't crash on broken MEMORY declaration.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 1 04:36:01 PST 2018


Author: grimar
Date: Thu Mar  1 04:36:01 2018
New Revision: 326431

URL: http://llvm.org/viewvc/llvm-project?rev=326431&view=rev
Log:
[ELF] - Don't crash on broken MEMORY declaration.

LLD crashes with broken scripts shown in testcase,
because fails to read memory regon name and accesses
MemoryRegions's element which is nullptr.
Patch fixes it.

Differential revision: https://reviews.llvm.org/D43866

Added:
    lld/trunk/test/ELF/linkerscript/broken-memory-declaration.s
Modified:
    lld/trunk/ELF/ScriptParser.cpp

Modified: lld/trunk/ELF/ScriptParser.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/ScriptParser.cpp?rev=326431&r1=326430&r2=326431&view=diff
==============================================================================
--- lld/trunk/ELF/ScriptParser.cpp (original)
+++ lld/trunk/ELF/ScriptParser.cpp Thu Mar  1 04:36:01 2018
@@ -889,7 +889,7 @@ Expr ScriptParser::readConstant() {
   if (S == "MAXPAGESIZE")
     return [] { return Config->MaxPageSize; };
   setError("unknown constant: " + S);
-  return {};
+  return [] { return 0; };
 }
 
 // Parses Tok as an integer. It recognizes hexadecimal (prefixed with
@@ -1052,8 +1052,10 @@ Expr ScriptParser::readPrimary() {
   }
   if (Tok == "LENGTH") {
     StringRef Name = readParenLiteral();
-    if (Script->MemoryRegions.count(Name) == 0)
+    if (Script->MemoryRegions.count(Name) == 0) {
       setError("memory region not defined: " + Name);
+      return [] { return 0; };
+    }
     return [=] { return Script->MemoryRegions[Name]->Length; };
   }
   if (Tok == "LOADADDR") {
@@ -1066,8 +1068,10 @@ Expr ScriptParser::readPrimary() {
   }
   if (Tok == "ORIGIN") {
     StringRef Name = readParenLiteral();
-    if (Script->MemoryRegions.count(Name) == 0)
+    if (Script->MemoryRegions.count(Name) == 0) {
       setError("memory region not defined: " + Name);
+      return [] { return 0; };
+    }
     return [=] { return Script->MemoryRegions[Name]->Origin; };
   }
   if (Tok == "SEGMENT_START") {

Added: lld/trunk/test/ELF/linkerscript/broken-memory-declaration.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/broken-memory-declaration.s?rev=326431&view=auto
==============================================================================
--- lld/trunk/test/ELF/linkerscript/broken-memory-declaration.s (added)
+++ lld/trunk/test/ELF/linkerscript/broken-memory-declaration.s Thu Mar  1 04:36:01 2018
@@ -0,0 +1,13 @@
+# REQUIRES: x86
+
+## Check we do not crash.
+
+# RUN: echo "MEMORY { FLASH (rx) : ORIGIN = 0x1000< LENGTH" > %t.script
+# RUN: not ld.lld -o %t --script %t.script 2>&1 | FileCheck %s
+# CHECK: unexpected EOF
+
+# RUN: echo "MEMORY { FLASH (rx) : ORIGIN = 0x1000< ORIGIN" > %t.script
+# RUN: not ld.lld -o %t --script %t.script 2>&1 | FileCheck %s
+
+# RUN: echo "MEMORY { FLASH (rx) : ORIGIN = 0x1000, LENGTH = CONSTANT" > %t.script
+# RUN: not ld.lld -o %t --script %t.script 2>&1 | FileCheck %s




More information about the llvm-commits mailing list