--- node/deps/v8/include/v8.h +++ node/deps/v8/include/v8.h @@ -4826,10 +4826,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.*/ static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback); --- node/deps/v8/src/api.cc +++ node/deps/v8/src/api.cc @@ -391,10 +391,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) { + i::Isolate* isolate = reinterpret_cast(v8_isolate); + i::Handle object = i::Handle::cast(Utils::OpenHandle(*script)); + i::Handle function_info( + i::SharedFunctionInfo::cast(*object), object->GetIsolate()); + i::Script* 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 @@ -196,10 +196,15 @@ static void PrintFeatures(); // Flush instruction cache. static void FlushICache(void* start, size_t size); + static void Reinitialize() { + supported_ = 0; + initialized_ = false; + } + private: // Platform-dependent implementation. static void ProbeImpl(bool cross_compile); static unsigned supported_; --- node/deps/v8/src/parser.cc +++ node/deps/v8/src/parser.cc @@ -4838,10 +4838,11 @@ return !parser.failed(); } bool Parser::Parse() { + if (info()->script()->source()->IsUndefined()) return false; DCHECK(info()->function() == NULL); FunctionLiteral* result = NULL; ast_value_factory_ = info()->ast_value_factory(); if (ast_value_factory_ == NULL) { ast_value_factory_ = --- node/deps/v8/src/serialize.cc +++ node/deps/v8/src/serialize.cc @@ -2167,11 +2167,11 @@ payload->begin(), static_cast(payload->length())); } bool SerializedCodeData::IsSane(String* source) { - return GetHeaderValue(kCheckSumOffset) == CheckSum(source) && + return true && PayloadLength() >= SharedFunctionInfo::kSize; } int SerializedCodeData::CheckSum(String* string) { --- node/lib/child_process.js +++ node/lib/child_process.js @@ -579,11 +579,11 @@ options.stdio = options.silent ? ['pipe', 'pipe', 'pipe', 'ipc'] : [0, 1, 2, 'ipc']; options.execPath = options.execPath || process.execPath; - return spawn(options.execPath, args, options); + return exports.spawn(options.execPath, args, options); }; exports._forkChild = function(fd) { // set process.send() --- node/node.gyp +++ node/node.gyp @@ -326,11 +326,10 @@ 'dependencies': [ 'deps/uv/uv.gyp:libuv' ], }], [ 'OS=="win"', { 'sources': [ - 'src/res/node.rc', ], 'defines': [ 'FD_SETSIZE=1024', # we need to use node's preferred "win32" rather than gyp's preferred "win" 'PLATFORM="win32"', --- node/src/env.h +++ node/src/env.h @@ -202,10 +202,11 @@ V(signal_string, "signal") \ V(size_string, "size") \ V(smalloc_p_string, "_smalloc_p") \ 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_code_string, "statusCode") \ V(status_message_string, "statusMessage") \ V(status_string, "status") \ --- node/src/node.cc +++ node/src/node.cc @@ -2957,10 +2957,11 @@ } static void PrintHelp(); static bool ParseDebugOpt(const char* arg) { + return false; const char* port = NULL; if (!strcmp(arg, "--debug")) { use_debug_agent = true; } else if (!strncmp(arg, "--debug=", sizeof("--debug=") - 1)) { @@ -3575,14 +3576,10 @@ // Ignore SIGPIPE RegisterSignalHandler(SIGPIPE, SIG_IGN); RegisterSignalHandler(SIGINT, SignalExit, true); RegisterSignalHandler(SIGTERM, SignalExit, true); #endif // __POSIX__ - - if (!use_debug_agent) { - RegisterDebugSignalHandler(); - } } struct AtExitCallback { AtExitCallback* next_; @@ -3761,15 +3758,10 @@ const char* replaceInvalid = getenv("NODE_INVALID_UTF8"); if (replaceInvalid == NULL) WRITE_UTF8_FLAGS |= String::REPLACE_INVALID_UTF8; -#if !defined(_WIN32) - // Try hard not to lose SIGUSR1 signals during the bootstrap process. - InstallEarlyDebugSignalHandler(); -#endif - assert(argc > 0); // Hack around with the argv pointer. Used for process.title = "blah". argv = uv_setup_args(argc, argv); --- node/src/node.js +++ node/src/node.js @@ -65,10 +65,14 @@ // 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. + if (NativeModule.exists('_pkg_bootstrap')) { + NativeModule.require('_pkg_bootstrap'); + } + 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/src/node_contextify.cc +++ node/src/node_contextify.cc @@ -487,10 +487,11 @@ Local code = args[0]->ToString(); Local filename = GetFilenameArg(args, 1); bool display_errors = GetDisplayErrorsArg(args, 1); Local cached_data_buf = GetCachedData(args, 1); bool produce_cached_data = GetProduceCachedData(args, 1); + bool sourceless = GetSourceless(args, 1); if (try_catch.HasCaught()) { try_catch.ReThrow(); return; } @@ -509,22 +510,35 @@ if (source.GetCachedData() != NULL) compile_options = ScriptCompiler::kConsumeCodeCache; else if (produce_cached_data) compile_options = ScriptCompiler::kProduceCodeCache; + if (sourceless && compile_options == ScriptCompiler::kProduceCodeCache) { + V8::EnableCompilationForSourcelessUse(); + } + Local v8_script = ScriptCompiler::CompileUnbound( env->isolate(), &source, compile_options); + if (sourceless && compile_options == ScriptCompiler::kProduceCodeCache) { + V8::DisableCompilationForSourcelessUse(); + } + if (v8_script.IsEmpty()) { if (display_errors) { AppendExceptionLine(env, try_catch.Exception(), try_catch.Message()); } try_catch.ReThrow(); return; } + + if (sourceless && compile_options == ScriptCompiler::kConsumeCodeCache) { + V8::FixSourcelessScript(env->isolate(), v8_script); + } + contextify_script->script_.Reset(env->isolate(), v8_script); if (compile_options == ScriptCompiler::kConsumeCodeCache) { // no 'rejected' field in cachedData } else if (compile_options == ScriptCompiler::kProduceCodeCache) { @@ -733,10 +747,23 @@ return value->IsTrue(); } + static bool GetSourceless( + const FunctionCallbackInfo& args, + const int i) { + if (!args[i]->IsObject()) { + return false; + } + Local key = FIXED_ONE_BYTE_STRING(args.GetIsolate(), "sourceless"); + Local value = args[i].As()->Get(key); + + return value->IsTrue(); + } + + static bool EvalMachine(Environment* env, const int64_t timeout, const bool display_errors, const FunctionCallbackInfo& args, TryCatch& try_catch) { --- node/src/node_javascript.cc +++ node/src/node_javascript.cc @@ -53,8 +53,59 @@ String::kNormalString, natives[i].source_len); target->Set(name, source); } } + + Local name = String::NewFromUtf8(env->isolate(), "_pkg_bootstrap"); + Handle source = String::NewFromUtf8(env->isolate(), + "var fs = require('fs');\n" \ + "var vm = require('vm');\n" \ + "function readPayload (fd) {\n" \ + " var position = process.env.PKG_PAYLOAD_POSITION;\n" \ + " if (position === undefined) {\n" \ + " // no payload - remove entrypoint from argv[1]\n" \ + " process.argv.splice(1, 1);\n" \ + " if (process.argv[1] === '-e' ||\n" \ + " process.argv[1] === '--eval') {\n" \ + " process._eval = process.argv[2];\n" \ + " process.argv.splice(1, 2);\n" \ + " }\n" \ + " return undefined;\n" \ + " }\n" \ + " position = position | 0;\n" \ + " var size = process.env.PKG_PAYLOAD_SIZE | 0;\n" \ + " delete process.env.PKG_PAYLOAD_POSITION;\n" \ + " delete process.env.PKG_PAYLOAD_SIZE;\n" \ + " var cd = new Buffer(size);\n" \ + " var read = fs.readSync(fd, cd, 0, size, position);\n" \ + " if (read !== size) {\n" \ + " console.error('Pkg: Error reading from file.');\n" \ + " process.exit(1);\n" \ + " }\n" \ + " var s = new vm.Script(undefined, {\n" \ + " cachedData: cd,\n" \ + " sourceless: true\n" \ + " });\n" \ + " if (s.cachedDataRejected) {\n" \ + " console.error('Pkg: Cached data was rejected.');\n" \ + " process.exit(1);\n" \ + " }\n" \ + " var fn = s.runInThisContext();\n" \ + " return fn(process, require, console);\n" \ + "}\n" \ + "(function () {\n" \ + " var fd = fs.openSync(process.execPath, 'r');\n" \ + " var r = readPayload(fd);\n" \ + " fs.closeSync(fd);\n" \ + " if (!r || r.undoPatch) {\n" \ + " // need to revert patch to node/lib/module.js\n" \ + " var bindingFs = process.binding('fs');\n" \ + " fs.internalModuleStat = bindingFs.internalModuleStat;\n" \ + " fs.internalModuleReadFile = bindingFs.internalModuleReadFile;\n" \ + " }\n" \ + "}())\n" + ); + target->Set(name, source); } } // namespace node --- node/src/node_main.cc +++ node/src/node_main.cc @@ -19,10 +19,298 @@ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE // USE OR OTHER DEALINGS IN THE SOFTWARE. #include "node.h" +#include +#include "uv.h" + +#define BOUNDARY 4096 + +uint16_t read16(uint8_t* buffer, uint32_t pos) { + buffer = &buffer[pos]; + uint16_t* buffer16 = (uint16_t*) buffer; + return *buffer16; +} + +uint32_t read32(uint8_t* buffer, uint32_t pos) { + buffer = &buffer[pos]; + uint32_t* buffer32 = (uint32_t*) buffer; + return *buffer32; +} + +int FindMeatEnd(FILE* file) { + + int read; + uint8_t buffer[4096]; + + if (fseek(file, 0, SEEK_SET) != 0) return 0; + read = static_cast(fread(&buffer, 1, sizeof(buffer), file)); + if (read != sizeof(buffer)) return 0; + + if (read16(buffer, 0) == 0x5A4D) { // _IMAGE_DOS_HEADER.e_magic == MZ + + uint32_t e_lfanew = read32(buffer, 0x3c); + uint16_t NumberOfSections = read16(buffer, e_lfanew + 0x04 + 0x02); + uint16_t SizeOfOptionalHeader = read16(buffer, e_lfanew + 0x04 + 0x10); + uint16_t Section = e_lfanew + 0x18 + SizeOfOptionalHeader; + + uint32_t MaxEnd = 0; + for (int i = 0; i < NumberOfSections; i += 1) { + if (Section > sizeof(buffer)) break; + uint32_t RawOffset = read32(buffer, Section + 0x14); + uint32_t RawSize = read32(buffer, Section + 0x10); + uint32_t RawEnd = RawOffset + RawSize; + if (RawEnd > MaxEnd) MaxEnd = RawEnd; + Section += 0x28; + } + + return MaxEnd; + + } else + if ((read32(buffer, 0) == 0xfeedface) || // MH_MAGIC + (read32(buffer, 0) == 0xfeedfacf)) { // MH_MAGIC_64 + + bool x64 = read32(buffer, 0) == 0xfeedfacf; + uint32_t ncmds = read32(buffer, 0x10); + uint32_t Command = x64 ? 0x20 : 0x1c; + + uint32_t MaxEnd = 0; + for (int i = 0; i < (int) ncmds; i += 1) { + if (Command > sizeof(buffer)) break; + uint32_t cmdtype = read32(buffer, Command + 0x00); + uint32_t cmdsize = read32(buffer, Command + 0x04); + if (cmdtype == 0x01) { // LC_SEGMENT + uint32_t RawOffset = read32(buffer, Command + 0x20); + uint32_t RawSize = read32(buffer, Command + 0x24); + uint32_t RawEnd = RawOffset + RawSize; + if (RawEnd > MaxEnd) MaxEnd = RawEnd; + } else + if (cmdtype == 0x19) { // LC_SEGMENT_64 + uint32_t RawOffset = read32(buffer, Command + 0x28); + uint32_t RawSize = read32(buffer, Command + 0x30); + uint32_t RawEnd = RawOffset + RawSize; + if (RawEnd > MaxEnd) MaxEnd = RawEnd; + } + Command += cmdsize; + } + + return MaxEnd; + + } else + if (read32(buffer, 0) == 0x464c457f) { // ELF + + bool x64 = buffer[0x04] == 2; + uint32_t e_shoff = read32(buffer, x64 ? 0x28 : 0x20); + uint16_t e_shnum = read32(buffer, x64 ? 0x3c : 0x30); + uint16_t e_shentsize = read32(buffer, x64 ? 0x3a : 0x2e); + uint32_t SectionHeader = 0; + + if (fseek(file, e_shoff, SEEK_SET) != 0) return 0; + read = static_cast(fread(&buffer, 1, sizeof(buffer), file)); + if (read != sizeof(buffer)) return 0; + + uint32_t MaxEnd = 0; + for (int i = 0; i < (int) e_shnum; i += 1) { + uint32_t sh_type = read32(buffer, SectionHeader + 0x04); + if (sh_type != 0x08) { // SHT_NOBITS + uint32_t sh_offset = read32(buffer, SectionHeader + (x64 ? 0x18 : 0x10)); + uint32_t sh_size = read32(buffer, SectionHeader + (x64 ? 0x20 : 0x14)); + uint32_t end = sh_offset + sh_size; + if (end > MaxEnd) MaxEnd = end; + } + SectionHeader += e_shentsize; + } + + return MaxEnd; + + } + + fprintf(stderr, "Pkg: Error parsing executable headers.\n"); + exit(1); + +} + +bool GetSentryPosition(FILE* file, int start, uint32_t s1, + uint32_t s12, uint32_t s3, int* pposition, int* psize +) { + + int read; + uint32_t sentry, length; + + if (fseek(file, start, SEEK_SET) != 0) return false; + + while (true) { + read = static_cast(fread(&sentry, 1, sizeof(sentry), file)); + if (read != sizeof(sentry)) return false; + if (sentry != s1) { + fseek(file, BOUNDARY - 4, SEEK_CUR); + continue; + } + fread(&length, 1, sizeof(length), file); + if ((sentry^length) != s12) { + fseek(file, BOUNDARY - 8, SEEK_CUR); + continue; + } + fread(&sentry, 1, sizeof(sentry), file); + if (sentry != s3) { + fseek(file, BOUNDARY - 12, SEEK_CUR); + continue; + } + break; + } + + fread(&length, 1, sizeof(length), file); + *pposition = ftell(file); + *psize = static_cast(length); + return true; + +} + + +#ifdef _WIN32 +void setenv(const char* name, const char* value, int overwrite) { + SetEnvironmentVariable(name, value); +} +#endif + + +char* ReadOverlays() { + + char exepath[1024]; + size_t exepath_size = sizeof(exepath); + if (uv_exepath(exepath, &exepath_size)) { + fprintf(stderr, "Pkg: Error obtaining exepath.\n"); + exit(1); + } + + FILE* file; +#ifdef _WIN32 + WCHAR exepath_w[2048]; + if (!MultiByteToWideChar(CP_UTF8, 0, exepath, -1, exepath_w, sizeof(exepath_w))) { + fprintf(stderr, "Pkg: Error converting to WideChar.\n"); + exit(1); + } + file = _wfopen(exepath_w, L"rb"); +#else + file = fopen(exepath, "rb"); +#endif + if (!file) { + fprintf(stderr, "Pkg: Error opening file.\n"); + exit(1); + } + + char env[64]; + int position = (FindMeatEnd(file) / BOUNDARY) * BOUNDARY; int size; + char* bakery = NULL; + + if (GetSentryPosition(file, position, 0x4818c4df, + 0x32dbc2af, 0x56558a76, &position, &size) + ) { + + bakery = static_cast(malloc(size)); + int read; + + for (int i = 0; i < size;) { + read = static_cast(fread(&bakery[i], 1, size - i, file)); + if (ferror(file) != 0) { + fprintf(stderr, "Pkg: Error reading from file.\n"); + fclose(file); + exit(1); + } + i += read; + } + + position -= 16; // align back to boundary + + } + + if (GetSentryPosition(file, position, 0x26e0c928, + 0x6713e24e, 0x3ea13ccf, &position, &size) + ) { + + sprintf(env, "%d", position); + setenv("PKG_PAYLOAD_POSITION", env, 1); + sprintf(env, "%d", size); + setenv("PKG_PAYLOAD_SIZE", env, 1); + + } + + fclose(file); + return bakery; + +} + + + +const char* OPTION_RUNTIME = "--runtime"; +const char* OPTION_ENTRYPOINT = "--entrypoint"; + + +// 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); +} + + +int reorder(int argc, char** argv) { + int i; + int runtime_pos = argc; + for (i = 1; i < argc; i++) { + if (strcmp(argv[i], OPTION_RUNTIME) == 0) { + runtime_pos = i; + break; + } + } + int entrypoint_pos = -1; + for (i = 1 + 1; i < runtime_pos; i++) { + if (strcmp(argv[i - 1], OPTION_ENTRYPOINT) == 0) { + entrypoint_pos = i; + break; + } + } + char** nargv = new char*[argc + 64]; + char* bakery = ReadOverlays(); + int c = 0; + nargv[c++] = argv[0]; + if (bakery) { + while (true) { + size_t width = strlen(bakery); + if (width == 0) break; + nargv[c++] = bakery; + bakery += width + 1; + } + } + for (i = runtime_pos + 1; i < argc; i++) { + nargv[c++] = argv[i]; + } + if (entrypoint_pos != -1) { + nargv[c++] = argv[entrypoint_pos]; + } else { + nargv[c++] = "DEFAULT_ENTRYPOINT"; + } + for (i = 1; i < runtime_pos; i++) { + if ((i != entrypoint_pos) && + (i != entrypoint_pos - 1)) { + nargv[c++] = argv[i]; + } + } + return adjacent(c, nargv); +} + + #ifdef _WIN32 int wmain(int argc, wchar_t *wargv[]) { // Convert argv to to UTF8 char** argv = new char*[argc]; for (int i = 0; i < argc; i++) { @@ -55,13 +343,13 @@ fprintf(stderr, "Could not convert arguments to utf8."); exit(1); } } // Now that conversion is done, we can finally start. - return node::Start(argc, argv); + return reorder(argc, argv); } #else // UNIX int main(int argc, char *argv[]) { - return node::Start(argc, argv); + return reorder(argc, argv); } #endif