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

memory leak in DiagnosticBuilder.into_diagnostic() #69600

Closed
dwrensha opened this issue Mar 1, 2020 · 6 comments · Fixed by #69628
Closed

memory leak in DiagnosticBuilder.into_diagnostic() #69600

dwrensha opened this issue Mar 1, 2020 · 6 comments · Fixed by #69628
Labels
C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@dwrensha
Copy link
Contributor

dwrensha commented Mar 1, 2020

This code leaks the Box at self.0:

unsafe {
diagnostic = std::ptr::read(&self.0.diagnostic);
std::mem::forget(self);
};

The leak was introduced in 2fcd870. cc @nnethercote

@dwrensha
Copy link
Contributor Author

dwrensha commented Mar 1, 2020

We could avoid the leak and the unsafe by doing

let diagnostic = self.0.diagnostic.clone();
self.cancel();

or if we're concerned about performance here, we could maybe do something with drop_in_place().

@jonas-schievink jonas-schievink added C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 1, 2020
@nnethercote
Copy link
Contributor

Thanks for the report, I will measure the perf effects of the different alternatives.

I'd like to know how you found this -- code inspection? Some kind of tool?

@dwrensha
Copy link
Contributor Author

dwrensha commented Mar 2, 2020

I found it while running fuzz-rustc and noticing that I kept running out of memory. I turned on -Z sanitizer=address and this popped out.

@dwrensha
Copy link
Contributor Author

dwrensha commented Mar 2, 2020

Another alternative could be to call std::mem::replace() to swap in a dummy diagnostic, instead of cloning the existing one.

@dwrensha
Copy link
Contributor Author

dwrensha commented Mar 2, 2020

Below is an example compiler input that triggers the leak:

str// bass (FIXMoo {
}


         )

@dwrensha
Copy link
Contributor Author

dwrensha commented Mar 2, 2020

Simpler example that triggers the leak: just a single closing brace

}

bors added a commit that referenced this issue Mar 2, 2020
…tic-leak, r=<try>

Fix a leak in `DiagnosticBuilder::into_diagnostic`.

Fixes #69600.

r? @Centril
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Mar 3, 2020
…nto_diagnostic-leak, r=Centril

Fix a leak in `DiagnosticBuilder::into_diagnostic`.

Fixes rust-lang#69600.

r? @Centril
JohnTitor added a commit to JohnTitor/rust that referenced this issue Mar 3, 2020
…nto_diagnostic-leak, r=Centril

Fix a leak in `DiagnosticBuilder::into_diagnostic`.

Fixes rust-lang#69600.

r? @Centril
@bors bors closed this as completed in 99a595e Mar 3, 2020
Mark-Simulacrum pushed a commit to Mark-Simulacrum/rust that referenced this issue Mar 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants