|
1 | 1 | import assert from 'node:assert/strict'; |
2 | 2 | import { createRequire } from 'node:module'; |
3 | | -import { dirname, resolve } from 'node:path'; |
| 3 | +import { basename, dirname, resolve } from 'node:path'; |
4 | 4 | import { fileURLToPath } from 'node:url'; |
5 | 5 | import { spawn } from 'node:child_process'; |
6 | 6 | import { createInterface } from 'node:readline'; |
@@ -29,53 +29,108 @@ const native = require(resolve( |
29 | 29 | platform, |
30 | 30 | 'splitscript_process_native.node', |
31 | 31 | )); |
32 | | -const fixtureName = process.platform === 'win32' |
33 | | - ? 'splitscript-process-fixture.exe' |
34 | | - : 'splitscript-process-fixture'; |
35 | | -const fixture = spawn( |
36 | | - resolve(repository, 'target', 'release', fixtureName), |
37 | | - [], |
38 | | - { stdio: ['pipe', 'pipe', 'inherit'] }, |
39 | | -); |
40 | 32 |
|
41 | | -try { |
42 | | - const line = await firstLine(fixture.stdout); |
43 | | - const fields = Object.fromEntries(line.split(';').map(field => field.split('=', 2))); |
44 | | - const pid = Number(fields.pid); |
45 | | - const length = Number(fields.length); |
46 | | - assert(Number.isInteger(pid) && pid > 0); |
47 | | - assert(Number.isInteger(length) && length > 0); |
| 33 | +if (process.platform === 'darwin') { |
| 34 | + probeCurrentProcess(); |
| 35 | +} else { |
| 36 | + await probeFixtureProcess(); |
| 37 | +} |
| 38 | + |
| 39 | +function probeCurrentProcess() { |
| 40 | + // GitHub-hosted macOS runners cannot grant the interactive debugger |
| 41 | + // authorization that task_for_pid requires for another process. Attaching |
| 42 | + // to the caller is always valid and still exercises the Mach memory path. |
| 43 | + const processName = basename(process.execPath); |
| 44 | + assert(native.listProcessesByName(processName).includes(process.pid)); |
48 | 45 |
|
49 | | - assert(native.listProcessesByName(fixtureName).includes(pid)); |
| 46 | + const handle = native.attachByPid(process.pid); |
| 47 | + try { |
| 48 | + assert.equal(native.processId(handle), process.pid); |
| 49 | + const processPath = native.processPath(handle); |
| 50 | + assert.equal(typeof processPath, 'string'); |
| 51 | + assert.equal(basename(processPath), processName); |
| 52 | + assert.equal(native.isOpen(handle), true); |
| 53 | + assert(BigInt(native.moduleAddress(handle, processName)) > 0n); |
| 54 | + assert(BigInt(native.moduleSize(handle, processName)) > 0n); |
| 55 | + assert.equal(basename(native.modulePath(handle, processName)), processName); |
| 56 | + |
| 57 | + const readable = firstReadableRange(native, handle); |
| 58 | + const actual = native.readProcessMemory(handle, readable.address, 32); |
| 59 | + assert.equal(actual.length, 32); |
| 60 | + } finally { |
| 61 | + assert.equal(native.detach(handle), true); |
| 62 | + assert.equal(native.detach(handle), false); |
| 63 | + } |
| 64 | + |
| 65 | + console.log( |
| 66 | + `Native ${platform} self-process probe passed: discovery, modules, ranges, and a 32-byte Mach memory read.`, |
| 67 | + ); |
| 68 | +} |
50 | 69 |
|
51 | | - const handle = native.attachByPid(pid); |
52 | | - assert.equal(native.processId(handle), pid); |
53 | | - assert.match(native.processPath(handle), new RegExp(`${escapeRegExp(fixtureName)}$`, 'i')); |
54 | | - assert.equal(native.isOpen(handle), true); |
55 | | - assert(BigInt(native.moduleAddress(handle, fixtureName)) > 0n); |
56 | | - assert(BigInt(native.moduleSize(handle, fixtureName)) > 0n); |
57 | | - assert.match( |
58 | | - native.modulePath(handle, fixtureName), |
59 | | - new RegExp(`${escapeRegExp(fixtureName)}$`, 'i'), |
| 70 | +async function probeFixtureProcess() { |
| 71 | + const fixtureName = process.platform === 'win32' |
| 72 | + ? 'splitscript-process-fixture.exe' |
| 73 | + : 'splitscript-process-fixture'; |
| 74 | + const fixture = spawn( |
| 75 | + resolve(repository, 'target', 'release', fixtureName), |
| 76 | + [], |
| 77 | + { stdio: ['pipe', 'pipe', 'inherit'] }, |
60 | 78 | ); |
| 79 | + |
| 80 | + try { |
| 81 | + const line = await firstLine(fixture.stdout); |
| 82 | + const fields = Object.fromEntries(line.split(';').map(field => field.split('=', 2))); |
| 83 | + const pid = Number(fields.pid); |
| 84 | + const length = Number(fields.length); |
| 85 | + assert(Number.isInteger(pid) && pid > 0); |
| 86 | + assert(Number.isInteger(length) && length > 0); |
| 87 | + |
| 88 | + assert(native.listProcessesByName(fixtureName).includes(pid)); |
| 89 | + |
| 90 | + const handle = native.attachByPid(pid); |
| 91 | + assert.equal(native.processId(handle), pid); |
| 92 | + assert.match(native.processPath(handle), new RegExp(`${escapeRegExp(fixtureName)}$`, 'i')); |
| 93 | + assert.equal(native.isOpen(handle), true); |
| 94 | + assert(BigInt(native.moduleAddress(handle, fixtureName)) > 0n); |
| 95 | + assert(BigInt(native.moduleSize(handle, fixtureName)) > 0n); |
| 96 | + assert.match( |
| 97 | + native.modulePath(handle, fixtureName), |
| 98 | + new RegExp(`${escapeRegExp(fixtureName)}$`, 'i'), |
| 99 | + ); |
| 100 | + const rangeCount = native.memoryRangeCount(handle); |
| 101 | + assert(rangeCount > 0); |
| 102 | + assert(BigInt(native.memoryRangeAddress(handle, 0)) > 0n); |
| 103 | + assert(BigInt(native.memoryRangeSize(handle, 0)) > 0n); |
| 104 | + assert(BigInt(native.memoryRangeFlags(handle, 0)) > 0n); |
| 105 | + const actual = native.readProcessMemory(handle, fields.address, length); |
| 106 | + assert.equal(Buffer.from(actual).toString('utf8'), fields.expected); |
| 107 | + assert.equal(native.detach(handle), true); |
| 108 | + assert.equal(native.detach(handle), false); |
| 109 | + const namedHandle = native.attachByName(fixtureName); |
| 110 | + assert.equal(native.processId(namedHandle), pid); |
| 111 | + assert.equal(native.detach(namedHandle), true); |
| 112 | + console.log( |
| 113 | + `Native ${platform} process probe passed: discovery, modules, ranges, and ${length}-byte read from PID ${pid}.`, |
| 114 | + ); |
| 115 | + } finally { |
| 116 | + fixture.stdin.end('\n'); |
| 117 | + await new Promise(resolvePromise => fixture.once('exit', resolvePromise)); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +function firstReadableRange(native, handle) { |
61 | 122 | const rangeCount = native.memoryRangeCount(handle); |
62 | 123 | assert(rangeCount > 0); |
63 | | - assert(BigInt(native.memoryRangeAddress(handle, 0)) > 0n); |
64 | | - assert(BigInt(native.memoryRangeSize(handle, 0)) > 0n); |
65 | | - assert(BigInt(native.memoryRangeFlags(handle, 0)) > 0n); |
66 | | - const actual = native.readProcessMemory(handle, fields.address, length); |
67 | | - assert.equal(Buffer.from(actual).toString('utf8'), fields.expected); |
68 | | - assert.equal(native.detach(handle), true); |
69 | | - assert.equal(native.detach(handle), false); |
70 | | - const namedHandle = native.attachByName(fixtureName); |
71 | | - assert.equal(native.processId(namedHandle), pid); |
72 | | - assert.equal(native.detach(namedHandle), true); |
73 | | - console.log( |
74 | | - `Native ${platform} process probe passed: discovery, modules, ranges, and ${length}-byte read from PID ${pid}.`, |
75 | | - ); |
76 | | -} finally { |
77 | | - fixture.stdin.end('\n'); |
78 | | - await new Promise(resolvePromise => fixture.once('exit', resolvePromise)); |
| 124 | + for (let index = 0; index < rangeCount; index++) { |
| 125 | + const address = native.memoryRangeAddress(handle, index); |
| 126 | + const size = BigInt(native.memoryRangeSize(handle, index)); |
| 127 | + const flags = BigInt(native.memoryRangeFlags(handle, index)); |
| 128 | + if ((flags & 2n) !== 0n && size >= 32n) { |
| 129 | + assert(BigInt(address) > 0n); |
| 130 | + return { address }; |
| 131 | + } |
| 132 | + } |
| 133 | + assert.fail('the current process has no readable 32-byte memory range'); |
79 | 134 | } |
80 | 135 |
|
81 | 136 | async function firstLine(stream) { |
|
0 commit comments