Skip to content

Conversation

@glbrntt
Copy link
Collaborator

@glbrntt glbrntt commented Jan 23, 2024

Motivation:

z_stream stores a pointer to itself in its internal state which it checks against in inflate/deflate. As we hold these within structs, and call through to C functions which take a pointer to a z_stream, this address can change as the struct is copied about. This results in errors when calling deflate/inflate.

Modifications:

  • Make Compressor/Decompressor classes
  • Move the initialize call to init and the end call to deinit.

Result:

Harder to misue compressor/decompressor

Motivation:

z_stream stores a pointer to itself in its internal state which it
checks against in inflate/deflate. As we hold these within structs, and
call through to C functions which take a pointer to a z_stream, this
address can change as the struct is copied about. This results in errors
when calling deflate/inflate.

Modifications:

- Make Compressor/Decompressor classes
- Move the initialize call to init and the end call to deinit.

Result:

Harder to misue compressor/decompressor
@glbrntt glbrntt requested a review from gjcairo January 23, 2024 16:12
@glbrntt glbrntt added the version/v2 Relates to v2 label Jan 23, 2024
/// must ``initialize()`` the compressor before using it.
struct Compressor {
/// This compressor is only suitable for compressing whole messages at a time.
final class Compressor {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we use a ~Copyable type here instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't think that'd solve the problem as the types can still be moved around.

// a pointer back to itself within its internal state. Calls to deflate and inflate do a state
// check ensuring that the stream pointer and its pointer-to-self held by its internal state
// are the same. However, within a struct held on the stack this location can change and results
// in errors in deflate/inflate.
Copy link
Collaborator

Choose a reason for hiding this comment

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

This isn't really enough. Swift doesn't entirely promise that this will do what we want: in particular, it's really easy for Swift to accidentally copy the type. If we want a stable address, we really need to store the z_stream object inside a pointer. That opens up @FranzBusch's proposal for a ~Copyable type, assuming we have sufficient support in older Swift versions.

We do then also want to be a bit careful to re-spell all our extension methods on z_stream as instead being extension methods on UnsafeMutablePointer<z_stream>.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah that makes sense, the semantics are closer to what we actually want. Using a class felt like a bit of a hack.

Copy link
Collaborator

@Lukasa Lukasa left a comment

Choose a reason for hiding this comment

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

See comment

@glbrntt glbrntt requested review from Lukasa and gjcairo January 24, 2024 11:04
@glbrntt glbrntt merged commit 3d74772 into grpc:main Jan 24, 2024
@glbrntt glbrntt deleted the compression-ref branch January 24, 2024 12:40
glbrntt added a commit to glbrntt/grpc-swift that referenced this pull request Feb 5, 2024
Motivation:

z_stream stores a pointer to itself in its internal state which it
checks against in inflate/deflate. As we hold these within structs, and
call through to C functions which take a pointer to a z_stream, this
address can change as the struct is copied about. This results in errors
when calling deflate/inflate.

Modifications:

- Hold a pointer to the z_stream

Result:

Harder to misue compressor/decompressor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

version/v2 Relates to v2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants