Skip to content

An issue about the Math.max(). #6519

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

Closed
NWU-NISL opened this issue Oct 8, 2020 · 14 comments · Fixed by #6851
Closed

An issue about the Math.max(). #6519

NWU-NISL opened this issue Oct 8, 2020 · 14 comments · Fixed by #6851

Comments

@NWU-NISL
Copy link

NWU-NISL commented Oct 8, 2020

When I passed NaN and an object with the "valueOf" attribute value as a callable function to the first and second parameters of Math.max, chakra did not execute this function. According to the ES10 standard, the ToNumber operation is performed on each parameter of Math.max, and the "valueOf" attribute value function of the second parameter will be executed.

version

chakra-1_11_22

command

ChakraCore/out/Debug/ch testcase.js

testcase

var NISLFuzzingFunc = function(){
    var a = {
        valueOf: function(){
            print(1);
        }
    };
    var b = Math.max(NaN,a);
};
NISLFuzzingFunc();

Output

No output.

Expected behavior

Output 1.

Contributor : @Haobin-Lee

@NWU-NISL NWU-NISL changed the title An issue about the Matn.max(). An issue about the Math.max(). Oct 8, 2020
@OmarMorales71
Copy link

OmarMorales71 commented Nov 30, 2020

@ppenzin Hello, I would like to contribute to this project solving this issue. I'm working on it :D

@OmarMorales71
Copy link

@ppenzin Where I can fine the definition of the Math.max() method?

@ppenzin
Copy link
Member

ppenzin commented Dec 1, 2020

@OmarMorales71 thank you for your interest! This is the most complete spec forMath.max():

https://tc39.es/ecma262/#sec-math.max

Feel free to ask if you have any further questions!

@rhuanjl
Copy link
Collaborator

rhuanjl commented Dec 5, 2020

Interestingly if you reverse the order of parameters Math.max(a,NaN) you get the expected output.

Also noted that this repros in both Master and release 1.11.

The implementation of Math.max was changed between the two, in 1.11 it's implemented here:
https://github.com/microsoft/ChakraCore/blob/d8cbaff6a2ef6750a51518c560644f6b64fce928/lib/Runtime/Library/MathLibrary.cpp#L694-L764

In master it's instead implemented with self-hosted JS here:
https://github.com/microsoft/ChakraCore/blob/d8cbaff6a2ef6750a51518c560644f6b64fce928/lib/Runtime/Library/JsBuiltIn/JsBuiltIn.js#L794-L841

The 1.11 C++ implementation is preserved in master as a fallback in case self-hosted JS is unavailable for some reason. It's interesting that both implementations reproduce this error.

@OmarMorales71 Looking at the JS implementation, to fix the error we unfortunately need to slightly de-optimise the toNumber step is done by doing value = +value we need to do that to every parameter before we do anything that can return - try to figure out how to do that without any unnecessary steps - also we want to avoid accessing indexed argument values in the cases where we don't need to. We should also fix the C++ implementation - it will probably be a similar issue.
Note if you fix the JS implementation you'll need to run the bytecode regen before then building to test it either test/xplatregenbytecode.py (on linux or macOS) OR regenallbytecode.cmd on windows

@Abdullah17m
Copy link

Hello there i am new to open source contribution. Where can i find this piece of code in your repository?

@rhuanjl
Copy link
Collaborator

rhuanjl commented Aug 18, 2021

Hello there i am new to open source contribution. Where can i find this piece of code in your repository?

Have a look at the links in my previous comment should take you to the correct source files.

@anshul2807
Copy link

Hey I want to Contribute on this Issue, Can I??

@rhuanjl
Copy link
Collaborator

rhuanjl commented Aug 18, 2021

@anshul2807 it's a good first issue if you want to take it - though not sure if @Abdullah17m was already intending to take it?

Don't want two people working on the same issue.

@ayushrana86
Copy link

ayushrana86 commented Sep 1, 2021

Hello, @rhuanjl is this issue still available.
I tried creating this error by reversing the order of parameters Math.max(a,NaN) but the results I got is NaN can put a snapshot of the result that you got, it will help a lot.

@rhuanjl
Copy link
Collaborator

rhuanjl commented Sep 2, 2021

Hello, @rhuanjl is this issue still available.

I tried creating this error by reversing the order of parameters Math.max(a,NaN) but the results I got is NaN can put a snapshot of the result that you got, it will help a lot.

The issue is still available, the error is that the function short cuts/does an early return of the first parameter is NaN.

@YaseenYo
Copy link

YaseenYo commented Sep 8, 2021

Hello, @rhuanjl can we solve this issue by adding a for loop before considering the first argument...

//can we add this, It just iterates over all the arguments
for (let i = 1; i < arguments.length; i++) { nextVal = +arguments[i]; }

//already in code
value1 = +value1; if (value1 !== value1) { return NaN; }

@YaseenYo
Copy link

YaseenYo commented Sep 8, 2021

@rhuanjl Can We do this?

    let max = value1;
    let nextVal;
    let isNaN = false;

    for (let i = 1; i < arguments.length; i++) {
        nextVal = +arguments[i];
        if (nextVal !== nextVal) {
            isNaN = true;
        }
        else if(isNaN){
             continue;
        }
        else if ((max < nextVal) || (max === nextVal && max === 0 && 1/max < 1/nextVal)) { 
            max = nextVal;
        } 
    }

    if(isNaN){
         return NaN;
    }
    else {
         return max;
    }

@Harsh-Shedge
Copy link

Is this issue resolved? Can I try to work on this?

@ppenzin
Copy link
Member

ppenzin commented Aug 2, 2022

@Harsh-Shedge sure, you can give it a try.

rhuanjl pushed a commit that referenced this issue Oct 7, 2022
According to the spec, Math.max and Math.min should convert all args to numbers (See ToNumber).
CC takes a shortcut by just returning NaN as soon as it finds a NaN arg.
Therefore, possibly missing calls to valueOf of a later arg.

Fix #6519
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants