seanmorris / php-wasm

PHP in Browser, powered by WebAssembly.
https://seanmorris.github.io/php-wasm/
Apache License 2.0
606 stars 32 forks source link

Memory problem while running project with 100+ classes #34

Closed uuf6429 closed 8 months ago

uuf6429 commented 8 months ago

After getting the demo to work recently, I tried to make it work for a side project I'm on.

The project is based on composer and since I couldn't find a way to populate/fill-in the WASM virtual filesystem (or any reference on how it would work), I used a tool that joins all the relevant/used classes in one big PHP file - about 106 classes/traits/interfaces and 15k lines in total. Finally, with some fiddling, I put the contents of that file in <script type="text/php"> as in the demo.

The PHP file by itself worked fine in PHP, however php-wasm fails with the following:

php-web.wasm:0x6fe3 Uncaught (in promise) RuntimeError: memory access out of bounds
    at php-web.wasm:0x6fe3
    at invoke_ii (php-web.mjs:8:124829)
    at php-web.wasm:0x451529
    at invoke_iiii (php-web.mjs:8:124532)
    at php-web.wasm:0x7fb438
    at Module._pib_run (php-web.mjs:8:123160)
    at Object.ccall (php-web.mjs:8:118781)
    at PhpBase.mjs:63:38

Some things I've tried:

  1. running the big php script by itself in PHP; it worked fine
  2. running phpinfo(); in php-wasm worked fine - PHP 8.2 with some 100mb of memory
  3. running ini_set('memory_limit', '1G') and then the big script - still failed; no difference
  4. putting echo <nameOfNamespace> before each namespace/class declaration - in PHP it worked fine, in php-wasm no output except the memory error

To me it feels like the failure could be coming at the parsing level or an earlier stage. Unfortunately, my knowledge in webassembly is very limited and I gave up as soon as I saw the disassembled code. I don't know what else to try debugging this.

It's worth noting that there is no PHP framework or any special php extensions in my project - it doesn't even use a db. So it's frankly baffling to hear people running drupal or wordpress (with thousands of files) over this tool.

I'm running Chrome 118 x64 on Windows 11, if that helps.

uuf6429 commented 8 months ago

Since the recent changes & deployment, the error changed to:

PhpWeb.mjs:77 RuntimeError: table index is out of bounds
    at php-web.wasm:0x1bc58d
    at php-web.wasm:0x20e7d6
    at php-web.wasm:0x9ca0
    at invoke_viii (php-web.mjs:8:129659)
    at php-web.wasm:0x112284
    at php-web.wasm:0x29c9b1
    at php-web.wasm:0x20e608
    at php-web.wasm:0x71e1
    at invoke_ii (php-web.mjs:8:129509)
    at php-web.wasm:0x44d5c5

I also noticed that the problem is not related to parsing PHP, but rather the size of the code; a simple static $x = 'very long string'; phpinfo(); fails after a certain string length (regardless of the actual content).

uuf6429 commented 8 months ago

I've managed to overcome this after following the instructions from https://github.com/soyuka/php-wasm.