bootstrap + node_main.cc (node0)

This commit is contained in:
Igor Klopov 2017-05-06 17:11:13 -07:00
parent 5e08524f2a
commit 874b4aadc4

View File

@ -201,15 +201,54 @@
--- node/src/node.js
+++ node/src/node.js
@@ -65,10 +65,14 @@
@@ -65,10 +65,53 @@
// 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');
+ }
+ (function () {
+ var fs = NativeModule.require('fs');
+ var vm = NativeModule.require('vm');
+ function readPrelude (fd) {
+ var pos = process.env.PKG_PRELUDE_POSITION | 0;
+ var size = process.env.PKG_PRELUDE_SIZE | 0;
+ var base = process.env.PKG_PAYLOAD_BASE | 0;
+ delete process.env.PKG_PRELUDE_POSITION;
+ delete process.env.PKG_PRELUDE_SIZE;
+ delete process.env.PKG_PAYLOAD_BASE;
+ if (!pos) {
+ // no prelude - remove entrypoint from argv[1]
+ process.argv.splice(1, 1);
+ if (process.argv[1] === '-e' ||
+ process.argv[1] === '--eval') {
+ process._eval = process.argv[2];
+ process.argv.splice(1, 2);
+ }
+ return undefined;
+ }
+ var prelude = new Buffer(size);
+ var read = fs.readSync(fd, prelude, 0, size, pos);
+ if (read !== size) {
+ console.error('Pkg: Error reading from file.');
+ process.exit(1);
+ }
+ var s = new vm.Script(prelude);
+ var fn = s.runInThisContext();
+ return fn(process, NativeModule.require, console, fd, base);
+ }
+ (function () {
+ var fd = fs.openSync(process.execPath, 'r');
+ var result = readPrelude(fd);
+ if (!result || result.undoPatch) {
+ fs.closeSync(fd);
+ // need to revert patch to node/lib/module.js
+ var bindingFs = process.binding('fs');
+ fs.internalModuleStat = bindingFs.internalModuleStat;
+ fs.internalModuleReadFile = bindingFs.internalModuleReadFile;
+ }
+ }());
+ }());
+
if (NativeModule.exists('_third_party_main')) {
// To allow people to extend Node in different ways, this hook allows
@ -292,7 +331,7 @@
TryCatch& try_catch) {
--- node/src/node_main.cc
+++ node/src/node_main.cc
@@ -19,10 +19,298 @@
@@ -19,10 +19,291 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
@ -316,7 +355,6 @@
+}
+
+int FindMeatEnd(FILE* file) {
+
+ int read;
+ uint8_t buffer[4096];
+
@ -325,7 +363,6 @@
+ 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);
@ -342,11 +379,9 @@
+ }
+
+ 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;
@ -372,7 +407,6 @@
+ }
+
+ return MaxEnd;
+
+ } else
+ if (read32(buffer, 0) == 0x464c457f) { // ELF
+
@ -399,18 +433,15 @@
+ }
+
+ 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;
+
@ -440,7 +471,6 @@
+ *pposition = ftell(file);
+ *psize = static_cast<int>(length);
+ return true;
+
+}
+
+
@ -452,7 +482,6 @@
+
+
+char* ReadOverlays() {
+
+ char exepath[1024];
+ size_t exepath_size = sizeof(exepath);
+ if (uv_exepath(exepath, &exepath_size)) {
@ -483,7 +512,6 @@
+ if (GetSentryPosition(file, position, 0x4818c4df,
+ 0x32dbc2af, 0x56558a76, &position, &size)
+ ) {
+
+ bakery = static_cast<char*>(malloc(size));
+ int read;
+
@ -498,27 +526,31 @@
+ }
+
+ 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);
+ setenv("PKG_PRELUDE_POSITION", env, 1);
+ sprintf(env, "%d", size);
+ setenv("PKG_PAYLOAD_SIZE", env, 1);
+ setenv("PKG_PRELUDE_SIZE", env, 1);
+
+ position -= 16; // align back to boundary
+ }
+
+ if (GetSentryPosition(file, position, 0x75148eba,
+ 0x1aa9270e, 0x2e20c08d, &position, &size)
+ ) {
+ sprintf(env, "%d", position);
+ setenv("PKG_PAYLOAD_BASE", env, 1);
+ }
+
+ fclose(file);
+ return bakery;
+
+}
+
+
+
+const char* OPTION_RUNTIME = "--runtime";
+const char* OPTION_ENTRYPOINT = "--entrypoint";
+
@ -591,7 +623,7 @@
// Convert argv to to UTF8
char** argv = new char*[argc];
for (int i = 0; i < argc; i++) {
@@ -55,13 +343,13 @@
@@ -55,13 +336,13 @@
fprintf(stderr, "Could not convert arguments to utf8.");
exit(1);
}