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

[WK2] Drop use of fixed-width types for size values in ArgumentCoder specializations #8781

Conversation

zdobersek
Copy link
Contributor

@zdobersek zdobersek commented Jan 18, 2023

d5514af

[WK2] Drop use of fixed-width types for size values in ArgumentCoder specializations
https://bugs.webkit.org/show_bug.cgi?id=250777

Reviewed by Kimmo Kinnunen.

Instead of finding better or worse fixed-sized-type matches for size values in
different ArgumentCoder specializations, the types of those size values should
be used. This means simply encoding values of size_t or unsigned types.

The target type of the size value should be noted in the code, just to provide
the necessary context of what is being encoded and have it match the decoding
side. This means the size type is explicitly used either in any temporary
variable where the value is stored, or an additional static_cast is used to
explicitly specify the type when the value is passed to the stream insertion
operator.

This still leaves open the possibility of using mismatched types, i.e. the
size value type not matching what the encoding enforces. Some sort of validation
could be implemented in the encoder class to make sure no undesired narrowing
conversions occur.

* Source/WebKit/Platform/IPC/ArgumentCoders.cpp:
(IPC::ArgumentCoder<CString>::encode):
(IPC::ArgumentCoder<CString>::decode):
(IPC::ArgumentCoder<String>::encode):
(IPC::decodeStringText):
(IPC::ArgumentCoder<String>::decode):
(IPC::ArgumentCoder<StringView>::encode):
* Source/WebKit/Platform/IPC/ArgumentCoders.h:

Canonical link: https://commits.webkit.org/259488@main

e44e2be

Misc iOS, tvOS & watchOS macOS Linux Windows
βœ… πŸ§ͺ style βœ… πŸ›  ios βœ… πŸ›  mac βœ… πŸ›  wpe
βœ… πŸ›  ios-sim βœ… πŸ›  mac-AS-debug βœ… πŸ›  gtk βœ… πŸ›  wincairo
βœ… πŸ§ͺ webkitperl βœ… πŸ§ͺ ios-wk2 βœ… πŸ§ͺ api-mac βœ… πŸ§ͺ gtk-wk2
βœ… πŸ§ͺ api-ios βœ… πŸ§ͺ mac-wk1 ❌ πŸ§ͺ api-gtk
βœ… πŸ›  tv βœ… πŸ§ͺ mac-wk2
βœ… πŸ›  tv-sim ❌ πŸ§ͺ mac-AS-debug-wk2
βœ… πŸ›  watch βœ… πŸ§ͺ mac-wk2-stress
βœ… πŸ›  πŸ§ͺ merge βœ… πŸ›  watch-sim

@zdobersek zdobersek self-assigned this Jan 18, 2023
@zdobersek zdobersek added the WebKit2 Bugs relating to the WebKit2 API layer label Jan 18, 2023
return;
}

encoder << static_cast<uint32_t>(string.length());
encoder << validateType<size_t>(string.length());
Copy link
Contributor

Choose a reason for hiding this comment

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

encode.template encode<size_t>(string.length());

Which is more appropriate? I don't know..

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Idea was to have compile-time validation of what the expected type returned by the size accessor is. This approach does it through a templated pass-through, avoiding adding static asserts everywhere.

Along with compile-time validation, it also specifies clearly the type that's going into the encoder, giving clear indication of the type that's expected on the decoding side. Just extra assurance against any future changes that might unknowingly end up tweaking these parts.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, that part is good.
What I mean is that there's an option:
Either:
a) validate via encoder << validateType<size_t>(...)
Or:
b) enforce via encode.template encode<size_t>(...)
Or:
c) enforce via encoder << static_cast<size_t>(..)

So the question is why validate instead of enforce?

If the enforce would be missing, in that case the validate would be missing aswell, so there's no additional security guarantee ?
Maybe the benefit of validate would be if there are warnings of redundant static_casts?
Or that we think that a developer will remove the redundant static cast?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Validating, as done here, forces adjustment to the type that's returned by the size accessor, no unwitting conversion to anything else allowed. It's a bit clunky to work with, and as seen it doesn't really cause anything to be fixed at the moment.

Enforcing can, on integer types, cause problematic conversions. But at the moment I think that's not happening in any of these specializations, so I would just switch to that. b) seems nicest, and it allows to eventually implement validation in a more refined way.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought encoder.template encoder<T>() exists, but it doesn't. Can be added later. Going with c).

@zdobersek zdobersek force-pushed the eng/WK2-Drop-use-of-fixed-width-types-for-size-values-in-ArgumentCoder-specializations branch from 9ae8312 to e44e2be Compare January 23, 2023 21:20
@zdobersek zdobersek marked this pull request as ready for review January 23, 2023 21:20
@webkit-ews-buildbot webkit-ews-buildbot added the merging-blocked Applied to prevent a change from being merged label Jan 24, 2023
@zdobersek zdobersek added merge-queue Applied to send a pull request to merge-queue and removed merging-blocked Applied to prevent a change from being merged labels Jan 27, 2023
…specializations

https://bugs.webkit.org/show_bug.cgi?id=250777

Reviewed by Kimmo Kinnunen.

Instead of finding better or worse fixed-sized-type matches for size values in
different ArgumentCoder specializations, the types of those size values should
be used. This means simply encoding values of size_t or unsigned types.

The target type of the size value should be noted in the code, just to provide
the necessary context of what is being encoded and have it match the decoding
side. This means the size type is explicitly used either in any temporary
variable where the value is stored, or an additional static_cast is used to
explicitly specify the type when the value is passed to the stream insertion
operator.

This still leaves open the possibility of using mismatched types, i.e. the
size value type not matching what the encoding enforces. Some sort of validation
could be implemented in the encoder class to make sure no undesired narrowing
conversions occur.

* Source/WebKit/Platform/IPC/ArgumentCoders.cpp:
(IPC::ArgumentCoder<CString>::encode):
(IPC::ArgumentCoder<CString>::decode):
(IPC::ArgumentCoder<String>::encode):
(IPC::decodeStringText):
(IPC::ArgumentCoder<String>::decode):
(IPC::ArgumentCoder<StringView>::encode):
* Source/WebKit/Platform/IPC/ArgumentCoders.h:

Canonical link: https://commits.webkit.org/259488@main
@webkit-commit-queue webkit-commit-queue force-pushed the eng/WK2-Drop-use-of-fixed-width-types-for-size-values-in-ArgumentCoder-specializations branch from e44e2be to d5514af Compare January 27, 2023 16:35
@webkit-commit-queue webkit-commit-queue merged commit d5514af into WebKit:main Jan 27, 2023
@webkit-commit-queue
Copy link
Collaborator

Committed 259488@main (d5514af): https://commits.webkit.org/259488@main

Reviewed commits have been landed. Closing PR #8781 and removing active labels.

@webkit-commit-queue webkit-commit-queue removed the merge-queue Applied to send a pull request to merge-queue label Jan 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
WebKit2 Bugs relating to the WebKit2 API layer
Projects
None yet
5 participants