ccxvii / mujs

An embeddable Javascript interpreter in C.
http://mujs.com/
ISC License
801 stars 97 forks source link

sql.js causes jump address integer overflow #89

Closed yurivict closed 4 years ago

yurivict commented 5 years ago

I tried to use SQLite translated into JavaScript: https://raw.githubusercontent.com/kripken/sql.js/master/js/sql.js but it fails:

$ mujs sql.js 
sql.js:20: warning: function statements are not standard
sql.js:20: warning: function statements are not standard
SyntaxError: jump address integer overflow

This might be also a bug in sql.js itself.

Please also note how the third line lacks line/column information, and the first two lines lack column information.

ccxvii commented 5 years ago

The function is simply too big for MuJS to handle. It would take more than 64k instructions, which exceeds the limit of our bytecode format.

yurivict commented 5 years ago

This isn't urgent, but in the long run you could make instructions count based on the compile-time option. There is no reason why it shoudn't be able to handle large JS scripts, especially when there are so many of them floating around.

yurivict commented 5 years ago

I tried this change:

-typedef unsigned short js_Instruction;
+typedef unsigned int js_Instruction;

mujs mostly works, but this code

var i = -1
print("i="+i)

prints

i=4294967295
ccxvii commented 5 years ago

Did you see this compiler warning that you get with that typedef?

/jscompile.c:78:12: warning: comparison of integers of different signs: 'int' and 'js_Instruction' (aka 'unsigned int')

You should be fine with

typedef int js_Instruction;

yurivict commented 4 years ago

Could you please make 32-bit addresses a build option?

ccxvii commented 4 years ago

32-bit addresses and instructions can be specified at build time with -DJS_INSTRUCTION=int (or any other SIGNED integer type).