[llvm] r228676 - [Orc] Fix a bug in the LazyEmittingLayer - capture names by value (as

Lang Hames lhames at gmail.com
Mon Feb 9 23:35:39 PST 2015


Author: lhames
Date: Tue Feb 10 01:35:39 2015
New Revision: 228676

URL: http://llvm.org/viewvc/llvm-project?rev=228676&view=rev
Log:
[Orc] Fix a bug in the LazyEmittingLayer - capture names by value (as
std::strings) rather than StringRefs in JITSymbol get-address lambda.

Capturing a StringRef by-value is still effectively capturing a reference, which
is no good here because the referenced string may be gone by the time the lambda
is being evaluated the original value may be gone. Make sure to capture a
std::string instead.

No test case: This bug doesn't manifest under OrcMCJITReplacement, since it
keeps IR modules (from which the StringRefs are sourced) alive permanently.

Modified:
    llvm/trunk/include/llvm/ExecutionEngine/Orc/LazyEmittingLayer.h

Modified: llvm/trunk/include/llvm/ExecutionEngine/Orc/LazyEmittingLayer.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ExecutionEngine/Orc/LazyEmittingLayer.h?rev=228676&r1=228675&r2=228676&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ExecutionEngine/Orc/LazyEmittingLayer.h (original)
+++ llvm/trunk/include/llvm/ExecutionEngine/Orc/LazyEmittingLayer.h Tue Feb 10 01:35:39 2015
@@ -42,20 +42,24 @@ private:
     JITSymbol find(StringRef Name, bool ExportedSymbolsOnly, BaseLayerT &B) {
       switch (EmitState) {
       case NotEmitted:
-        if (provides(Name, ExportedSymbolsOnly))
+        if (provides(Name, ExportedSymbolsOnly)) {
+          // Create a std::string version of Name to capture here - the argument
+          // (a StringRef) may go away before the lambda is executed.
+          // FIXME: Use capture-init when we move to C++14. 
+          std::string PName = Name;
           return JITSymbol(
-              [this,ExportedSymbolsOnly,Name,&B]() -> TargetAddress {
+              [this, ExportedSymbolsOnly, PName, &B]() -> TargetAddress {
                 if (this->EmitState == Emitting)
                   return 0;
-                else if (this->EmitState != Emitted) {
+                else if (this->EmitState == NotEmitted) {
                   this->EmitState = Emitting;
                   Handle = this->emit(B);
                   this->EmitState = Emitted;
                 }
-                return B.findSymbolIn(Handle, Name, ExportedSymbolsOnly)
+                return B.findSymbolIn(Handle, PName, ExportedSymbolsOnly)
                           .getAddress();
               });
-        else
+        } else
           return nullptr;
       case Emitting:
         // Calling "emit" can trigger external symbol lookup (e.g. to check for





More information about the llvm-commits mailing list