-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnm_d8.js
More file actions
272 lines (242 loc) · 8.31 KB
/
nm_d8.js
File metadata and controls
272 lines (242 loc) · 8.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/usr/bin/env -S /home/user/.esvu/engines/v8/d8 --enable-os-system --enable-top-level-await --expose-gc --module
// d8 Native Messaging host
// guest271314 7-7-2023
function encodeMessage(str) {
const u8 = new Uint8Array(str.length);
for (let i = 0; i < str.length; i++) {
u8[i] = str[i].codePointAt(0);
}
return u8;
}
function decodeMessage(u8) {
let str = "";
for (let i = 0; i < u8.length; i++) {
str += String.fromCodePoint(u8[i]);
}
return str;
}
function getMessage(pid, [command, argv]) {
try {
const stdin = (os.system(command, argv)).trim();
if (stdin != undefined && stdin != null && stdin.length) {
const cmd = command.split(/[/-]/).pop();
if (cmd === "bash") {
const message = encodeMessage(stdin);
return message;
}
if (cmd === "qjs") {
const data = encodeMessage(stdin);
const view = new DataView(data.subarray(0, 4).buffer);
const length = view.getUint32(0, true);
// sendMessage(encodeMessage(JSON.stringify({ length })));
return data.subarray(4, 4 + length);
}
} else {
quit(1);
}
} catch (e) {
writeFile("getMessageError.txt", encodeMessage(e.message));
}
}
function sendMessage(message) {
// Constants for readability
const COMMA = 44;
const OPEN_BRACKET = 91; // [
const CLOSE_BRACKET = 93; // ]
const CHUNK_SIZE = 1024 * 1024; // 1MB
// If small enough, send directly (Native endianness handling recommended)
if (message.length <= CHUNK_SIZE) {
const header = new Uint8Array(4);
header[0] = (message.length >> 0) & 0xff;
header[1] = (message.length >> 8) & 0xff;
header[2] = (message.length >> 16) & 0xff;
header[3] = (message.length >> 24) & 0xff;
// Two writes are often better than allocating a new joined buffer
// if the engine supports it. If not, combine them.
const output = new Uint8Array(4 + message.length);
output.set(header, 0);
output.set(message, 4);
writeFile("/proc/self/fd/1", output);
gc();
return;
}
let index = 0;
// Iterate through the message until we reach the end
while (index < message.length) {
let splitIndex;
// 1. Determine where to cut the chunk
// Try to jump forward 1MB
let searchStart = index + CHUNK_SIZE - 8;
if (searchStart >= message.length) {
// We are near the end, take everything remaining
splitIndex = message.length;
} else {
// Find the next safe comma to split on
splitIndex = message.indexOf(COMMA, searchStart);
if (splitIndex === -1) {
splitIndex = message.length; // No more commas, take the rest
}
}
// 2. Extract the raw chunk (No copy yet, just a view)
const rawChunk = message.subarray(index, splitIndex);
// 3. Prepare the final payload buffer
// We calculate size first to allocate exactly once per chunk
const startByte = rawChunk[0];
const endByte = rawChunk[rawChunk.length - 1];
let prepend = null;
let append = null;
// Logic to ensure every chunk is a valid JSON array [...]
// Case A: Starts with '[' (First chunk), needs ']' at end if not present
if (startByte === OPEN_BRACKET && endByte !== CLOSE_BRACKET) {
append = CLOSE_BRACKET;
} // Case B: Starts with ',' (Middle chunks), needs '[' at start
else if (startByte === COMMA) {
prepend = OPEN_BRACKET;
// If it doesn't end with ']', it needs one
if (endByte !== CLOSE_BRACKET) {
append = CLOSE_BRACKET;
}
// Note: We skip the leading comma in the raw copy later by offsetting
}
// 4. Construct the output buffer
// Calculate final length: Header (4) + (Prepend?) + Body + (Append?)
// Note: If startByte was COMMA, we usually want to overwrite it with '[',
// but your original logic kept the comma data or shifted.
// Standard approach:
// If raw starts with comma, we replace comma with '[' or insert '['?
// Your logic: Replaced [0] if it was comma.
// Optimized construction based on your logic pattern:
let bodyLength = rawChunk.length;
let payloadOffset = 4; // Start after 4-byte header
// Adjust sizes based on brackets
const hasPrepend = prepend !== null;
const hasAppend = append !== null;
// Special handling for the "Comma Start" case to match your logic:
// Your logic: x[0] = 91; x[i] = data[i]. Effectively replaces comma with '['
let sourceOffset = 0;
if (startByte === COMMA) {
sourceOffset = 1; // Skip the comma from source
bodyLength -= 1; // Reduce source len
// We implicitly assume we prepend '[' in this slot
}
const totalLength = 4 + (hasPrepend ? 1 : 0) + bodyLength +
(hasAppend ? 1 : 0);
const output = new Uint8Array(totalLength);
// Write Length Header (Little Endian example)
const dataLen = totalLength - 4;
output[0] = (dataLen >> 0) & 0xff;
output[1] = (dataLen >> 8) & 0xff;
output[2] = (dataLen >> 16) & 0xff;
output[3] = (dataLen >> 24) & 0xff;
// Write Prepend (e.g. '[')
let cursor = 4;
if (hasPrepend) {
output[cursor] = prepend;
cursor++;
} else if (startByte === COMMA) {
// If we didn't flag prepend but stripped comma, likely need bracket
// Based on your specific logic "x[0] = 91", we treat that as a prepend
output[cursor] = OPEN_BRACKET;
cursor++;
}
// Write Body (Fast copy)
// We use .set() which is much faster than a loop
output.set(rawChunk.subarray(sourceOffset), cursor);
cursor += bodyLength;
// Write Append (e.g. ']')
if (hasAppend) {
output[cursor] = append;
}
// 5. Send immediately
writeFile("/proc/self/fd/1", output);
// Force GC only occasionally if needed (every chunk is often too frequent)
gc();
// Move index for next iteration
index = splitIndex;
}
}
function main() {
// Get PID of current process
const pid = (os.system("pgrep", ["-n", "-f", os.d8Path])).replace(/\D+/g, "")
.trim();
// Get PPID of current process
const ppid = os.system("ps", ["-o", "ppid=", "-p", JSON.parse(pid)]);
// readline() doesn't read past the Uint32Array equivalent message length
// V8 authors are not interested in reading STDIN to an ArrayBuffer in d8
// https://groups.google.com/g/v8-users/c/NsnStT6bx3Y/m/Yr_Z1FwgAQAJ
// Use dd https://pubs.opengroup.org/onlinepubs/9699919799/utilities/dd.html
// or GNU Coreutils head https://www.gnu.org/software/coreutils/manual/html_node/head-invocation.html
// or QuickJS https://bellard.org/quickjs/
const bash = ["bash", [
"--posix",
"-c",
`
length=$(head -q -z --bytes=4 /proc/${pid}/fd/0 | od -An -td4 -)
message=$(head -q -z --bytes=$((length)) /proc/${pid}/fd/0)
# length=$(dd iflag=fullblock oflag=nocache conv=notrunc,fdatasync bs=4 count=1 if=/proc/${pid}/fd/0 | od -An -td4 -)
# message=$(dd iflag=fullblock oflag=nocache conv=notrunc,fdatasync bs=$((length)) count=1 if=/proc/${pid}/fd/0)
printf "$message"
`,
]];
const qjs = ["/home/user/bin/qjs", [
"--std",
"-m",
"-e",
`const path = "/proc/${pid}/fd/0";
try {
const size = new Uint32Array(1);
const err = { errno: 0 };
const pipe = std.open(
path,
"rb",
err,
);
if (err.errno !== 0) {
throw std.strerror(err.errno);
}
pipe.read(size.buffer, 0, 4);
// writeFile("len.txt", size);
// {error: 'writeFile is not defined'
const output = new Uint8Array(size[0]);
pipe.read(output.buffer, 0);
const res = new Uint8Array([...new Uint8Array(size.buffer),...output]);
std.out.write(res.buffer, 0, res.length);
std.out.flush();
std.exit(0);
} catch (e) {
const json = JSON.stringify({error:he.message});
std.out.write(Uint32Array.of(json.length).buffer, 0, 4);
std.out.puts(json);
std.out.flush();
std.exit(0);
}
`,
]];
while (true) {
// Terminate current process when chrome processes close
if (!(os.system("pgrep", ["-P", JSON.parse(ppid)]))) {
break;
}
const message = getMessage(
pid,
bash,
);
if (message) {
sendMessage(message);
gc();
if (
// Handle error from qjs
String.fromCodePoint.apply(null, [...message.subarray(1, 8)]) ===
`"error"` // JSON
) {
break;
}
}
}
}
try {
main();
} catch (e) {
writeFile("mainError.txt", encodeMessage(e.message));
quit();
}