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

Simplify header handling in HSTRING #1747

Merged
merged 2 commits into from
May 10, 2022

Conversation

rylev
Copy link
Contributor

@rylev rylev commented May 9, 2022

This simplifies the HSTRING header and adds more safety documentation.

@rylev rylev requested review from eholk and kennykerr May 9, 2022 13:52
@kennykerr
Copy link
Collaborator

Let's merge #1749 first just to be sure we don't break anything. 😉

eholk
eholk previously approved these changes May 9, 2022
Copy link
Collaborator

@eholk eholk left a comment

Choose a reason for hiding this comment

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

Looks good! I added a few comments, but they're mostly just (bad) ideas to consider for the future.

}

#[repr(C)]
struct Shared {
count: RefCount,
buffer_start: u16,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm guessing this is this way to match the way things already were in COM-land, but it's a little weird to me that buffer_start is not zero-sized.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not having this as a zero-sized type caused some confusion that I was discussing with @kennykerr - namely, that we allocate std::mem::size_of::<Header> + 2 * length_of_hstring bytes we're allocating enough space for string data that fits all of the contents accounted for by length_of_hstring plus a terminating null u16 which length_of_string does not account for but that extra u16 in Header does.

Definitely something to consider...

Copy link
Collaborator

@kennykerr kennykerr May 10, 2022

Choose a reason for hiding this comment

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

This is just the way the HSTRING was implemented internally by the OS, so it helps to keep the header representation similar for clarity. Note that an empty, or zero-length, HSTRING is internally represented by a null pointer so there will never be a zero-length header.

crates/libs/windows/src/core/hstring.rs Show resolved Hide resolved
@rylev
Copy link
Contributor Author

rylev commented May 10, 2022

@kennykerr this should be ready to go now.

While I have your attention, it seems we're debug asserting in Hstring::clear that we're not trying to clear a "fast pass" string. This seems wrong, since we should also not call heap_free in production when debug asserts are not enabled. What is the correct way to handle this? Should self.0 still be set to null?

@kennykerr
Copy link
Collaborator

It is strictly a bug in the language projection if this debug assert is true. I added it just to validate my implementation was correct when I first wrote the code but it can never happen, short of a logic bug in the HSTRING implementation.

@rylev rylev merged commit 7efe684 into microsoft:master May 10, 2022
@rylev rylev deleted the small-hstring-improvements branch May 10, 2022 15:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants