Skip to content
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

Audit source-generated marshallers for integer overflow bugs #69532

Closed
jkotas opened this issue May 19, 2022 · 7 comments · Fixed by #69619
Closed

Audit source-generated marshallers for integer overflow bugs #69532

jkotas opened this issue May 19, 2022 · 7 comments · Fixed by #69619
Assignees
Labels
area-System.Runtime.InteropServices source-generator Indicates an issue with a source generator feature
Milestone

Comments

@jkotas
Copy link
Member

jkotas commented May 19, 2022

For example, here:

int spaceToAllocate = Math.Max(array.Length * _sizeOfNativeElement, 1);

What happens when this multiplication overflows?

@ghost ghost added the untriaged New issue has not been triaged by the area owner label May 19, 2022
@ghost
Copy link

ghost commented May 19, 2022

Tagging subscribers to this area: @dotnet/interop-contrib
See info in area-owners.md if you want to be subscribed.

Issue Details

For example, here:

int spaceToAllocate = Math.Max(array.Length * _sizeOfNativeElement, 1);

What happens when this multiplication overflows?

Author: jkotas
Assignees: -
Labels:

area-System.Runtime.InteropServices

Milestone: -

@AaronRobinsonMSFT AaronRobinsonMSFT added the source-generator Indicates an issue with a source generator feature label May 19, 2022
@AaronRobinsonMSFT AaronRobinsonMSFT added this to the 7.0.0 milestone May 19, 2022
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label May 19, 2022
@AaronRobinsonMSFT
Copy link
Member

Should we be wrapping most arithmetic operations in checked?

e.g., checked(array.Length * _sizeOfNativeElement)

I believe this would use the mul.ovf vs mul. Do we have official .NET guidance on this sort of thing?

@jkotas
Copy link
Member Author

jkotas commented May 19, 2022

Should we be wrapping most arithmetic operations in checked?

Yes. And/or do buffer size calculations using nuints if it is appropriate.

Do we have official .NET guidance on this sort of thing?

There is a lot of generic guidance about integer overflows. I do not know about a good .NET specific guidance. @GrabYourPitchforks ?

@AaronRobinsonMSFT
Copy link
Member

AaronRobinsonMSFT commented May 19, 2022

There is a lot of generic guidance about integer overflows.

Yeah I get the general problem. My first thought was that .NET always checked this by default - didn't realize this wasn't the case. The second part of this query is after reading the JIT code for mul.ovf it does have some overhead. That begs the question should we be doing a check upfront to determine if we should throw ourselves and avoid the checked approach or should we always use the checked and rely on the JIT.

@jkotas
Copy link
Member Author

jkotas commented May 19, 2022

I think it is fine to use checked and depend on the JIT. checked is only necessary for the initial buffer size calculation. Once the buffer is allocated, it is typically not necessary to use checked since you know that it is not going to overflow.

@tannergooding
Copy link
Member

tannergooding commented May 19, 2022

This might also be able to drive some work for the JIT to handle cases where checked is used but no overflow is possible. The JIT doesn't really optimize those cases at all today

I'd agree that using checked is desirable here, however. Especially if its just a "one time" cost. The check is often ins; jcc rather than an explicit cmp, jcc, ins or other more complex sequence

@GrabYourPitchforks
Copy link
Member

I noticed this in the string marshallers as well the other day, e.g.:

I think the worst that will happen here is a runtime exception since somebody somewhere will detect the overflow, but I don't think we want to rely on the good graces of the implementation to handle error checking on our behalf.

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label May 20, 2022
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label May 25, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Jun 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Runtime.InteropServices source-generator Indicates an issue with a source generator feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants