nand2tetris / web-ide

A web-based IDE for https://nand2tetris.org
https://nand2tetris.github.io/web-ide
Other
84 stars 22 forks source link

[bug]: Sometimes when I run a jack program I get Error: Current operation step beyond end of function operations (290 > 39) #454

Open happytomatoe opened 2 months ago

happytomatoe commented 2 months ago

Tool

VM Emulator

Interface

None

Contact Details

No response

What happened?

Sometimes when I run my jack program I get


main.9984f23c.js:2 Uncaught Error: Current operation step beyond end of function operations (290 > 39)
    at get operation (main.9984f23c.js:2:468893)
    at x.derivedLine (main.9984f23c.js:2:475281)
    at S (main.9984f23c.js:2:238859)
    at Object.update (main.9984f23c.js:2:239816)
    at main.9984f23c.js:2:223865
    at produce (main.9984f23c.js:2:222483)
    at main.9984f23c.js:2:223845
    at Object.wa [as useReducer] (main.9984f23c.js:2:104836)
    at t.useReducer (main.9984f23c.js:2:203526)
    at q (main.9984f23c.js:2:223836)

What does this error mean?

Additional Comments

No response

Do you want to try to fix this bug?

Code of Conduct

happytomatoe commented 2 months ago

Whole log nand2tetris.github.io-1726296150652.log

happytomatoe commented 2 months ago

After debugging somehow "this" pointer (address number 3 ) becomes 0 inside of IntQueue.clear. How to recreate this - compile and run this

I don't get this error if I comment out this line

DavidSouther commented 2 months ago

@happytomatoe thank you for opening this issue. It will probably be easier to troubleshoot these if you're running in development mode locally. You can do this by installing Node.JS, cloning the repo, running npm install, and then npm start to have a local copy running. This will use source maps for the javascript, which include the original line numbers, and we can get a better look at the problem.

happytomatoe commented 2 months ago

@DavidSouther Thanks for the suggestion. Only today I've realised how to debug web IDE using VS code. BTW I am no longer receiving this error. I don't know how it was fixed. Any comment on what this error means or situations that can lead to that?

DavidSouther commented 2 months ago

opPtr is the Program Counter as far as the VM Emulator is concerned. However, it's not a global program counter, rather, a PC per function. So this error means the VM emulator has gone "off the end" of the function. opPtr gets incremented in a couple places, but more importantly, during return and goto it gets set to a new value. Usually I'd expected this error to have an off-by-one difference between the two values - 40 > 39, indicating that the 39th function instruction wasn't a return statement.

Looking at the other two cases, the value in return is itself the return value from memory.popStack, which does a very literal memory lookup for the return value. If a function were to overwrite the stack, that would certainly cause an issue. This is a bit harder than traditional stack overruns, as Hack's stack grows up, not down, but the os Memory module certainly gives full access to the entirety of the Hack's address space.

The other place, in goto, sets the opPtr based on the map in the current function's (Jack) labels. So these are effectively within-function offsets, rather than program global offsets. If that table were computed incorrectly, a jump would go to the wrong place.