--- node/deps/v8/include/v8.h +++ node/deps/v8/include/v8.h @@ -6429,10 +6429,14 @@ */ static void SetFlagsFromCommandLine(int* argc, char** argv, bool remove_flags); + static void EnableCompilationForSourcelessUse(); + static void DisableCompilationForSourcelessUse(); + static void FixSourcelessScript(Isolate* v8_isolate, Local script); + /** Get the version string. */ static const char* GetVersion(); /** Callback function for reporting failed access checks.*/ V8_INLINE static V8_DEPRECATED( --- node/deps/v8/src/api.cc +++ node/deps/v8/src/api.cc @@ -547,10 +547,46 @@ void V8::SetFlagsFromCommandLine(int* argc, char** argv, bool remove_flags) { i::FlagList::SetFlagsFromCommandLine(argc, argv, remove_flags); } +bool save_lazy; +bool save_predictable; +bool save_serialize_toplevel; + + +void V8::EnableCompilationForSourcelessUse() { + save_lazy = i::FLAG_lazy; + i::FLAG_lazy = false; + save_predictable = i::FLAG_predictable; + i::FLAG_predictable = true; + save_serialize_toplevel = i::FLAG_serialize_toplevel; + i::FLAG_serialize_toplevel = true; + i::CpuFeatures::Reinitialize(); + i::CpuFeatures::Probe(true); +} + + +void V8::DisableCompilationForSourcelessUse() { + i::FLAG_lazy = save_lazy; + i::FLAG_predictable = save_predictable; + i::FLAG_serialize_toplevel = save_serialize_toplevel; + i::CpuFeatures::Reinitialize(); + i::CpuFeatures::Probe(false); +} + + +void V8::FixSourcelessScript(Isolate* v8_isolate, Local script) { + auto isolate = reinterpret_cast(v8_isolate); + auto object = i::Handle::cast(Utils::OpenHandle(*script)); + i::Handle function_info( + i::SharedFunctionInfo::cast(*object), object->GetIsolate()); + auto s = reinterpret_cast(function_info->script()); + s->set_source(isolate->heap()->undefined_value()); +} + + RegisteredExtension* RegisteredExtension::first_extension_ = NULL; RegisteredExtension::RegisteredExtension(Extension* extension) : extension_(extension) { } --- node/deps/v8/src/assembler.h +++ node/deps/v8/src/assembler.h @@ -235,10 +235,15 @@ } static void PrintTarget(); static void PrintFeatures(); + static void Reinitialize() { + supported_ = 0; + initialized_ = false; + } + private: friend class ExternalReference; friend class AssemblerBase; // Flush instruction cache. static void FlushICache(void* start, size_t size); --- node/deps/v8/src/objects.cc +++ node/deps/v8/src/objects.cc @@ -13396,10 +13396,13 @@ // Check if we should print {function} as a class. Handle class_start_position = JSReceiver::GetDataProperty( function, isolate->factory()->class_start_position_symbol()); if (class_start_position->IsSmi()) { + if (Script::cast(shared_info->script())->source()->IsUndefined()) { + return isolate->factory()->NewStringFromAsciiChecked("class {}"); + } Handle class_end_position = JSReceiver::GetDataProperty( function, isolate->factory()->class_end_position_symbol()); Handle script_source( String::cast(Script::cast(shared_info->script())->source()), isolate); return isolate->factory()->NewSubString( --- node/deps/v8/src/parsing/parser.cc +++ node/deps/v8/src/parsing/parser.cc @@ -5057,10 +5057,11 @@ return false; } bool Parser::Parse(ParseInfo* info) { + if (info->script()->source()->IsUndefined()) return false; DCHECK(info->literal() == NULL); FunctionLiteral* result = NULL; // Ok to use Isolate here; this function is only called in the main thread. DCHECK(parsing_on_main_thread_); Isolate* isolate = info->isolate(); --- node/deps/v8/src/snapshot/code-serializer.cc +++ node/deps/v8/src/snapshot/code-serializer.cc @@ -342,24 +342,36 @@ } SerializedCodeData::SanityCheckResult SerializedCodeData::SanityCheck( Isolate* isolate, String* source) const { uint32_t magic_number = GetMagicNumber(); - if (magic_number != ComputeMagicNumber(isolate)) return MAGIC_NUMBER_MISMATCH; + if (magic_number != ComputeMagicNumber(isolate)) { + // base::OS::PrintError("Pkg: MAGIC_NUMBER_MISMATCH\n"); // TODO enable after solving v8-cache/ncc issue + return MAGIC_NUMBER_MISMATCH; + } uint32_t version_hash = GetHeaderValue(kVersionHashOffset); - uint32_t source_hash = GetHeaderValue(kSourceHashOffset); uint32_t cpu_features = GetHeaderValue(kCpuFeaturesOffset); uint32_t flags_hash = GetHeaderValue(kFlagHashOffset); uint32_t c1 = GetHeaderValue(kChecksum1Offset); uint32_t c2 = GetHeaderValue(kChecksum2Offset); - if (version_hash != Version::Hash()) return VERSION_MISMATCH; - if (source_hash != SourceHash(source)) return SOURCE_MISMATCH; - if (cpu_features != static_cast(CpuFeatures::SupportedFeatures())) { + if (version_hash != Version::Hash()) { + base::OS::PrintError("Pkg: VERSION_MISMATCH\n"); + return VERSION_MISMATCH; + } + uint32_t host_features = static_cast(CpuFeatures::SupportedFeatures()); + if (cpu_features & (~host_features)) { + base::OS::PrintError("Pkg: CPU_FEATURES_MISMATCH\n"); return CPU_FEATURES_MISMATCH; } - if (flags_hash != FlagList::Hash()) return FLAGS_MISMATCH; - if (!Checksum(Payload()).Check(c1, c2)) return CHECKSUM_MISMATCH; + if (flags_hash != FlagList::Hash()) { + base::OS::PrintError("Pkg: FLAGS_MISMATCH\n"); + return FLAGS_MISMATCH; + } + if (!Checksum(Payload()).Check(c1, c2)) { + base::OS::PrintError("Pkg: CHECKSUM_MISMATCH\n"); + return CHECKSUM_MISMATCH; + } return CHECK_SUCCESS; } uint32_t SerializedCodeData::SourceHash(String* source) const { return source->length(); --- node/lib/child_process.js +++ node/lib/child_process.js @@ -54,11 +54,11 @@ } options.execPath = options.execPath || process.execPath; options.shell = false; - return spawn(options.execPath, args, options); + return exports.spawn(options.execPath, args, options); }; exports._forkChild = function(fd) { // set process.send() --- node/lib/internal/bootstrap_node.js +++ node/lib/internal/bootstrap_node.js @@ -83,10 +83,46 @@ // There are various modes that Node can run in. The most common two // are running from a script and running the REPL - but there are a few // others like the debugger or running --eval arguments. Here we decide // which mode we run in. + (function () { + var fs = NativeModule.require('fs'); + var vm = NativeModule.require('vm'); + function readPrelude (fd) { + var PAYLOAD_POSITION = '// PAYLOAD_POSITION //' | 0; + var PAYLOAD_SIZE = '// PAYLOAD_SIZE //' | 0; + var PRELUDE_POSITION = '// PRELUDE_POSITION //' | 0; + var PRELUDE_SIZE = '// PRELUDE_SIZE //' | 0; + if (!PRELUDE_POSITION) { + // no prelude - remove entrypoint from argv[1] + process.argv.splice(1, 1); + return { undoPatch: true }; + } + var prelude = new Buffer(PRELUDE_SIZE); + var read = fs.readSync(fd, prelude, 0, PRELUDE_SIZE, PRELUDE_POSITION); + if (read !== PRELUDE_SIZE) { + console.error('Pkg: Error reading from file.'); + process.exit(1); + } + var s = new vm.Script(prelude, { filename: 'pkg/prelude/bootstrap.js' }); + var fn = s.runInThisContext(); + return fn(process, NativeModule.require, + console, fd, PAYLOAD_POSITION, PAYLOAD_SIZE); + } + (function () { + var fd = fs.openSync(process.execPath, 'r'); + var result = readPrelude(fd); + if (result && result.undoPatch) { + var bindingFs = process.binding('fs'); + fs.internalModuleStat = bindingFs.internalModuleStat; + fs.internalModuleReadFile = bindingFs.internalModuleReadFile; + fs.closeSync(fd); + } + }()); + }()); + if (NativeModule.exists('_third_party_main')) { // To allow people to extend Node in different ways, this hook allows // one to drop a file lib/_third_party_main.js into the build // directory which will be executed instead of Node's normal loading. process.nextTick(function() { --- node/lib/module.js +++ node/lib/module.js @@ -6,12 +6,12 @@ const internalUtil = require('internal/util'); const vm = require('vm'); const assert = require('assert').ok; const fs = require('fs'); const path = require('path'); -const internalModuleReadFile = process.binding('fs').internalModuleReadFile; -const internalModuleStat = process.binding('fs').internalModuleStat; +const internalModuleReadFile = function (f) { return require('fs').internalModuleReadFile(f); }; +const internalModuleStat = function (f) { return require('fs').internalModuleStat(f); }; const preserveSymlinks = !!process.binding('config').preserveSymlinks; // If obj.hasOwnProperty has been overridden, then calling // obj.hasOwnProperty(prop) will break. // See: https://github.com/joyent/node/issues/1707 --- node/src/env.h +++ node/src/env.h @@ -192,10 +192,11 @@ V(shell_string, "shell") \ V(signal_string, "signal") \ V(size_string, "size") \ V(sni_context_err_string, "Invalid SNI context") \ V(sni_context_string, "sni_context") \ + V(sourceless_string, "sourceless") \ V(speed_string, "speed") \ V(stack_string, "stack") \ V(status_string, "status") \ V(stdio_string, "stdio") \ V(subject_string, "subject") \ --- node/src/node.cc +++ node/src/node.cc @@ -3621,10 +3621,11 @@ static void PrintHelp(); static bool ParseDebugOpt(const char* arg) { + return false; const char* port = nullptr; if (!strcmp(arg, "--debug")) { use_debug_agent = true; } else if (!strncmp(arg, "--debug=", sizeof("--debug=") - 1)) { @@ -4435,15 +4436,10 @@ } inline void PlatformInit() { #ifdef __POSIX__ - sigset_t sigmask; - sigemptyset(&sigmask); - sigaddset(&sigmask, SIGUSR1); - const int err = pthread_sigmask(SIG_SETMASK, &sigmask, nullptr); - // Make sure file descriptors 0-2 are valid before we start logging anything. for (int fd = STDIN_FILENO; fd <= STDERR_FILENO; fd += 1) { struct stat ignored; if (fstat(fd, &ignored) == 0) continue; @@ -4453,12 +4449,10 @@ ABORT(); if (fd != open("/dev/null", O_RDWR)) ABORT(); } - CHECK_EQ(err, 0); - #ifndef NODE_SHARED_MODE // Restore signal dispositions, the parent process may have changed them. struct sigaction act; memset(&act, 0, sizeof(act)); @@ -4637,14 +4631,10 @@ // is to prevent memory pointers from being moved around that are returned by // Buffer::Data(). const char no_typed_array_heap[] = "--typed_array_max_size_in_heap=0"; V8::SetFlagsFromString(no_typed_array_heap, sizeof(no_typed_array_heap) - 1); - if (!use_debug_agent) { - RegisterDebugSignalHandler(); - } - // We should set node_is_initialized here instead of in node::Start, // otherwise embedders using node::Init to initialize everything will not be // able to set it and native modules will not load for them. node_is_initialized = true; } --- node/src/node_contextify.cc +++ node/src/node_contextify.cc @@ -38,10 +38,11 @@ using v8::ScriptOrigin; using v8::String; using v8::TryCatch; using v8::Uint8Array; using v8::UnboundScript; +using v8::V8; using v8::Value; using v8::WeakCallbackInfo; class ContextifyContext { @@ -499,17 +500,19 @@ MaybeLocal lineOffset = GetLineOffsetArg(env, options); MaybeLocal columnOffset = GetColumnOffsetArg(env, options); Maybe maybe_display_errors = GetDisplayErrorsArg(env, options); MaybeLocal cached_data_buf = GetCachedData(env, options); Maybe maybe_produce_cached_data = GetProduceCachedData(env, options); + Maybe maybe_sourceless = GetSourceless(env, options); if (try_catch.HasCaught()) { try_catch.ReThrow(); return; } bool display_errors = maybe_display_errors.FromJust(); bool produce_cached_data = maybe_produce_cached_data.FromJust(); + bool sourceless = maybe_sourceless.FromJust(); ScriptCompiler::CachedData* cached_data = nullptr; if (!cached_data_buf.IsEmpty()) { Local ui8 = cached_data_buf.ToLocalChecked(); ArrayBuffer::Contents contents = ui8->Buffer()->GetContents(); @@ -527,22 +530,37 @@ if (source.GetCachedData() != nullptr) compile_options = ScriptCompiler::kConsumeCodeCache; else if (produce_cached_data) compile_options = ScriptCompiler::kProduceCodeCache; + if (sourceless && compile_options == ScriptCompiler::kProduceCodeCache) { + V8::EnableCompilationForSourcelessUse(); + } + MaybeLocal v8_script = ScriptCompiler::CompileUnboundScript( env->isolate(), &source, compile_options); + if (sourceless && compile_options == ScriptCompiler::kProduceCodeCache) { + V8::DisableCompilationForSourcelessUse(); + } + if (v8_script.IsEmpty()) { if (display_errors) { DecorateErrorStack(env, try_catch); } try_catch.ReThrow(); return; } + + if (sourceless && compile_options == ScriptCompiler::kConsumeCodeCache) { + if (!source.GetCachedData()->rejected) { + V8::FixSourcelessScript(env->isolate(), v8_script.ToLocalChecked()); + } + } + contextify_script->script_.Reset(env->isolate(), v8_script.ToLocalChecked()); if (compile_options == ScriptCompiler::kConsumeCodeCache) { args.This()->Set( @@ -836,10 +854,27 @@ Local value = maybe_value.ToLocalChecked(); return Just(value->IsTrue()); } + static Maybe GetSourceless(Environment* env, Local options) { + if (!options->IsObject()) { + return Just(false); + } + + MaybeLocal maybe_value = + options.As()->Get(env->context(), + env->sourceless_string()); + + if (maybe_value.IsEmpty()) + return Nothing(); + + Local value = maybe_value.ToLocalChecked(); + return Just(value->IsTrue()); + } + + static MaybeLocal GetLineOffsetArg(Environment* env, Local options) { Local defaultLineOffset = Integer::New(env->isolate(), 0); if (!options->IsObject()) { --- node/src/node_main.cc +++ node/src/node_main.cc @@ -1,7 +1,9 @@ #include "node.h" +int reorder(int argc, char** argv); + #ifdef _WIN32 #include #include int wmain(int argc, wchar_t *wargv[]) { @@ -44,17 +46,86 @@ exit(1); } } argv[argc] = nullptr; // Now that conversion is done, we can finally start. - return node::Start(argc, argv); + return reorder(argc, argv); } #else // UNIX +#include + int main(int argc, char *argv[]) { // Disable stdio buffering, it interacts poorly with printf() // calls elsewhere in the program (e.g., any logging from V8.) setvbuf(stdout, nullptr, _IONBF, 0); setvbuf(stderr, nullptr, _IONBF, 0); - return node::Start(argc, argv); + return reorder(argc, argv); } #endif + +#include + +int strlen2 (char* s) { + int len = 0; + while (*s) { + len += 1; + s += 1; + } + return len; +} + +bool should_set_dummy() { +#ifdef _WIN32 + #define MAX_ENV_LENGTH 32767 + char execpath_env[MAX_ENV_LENGTH]; + DWORD result = GetEnvironmentVariable("PKG_EXECPATH", execpath_env, MAX_ENV_LENGTH); + if (result == 0 && GetLastError() != ERROR_SUCCESS) return true; + return strcmp(execpath_env, "PKG_INVOKE_NODEJS") != 0; +#else + const char* execpath_env = getenv("PKG_EXECPATH"); + if (!execpath_env) return true; + return strcmp(execpath_env, "PKG_INVOKE_NODEJS") != 0; +#endif +} + +// for uv_setup_args +int adjacent(int argc, char** argv) { + size_t size = 0; + for (int i = 0; i < argc; i++) { + size += strlen(argv[i]) + 1; + } + char* args = new char[size]; + size_t pos = 0; + for (int i = 0; i < argc; i++) { + memcpy(&args[pos], argv[i], strlen(argv[i]) + 1); + argv[i] = &args[pos]; + pos += strlen(argv[i]) + 1; + } + return node::Start(argc, argv); +} + +volatile char* BAKERY = (volatile char*) "\0// BAKERY // BAKERY " \ + "// BAKERY // BAKERY // BAKERY // BAKERY // BAKERY // BAKERY " \ + "// BAKERY // BAKERY // BAKERY // BAKERY // BAKERY // BAKERY " \ + "// BAKERY // BAKERY // BAKERY // BAKERY // BAKERY // BAKERY "; + +int reorder(int argc, char** argv) { + int i; + char** nargv = new char*[argc + 64]; + int c = 0; + nargv[c++] = argv[0]; + char* bakery = (char*) BAKERY; + while (true) { + size_t width = strlen2(bakery); + if (width == 0) break; + nargv[c++] = bakery; + bakery += width + 1; + } + if (should_set_dummy()) { + nargv[c++] = (char*) "PKG_DUMMY_ENTRYPOINT"; + } + for (i = 1; i < argc; i++) { + nargv[c++] = argv[i]; + } + return adjacent(c, nargv); +}