-
-
Notifications
You must be signed in to change notification settings - Fork 415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Strange issue with postfix increment and array indexing #1917
Comments
I did some debugging here, and the reason behind this is the order in which we evaluate expressions. This code: arr[i>>2] = str.charCodeAt(i++)<<24 | str.charCodeAt(i++)<<16 | str.charCodeAt(i++)<<8 | str.charCodeAt(i++); In theory, the first thing that should get evaluated is But with Boa, we seem to be evaluating first the right hand side, and then the left hand side, which in the first iteration, |
This seems to be a very interesting issue. The following example returns different results in v8, spidermonkey and boa: i = 0
arr = []
arr[i++] = i++ boa arr[0] // undefined
arr[1] // 0 v8 arr[0] // undefined
arr[1] // 2 spidermonkey arr[0] // 1
arr[1] // undefined |
They are all wrong, right? I guess the correct result should be: arr[0]; // 1
arr.length; // 1
i; // 2 |
That's the correct result, but SpiderMonkey does get it right, since |
From our VM perspective, this is tricky to get right, because we have to first calculate the object node ( The way we do it right now is the inverse. We first calculate the value, then the field name, then the object node. This is much easier, because in a post-increment node, we can push the final result of adding But, since we need to first get the object node, then the field name calculation, and finally the value, the post-increment result needs to be "stored" until the whole field name gets calculated. This gets even more complex with expressions such as this one: arr[i++][i++] = i++; Which, it's the same as writing: arr[0][1] = 2;
i = 3; I'm a bit out of my depth on how to do this, to be honest xD if anyone has some ideas, I'm happy to read and try to implement it since the VM is definitely not my specialty, and it would be a nice learning opportunity for me. |
Would it be feasible to treat in-place increments as syntax sugar? arr[i++][i++] = i++; into this let 1i = i++;
let 2i = i++;
let 3i = i++;
arr [1i][2i] = 3i; That way we don't need to juggle with the stack, we just need to extract the increments on parse and substitute the increments with variable names |
This could be a posibility, yes, but we have not yet done any post-parsing / pre-compilation modification of the code. I guess this would be another phase. We could also add optimizations here too. So if we encounter |
#1954 should handle this, but implementors should be careful that @jedel1043's solution is incomplete for scenarios like: while ((arr[i++][i++] = i++) != 0) {} It might be necessary to juggle the stack or to generate VM instructions which are not one-to-one with JS. An alternative would be to pre-declare some variables and use a comma-operator, e.g.: let 1i;
let 2i;
let 3i;
while (1i = i++, 2i = i++, 3i = i++, (arr[1i][2i] = 3i) != 0) {} Which may work in all cases, though I haven't tested this obviously :) |
Or we could just put the increments at the end of the loop, I think. let 1i = i++;
let 2i = i++;
let 3i = i++;
while (arr[1i][2i] = 3i){
... Statements ...
1i = i++;
2i = i++;
3i = i++;
} Interestingly, this is very close to how some languages desugar a |
I believe this library is broken because of this issue: https://github.com/emn178/js-sha256 This is a pretty scary issue for our users, as any library could appear to function properly (no errors) but simply produce incorrect results. |
Well, all libraries that have a bug produce incorrect results without giving any feedback that could indicate that a bug was triggered. The question is "how critical is the bug?" In this case, it seems that V8, one of the most used JS libraries has a similar bug to Boa too, so it seems it's either not so critical or very difficult to solve (which I think it's the case). In any case, Boa is not ready for production use, as it can be seen from the sub 1.0 version number, and the "pre-release" marking in all the releases, so these bugs are expected. Note that even on the latest commit in the main branch, 40% of the official test suite is still failing, so we definitely have a ton of things to fix. That said, this is on our radar, it's just tricky to get it right. |
The code for js-sha256 works fine in V8 (Node.js). The bugs found in this issue with the libraries I've tested have never presented in V8 (Node.js) during my testing. I'm pretty sure I used Node.js and possibly Chrome to ensure that the issue was only with Boa. This issue seems to indicate a critical bug that V8 (and I would assume the other major JS engines) do not have. |
So, V8 has a bug, but it's not affecting this particular usage of the js-sha256 library. We have a different bug (but very much related), and in this case, it affects this library. What I mean is that a different library could break with V8 due to this bug, but many might have just been designed to work around the V8 bug, by testing them there. We will try to fix this, of course, and I don't think we should release a 1.0 version before this gets fixed. |
We suspect this is now breaking a |
@jedel1043 @raskad do you think the solution offered above could be easily implemented in the byte compiler? @lastmjs if you find resources to work on this, help is very much welcome. |
If no one is working on this, I'd like to try and solve this :) |
I at least am not working on it, and I would love for this to be solved @HalidOdat |
I am revisiting the AST walking issue today. With AST walking, this should be a straightfoward rewrite by walking over the instructions and replacing with temporaries. |
Describe the bug
While incorporating a sha224 library into one of my boa projects, I noticed the sha224 implementation was producing incorrect hashes when run through boa. With enough fiddling, I seem to have found a couple issues. This bug report highlights one of them. The code below describes how to reproduce it.
To Reproduce
Here is the code to run through boa, focusing on the original
strToInt
function. It is just one of the functions that was causing the sha224 hashing to be incorrect. This same or a similar problem manifests in0.13
or on the main branch as of yesterday:The return result printed with Rust will be this:
Expected behavior
There is an extra null element in the
strToIntWithBug
result. This element should not be there.Build environment (please complete the following information):
Additional context
A similar issue can be found here: #1918
The text was updated successfully, but these errors were encountered: