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

RuntimeError: memory access out of bounds (php-web-drupal.wasm) #16

Closed gzurl closed 1 week ago

gzurl commented 2 years ago

Trying to build the target php-web-drupal.wasm in the Makefile from a clean repo, everything compiles successfully but I'm getting the following errors when loading the demo page:

php-web-drupal-error

If I substitute the php-web-drupal.* files (.wasm, .js and .data) from the ones in the docs/ folder in master branch, the demo webapp can load but I get this new error when selecting the "Drupal 7" demo:

Captura de pantalla 2022-06-11 a las 0 07 25

The only way I've found to see Drupal 7 working in the browser is by using the wasm module from the oraoto repo, this is, the php-web-drupal.* files available in docs/ in the master branch.

Captura de pantalla 2022-06-11 a las 0 23 51

In summary, I could identify up to three different groups of php-web-drupal.* files: 1) The ones built by the target php-web-drupal.wasm in the seanmorris/php-wasm/Makefile. They produce a failure loading the page as in the first screenshot above.

-rw-r--r--  1 root  root  15242062 10 jun 21:17 php-web-drupal.data
-rw-r--r--  1 root  root    297299 10 jun 21:17 php-web-drupal.js
-rw-r--r--  1 root  root   9577032 10 jun 21:17 php-web-drupal.wasm

2) The ones existing in seanmorris/php-wasm/docs/, that can load the page but fail to load the Drupal 7 demo when it's selected (second screenshot above):

-rw-r--r--  1 root  root  15242062 11 jun 00:39 docs/php-web-drupal.data
-rw-r--r--  1 root  root    297335 11 jun 00:39 docs/php-web-drupal.js
-rw-r--r--  1 root  root   9577034 11 jun 00:39 docs/php-web-drupal.wasm

3) The ones existing in oraoto/pib/docs/, the only ones that apparently work (third screenshot):

-rw-r--r--  1 root  root   21308238 10 jun 11:17 ../pib/docs/php-web-drupal.data
-rw-r--r--  1 root  root     297565 10 jun 11:17 ../pib/docs/php-web-drupal.js
-rw-r--r--  1 root  root    9577052 10 jun 11:17 ../pib/docs//php-web-drupal.wasm

I tried to build the files from the oraoto/pib repo with the same result than in case 1) :-(

So I think there are a few interesting issues to comment on here:

A) The size of the working php-web-drupal.data file from set 3) is considerably bigger than 1) and 2) (21308238 bytes vs. 15242062 bytes). Searching on the loadPackage section of the .js file, where the preload files location is set, I could identify two files missing:

/preload/drupal-7.59/sites/default/files/2.4mb-.ht.sqlite
/preload/drupal-7.59/sites/default/files/3.4mb-.ht.sqlite

These files are related to SQLite data and I believe they are created once Drupal is initialized and store its first entries. So I extracted them from the oraoto/pib/docs/php-web-drupal.data file, put them into the extra/drowser-files/ folder in the php-wasm repo, and updated the Makefile accordingly. This way, I could generate an identical .data file than the working set and fix the problem in 2). Unfortunately, this didn't fix the problem 1) seen above on loading the php-web-drupal.wasm module.

B) The .js files are 'almost identical' with the exception of the order where the preloaded files are listed (which I don't consider problematic) and the values of variables DYNAMIC_BASE and DYNAMICTOP_PTR:

<             var DYNAMIC_BASE = 7748784,
<                 DYNAMICTOP_PTR = 2505712;
---
>             var DYNAMIC_BASE = 539376800,
>                 DYNAMICTOP_PTR = 2505696;

I tried the original values but got the same error again on loading the module.

C) So the only difference remaining I can think of are the .wasm files themselves. The sizes are very similar but they have many differences inside. Since the .wasm binary files are built by using the Emscripten environment from the seanmorris/php-emscripten-builder:latest Docker image, I assumed they are bein generated with exactly the same context and therefore they should build the same files. Unfortunately, this is not the case and I could not find the reason.


In summary, I couldn't find any way to build the working php-web-drupal.wasm module needed for the Drupal 7 demo successfully. The only way I've found is by copying the files from /docs available in the oraoto/pib repo.

Any thoughts about the cause? Thanks!

kenorb commented 2 years ago

I've just tested the latest master by running Drupal locally and it now works for me.

Steps to run the app:

$ cd docs
$ ln -s . php-wasm
$ python3 -m http.server

It just gives SIGTRAP at random sometimes in Chrome (see: https://github.com/oraoto/pib/issues/60), but apart of it, works fine.

seanmorris commented 2 years ago

@gzurl Is the issue still present for you on the latest master?

gzurl commented 2 years ago

I've just tested the latest master by running Drupal locally and it now works for me.

Steps to run the app:

$ cd docs
$ ln -s . php-wasm
$ python3 -m http.server

It just gives SIGTRAP at random sometimes in Chrome (see: oraoto#60), but apart of it, works fine.

@kenorb I think that ONLY works if you don't rebuild php-web-drupal.wasm (via $ make php-web-drupal.wasm).

But if you clone the repo, then build a new php-web-drupal.wasm, it fails.

gzurl commented 2 years ago

@gzurl Is the issue still present for you on the latest master?

Yes, just tested and I'm afraid it still fails.

I believe the reason is that once the missing SQLite files are used in the building process of php-web-drupal.wasm, the javascript glue code generated by EMScripten is not embedded in the final version of app.js.

I tried to build it via $ npx brunch build, but the process is not adding the new code into app.js, so the only way to make it work is to substitute manually the following line (526): var Module=typeof PHP!=="undefined"?PHP:{};var readyPromiseResolve,readyPromiseReject ...

By the way, I had to install $ npm install caniuse since it seems to be a dependency that in my case wasn't installed automatically.

seanmorris commented 1 week ago

Closing since Drupal is no longer a custom build. Everything now runs on stock php-wasm & php-cgi-wasm.