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

Improve memory management in Channel #368

Merged
merged 3 commits into from
Feb 14, 2019
Merged

Conversation

kevints
Copy link
Contributor

@kevints kevints commented Feb 14, 2019

  • Use the gpr family of allocation functions instead of malloc/free (as
    these cannot return NULL).
  • Explicitly extend lifetime of argumentWrappers - Swift doesn't
    guarantee that objects will live to the end of their scope, so
    the optimizer is free to call Channel.Argument.Wrapper.deinit before
    the call to cgrpc_channel_create_secure, which would result in a
    use-after-free.

* Use the gpr family of allocation functions instead of malloc/free (as
  these cannot return `NULL`).
* Explicitly extend lifetime of `argumentWrappers` - Swift doesn't
  guarantee that objects will live to the end of their scope, so
  the optimizer is free to call Channel.Argument.Wrapper.deinit before
  the call to cgrpc_channel_create_secure, which would result in a
  use-after-free.
Copy link
Collaborator

@MrMage MrMage left a comment

Choose a reason for hiding this comment

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

Nice! I meant to change the malloc/free calls (#134), but apparently never got around to actually do that.

While at it, would you mind adding the one-line change mentioned in #340 as well, given that it is in a similar spirit?

@@ -105,8 +102,7 @@ cgrpc_call *cgrpc_channel_create_call(cgrpc_channel *channel,
NULL);
grpc_slice_unref(host_slice);
grpc_slice_unref(method_slice);
cgrpc_call *call = (cgrpc_call *) malloc(sizeof(cgrpc_call));
memset(call, 0, sizeof(cgrpc_call));
cgrpc_call *call = (cgrpc_call *) gpr_zalloc(sizeof(cgrpc_call));
Copy link
Collaborator

Choose a reason for hiding this comment

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

I assume gpr_zalloc is guaranteed to return zeroed memory?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kevints
Copy link
Contributor Author

kevints commented Feb 14, 2019

Nice! I meant to change the malloc/free calls (#134), but apparently never got around to actually do that.

While at it, would you mind adding the one-line change mentioned in #340 as well, given that it is in a similar spirit?

It's not clear to me from the docs that grpc_secure_channel_create copies the creds object, but tracing through the code on master it does appear to. Since a memory leak is much better than a use-after-free I'll post that change as a follow-up so that someone more knowledgable can chime in on the review.

@MrMage
Copy link
Collaborator

MrMage commented Feb 14, 2019

Nice! I meant to change the malloc/free calls (#134), but apparently never got around to actually do that.
While at it, would you mind adding the one-line change mentioned in #340 as well, given that it is in a similar spirit?

It's not clear to me from the docs that grpc_secure_channel_create copies the creds object, but tracing through the code on master it does appear to. Since a memory leak is much better than a use-after-free I'll post that change as a follow-up so that someone more knowledgable can chime in on the review.

Good point! It does feel like ownership semantics like this are poorly documented in the gRPC-Core API.

@MrMage MrMage merged commit fcb8ab3 into grpc:master Feb 14, 2019
@kevints kevints deleted the lifetime-management branch February 14, 2019 18:40
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.

2 participants