From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Samuel Attard <sattard@slack-corp.com>
Date: Wed, 22 May 2019 13:34:18 -0700
Subject: chore: prevent / warn non context-aware native modules being loaded

This should be updated to take advantage of https://github.com/nodejs/node/pull/29631
once we stop warning and begin to unilaterally prevent non-context aware modules
from being loaded.

diff --git a/lib/internal/bootstrap/pre_execution.js b/lib/internal/bootstrap/pre_execution.js
index 0a7c4a47c11eb4bb360b6a46fccb4692241bd2dc..b4a0f71af5853f427a10449b52509052fbe3facd 100644
--- a/lib/internal/bootstrap/pre_execution.js
+++ b/lib/internal/bootstrap/pre_execution.js
@@ -89,8 +89,10 @@ function patchProcessObject(expandArgv1) {
 
   if (expandArgv1 && process.argv[1] && !process.argv[1].startsWith('-')) {
     // Expand process.argv[1] into a full path.
+    if (!process.argv[1] || !process.argv[1].startsWith('electron/js2c')) {
     const path = require('path');
     process.argv[1] = path.resolve(process.argv[1]);
+    }
   }
 
   // TODO(joyeecheung): most of these should be deprecated and removed,
diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js
index 7d08fb05e9a0abeb82d416891968b23002dc3685..1f6f5e0cee36fb2a07963264f513a930c349b8fa 100644
--- a/lib/internal/modules/cjs/loader.js
+++ b/lib/internal/modules/cjs/loader.js
@@ -1111,6 +1111,13 @@ Module.prototype._compile = function(content, filename) {
   if (getOptionValue('--inspect-brk') && process._eval == null) {
     if (!resolvedArgv) {
       // We enter the repl if we're not given a filename argument.
+      // process._firstFileName is used by Embedders to tell node what
+      // the first "real" file is when they use themselves as the entry
+      // point
+      if (process._firstFileName) {
+        resolvedArgv = process._firstFileName
+        delete process._firstFileName
+      } else
       if (process.argv[1]) {
         try {
           resolvedArgv = Module._resolveFilename(process.argv[1], null, false);
diff --git a/src/env.h b/src/env.h
index d22b579b25ce4e6af8ec042e282e94248ea14162..67cefbe35f390ba25b49e422d10bca8b423a49a8 100644
--- a/src/env.h
+++ b/src/env.h
@@ -890,6 +890,15 @@ class Environment : public MemoryRetainer {
               uint64_t thread_id = kNoThreadId);
   ~Environment() override;
 
+  void ForceOnlyContextAwareNativeModules() {
+    force_context_aware_ = true;
+  }
+  void WarnNonContextAwareNativeModules() {
+    warn_non_context_aware_ = true;
+  }
+  bool force_context_aware() { return force_context_aware_; }
+  bool warn_non_context_aware() { return warn_non_context_aware_; }
+
   void InitializeLibuv(bool start_profiler_idle_notifier);
   inline const std::vector<std::string>& exec_argv();
   inline const std::vector<std::string>& argv();
@@ -1271,6 +1280,9 @@ class Environment : public MemoryRetainer {
   inline void ThrowError(v8::Local<v8::Value> (*fun)(v8::Local<v8::String>),
                          const char* errmsg);
 
+  bool force_context_aware_ = false;
+  bool warn_non_context_aware_ = false;
+
   std::list<binding::DLib> loaded_addons_;
   v8::Isolate* const isolate_;
   IsolateData* const isolate_data_;
diff --git a/src/node_binding.cc b/src/node_binding.cc
index 5291858bb164a262ca1d69d2582e037aeab23d55..a9ce41fbba4e8b0c4704c1d7795308ce18916739 100644
--- a/src/node_binding.cc
+++ b/src/node_binding.cc
@@ -3,6 +3,7 @@
 #include <atomic>
 #include "env-inl.h"
 #include "node_native_module_env.h"
+#include "node_process.h"
 #include "util.h"
 
 #if HAVE_OPENSSL
@@ -461,10 +462,22 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) {
 
     if (mp != nullptr) {
       if (mp->nm_context_register_func == nullptr) {
-        if (env->options()->force_context_aware) {
+        if (env->force_context_aware()) {
           dlib->Close();
-          THROW_ERR_NON_CONTEXT_AWARE_DISABLED(env);
+          char errmsg[1024];
+          snprintf(errmsg,
+                   sizeof(errmsg),
+                   "Loading non-context-aware native module in renderer: '%s', but app.allowRendererProcessReuse is true. See https://github.com/electron/electron/issues/18397.",
+                   *filename);
+          env->ThrowError(errmsg);
           return false;
+        } else if (env->warn_non_context_aware()) {
+          char errmsg[1024];
+          snprintf(errmsg,
+                   sizeof(errmsg),
+                   "Loading non-context-aware native module in renderer: '%s'. This is deprecated, see https://github.com/electron/electron/issues/18397.",
+                   *filename);
+          ProcessEmitWarningGeneric(env, errmsg, "Electron");
         }
       }
       mp->nm_dso_handle = dlib->handle_;
