Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 35 additions & 5 deletions dev/AppLifecycle/AppInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
#include "PushNotificationManager.h"
#include "AppNotificationManager.h"

#include <FrameworkUdk/Containment.h>

// 63876312: Prevent AppInstance::GetInstances from registering an invalid process handle.
#define WINAPPSDK_CHANGEID_63876312 63876312

using namespace winrt;
using namespace winrt::Windows::Foundation;
using namespace winrt::Windows::Foundation::Collections;
Expand Down Expand Up @@ -96,7 +101,13 @@ namespace winrt::Microsoft::Windows::AppLifecycle::implementation
return { kind, data };
}

AppInstance::AppInstance(uint32_t processId)
AppInstance::AppInstance() :
AppInstance(GetCurrentProcessId(), {})
{
}

AppInstance::AppInstance(uint32_t processId, wil::unique_handle processHandle) :
m_instanceHandle(std::move(processHandle))
{
m_processId = processId;
m_isCurrent = (GetCurrentProcessId() == processId);
Expand Down Expand Up @@ -146,7 +157,15 @@ namespace winrt::Microsoft::Windows::AppLifecycle::implementation
}
else
{
m_instanceHandle.reset(OpenProcess(SYNCHRONIZE, FALSE, processId));
if (!m_instanceHandle)
{
m_instanceHandle.reset(OpenProcess(SYNCHRONIZE, FALSE, processId));
}

if (WinAppSdk::Containment::IsChangeEnabled<WINAPPSDK_CHANGEID_63876312>())
{
THROW_HR_IF(E_INVALIDARG, !m_instanceHandle);
}

// Create a monitor thread to handle cleaning up this instance if the backing process terminates.
auto onInstanceTerminated = [](_In_ void* context, _In_ BOOLEAN /*reason*/) -> void
Expand Down Expand Up @@ -277,7 +296,7 @@ namespace winrt::Microsoft::Windows::AppLifecycle::implementation
{
auto initInstance = []
{
s_current = winrt::make_self<AppInstance>(GetCurrentProcessId());
s_current = winrt::make_self<AppInstance>();
};

wil::init_once(s_initOnce, initInstance);
Expand All @@ -290,6 +309,9 @@ namespace winrt::Microsoft::Windows::AppLifecycle::implementation
// Force the singleton init.
GetCurrent();

const bool processHandleFixEnabled{
WinAppSdk::Containment::IsChangeEnabled<WINAPPSDK_CHANGEID_63876312>() };

IVector<Microsoft::Windows::AppLifecycle::AppInstance> instances{ winrt::single_threaded_vector<Microsoft::Windows::AppLifecycle::AppInstance>() };

// Grab the list of processes while under the lock, and then drop it since we'll be calling out to other code.
Expand Down Expand Up @@ -318,10 +340,18 @@ namespace winrt::Microsoft::Windows::AppLifecycle::implementation
}
else
{
wil::unique_handle process(::OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid));
const DWORD desiredAccess{
processHandleFixEnabled ? static_cast<DWORD>(SYNCHRONIZE) :
static_cast<DWORD>(PROCESS_QUERY_LIMITED_INFORMATION) };
wil::unique_handle process(::OpenProcess(desiredAccess, FALSE, pid));
if (process != nullptr)
{
instances.Append(make<AppInstance>(pid));
wil::unique_handle instanceHandle;
if (processHandleFixEnabled)
{
instanceHandle = std::move(process);
}
instances.Append(make<AppInstance>(pid, std::move(instanceHandle)));
}
else
{
Expand Down
3 changes: 2 additions & 1 deletion dev/AppLifecycle/AppInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ namespace winrt::Microsoft::Windows::AppLifecycle::implementation
struct AppInstance : AppInstanceT<AppInstance>
{
// No interface public methods.
AppInstance(uint32_t processId);
AppInstance();
AppInstance(uint32_t processId, wil::unique_handle processHandle);
~AppInstance()
{
if (m_terminationWatcherWaitHandle)
Expand Down