@disjukr/execute 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +42 -2
- package/bin/copy-assets.js +15 -0
- package/dist/assets/gdi32.dll +0 -0
- package/dist/assets/i386-display.dll +0 -0
- package/dist/assets/i386-dsound.dll +0 -0
- package/dist/assets/i386-gdi32.dll +0 -0
- package/dist/assets/i386-kernel32.dll +0 -0
- package/dist/assets/i386-shell32.dll +0 -0
- package/dist/assets/i386-user32.dll +0 -0
- package/dist/assets/i386-winmm.dll +0 -0
- package/dist/assets/kernelbase-936.dll +0 -0
- package/dist/assets/kernelbase-949.dll +0 -0
- package/dist/assets/kernelbase-950.dll +0 -0
- package/dist/assets/kernelbase.dll +0 -0
- package/dist/assets/pe.js +6 -0
- package/dist/assets/process-server.wasm +0 -0
- package/dist/assets/process-services.js +45 -0
- package/dist/assets/process-worker.js +73 -0
- package/dist/assets/process.wasm +0 -0
- package/dist/assets/run.js +531 -0
- package/dist/assets/server-worker.js +397 -0
- package/dist/assets/shell32.dll +0 -0
- package/dist/assets/threads.js +60 -0
- package/dist/assets/user32.dll +0 -0
- package/dist/assets/webaudio-tinysynth.min.js +1 -0
- package/dist/assets/wow64host.exe +0 -0
- package/dist/index.js +1347 -0
- package/dist/licenses/GPL-3.0-only.txt +232 -0
- package/dist/licenses/LICENSE +44 -0
- package/dist/licenses/TinySynth +201 -0
- package/dist/licenses/Wine +17 -0
- package/dist/licenses/Wine-LGPL +502 -0
- package/dist/licenses/kwemu +340 -0
- package/dist/style.css +809 -0
- package/dist/types/demo.d.ts +1 -0
- package/dist/types/executable-icon.d.ts +1 -0
- package/dist/types/file-system.d.ts +13 -0
- package/dist/types/guest-files.d.ts +29 -0
- package/dist/types/index.d.ts +27 -0
- package/dist/types/launch.d.ts +91 -0
- package/dist/types/pe.d.ts +1 -0
- package/dist/types/process-group.d.ts +1 -0
- package/dist/types/window-thumbnail.d.ts +11 -0
- package/dist/types/windows-path.d.ts +1 -0
- package/package.json +49 -4
|
@@ -0,0 +1,531 @@
|
|
|
1
|
+
function runExecutable(module, file, write, options = {}) {
|
|
2
|
+
const PAGE = 4096;
|
|
3
|
+
const builtinNames = ['msvcrt.dll', 'kernel32.dll', 'ntdll.dll', 'display.dll'];
|
|
4
|
+
function readMenu(root, pointerSize) {
|
|
5
|
+
// menu-port.h uses native guest pointers.
|
|
6
|
+
const seen=new Set();
|
|
7
|
+
function field(address,offset,width) {
|
|
8
|
+
const va=core.guestSpan(address,BigInt(offset),BigInt(width),1);
|
|
9
|
+
const view=new DataView(core.memory.buffer,Number(va),width);
|
|
10
|
+
return width===8 ? view.getBigUint64(0,true) : view.getUint32(0,true);
|
|
11
|
+
}
|
|
12
|
+
const pointer=(address,index) => BigInt(field(address,index*pointerSize,pointerSize));
|
|
13
|
+
function items(menu) {
|
|
14
|
+
const result=[];
|
|
15
|
+
for (let p=menu && pointer(menu,0);p;p=pointer(p,0)) {
|
|
16
|
+
check(!seen.has(p),'Cyclic guest menu');
|
|
17
|
+
seen.add(p);
|
|
18
|
+
const text=pointer(p,2);
|
|
19
|
+
result.push({address:p,text:text ? guestString(text,true) : '',id:field(p,3*pointerSize,4),
|
|
20
|
+
flags:field(p,3*pointerSize+4,4),children:items(pointer(p,1))});
|
|
21
|
+
}
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
return items(root);
|
|
25
|
+
}
|
|
26
|
+
function guestString(va, wide) {
|
|
27
|
+
check(!wide || !(va & 1n), 'Invalid guest string');
|
|
28
|
+
let result = '';
|
|
29
|
+
for (let i=0; i<32768; ++i) {
|
|
30
|
+
const width = wide ? 2 : 1;
|
|
31
|
+
const ptr = core.guestSpan(va,BigInt(i*width),BigInt(width),1);
|
|
32
|
+
const view = new DataView(core.memory.buffer,Number(ptr),width);
|
|
33
|
+
const ch = wide ? view.getUint16(0,true) : view.getUint8(0);
|
|
34
|
+
if (!ch) return result;
|
|
35
|
+
result += String.fromCharCode(ch);
|
|
36
|
+
}
|
|
37
|
+
throw Error('Unterminated guest string');
|
|
38
|
+
}
|
|
39
|
+
function check(ok, message) { if (!ok) throw Error(message); }
|
|
40
|
+
function wide(ptr, value, capacity) {
|
|
41
|
+
check(ptr && value.length < capacity && !value.includes('\0'), 'Invalid startup string');
|
|
42
|
+
const out = new Uint16Array(core.memory.buffer, Number(ptr), capacity);
|
|
43
|
+
for (let i = 0; i < value.length; ++i) out[i] = value.charCodeAt(i);
|
|
44
|
+
out[value.length] = 0;
|
|
45
|
+
}
|
|
46
|
+
function processRequest(op, handle, timeout, app, cmd, out) {
|
|
47
|
+
const terminateProcess = 7; // RPC_PROCESS_TERMINATE in process-services.h
|
|
48
|
+
if (!options.request) {
|
|
49
|
+
if (op !== terminateProcess) return 0xc00000bb;
|
|
50
|
+
new Uint32Array(core.memory.buffer,Number(out),1)[0] = timeout;
|
|
51
|
+
return 0;
|
|
52
|
+
}
|
|
53
|
+
const wideString = ptr => ptr ? guestString(ptr,true) : null;
|
|
54
|
+
const message = {op, handle:handle >>> 0, timeout:timeout >>> 0, path:wideString(app),command:wideString(cmd)};
|
|
55
|
+
const reply = options.request(message);
|
|
56
|
+
new Uint32Array(core.memory.buffer,Number(out),4).set(reply.values);
|
|
57
|
+
return reply.status;
|
|
58
|
+
}
|
|
59
|
+
const fileChunkSize = 65536;
|
|
60
|
+
let fileBuffer;
|
|
61
|
+
function readChunks(handle,count,copy) {
|
|
62
|
+
// Reserve a single file range per ReadFile even when several threads share the handle.
|
|
63
|
+
const range=options.request({op:'fileBeginRead',handle,count});
|
|
64
|
+
if(range.status) return {error:range.status,offset:0};
|
|
65
|
+
const [start,length]=range.values;
|
|
66
|
+
fileBuffer ??= new SharedArrayBuffer(fileChunkSize);
|
|
67
|
+
let offset=0;
|
|
68
|
+
while(offset<length) {
|
|
69
|
+
const size=Math.min(length-offset,fileChunkSize);
|
|
70
|
+
const reply=options.request({op:'fileRead',handle,count:size,offset:start+offset,buffer:fileBuffer});
|
|
71
|
+
if(reply.status) return {error:reply.status,offset};
|
|
72
|
+
copy(new Uint8Array(fileBuffer,0,size),offset);
|
|
73
|
+
offset+=size;
|
|
74
|
+
}
|
|
75
|
+
return {error:0,offset};
|
|
76
|
+
}
|
|
77
|
+
function readLibrary(path) {
|
|
78
|
+
if(!options.request) return;
|
|
79
|
+
const open=options.request({op:'fileOpen',path});
|
|
80
|
+
if(open.status) return;
|
|
81
|
+
const [handle,size]=open.values;
|
|
82
|
+
try {
|
|
83
|
+
const image=new Uint8Array(size);
|
|
84
|
+
const result=readChunks(handle,size,(bytes,offset)=>image.set(bytes,offset));
|
|
85
|
+
if(result.error || result.offset!==size) throw Error('Could not read '+path);
|
|
86
|
+
return image;
|
|
87
|
+
} finally {options.request({op:'fileClose',handle});}
|
|
88
|
+
}
|
|
89
|
+
const memory = options.thread?.memory ?? (WebAssembly.Module.imports(module).some(i => i.kind === 'memory')
|
|
90
|
+
? new WebAssembly.Memory({address:'i64', initial:256n, maximum:262144n, shared:true}) : undefined);
|
|
91
|
+
const core = new WebAssembly.Instance(module, {browser: {
|
|
92
|
+
memory,
|
|
93
|
+
ticks() { return Math.floor(performance.now()); },
|
|
94
|
+
audioBuffer(op,va) {
|
|
95
|
+
const words=Array.from({length:9},(_,i)=>Number(core.gather(va,BigInt(4*i),4)));
|
|
96
|
+
const [id,ptr,size,rate,channels,bits,position,pan,volume]=words;
|
|
97
|
+
if (!options.audio || size>0x10000000) return 0;
|
|
98
|
+
let bytes;
|
|
99
|
+
if(op===1 || op===4) {
|
|
100
|
+
bytes=new Uint8Array(size);
|
|
101
|
+
for(let offset=0;offset<size;) {
|
|
102
|
+
const count=Math.min(size-offset,4096-((ptr+offset)%4096));
|
|
103
|
+
const src=core.guestSpan(BigInt(ptr),BigInt(offset),BigInt(count),1);
|
|
104
|
+
bytes.set(new Uint8Array(core.memory.buffer,Number(src),count),offset);
|
|
105
|
+
offset+=count;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
options.audio({op,id,bytes,rate,channels,bits,position,pan:pan|0,volume:volume|0});return 1;
|
|
109
|
+
},
|
|
110
|
+
systemTime() { return BigInt(Date.now()) * 10000n + 116444736000000000n; },
|
|
111
|
+
futexWake(ptr) { Atomics.notify(new Int32Array(core.memory.buffer,Number(ptr),1),0,1); },
|
|
112
|
+
futexWait(ptr,ms) {
|
|
113
|
+
const word = new Int32Array(core.memory.buffer,Number(ptr),1);
|
|
114
|
+
options.execution.release();
|
|
115
|
+
try { return Atomics.wait(word,0,0,ms) === 'timed-out' ? -1 : 0; }
|
|
116
|
+
finally { options.execution.acquire(); }
|
|
117
|
+
},
|
|
118
|
+
endThread() { throw threadStopped; },
|
|
119
|
+
endProcess(status) { exitCode = status >>> 0; throw stopped; },
|
|
120
|
+
beginMapping() { options.execution?.beginMapping(); },
|
|
121
|
+
endMapping() { options.execution?.endMapping(); },
|
|
122
|
+
pollMapping() { options.execution?.checkpoint(); },
|
|
123
|
+
yieldCpu() {
|
|
124
|
+
if (options.execution) options.execution.checkpoint();
|
|
125
|
+
},
|
|
126
|
+
registerThread(state,tid,nativeStack,wineStack) {
|
|
127
|
+
options.registerThread({memory,state,tid,nativeStack,wineStack,wineDataBase:core.wineDataBase.value});
|
|
128
|
+
},
|
|
129
|
+
publishSharedJit(slot,generation,pc,ptr,length) {
|
|
130
|
+
const bytes = new Uint8Array(memory.buffer,Number(ptr),Number(length)).slice();
|
|
131
|
+
options.jit.publish({slot,generation,pc,bytes});
|
|
132
|
+
},
|
|
133
|
+
retireSharedJit(slot,generation) { options.jit.retire({slot,generation}); },
|
|
134
|
+
processRequest,
|
|
135
|
+
fileOpen(va,wide,access,share,mode,lastError) {
|
|
136
|
+
if(!options.request) return -2;
|
|
137
|
+
let path=guestString(va,wide);
|
|
138
|
+
if(!wide) path=new TextDecoder(options.encoding ?? 'shift_jis').decode(Uint8Array.from(path,c=>c.charCodeAt(0)));
|
|
139
|
+
const reply=options.request({op:'fileOpen',path,access:access>>>0,share,mode});
|
|
140
|
+
new DataView(memory.buffer).setUint32(Number(lastError),reply.values[2] ?? 0,true);
|
|
141
|
+
return reply.status ? -reply.status : reply.values[0];
|
|
142
|
+
},
|
|
143
|
+
fileClose(handle) { return options.request?.({op:'fileClose',handle})?.status ?? 6; },
|
|
144
|
+
fileWrite(handle,src,count,out) {
|
|
145
|
+
core.scatter(out,0n,4,0n);
|
|
146
|
+
if(!options.request) return 6;
|
|
147
|
+
let bytes;
|
|
148
|
+
try {bytes=new Uint8Array(count>>>0);} catch {return 8;}
|
|
149
|
+
for(let i=0;i<bytes.length;) {
|
|
150
|
+
const n=Math.min(bytes.length-i,PAGE-Number((src+BigInt(i))%4096n));
|
|
151
|
+
const ptr=core.guestSpan(src,BigInt(i),BigInt(n),1);
|
|
152
|
+
bytes.set(new Uint8Array(core.memory.buffer,Number(ptr),n),i);
|
|
153
|
+
i+=n;
|
|
154
|
+
}
|
|
155
|
+
const reply=options.request({op:'fileWrite',handle,bytes});
|
|
156
|
+
if(!reply.status) core.scatter(out,0n,4,BigInt(reply.values[0]));
|
|
157
|
+
return reply.status;
|
|
158
|
+
},
|
|
159
|
+
fileRead(handle,dest,count,out) {
|
|
160
|
+
core.scatter(out,0n,4,0n);
|
|
161
|
+
if(!options.request) return 6;
|
|
162
|
+
const result=readChunks(handle,count>>>0,(bytes,offset)=>{
|
|
163
|
+
for(let i=0;i<bytes.length;) {
|
|
164
|
+
const n=Math.min(bytes.length-i,PAGE-Number((dest+BigInt(offset+i))%4096n));
|
|
165
|
+
const ptr=core.guestSpan(dest,BigInt(offset+i),BigInt(n),2);
|
|
166
|
+
new Uint8Array(core.memory.buffer,Number(ptr),n).set(bytes.subarray(i,i+n));
|
|
167
|
+
i+=n;
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
core.scatter(out,0n,4,BigInt(result.offset));
|
|
171
|
+
return result.error;
|
|
172
|
+
},
|
|
173
|
+
readImage(offset, dest, size) {
|
|
174
|
+
new Uint8Array(core.memory.buffer, Number(dest), size).set(imageFile.subarray(offset, offset+size));
|
|
175
|
+
},
|
|
176
|
+
mapLibrary(ptr) {
|
|
177
|
+
const name=guestString(ptr,true).toLowerCase();
|
|
178
|
+
const index=builtinNames.indexOf(name);
|
|
179
|
+
const bytes=index<0 ? (options.libraries?.[name] ?? readLibrary(name)) :
|
|
180
|
+
new Uint8Array(core.memory.buffer,Number(core.builtinImage(index)),core.builtinSize(index)).slice();
|
|
181
|
+
check(bytes instanceof Uint8Array,'Missing dependency '+name);
|
|
182
|
+
return load(bytes,name,true).handle;
|
|
183
|
+
},
|
|
184
|
+
registerClass(name,proc) {
|
|
185
|
+
if (!options.surface || name<65536n) return 0;
|
|
186
|
+
return options.surface.registerClass(guestString(name,true),proc);
|
|
187
|
+
},
|
|
188
|
+
surfaceOpen(title,width,height,cls,menu,pointerSize,style,exStyle,owner,x,y) {
|
|
189
|
+
if (!options.surface || width<=0 || height<=0 || width>4096 || height>4096) return 0;
|
|
190
|
+
return options.surface.open(title ? guestString(title,true) : '',width,height,cls<65536n ? Number(cls) : guestString(cls,true),readMenu(menu,pointerSize),style>>>0,exStyle>>>0,owner>>>0,x,y);
|
|
191
|
+
},
|
|
192
|
+
guiRequest(hwnd,op,arg,reply) {
|
|
193
|
+
if (op===GUI_SCREEN_DIMENSION)
|
|
194
|
+
return options.screenSize && (arg===0 || arg===1) ? Atomics.load(options.screenSize,arg) : 0;
|
|
195
|
+
if (op===GUI_MENU_ITEM_STATE) {
|
|
196
|
+
options.surface?.menuState(reply,arg);
|
|
197
|
+
return 1;
|
|
198
|
+
}
|
|
199
|
+
if (op===GUI_TRAY_NOTIFY || op===GUI_TRACK_POPUP || op===GUI_ICON_RESOURCE || op===GUI_CLASS_ICON) {
|
|
200
|
+
const word=i=>new DataView(core.memory.buffer).getUint32(Number(core.guestSpan(reply,BigInt(i*4),4n,1)),true);
|
|
201
|
+
if (op===GUI_CLASS_ICON) return options.request({op:'classIcon',atom:arg,
|
|
202
|
+
small:[word(0),word(1)].join('/'),large:[word(2),word(3)].join('/')}).values[0];
|
|
203
|
+
if (op===GUI_ICON_RESOURCE) {
|
|
204
|
+
const size=word(4), address=(BigInt(word(3))<<32n)|BigInt(word(2));
|
|
205
|
+
if(!size || size>16*1024*1024)return 0;
|
|
206
|
+
const ico=new Uint8Array(22+size), view=new DataView(ico.buffer);
|
|
207
|
+
view.setUint16(2,1,true);view.setUint16(4,1,true);view.setUint32(6,word(5),true);
|
|
208
|
+
view.setUint32(10,word(6),true);view.setUint32(14,size,true);view.setUint32(18,22,true);
|
|
209
|
+
for(let i=0;i<size;){
|
|
210
|
+
const n=Math.min(size-i,4096-Number((address+BigInt(i))%4096n));
|
|
211
|
+
const p=core.guestSpan(address,BigInt(i),BigInt(n),1);
|
|
212
|
+
ico.set(new Uint8Array(core.memory.buffer,Number(p),n),22+i);i+=n;
|
|
213
|
+
}
|
|
214
|
+
return options.request({op:'iconImage',handle:[word(0),word(1)].join('/'),ico}).values[0];
|
|
215
|
+
}
|
|
216
|
+
if (op===GUI_TRAY_NOTIFY) return options.request({op:'tray',action:arg,hwnd,id:word(0),
|
|
217
|
+
flags:word(1),callback:word(2),version:word(3),handle:[word(4),word(5)].join('/'),tip:guestString(reply+24n,true)}).values[0];
|
|
218
|
+
const menu=BigInt(word(0)) | (BigInt(word(1))<<32n);
|
|
219
|
+
return options.request({op:'popup',hwnd,items:readMenu(menu,arg),flags:word(2),x:word(3)|0,y:word(4)|0}).values[0];
|
|
220
|
+
}
|
|
221
|
+
if(op===GUI_DIALOG_CONTROL && options.surface) {
|
|
222
|
+
const fields=Array.from({length:7},(_,i)=>{
|
|
223
|
+
const p=core.guestSpan(BigInt(arg>>>0),BigInt(4*i),4n,1);
|
|
224
|
+
return new DataView(core.memory.buffer).getInt32(Number(p),true);
|
|
225
|
+
});
|
|
226
|
+
const [x,y,w,h,button,id,label]=fields;
|
|
227
|
+
options.surface.control(hwnd,{x,y,w,h,button,id,text:label ? guestString(BigInt(label>>>0),true) : ''});
|
|
228
|
+
return 1;
|
|
229
|
+
}
|
|
230
|
+
if(options.keyState && op===GUI_KEYBOARD_STATE) {
|
|
231
|
+
for(let i=0;i<256;) {
|
|
232
|
+
const n=Math.min(256-i,4096-Number((reply+BigInt(i))%4096n));
|
|
233
|
+
const at=core.guestSpan(reply,BigInt(i),BigInt(n),2);
|
|
234
|
+
new Uint8Array(core.memory.buffer,Number(at),n).set(options.keyState.subarray(i,i+n));
|
|
235
|
+
i+=n;
|
|
236
|
+
}
|
|
237
|
+
return 1;
|
|
238
|
+
}
|
|
239
|
+
if (!options.surface) return 0;
|
|
240
|
+
const response=options.surface.call(hwnd,op,arg);
|
|
241
|
+
if (response.status) return 0;
|
|
242
|
+
if (reply) for (let i=0;i<4;i++) {
|
|
243
|
+
const ptr=core.guestSpan(reply,BigInt(i*4),4n,2);
|
|
244
|
+
new DataView(core.memory.buffer).setUint32(Number(ptr),response.values[i],true);
|
|
245
|
+
}
|
|
246
|
+
return 1;
|
|
247
|
+
},
|
|
248
|
+
surfacePresent(hwnd,va,size) {
|
|
249
|
+
if (!options.surface?.sizes.has(hwnd >>> 0) || size!==BigInt(options.surface.sizes.get(hwnd >>> 0))) throw Error('Invalid paint buffer');
|
|
250
|
+
const pixels=new Uint8ClampedArray(Number(size));
|
|
251
|
+
for (let off=0; off<pixels.length;) {
|
|
252
|
+
const n=Math.min(4096-Number((va+BigInt(off))%4096n),pixels.length-off);
|
|
253
|
+
const ptr=core.guestSpan(va,BigInt(off),BigInt(n),1);
|
|
254
|
+
pixels.set(new Uint8Array(core.memory.buffer,Number(ptr),n),off);
|
|
255
|
+
off+=n;
|
|
256
|
+
}
|
|
257
|
+
options.surface.present(hwnd >>> 0,pixels);
|
|
258
|
+
},
|
|
259
|
+
consoleWrite(ptr, length) { write(new Uint8Array(core.memory.buffer, Number(ptr), length)); },
|
|
260
|
+
fault(addr, access) {
|
|
261
|
+
const fault = Error('Guest access violation at 0x' + addr.toString(16) + ' (access ' + access + ')');
|
|
262
|
+
fault.ntstatus = 0xc0000005;
|
|
263
|
+
throw fault;
|
|
264
|
+
}
|
|
265
|
+
}}).exports;
|
|
266
|
+
options.execution?.attach(core);
|
|
267
|
+
let imageFile;
|
|
268
|
+
function initImage(base,entry,path) {
|
|
269
|
+
if (options.jit) core.setSharedJitThreshold(4096);
|
|
270
|
+
core.init(base,base+BigInt(entry),options.pid ?? 32,options.tid ?? 36);
|
|
271
|
+
wide(core.commandBuffer(),options.commandLine ?? (/\s/.test(path) ? `"${path}"` : path),2048);
|
|
272
|
+
}
|
|
273
|
+
// Fixed-base i386 images share the same Wine VM view tree as native images.
|
|
274
|
+
// Bind against the i386 catalog only. Native exports have a different ABI.
|
|
275
|
+
function loadI386(bytes,path,cache,dll=false) {
|
|
276
|
+
const view=new DataView(bytes.buffer,bytes.byteOffset,bytes.byteLength);
|
|
277
|
+
const w=p=>view.getUint16(p,true), d=p=>view.getUint32(p,true);
|
|
278
|
+
const nt=d(60), opt=nt+24, table=opt+w(nt+20);
|
|
279
|
+
check(w(0)===0x5a4d && d(nt)===0x4550 && w(nt+4)===0x14c && w(opt)===0x10b &&
|
|
280
|
+
!!(w(nt+22)&0x2000)===dll && (dll || [2,3].includes(w(opt+68))), 'Invalid i386 PE');
|
|
281
|
+
const preferred=BigInt(d(opt+28)), entry=d(opt+16), size=d(opt+56),headers=d(opt+60),dirs=d(opt+92);
|
|
282
|
+
check(preferred>=65536n && preferred%65536n===0n && preferred+BigInt(size)<=0x100000000n &&
|
|
283
|
+
size && size%PAGE===0 && d(opt+32)===PAGE && entry<size && (dll ? !entry : entry) &&
|
|
284
|
+
table>=opt+96+8*dirs && table+40*w(nt+6)<=headers && headers<=Math.min(size,bytes.length),
|
|
285
|
+
'Unsupported i386 PE layout or DLL entry point: '+path);
|
|
286
|
+
function range(r,n) {check(r>0 && n>=0 && r+n<=size,'Invalid i386 RVA in '+path);return r;}
|
|
287
|
+
function directory(i) {
|
|
288
|
+
if(i>=dirs) return [0,0];
|
|
289
|
+
const r=d(opt+96+8*i), n=d(opt+100+8*i);
|
|
290
|
+
if(r || n) range(r,n);
|
|
291
|
+
return [r,n];
|
|
292
|
+
}
|
|
293
|
+
check(!directory(9)[0] && !directory(13)[0],'Unsupported i386 TLS or delay imports');
|
|
294
|
+
if(!dll) initImage(preferred,entry,path);
|
|
295
|
+
imageFile=bytes;
|
|
296
|
+
let va;
|
|
297
|
+
try { va=core.mapImage(preferred,size,headers,bytes.length); }
|
|
298
|
+
finally { imageFile=null; }
|
|
299
|
+
check(va===preferred,'Cannot map i386 image at its preferred address: '+path);
|
|
300
|
+
const read=(r,n)=>Number(core.gather(va,BigInt(range(r,n)),n));
|
|
301
|
+
function ascii(r) {
|
|
302
|
+
let str='';
|
|
303
|
+
for(let i=0;i<65536;++i) {
|
|
304
|
+
const ch=read(r+i,1);
|
|
305
|
+
if(!ch) return str;
|
|
306
|
+
str+=String.fromCharCode(ch);
|
|
307
|
+
}
|
|
308
|
+
throw Error('Unterminated i386 import name');
|
|
309
|
+
}
|
|
310
|
+
const exports=new Map();
|
|
311
|
+
cache.set(path,exports);
|
|
312
|
+
const [exp,expSize]=directory(0);
|
|
313
|
+
if(exp) {
|
|
314
|
+
range(exp,40);
|
|
315
|
+
const count=read(exp+20,4), names=read(exp+24,4), base=read(exp+16,4);
|
|
316
|
+
const funcs=range(read(exp+28,4),count*4);
|
|
317
|
+
for(let i=0;i<count;++i) {
|
|
318
|
+
const r=read(funcs+4*i,4);
|
|
319
|
+
if(!r) continue;
|
|
320
|
+
check(r<exp || r>=exp+expSize,'Unsupported i386 forwarded export');
|
|
321
|
+
exports.set(base+i,va+BigInt(range(r,1)));
|
|
322
|
+
}
|
|
323
|
+
if(names) {
|
|
324
|
+
const nameTable=range(read(exp+32,4),names*4), ord=range(read(exp+36,4),names*2);
|
|
325
|
+
for(let i=0;i<names;++i) {
|
|
326
|
+
const ordinal=read(ord+2*i,2);
|
|
327
|
+
check(ordinal<count,'Invalid i386 export ordinal');
|
|
328
|
+
exports.set(ascii(read(nameTable+4*i,4)),exports.get(base+ordinal));
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
const [imp,len]=directory(1);
|
|
333
|
+
for(let desc=imp;imp;desc+=20) {
|
|
334
|
+
check(desc+20<=imp+len,'Unterminated i386 import directory');
|
|
335
|
+
if(!read(desc+12,4)) break;
|
|
336
|
+
const lib=ascii(read(desc+12,4)).toLowerCase();
|
|
337
|
+
if(!cache.has(lib)) {
|
|
338
|
+
const dependency=options.libraries?.['i386-'+lib] ?? readLibrary(lib);
|
|
339
|
+
check(dependency instanceof Uint8Array,'Missing i386 dependency '+lib);
|
|
340
|
+
loadI386(dependency,lib,cache,true);
|
|
341
|
+
}
|
|
342
|
+
let slot=read(desc+16,4), lookup=read(desc,4)||slot;
|
|
343
|
+
for(;;lookup+=4,slot+=4) {
|
|
344
|
+
const hint=read(lookup,4);
|
|
345
|
+
if(!hint) break;
|
|
346
|
+
const symbol=hint>=0x80000000 ? hint&65535 : ascii(hint+2);
|
|
347
|
+
const value=cache.get(lib).get(symbol);
|
|
348
|
+
check(value,'Missing i386 export '+lib+'!'+symbol);
|
|
349
|
+
core.bindI386Slot(va+BigInt(range(slot,4)),Number(value));
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
function load(bytes, path, dll, initialized=false) {
|
|
354
|
+
const pe = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
355
|
+
const word = p => pe.getUint16(p, true), dword = p => pe.getUint32(p, true);
|
|
356
|
+
check(bytes.length >= 64 && word(0) === 0x5a4d, 'Missing DOS header');
|
|
357
|
+
const nt = dword(60), opt = nt + 24, table = opt + word(nt + 20);
|
|
358
|
+
check(dword(nt) === 0x4550 && word(nt+4) === 0x8664 && word(opt) === 0x20b &&
|
|
359
|
+
Boolean(word(nt+22) & 0x2000) === dll && (dll || [2,3].includes(word(opt+68))), 'Expected AMD64 Windows EXE or DLL');
|
|
360
|
+
const preferred = pe.getBigUint64(opt+24, true), size = dword(opt+56), entry = dword(opt+16);
|
|
361
|
+
const headers = dword(opt+60), dirs = dword(opt+108);
|
|
362
|
+
check(entry < size && (entry || dll) && size && size % PAGE === 0 && dword(opt+32) === PAGE &&
|
|
363
|
+
table >= opt+112+dirs*8 && table <= headers &&
|
|
364
|
+
headers <= bytes.length && headers <= size, 'Unsupported image layout');
|
|
365
|
+
|
|
366
|
+
if (!dll && !initialized) initImage(preferred,entry,path);
|
|
367
|
+
imageFile = bytes;
|
|
368
|
+
const va = core.mapImage(dll ? 0n : preferred, size, headers, bytes.length);
|
|
369
|
+
imageFile = null;
|
|
370
|
+
check(va, 'Invalid or unsupported image sections');
|
|
371
|
+
function span(r, len) { check(r > 0 && r+len <= size, 'Invalid image RVA'); return r; }
|
|
372
|
+
const read = (r,width) => core.gather(va,BigInt(span(r,width)),width);
|
|
373
|
+
const u32 = r => Number(read(r,4)), u16 = r => Number(read(r,2));
|
|
374
|
+
function name(r) {
|
|
375
|
+
span(r,1);
|
|
376
|
+
while (r<size) {
|
|
377
|
+
const len = Math.min(PAGE-r%PAGE,size-r);
|
|
378
|
+
const ptr = Number(core.guestSpan(va,BigInt(r),BigInt(len),1));
|
|
379
|
+
if (new Uint8Array(core.memory.buffer,ptr,len).includes(0)) return;
|
|
380
|
+
r+=len;
|
|
381
|
+
}
|
|
382
|
+
throw Error('Unterminated image name');
|
|
383
|
+
}
|
|
384
|
+
function dir(i) {
|
|
385
|
+
if (i >= dirs) return [0,0];
|
|
386
|
+
const pos = opt+112+i*8, r = dword(pos), len = dword(pos+4);
|
|
387
|
+
if (r || len) { check(len, 'Empty image directory'); span(r,len); }
|
|
388
|
+
return [r,len];
|
|
389
|
+
}
|
|
390
|
+
check(!dir(9)[0] && !dir(13)[0], 'TLS and delay imports require additional loader support');
|
|
391
|
+
if (va !== preferred) {
|
|
392
|
+
const [r,len] = dir(5);
|
|
393
|
+
for (let p=r; p<r+len;) {
|
|
394
|
+
check(p+8<=r+len, 'Incomplete relocation block');
|
|
395
|
+
const page=u32(p), block=u32(p+4);
|
|
396
|
+
check(block>=8 && !(block%2) && p+block<=r+len, 'Invalid relocation block');
|
|
397
|
+
for (let j=p+8;j<p+block;j+=2) {
|
|
398
|
+
const item=u16(j), type=item>>>12;
|
|
399
|
+
if (!type) continue;
|
|
400
|
+
check(type===10, 'Unsupported AMD64 relocation');
|
|
401
|
+
span(page+(item&4095),8);
|
|
402
|
+
}
|
|
403
|
+
p+=block;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
// Check file-derived vector bounds before Wine reads the mapped image.
|
|
407
|
+
const [exp, exportSize]=dir(0);
|
|
408
|
+
if (exp) {
|
|
409
|
+
check(exportSize>=40,'Incomplete export directory');
|
|
410
|
+
const funcs=u32(exp+20), names=u32(exp+24);
|
|
411
|
+
const ft=span(u32(exp+28),funcs*4);
|
|
412
|
+
if (names) {
|
|
413
|
+
const nt=span(u32(exp+32),names*4), ot=span(u32(exp+36),names*2);
|
|
414
|
+
for (let i=0;i<names;++i) {
|
|
415
|
+
name(u32(nt+i*4));
|
|
416
|
+
check(u16(ot+i*2)<funcs,'Invalid export ordinal');
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
for (let i=0;i<funcs;++i) {
|
|
420
|
+
const r=u32(ft+i*4);
|
|
421
|
+
if (r) span(r,1);
|
|
422
|
+
if (r>=exp && r<exp+exportSize) name(r);
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
const [imp,len]=dir(1);
|
|
426
|
+
if (imp) {
|
|
427
|
+
for (let d=imp;;d+=20) {
|
|
428
|
+
check(d+20<=imp+len,'Unterminated import directory');
|
|
429
|
+
if (!u32(d+12)) break;
|
|
430
|
+
name(u32(d+12));
|
|
431
|
+
let slot=u32(d+16), lookup=u32(d)||slot;
|
|
432
|
+
for (;;lookup+=8,slot+=8) {
|
|
433
|
+
const hint=read(lookup,8);
|
|
434
|
+
span(slot,8);
|
|
435
|
+
if (!hint) break;
|
|
436
|
+
if (hint>>63n) continue;
|
|
437
|
+
check(hint+2n<BigInt(size),'Invalid import RVA');
|
|
438
|
+
name(Number(hint)+2);
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
const pathPtr=core.imageName();
|
|
443
|
+
wide(pathPtr,initialized && !dll ? options.imagePath ?? path : path,2048);
|
|
444
|
+
if (!dll) {
|
|
445
|
+
check(core.begin(options.execution ? Math.min(navigator.hardwareConcurrency || 1,64) : 1),'Wine process startup failed');
|
|
446
|
+
}
|
|
447
|
+
check(!core.relocate_image(va),'Wine image relocation failed');
|
|
448
|
+
const handle=core.register_image(va,core.imageNameVA());
|
|
449
|
+
check(handle,'Cannot register '+path);
|
|
450
|
+
return {va,entry,handle};
|
|
451
|
+
}
|
|
452
|
+
const stopped = Symbol('process exit'), threadStopped = Symbol('thread exit');
|
|
453
|
+
let exitCode;
|
|
454
|
+
try {
|
|
455
|
+
if (options.thread) {
|
|
456
|
+
const thread = options.thread;
|
|
457
|
+
core.__stack_pointer.value = thread.nativeStack;
|
|
458
|
+
core.wineStackPointer.value = thread.wineStack;
|
|
459
|
+
core.wineDataBase.value = thread.wineDataBase;
|
|
460
|
+
core.threadState.value = thread.state;
|
|
461
|
+
if (options.request({op:RPC_THREAD_START}).status) throw Error('Could not start thread');
|
|
462
|
+
core.attach_guest_thread();
|
|
463
|
+
} else {
|
|
464
|
+
core.initializeProcessMemory?.();
|
|
465
|
+
let exe;
|
|
466
|
+
try {
|
|
467
|
+
const path=options.imagePath ?? 'C:\\hello.exe';
|
|
468
|
+
if(isI386(file)) {
|
|
469
|
+
const host=options.libraries?.['wow64host.exe'];
|
|
470
|
+
check(host instanceof Uint8Array,'Missing native WoW64 host');
|
|
471
|
+
loadI386(file,path,new Map());
|
|
472
|
+
exe=load(host,'wow64host.exe',false,true);
|
|
473
|
+
} else exe=load(file,path,false);
|
|
474
|
+
} catch (error) {
|
|
475
|
+
// A file rejected before init_process_done is a failed CreateProcessW.
|
|
476
|
+
error.ntstatus ??= 0xc000007b; // STATUS_INVALID_IMAGE_FORMAT
|
|
477
|
+
throw error;
|
|
478
|
+
}
|
|
479
|
+
options.ready?.();
|
|
480
|
+
const status=core.bind_image(exe.handle);
|
|
481
|
+
if (status && core.missing_dll()) {
|
|
482
|
+
const sym=core.missing_symbol();
|
|
483
|
+
throw Error('Missing export '+(sym<65536n ? '#'+sym : guestString(sym,false))+
|
|
484
|
+
' in '+guestString(core.missing_dll(),false));
|
|
485
|
+
}
|
|
486
|
+
check(!status,'Wine import binding failed');
|
|
487
|
+
const attached=core.attach_dependencies(exe.handle);
|
|
488
|
+
check(!attached,'DLL_PROCESS_ATTACH failed');
|
|
489
|
+
core.enter(exe.va+BigInt(exe.entry),exe.va,0);
|
|
490
|
+
}
|
|
491
|
+
if (options.jit) return driveWorker();
|
|
492
|
+
driveSlice(Infinity);
|
|
493
|
+
} catch (error) { return finish(error); }
|
|
494
|
+
|
|
495
|
+
function driveSlice(deadline) {
|
|
496
|
+
do {
|
|
497
|
+
const result=core.step();
|
|
498
|
+
if (result===2 || result===3) {
|
|
499
|
+
const code = core.exitCode();
|
|
500
|
+
if ((!options.thread && result===2) || !options.request) core.RtlExitUserProcess(code);
|
|
501
|
+
core.RtlExitUserThread(code);
|
|
502
|
+
}
|
|
503
|
+
check(result===0,'kwemu event '+(-result)+' at 0x'+core.pc().toString(16));
|
|
504
|
+
} while (!options.jit || performance.now()<deadline);
|
|
505
|
+
}
|
|
506
|
+
async function driveWorker() {
|
|
507
|
+
try {
|
|
508
|
+
for (;;) {
|
|
509
|
+
options.jit.install(core);
|
|
510
|
+
driveSlice(performance.now()+4);
|
|
511
|
+
options.execution.release();
|
|
512
|
+
try {
|
|
513
|
+
if (options.jit.hasNotifications()) await new Promise(resolve => setTimeout(resolve,0));
|
|
514
|
+
}
|
|
515
|
+
finally { options.execution.acquire(); }
|
|
516
|
+
}
|
|
517
|
+
} catch (error) { return finish(error); }
|
|
518
|
+
}
|
|
519
|
+
function finish(error) {
|
|
520
|
+
if (error === stopped) return exitCode;
|
|
521
|
+
if (error === threadStopped) {
|
|
522
|
+
if (options.thread) {
|
|
523
|
+
options.execution.beginMapping();
|
|
524
|
+
try { core.freeThread(); }
|
|
525
|
+
finally { options.execution.endMapping(); }
|
|
526
|
+
}
|
|
527
|
+
return;
|
|
528
|
+
}
|
|
529
|
+
throw error;
|
|
530
|
+
}
|
|
531
|
+
}
|