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

Fix a memory leak in onecore interactivity #12340

Merged
1 commit merged into from
Feb 15, 2022

Conversation

zadjii-msft
Copy link
Member

As noted in #6759:

RtlCreateUnicodeString creates a copy of the string on the process heap and the PortName variable has local-scope. The string doesn't get freed with RtlFreeUnicodeString before the function returns creating a memory leak.
CIS_ALPC_PORT_NAME is a constant string and the PortName variable should instead be initialized using the RTL_CONSTANT_STRING macro:

static UNICODE_STRING PortName = RTL_CONSTANT_STRING(CIS_ALPC_PORT_NAME);

I actually built this in the OS repo to make sure it'll still build, because this code doesn't even build outside Windows.

As noted in #6759:

> `RtlCreateUnicodeString` creates a copy of the string on the process heap and the `PortName` variable has local-scope. The string doesn't get freed with `RtlFreeUnicodeString` before the function returns creating a memory leak.
> `CIS_ALPC_PORT_NAME` is a constant string and the `PortName` variable should instead be initialized using the `RTL_CONSTANT_STRING` macro:
>
> ```c++
> static UNICODE_STRING PortName = RTL_CONSTANT_STRING(CIS_ALPC_PORT_NAME);
> ```

I actually built this in the OS repo to make sure it'll still build, because this code doesn't even build outside Windows.

* [x] Closes #6759
* I work here.
@ghost ghost added Area-CodeHealth Issues related to code cleanliness, linting, rules, warnings, errors, static analysis, etc. Issue-Bug It either shouldn't be doing this or needs an investigation. Priority-2 A description (P2) Product-Conhost For issues in the Console codebase labels Feb 3, 2022
NTSTATUS Status = STATUS_SUCCESS;

// Port handle and name.
HANDLE PortHandle;
UNICODE_STRING PortName;
static UNICODE_STRING PortName = RTL_CONSTANT_STRING(CIS_ALPC_PORT_NAME);
Copy link
Member

Choose a reason for hiding this comment

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

static constexpr? If not then static const.

Copy link
Member

Choose a reason for hiding this comment

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

if constexpr is possible, let's do it. I can validate this in a quick razzle build (if @zadjii-msft wants that!)

Copy link
Member

@DHowett DHowett Feb 8, 2022

Choose a reason for hiding this comment

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

This requires a deadly const_cast -- if that is okay, let's do constexpr!

diff --git a/onecore/windows/core/console/open/src/interactivity/onecore/ConIoSrvComm.cpp b/onecore/windows/core/console/open/src/interactivity/onecore/ConIoSrvComm.cpp
index cdeebed38c365..6c1b4ffc465b0 100644
--- a/onecore/windows/core/console/open/src/interactivity/onecore/ConIoSrvComm.cpp
+++ b/onecore/windows/core/console/open/src/interactivity/onecore/ConIoSrvComm.cpp
@@ -153,7 +145,7 @@
     // Request to connect to the server.
     ConnectionMessageLength = sizeof(CIS_MSG);
     Status = NtAlpcConnectPort(&PortHandle,
-                               &PortName,
+                               const_cast<PUNICODE_STRING>(&PortName),
                                NULL,
                                &PortAttributes,
                                ALPC_MSGFLG_SYNC_REQUEST,

Copy link
Member

Choose a reason for hiding this comment

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

In that case I'd personally use a regular static... Better safe than sorry, since it's possible it might do reference counting on it, right?

Copy link
Member

@miniksa miniksa left a comment

Choose a reason for hiding this comment

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

I'd rather it just stay the static at this point too.

@lhecker lhecker added the AutoMerge Marked for automatic merge by the bot when requirements are met label Feb 15, 2022
@ghost
Copy link

ghost commented Feb 15, 2022

Hello @lhecker!

Because this pull request has the AutoMerge label, I will be glad to assist with helping to merge this pull request once all check-in policies pass.

p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (@msftbot) and give me an instruction to get started! Learn more here.

@ghost ghost merged commit 349b767 into main Feb 15, 2022
@ghost ghost deleted the dev/migrie/b/6759-this-doesnt-even-build-in-oss branch February 15, 2022 14:30
DHowett pushed a commit that referenced this pull request Mar 24, 2022
As noted in #6759:

> `RtlCreateUnicodeString` creates a copy of the string on the process heap and the `PortName` variable has local-scope. The string doesn't get freed with `RtlFreeUnicodeString` before the function returns creating a memory leak.
> `CIS_ALPC_PORT_NAME` is a constant string and the `PortName` variable should instead be initialized using the `RTL_CONSTANT_STRING` macro:
>
> ```c++
> static UNICODE_STRING PortName = RTL_CONSTANT_STRING(CIS_ALPC_PORT_NAME);
> ```

I actually built this in the OS repo to make sure it'll still build, because this code doesn't even build outside Windows.

* [x] Closes #6759
* I work here.

(cherry picked from commit 349b767)
@ghost
Copy link

ghost commented May 24, 2022

🎉Windows Terminal v1.13.1143 has been released which incorporates this pull request.:tada:

Handy links:

@ghost ghost mentioned this pull request May 24, 2022
@ghost
Copy link

ghost commented May 24, 2022

🎉Windows Terminal Preview v1.14.143 has been released which incorporates this pull request.:tada:

Handy links:

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-CodeHealth Issues related to code cleanliness, linting, rules, warnings, errors, static analysis, etc. AutoMerge Marked for automatic merge by the bot when requirements are met Issue-Bug It either shouldn't be doing this or needs an investigation. Priority-2 A description (P2) Product-Conhost For issues in the Console codebase
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ConIoSrvComm::Connect memory leak
4 participants