about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/libexpr/eval.cc22
-rw-r--r--src/libexpr/eval.hh8
-rw-r--r--src/libexpr/primops.cc27
-rw-r--r--src/nix-instantiate/nix-instantiate.cc2
4 files changed, 59 insertions, 0 deletions
diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc
index 2af6a6787dfc..70b3365639b2 100644
--- a/src/libexpr/eval.cc
+++ b/src/libexpr/eval.cc
@@ -230,6 +230,8 @@ EvalState::EvalState(const Strings & _searchPath)
 
 EvalState::~EvalState()
 {
+    fileEvalCache.clear();
+    printCanaries();
 }
 
 
@@ -1464,4 +1466,24 @@ void EvalState::printStats()
 }
 
 
+void EvalState::printCanaries()
+{
+#if HAVE_BOEHMGC
+    if (!settings.get("debug-gc", false)) return;
+
+    GC_gcollect();
+
+    if (gcCanaries.empty()) {
+        printMsg(lvlError, "all canaries have been garbage-collected");
+        return;
+    }
+
+    printMsg(lvlError, "the following canaries have not been garbage-collected:");
+
+    for (auto i : gcCanaries)
+        printMsg(lvlError, format("  %1%") % i->string.s);
+#endif
+}
+
+
 }
diff --git a/src/libexpr/eval.hh b/src/libexpr/eval.hh
index 82c1a43b13d6..af9452c7539f 100644
--- a/src/libexpr/eval.hh
+++ b/src/libexpr/eval.hh
@@ -255,6 +255,8 @@ public:
     /* Print statistics. */
     void printStats();
 
+    void printCanaries();
+
 private:
 
     unsigned long nrEnvs;
@@ -285,6 +287,12 @@ private:
     friend struct ExprOpConcatLists;
     friend struct ExprSelect;
     friend void prim_getAttr(EvalState & state, const Pos & pos, Value * * args, Value & v);
+
+#if HAVE_BOEHMGC
+    std::set<Value *> gcCanaries;
+    friend void canaryFinalizer(GC_PTR obj, GC_PTR client_data);
+    friend void prim_gcCanary(EvalState & state, const Pos & pos, Value * * args, Value & v);
+#endif
 };
 
 
diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc
index 0c4381b11368..2dd242a43f74 100644
--- a/src/libexpr/primops.cc
+++ b/src/libexpr/primops.cc
@@ -397,6 +397,32 @@ static void prim_trace(EvalState & state, const Pos & pos, Value * * args, Value
 }
 
 
+#if HAVE_BOEHMGC
+void canaryFinalizer(GC_PTR obj, GC_PTR client_data)
+{
+    Value * v = (Value *) obj;
+    EvalState & state(* (EvalState *) client_data);
+    printMsg(lvlError, format("canary ‘%1%’ garbage-collected") % v->string.s);
+    auto i = state.gcCanaries.find(v);
+    assert(i != state.gcCanaries.end());
+    state.gcCanaries.erase(i);
+}
+#endif
+
+
+void prim_gcCanary(EvalState & state, const Pos & pos, Value * * args, Value & v)
+{
+    string s = state.forceStringNoCtx(*args[0], pos);
+    state.mkList(v, 1);
+    Value * canary = v.list.elems[0] = state.allocValue();
+#if HAVE_BOEHMGC
+    state.gcCanaries.insert(canary);
+    GC_register_finalizer(canary, canaryFinalizer, &state, 0, 0);
+#endif
+    mkString(*canary, s);
+}
+
+
 /*************************************************************
  * Derivations
  *************************************************************/
@@ -1393,6 +1419,7 @@ void EvalState::createBaseEnv()
     addPrimOp("__tryEval", 1, prim_tryEval);
     addPrimOp("__getEnv", 1, prim_getEnv);
     addPrimOp("__trace", 2, prim_trace);
+    addPrimOp("__gcCanary", 1, prim_gcCanary);
 
     // Paths
     addPrimOp("__toPath", 1, prim_toPath);
diff --git a/src/nix-instantiate/nix-instantiate.cc b/src/nix-instantiate/nix-instantiate.cc
index 5080160c4015..7a38f2ac4269 100644
--- a/src/nix-instantiate/nix-instantiate.cc
+++ b/src/nix-instantiate/nix-instantiate.cc
@@ -86,6 +86,8 @@ void processExpr(EvalState & state, const Strings & attrPaths,
             }
         }
     }
+
+    state.printCanaries();
 }