| 1 |
import { dlinit } from "./dlinit"; |
| 2 |
import { WASI } from "./filesystem/wasi"; |
| 3 |
import { clearArray } from "./utils/clear-array"; |
| 4 |
import { uint2String } from "./utils/text-encoders.js"; |
| 5 |
import { logWasmModule as log } from "./logger"; |
| 6 |
|
| 7 |
goog.require("Zlib"); |
| 8 |
goog.require("Zlib.Inflate"); |
| 9 |
|
| 10 |
const { assert } = goog.require("goog.asserts"); |
| 11 |
|
| 12 |
const PAGE_SIZE = 65536; |
| 13 |
const PAGES_PER_MB = 16; // 1048576 bytes per MB / PAGE_SIZE |
| 14 |
|
| 15 |
export const csoundWasiJsMessageCallback = ({ memory, messagePort, streamBuffer, wasi }) => { |
| 16 |
return function (csound_, attribute, length_, offset) { |
| 17 |
if (!memory) { |
| 18 |
return; |
| 19 |
} |
| 20 |
const buf = new Uint8Array(memory.buffer, offset, length_); |
| 21 |
const string = uint2String(buf); |
| 22 |
const endsWithNewline = /\n$/g.test(string); |
| 23 |
const startsWithNewline = /^\n/g.test(string); |
| 24 |
const chunks = string.split("\n").filter((item) => item.length > 0); |
| 25 |
const printableChunks = []; |
| 26 |
if ((chunks.length === 0 && endsWithNewline) || startsWithNewline) { |
| 27 |
printableChunks.push(streamBuffer.join("")); |
| 28 |
clearArray(streamBuffer); |
| 29 |
} |
| 30 |
chunks.forEach((chunk, index) => { |
| 31 |
// if it's last chunk |
| 32 |
if (index + 1 === chunks.length) { |
| 33 |
if (endsWithNewline) { |
| 34 |
if (index === 0) { |
| 35 |
printableChunks.push(streamBuffer.join("") + chunk); |
| 36 |
clearArray(streamBuffer); |
| 37 |
} else { |
| 38 |
printableChunks.push(chunk); |
| 39 |
} |
| 40 |
} else { |
| 41 |
streamBuffer.push(chunk); |
| 42 |
} |
| 43 |
} else if (index === 0) { |
| 44 |
printableChunks.push(streamBuffer.join("") + chunk); |
| 45 |
clearArray(streamBuffer); |
| 46 |
} else { |
| 47 |
printableChunks.push(chunk); |
| 48 |
} |
| 49 |
}); |
| 50 |
printableChunks.forEach((chunk) => { |
| 51 |
const maybePrintable = chunk.replace(/(\r\n|\n|\r)/gm, ""); |
| 52 |
if (maybePrintable) { |
| 53 |
messagePort.post({ log: chunk }); |
| 54 |
} |
| 55 |
}); |
| 56 |
}; |
| 57 |
}; |
| 58 |
|
| 59 |
const assertPluginExports = (pluginInstance) => { |
| 60 |
if ( |
| 61 |
!pluginInstance || |
| 62 |
typeof pluginInstance !== "object" || |
| 63 |
typeof pluginInstance.exports !== "object" |
| 64 |
) { |
| 65 |
console.error("Error instantiating a csound plugin, instance and/or export is missing!"); |
| 66 |
return false; |
| 67 |
} else if (!pluginInstance.exports.__wasm_call_ctors) { |
| 68 |
console.error( |
| 69 |
"A csound plugin didn't export __wasm_call_ctors.\n" + |
| 70 |
"Please re-run wasm-ld with either --export-all or include --export=__wasm_call_ctors", |
| 71 |
); |
| 72 |
return false; |
| 73 |
} else if ( |
| 74 |
!pluginInstance.exports.csoundModuleCreate && |
| 75 |
!pluginInstance.exports.csound_opcode_init && |
| 76 |
!pluginInstance.exports.csound_fgen_init |
| 77 |
) { |
| 78 |
console.error( |
| 79 |
pluginInstance.exports, |
| 80 |
"A csound plugin turns out to be neither a plugin, opcode or module.\n" + |
| 81 |
"Perhaps csdl.h or module.h wasn't imported correctly?", |
| 82 |
); |
| 83 |
return false; |
| 84 |
} else { |
| 85 |
return true; |
| 86 |
} |
| 87 |
}; |
| 88 |
|
| 89 |
const getBinaryHeaderData = (wasmBytes) => { |
| 90 |
const magicBytes = new Uint32Array(new Uint8Array(wasmBytes.subarray(0, 24)).buffer); |
| 91 |
// eslint-disable-next-line unicorn/number-literal-case |
| 92 |
if (magicBytes[0] !== 0x6d736100) { |
| 93 |
console.error("Wasm magic number is missing!"); |
| 94 |
} |
| 95 |
if (wasmBytes[8] !== 0) { |
| 96 |
log("Dylink section wasn't found in wasm binary, assuming static wasm."); |
| 97 |
return "static"; |
| 98 |
} |
| 99 |
|
| 100 |
let next = 9; |
| 101 |
function getLEB() { |
| 102 |
let returnValue = 0; |
| 103 |
let mul = 1; |
| 104 |
while (1) { |
| 105 |
const byte = wasmBytes[next++]; |
| 106 |
// eslint-disable-next-line unicorn/number-literal-case |
| 107 |
returnValue += (byte & 0x7f) * mul; |
| 108 |
mul *= 0x80; |
| 109 |
if (!(byte & 0x80)) break; |
| 110 |
} |
| 111 |
return returnValue; |
| 112 |
} |
| 113 |
|
| 114 |
const sectionSize = getLEB(); |
| 115 |
next++; // size of "dylink" string |
| 116 |
assert(wasmBytes[next] === "d".codePointAt(0)); |
| 117 |
next++; |
| 118 |
assert(wasmBytes[next] === "y".codePointAt(0)); |
| 119 |
next++; |
| 120 |
assert(wasmBytes[next] === "l".codePointAt(0)); |
| 121 |
next++; |
| 122 |
assert(wasmBytes[next] === "i".codePointAt(0)); |
| 123 |
next++; |
| 124 |
assert(wasmBytes[next] === "n".codePointAt(0)); |
| 125 |
next++; |
| 126 |
assert(wasmBytes[next] === "k".codePointAt(0)); |
| 127 |
next++; |
| 128 |
assert(wasmBytes[next] === ".".codePointAt(0)); |
| 129 |
next++; |
| 130 |
assert(wasmBytes[next] === "0".codePointAt(0)); |
| 131 |
next += 3; |
| 132 |
|
| 133 |
const memorySize = getLEB(); |
| 134 |
const memoryAlign = getLEB(); |
| 135 |
const tableSize = getLEB(); |
| 136 |
const tableAlign = getLEB(); |
| 137 |
const neededDynlibsCount = getLEB(); |
| 138 |
|
| 139 |
return { sectionSize, memorySize, memoryAlign, neededDynlibsCount, tableSize, tableAlign }; |
| 140 |
}; |
| 141 |
|
| 142 |
// currently dl is default, static is good for low level debugging |
| 143 |
const loadStaticWasm = async ({ wasmBytes, wasmFs, wasi, messagePort }) => { |
| 144 |
const module = await WebAssembly.compile(wasmBytes); |
| 145 |
const memory = new WebAssembly.Memory({ initial: 65536 / 4 }); |
| 146 |
const streamBuffer = []; |
| 147 |
const options = wasi.getImports(module); |
| 148 |
options.env = options.env || {}; |
| 149 |
options.env.csoundLoadModules = () => 0; |
| 150 |
options.env.memory = memory; |
| 151 |
options.env.csoundWasiJsMessageCallback = csoundWasiJsMessageCallback({ |
| 152 |
memory: options.env.memory, |
| 153 |
streamBuffer, |
| 154 |
messagePort, |
| 155 |
}); |
| 156 |
|
| 157 |
const instance = await WebAssembly.instantiate(module, options); |
| 158 |
|
| 159 |
wasi.setMemory(memory); |
| 160 |
wasi.start(instance); |
| 161 |
instance.exports.__wasi_js_csoundSetMessageStringCallback(); |
| 162 |
return [instance, wasi]; |
| 163 |
}; |
| 164 |
|
| 165 |
export default async function ({ wasmDataURI, withPlugins = [], messagePort }) { |
| 166 |
const wasmFs = {}; |
| 167 |
|
| 168 |
const wasi = new WASI({ preopens: { "/": "/" } }); |
| 169 |
|
| 170 |
const wasmCompressed = new Uint8Array(wasmDataURI); |
| 171 |
const wasmZlib = new Zlib.Inflate(wasmCompressed); |
| 172 |
|
| 173 |
const wasmBytes = wasmZlib.decompress(); |
| 174 |
|
| 175 |
const magicData = getBinaryHeaderData(wasmBytes); |
| 176 |
if (magicData === "static") { |
| 177 |
return await loadStaticWasm({ messagePort, wasmBytes, wasmFs, wasi }); |
| 178 |
} |
| 179 |
const { memorySize, memoryAlign, tableSize } = magicData; |
| 180 |
|
| 181 |
// get the header data from plugins which we need before |
| 182 |
// initializing the main module |
| 183 |
withPlugins = await withPlugins.reduce(async (accumulator, wasmPlugin) => { |
| 184 |
const accumulator_ = await accumulator; |
| 185 |
|
| 186 |
let wasmPluginBytes; |
| 187 |
let pluginHeaderData; |
| 188 |
try { |
| 189 |
wasmPluginBytes = new Uint8Array(wasmPlugin); |
| 190 |
pluginHeaderData = getBinaryHeaderData(wasmPluginBytes); |
| 191 |
} catch (error) { |
| 192 |
console.error("Error in plugin", error); |
| 193 |
} |
| 194 |
if (pluginHeaderData) { |
| 195 |
accumulator_.push({ headerData: pluginHeaderData, wasmPluginBytes }); |
| 196 |
} |
| 197 |
return accumulator_; |
| 198 |
}, []); |
| 199 |
|
| 200 |
// The `fixedMemoryBase` is equivalent to the stack size. Note that the stack size grows down towards the code |
| 201 |
// section. This means that if the stack overflows then it will write over the Csound and plugin code which will |
| 202 |
// cause all kinds of strange behavior including errors that make no sense, no output of sound, or sound output will |
| 203 |
// be horrendously loud static and garbage sounds. |
| 204 |
// |
| 205 |
// TODO: Investigate using the --stack-first linker flag to move the stack to the beginning of memory so it doesn't |
| 206 |
// write over anything if it overflows. |
| 207 |
// |
| 208 |
const fixedMemoryBase = 128 * PAGES_PER_MB; |
| 209 |
const initialMemory = Math.ceil((memorySize + memoryAlign) / PAGE_SIZE); |
| 210 |
const pluginsMemory = Math.ceil( |
| 211 |
withPlugins.reduce( |
| 212 |
(accumulator, { headerData }) => |
| 213 |
headerData === "static" ? 0 : accumulator + (headerData.memorySize + memoryAlign), |
| 214 |
0, |
| 215 |
) / PAGE_SIZE, |
| 216 |
); |
| 217 |
|
| 218 |
const totalInitialMemory = initialMemory + pluginsMemory + fixedMemoryBase; |
| 219 |
|
| 220 |
// Request a max of 1gb of memory so devices use less CPU when growing memory. This has a noticeable effect on low- |
| 221 |
// powered devices like the Oculus Quest 2. |
| 222 |
const memory = new WebAssembly.Memory({ |
| 223 |
initial: totalInitialMemory, |
| 224 |
maximum: 1024 * PAGES_PER_MB, |
| 225 |
}); |
| 226 |
|
| 227 |
const table = new WebAssembly.Table({ initial: tableSize + 1, element: "anyfunc" }); |
| 228 |
|
| 229 |
wasi.setMemory(memory); |
| 230 |
|
| 231 |
const stackPointer = new WebAssembly.Global( |
| 232 |
{ value: "i32", mutable: true }, |
| 233 |
totalInitialMemory * PAGE_SIZE, |
| 234 |
); |
| 235 |
const heapBase = new WebAssembly.Global( |
| 236 |
{ value: "i32", mutable: true }, |
| 237 |
totalInitialMemory * PAGE_SIZE, |
| 238 |
); |
| 239 |
const memoryBase = new WebAssembly.Global({ value: "i32", mutable: false }, fixedMemoryBase); |
| 240 |
const tableBase = new WebAssembly.Global({ value: "i32", mutable: false }, 1); |
| 241 |
const __dummy = new WebAssembly.Global({ value: "i32", mutable: true }, 0); |
| 242 |
|
| 243 |
const module = await WebAssembly.compile(wasmBytes); |
| 244 |
const options = wasi.getImports(module); |
| 245 |
let withPlugins_ = []; |
| 246 |
|
| 247 |
let currentMemorySegment = initialMemory; |
| 248 |
|
| 249 |
const csoundLoadModules = (csoundInstance) => { |
| 250 |
withPlugins_.forEach((pluginInstance) => { |
| 251 |
if (typeof instance !== "undefined") { |
| 252 |
dlinit(instance, pluginInstance, table, csoundInstance); |
| 253 |
} else { |
| 254 |
console.error("csound-wasm internal: timing problem detected!"); |
| 255 |
} |
| 256 |
}); |
| 257 |
return 0; |
| 258 |
}; |
| 259 |
|
| 260 |
options.env = options.env || {}; |
| 261 |
options.env.memory = memory; |
| 262 |
options.env.__indirect_function_table = table; |
| 263 |
options.env.__stack_pointer = stackPointer; |
| 264 |
options.env.__memory_base = memoryBase; |
| 265 |
options.env.__table_base = tableBase; |
| 266 |
options.env.csoundLoadModules = csoundLoadModules; |
| 267 |
|
| 268 |
// TODO find out what's leaking this thread-local errno (cpp?) |
| 269 |
options.env._ZTH5errno = function () {}; |
| 270 |
|
| 271 |
const streamBuffer = []; |
| 272 |
options.env.csoundWasiJsMessageCallback = csoundWasiJsMessageCallback({ |
| 273 |
memory, |
| 274 |
messagePort, |
| 275 |
streamBuffer, |
| 276 |
}); |
| 277 |
|
| 278 |
options.env.printDebugCallback = (offset, length) => { |
| 279 |
const buf = new Uint8Array(memory.buffer, offset, length); |
| 280 |
const string = uint2String(buf); |
| 281 |
console.log(string); |
| 282 |
}; |
| 283 |
|
| 284 |
options["GOT.mem"] = options["GOT.mem"] || {}; |
| 285 |
options["GOT.mem"].__heap_base = heapBase; |
| 286 |
|
| 287 |
options["GOT.func"] = options["GOT.func"] || {}; |
| 288 |
|
| 289 |
const instance = await WebAssembly.instantiate(module, options); |
| 290 |
const moduleExports = Object.assign({}, instance.exports); |
| 291 |
const instance_ = {}; |
| 292 |
instance_.exports = Object.assign(moduleExports, { |
| 293 |
memory, |
| 294 |
}); |
| 295 |
|
| 296 |
withPlugins_ = await withPlugins.reduce(async (accumulator, { headerData, wasmPluginBytes }) => { |
| 297 |
accumulator = await accumulator; |
| 298 |
try { |
| 299 |
const { |
| 300 |
memorySize: pluginMemorySize, |
| 301 |
memoryAlign: pluginMemoryAlign, |
| 302 |
tableSize: pluginTableSize, |
| 303 |
} = headerData; |
| 304 |
|
| 305 |
const plugin = await WebAssembly.compile(wasmPluginBytes); |
| 306 |
const pluginOptions = wasi.getImports(plugin); |
| 307 |
|
| 308 |
const pluginMemoryBase = new WebAssembly.Global( |
| 309 |
{ value: "i32", mutable: false }, |
| 310 |
currentMemorySegment * PAGE_SIZE, |
| 311 |
); |
| 312 |
|
| 313 |
table.grow(pluginTableSize); |
| 314 |
|
| 315 |
pluginOptions.env = Object.assign({}, pluginOptions.env); |
| 316 |
pluginOptions.env.memory = memory; |
| 317 |
pluginOptions.env.__indirect_function_table = table; |
| 318 |
pluginOptions.env.__memory_base = pluginMemoryBase; |
| 319 |
pluginOptions.env.__stack_pointer = stackPointer; |
| 320 |
pluginOptions.env.__table_base = tableBase; |
| 321 |
pluginOptions.env.csoundLoadModules = __dummy; |
| 322 |
delete pluginOptions.env.csoundWasiJsMessageCallback; |
| 323 |
|
| 324 |
currentMemorySegment += Math.ceil((pluginMemorySize + pluginMemoryAlign) / PAGE_SIZE); |
| 325 |
|
| 326 |
const pluginInstance = await WebAssembly.instantiate(plugin, pluginOptions); |
| 327 |
|
| 328 |
if (assertPluginExports(pluginInstance)) { |
| 329 |
pluginInstance.exports.__wasm_call_ctors(); |
| 330 |
accumulator.push(pluginInstance); |
| 331 |
} |
| 332 |
} catch (error) { |
| 333 |
console.error("Error while compiling csound-plugin", error); |
| 334 |
} |
| 335 |
return accumulator; |
| 336 |
}, []); |
| 337 |
|
| 338 |
wasi.start(instance_); |
| 339 |
|
| 340 |
instance_.exports.__wasi_js_csoundSetMessageStringCallback(); |
| 341 |
return [instance_, wasi]; |
| 342 |
} |