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

Introduce chromium safe math #4144

Merged
8 commits merged into from
Jan 16, 2020
Merged

Introduce chromium safe math #4144

8 commits merged into from
Jan 16, 2020

Conversation

miniksa
Copy link
Member

@miniksa miniksa commented Jan 7, 2020

Summary of the Pull Request

PR Checklist

  • Closes Find a safer safe math #4013
  • I work here.
  • Existing tests should be OK. Real changes, just adding a lib to use.
  • Couldn't find any existing docs about intsafe.
  • Am core contributor.

Detailed Description of the Pull Request / Additional comments

Files with old safe math:

  • TerminalControl: TSFInputControl.cpp
  • TerminalCore: TerminalDispatch.cpp
  • TerminalCore: TerminalSelection.cpp
  • Host: directio.cpp
  • RendererGdi: invalidate.cpp
  • RendererGdi: math.cpp
  • RendererGdi: paint.cpp
  • RendererVt: paint.cpp
  • TerminalAdapter: adaptDispatch.cpp
  • Types: viewport.cpp
  • Types: WindowUiaProviderBase.cpp

Validation Steps Performed

…appropriate with license. Register component governance information.
…e path to base directory to common pre props.
…s conflicts with the min/max definitions provided by minwinbase.h. We usually stomp these out, but some of our projects have more complicated/interesting precomp defines which are hell to detangle (or we would have done it already). To work around, put NOMINMAX at the top of all of these non-unified precomps and reintroduce it at the very bottom for the only two projects that actually make use of it.
@miniksa miniksa added Issue-Task It's a feature request, but it doesn't really need a major design. Product-Conhost For issues in the Console codebase Product-Conpty For console issues specifically related to conpty Product-Meta The product is the management of the products. Product-Terminal The new Windows Terminal. Area-CodeHealth Issues related to code cleanliness, linting, rules, warnings, errors, static analysis, etc. labels Jan 7, 2020
@miniksa miniksa marked this pull request as ready for review January 8, 2020 21:25
@miniksa miniksa requested review from DHowett-MSFT and zadjii-msft and removed request for DHowett-MSFT January 9, 2020 17:06
@miniksa
Copy link
Member Author

miniksa commented Jan 9, 2020

I can't formally request @j4james and @lhecker on this review, but feel free to chime in if you like.

@WSLUser
Copy link
Contributor

WSLUser commented Jan 9, 2020

It's very interesting seeing Windows Terminal will have some of it's source code coming from Chromium. I'd also suggest configuring dependabot to update this dependency source from the github mirror as updates to the source occur.

Quick (likely dumb) question: Does Windows Calculator have code we can use vice Chromium? Or is it just much easier re-using what Chromium already uses?

@lhecker
Copy link
Member

lhecker commented Jan 9, 2020

@WSLUser Open-Source FTW! 🎉

Windows Calculator doesn't use "native" numeric types to represent numbers, but rather this.
As such we can't use it for our purpose.

@WSLUser
Copy link
Contributor

WSLUser commented Jan 9, 2020

Thanks for pointing at that. Yeah that won't be helpful in our case. I'm sure they could extend out the functionality if a use-case was provided. If it had been something a bit more "native", using code from the same authority (MS) would of been better from a support perspective. Thanks for addressing.

@miniksa
Copy link
Member Author

miniksa commented Jan 9, 2020

FYI, Chromium usage is an easy sell because of its compatible licensing and our company's existing engagement with Chromium in the form of the new Edge product. So yes: we do live in interesting times.

I've also moved the dependabot idea to #4165. It's a good idea, but I'm not going to bind it to this particular PR.

Copy link
Member

@lhecker lhecker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this library will be very helpful, especially because the different classes and methods neatly integrate with each other.
IMHO it'd be helpful if you document somewhere (in any way) what kind of changes you did to integrate this library. This would help when someone wants to update the library from the upstream (i.e. chromium). This of course only applies if you did any changes at all.
You can also remove the 3 build files if you'd like (BUILD.gn, DEPS and cgmanifest.json).

