[PATCH] D37217: Keep an instance of COFFOptTable alive as long as InputArgList is alive.

Rui Ueyama via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 28 09:25:10 PDT 2017


ruiu created this revision.

ArgParser created an instance of COFFOptTable on stack to use it to
parser command line arguments. Parsed arguments were then returned from
the function as InputArgList. This was safe because InputArgList referred
only statically-allocated InfoTable.

That is not a safe assumption after https://reviews.llvm.org/D36782,
which changes the type of its internal table from ArrayRef to std::vector.
To make lld work with that patch, we need to keep an instance of
COFFOptTable at least as long as an InputArgList is alive. This patch
does that.


https://reviews.llvm.org/D37217

Files:
  lld/COFF/Driver.cpp
  lld/COFF/Driver.h
  lld/COFF/DriverUtils.cpp


Index: lld/COFF/DriverUtils.cpp
===================================================================
--- lld/COFF/DriverUtils.cpp
+++ lld/COFF/DriverUtils.cpp
@@ -716,18 +716,14 @@
 #undef OPTION
 };
 
-class COFFOptTable : public llvm::opt::OptTable {
-public:
-  COFFOptTable() : OptTable(InfoTable, true) {}
-};
+COFFOptTable::COFFOptTable() : OptTable(InfoTable, true) {}
 
 // Parses a given list of options.
 opt::InputArgList ArgParser::parse(ArrayRef<const char *> ArgsArr) {
   // First, replace respnose files (@<file>-style options).
   std::vector<const char *> Argv = replaceResponseFiles(ArgsArr);
 
   // Make InputArgList from string vectors.
-  COFFOptTable Table;
   unsigned MissingIndex;
   unsigned MissingCount;
   opt::InputArgList Args = Table.ParseArgs(Argv, MissingIndex, MissingCount);
Index: lld/COFF/Driver.h
===================================================================
--- lld/COFF/Driver.h
+++ lld/COFF/Driver.h
@@ -41,6 +41,11 @@
 // Implemented in ICF.cpp.
 void doICF(const std::vector<Chunk *> &Chunks);
 
+class COFFOptTable : public llvm::opt::OptTable {
+public:
+  COFFOptTable();
+};
+
 class ArgParser {
 public:
   // Parses command line options.
@@ -54,8 +59,9 @@
 
 private:
   std::vector<const char *> tokenize(StringRef S);
-
   std::vector<const char *> replaceResponseFiles(std::vector<const char *>);
+
+  COFFOptTable Table;
 };
 
 class LinkerDriver {
@@ -71,7 +77,6 @@
                             StringRef ParentName);
 
 private:
-  ArgParser Parser;
   SymbolTable Symtab;
 
   std::unique_ptr<llvm::TarWriter> Tar; // for /linkrepro
Index: lld/COFF/Driver.cpp
===================================================================
--- lld/COFF/Driver.cpp
+++ lld/COFF/Driver.cpp
@@ -197,6 +197,7 @@
 // Parses .drectve section contents and returns a list of files
 // specified by /defaultlib.
 void LinkerDriver::parseDirectives(StringRef S) {
+  ArgParser Parser;
   opt::InputArgList Args = Parser.parse(S);
 
   for (auto *Arg : Args) {
@@ -691,6 +692,7 @@
   InitializeAllDisassemblers();
 
   // Parse command line options.
+  ArgParser Parser;
   opt::InputArgList Args = Parser.parseLINK(ArgsArr.slice(1));
 
   // Parse and evaluate -mllvm options.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D37217.112907.patch
Type: text/x-patch
Size: 2219 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170828/a47b85b4/attachment.bin>


More information about the llvm-commits mailing list