template <typename T>
static T HandleFailure() {
#if defined(_MSC_VER)
__debugbreak();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only line which you need to keep in mind when integrating this code IMO.
AFAICS SafeInt only throws an exception in case of a failure, whereas this library causes a hard crash.
You could modify the code to throw an exception instead - for instance a primitive subclass of std::range_error. 🙂

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I honestly didn't read every line of this before integrating it here. I presumed that one of the 4 classes of math here would throw on failure instead of crashing hard or that there was a template argument to override the behavior. Is that not true? If we need to modify this, we will. I was hoping to get this in, start using it, then modify it only if necessary when we discover problems.

Copy link
Collaborator

@j4james j4james Jan 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I presumed that one of the 4 classes of math here would throw on failure instead of crashing hard

I hadn't looked at it in detail either, but I think you're right. Based on what they say in the README, I think the "checked" APIs crash on error, and the "strict" ones throw. Actually I misread that. The "strict" ones do compile time checking - not run time. But there does seem to be a template parameter that can control the error behaviour, so if there isn't already a version that throws, we should be able to derive one.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will not really be an issue in the beginning @miniksa @j4james. 🙂
Only 2 parts of the numerics API will call HandleFailure() and "crash" the process:

  • CheckedNumeric's ValueOrDie - I suppose the naming of the function is very obvious 😄
  • checked_cast<Dst, CheckHandler = CheckOnFailure, Src>() - we can just use gsl::narrow_cast instead, or simply create an alias for it in the til namespace with a replacement for the CheckHandler

Either way we should really only be careful about checked_cast at most IMO.
Also "crashing" during an erroneous checked cast is actually not even that bad of an idea in my opinion. 😶
I just wanted to mention this circumstance because I know that gsl::narrow_cast is being used, which throws an exception instead.

@miniksa
Copy link
Member Author

miniksa commented Jan 9, 2020

IMHO it'd be helpful if you document somewhere (in any way) what kind of changes you did to integrate this library. This would help when someone wants to update the library from the upstream (i.e. chromium). This of course only applies if you did any changes at all.
You can also remove the 3 build files if you'd like (BUILD.gn, DEPS and cgmanifest.json).

I did zero changes to it. That's why BUILD.gn and DEPS are still there.
I introduced cgmanifest.json myself. That file is one of the ways Microsoft tracks dependencies in our own governance systems.
Then the only other special thing I did was copy the LICENSE into a subsection of our NOTICE file in the root.

Do you think I should put all that in a MAINTAINER_README.md or something?

@DHowett-MSFT
Copy link
Contributor

We may actually want to namespace a bunch of this stuff into til (with using), more than what @lhecker's already suggested. It'll make us more robust if we ever do want to change things up in the future, and avoid littering chromium::numerics::whatever all over every part of the code.

@lhecker
Copy link
Member

lhecker commented Jan 9, 2020

IMO briefly documenting how you vendored dependencies is worth the time, because the description can be quite short, but still be tremendously helpful.
For instance imagine if you note down:

  • the source URL and the Git hash of the version you checked out
  • just a short "yeah I didn't change anything" message

...it will allow the next person to be like: "Yup, I just need to download it from here and don't need to be careful at all since nothing was changed previously either. ™️ That 9000 line diff is all due to upstream changes which don't need any review. ™️" 😄 But in all seriousness, as you can imagine, that can be quite a stress relief.
In this particular case it might be of small benefit, but in general? I guess yeah, it's a good idea.

@miniksa
Copy link
Member Author

miniksa commented Jan 10, 2020

IMO briefly documenting how you vendored dependencies is worth the time, because the description can be quite short, but still be tremendously helpful.
For instance imagine if you note down:

  • the source URL and the Git hash of the version you checked out
  • just a short "yeah I didn't change anything" message

...it will allow the next person to be like: "Yup, I just need to download it from here and don't need to be careful at all since nothing was changed previously either. ™️ That 9000 line diff is all due to upstream changes which don't need any review. ™️" 😄 But in all seriousness, as you can imagine, that can be quite a stress relief.
In this particular case it might be of small benefit, but in general? I guess yeah, it's a good idea.

It's done.

@miniksa
Copy link
Member Author

miniksa commented Jan 10, 2020

We may actually want to namespace a bunch of this stuff into til (with using), more than what @lhecker's already suggested. It'll make us more robust if we ever do want to change things up in the future, and avoid littering chromium::numerics::whatever all over every part of the code.

Now in #4179 for someone enterprising (my bet is on @skyline75489) to pick up and do after this is merged.

Copy link
Contributor

@DHowett-MSFT DHowett-MSFT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

min and max were kill, so fix those up and we're good to go (like the crunchwrap supreme)

@miniksa miniksa added the AutoMerge Marked for automatic merge by the bot when requirements are met label Jan 16, 2020
@ghost
Copy link

ghost commented Jan 16, 2020

Hello @miniksa!

Because this pull request has the AutoMerge label, I will be glad to assist with helping to merge this pull request once all check-in policies pass.

p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (@msftbot) and give me an instruction to get started! Learn more here.

@ghost ghost merged commit 4d1c7cf into master Jan 16, 2020
@ghost ghost deleted the dev/miniksa/math branch January 16, 2020 18:51
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-CodeHealth Issues related to code cleanliness, linting, rules, warnings, errors, static analysis, etc. AutoMerge Marked for automatic merge by the bot when requirements are met Issue-Task It's a feature request, but it doesn't really need a major design. Product-Conhost For issues in the Console codebase Product-Conpty For console issues specifically related to conpty Product-Meta The product is the management of the products. Product-Terminal The new Windows Terminal.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Find a safer safe math
9 participants