Skip to content

Commit eaaa885

Browse files
committed
test fixes
1 parent be94dfb commit eaaa885

9 files changed

Lines changed: 101 additions & 13 deletions

‎packages.config‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<package id="Microsoft.WSL.DeviceHost" version="1.2.62-0" />
2323
<package id="Microsoft.WSL.Kernel" version="6.18.40.1-1" targetFramework="native" />
2424
<package id="Microsoft.WSL.LinuxSdk" version="1.23.0" targetFramework="native" />
25-
<package id="Microsoft.WSL.OpenVMM" version="0.0.2-6" targetFramework="native" />
25+
<package id="Microsoft.WSL.OpenVMM" version="0.0.2-9" targetFramework="native" />
2626
<package id="Microsoft.WSL.TestData" version="0.6.0" />
2727
<package id="Microsoft.WSL.TestDistro" version="2.7.1-1" />
2828
<package id="Microsoft.WSLg" version="1.0.79" />

‎src/windows/common/HcsVirtualMachineBackend.cpp‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,18 +364,25 @@ HcsVirtualMachineBackend::HcsVirtualMachineBackend() = default;
364364

365365
HcsVirtualMachineBackend::~HcsVirtualMachineBackend() noexcept
366366
{
367+
std::shared_ptr<GuestDeviceManager> guestDeviceManager;
367368
schema::unique_hcs_system system;
368369
std::map<std::uint64_t, AttachedDisk> attachedDisks;
369370
{
370371
auto lock = m_lock.lock_exclusive();
371372
CloseNetworkAdaptersLocked();
373+
guestDeviceManager = std::move(m_guestDeviceManager);
372374
CloseGuestDevicesLocked();
373375
system = std::move(m_system);
374376
attachedDisks = std::move(m_attachedDisks);
375377
CloseGuestListenersLocked(m_configuration.Description.Identity);
376378
}
377379

380+
if (system)
381+
{
382+
LOG_IF_FAILED(wil::ResultFromException([&] { schema::TerminateComputeSystem(system.get()); }));
383+
}
378384
system.reset();
385+
guestDeviceManager.reset();
379386
CleanupAttachedDisks(std::move(attachedDisks));
380387

381388
try
@@ -590,12 +597,14 @@ void HcsVirtualMachineBackend::Terminate()
590597
// AddPersistentMemory releases m_lock while the device host attaches the device and the caller
591598
// waits for it. Serialize termination with that entire lifecycle.
592599
auto persistentMemoryLock = m_persistentMemoryLock.lock_exclusive();
600+
std::shared_ptr<GuestDeviceManager> guestDeviceManager;
593601
schema::unique_hcs_system system;
594602
std::map<std::uint64_t, AttachedDisk> attachedDisks;
595603
{
596604
auto lock = m_lock.lock_exclusive();
597605
THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_INVALID_STATE), !m_system);
598606
CloseNetworkAdaptersLocked();
607+
guestDeviceManager = std::move(m_guestDeviceManager);
599608
CloseGuestDevicesLocked();
600609
system = std::move(m_system);
601610
m_state = VmState::Stopped;
@@ -605,6 +614,7 @@ void HcsVirtualMachineBackend::Terminate()
605614

606615
auto cleanup = wil::scope_exit([&] {
607616
system.reset();
617+
guestDeviceManager.reset();
608618
CleanupAttachedDisks(std::move(attachedDisks));
609619
m_terminatingEvent.SetEvent();
610620
NotifyTerminated(m_configuration.Description.Identity);
@@ -907,8 +917,7 @@ void HcsVirtualMachineBackend::CloseGuestDevicesLocked() noexcept
907917
}
908918
}
909919

910-
// Device hosts must be shut down while the compute system and callback context still exist.
911-
m_guestDeviceManager.reset();
920+
// The caller retains the device manager until HCS has finished with its hosts.
912921
m_fileSystemShares.clear();
913922
m_fileSystemDevices.clear();
914923
m_persistentMemoryDevices.clear();

‎src/windows/common/OpenVmmVirtualMachineBackend.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ void OpenVmmVirtualMachineBackend::Initialize(const VmCreateRequest& Request)
319319
wsl::shared::string::GuidToString<wchar_t>(attachment.GuestInstanceId.value(), wsl::shared::string::GuidToStringFlags::None);
320320
const auto& configuration = std::get<VmUserModeNatNetwork>(attachment.EffectiveConfiguration);
321321
const auto macAddress = wsl::shared::string::FormatMacAddress(configuration.ClientMacAddress(), L'-');
322-
THROW_IF_FAILED(WslOpenVmmConfigSetConsommeNic(config.get(), nicId.c_str(), macAddress.c_str()));
322+
THROW_IF_FAILED(WslOpenVmmConfigSetConsommeNic(config.get(), nicId.c_str(), macAddress.c_str(), L""));
323323
m_networkAdapters.emplace(attachment.Id.Value, NetworkAdapter{attachment, {nicId}});
324324
m_nextDeviceId = attachment.Id.Value + 1;
325325
}

‎src/windows/service/exe/WslCoreVm.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,7 @@ void WslCoreVm::AddPlan9Share(
958958
if (!matchingDevice)
959959
{
960960
VmFileSystemDeviceRequest request{};
961+
request.UserToken = wil::shared_handle{wsl::windows::common::wslutil::DuplicateHandle(UserToken)};
961962
if (m_vmConfig.EnableVirtio9p)
962963
{
963964
request.Transport = VmPlan9VirtioDevice{

‎test/windows/HcsVirtualMachineBackendTests.cpp‎

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,28 @@ class HcsVirtualMachineBackendTests
9696
{
9797
WSL_TEST_CLASS(HcsVirtualMachineBackendTests)
9898

99+
TEST_CLASS_SETUP(TestClassSetup)
100+
{
101+
WSADATA data{};
102+
THROW_IF_WIN32_ERROR(WSAStartup(MAKEWORD(2, 2), &data));
103+
return true;
104+
}
105+
106+
TEST_CLASS_CLEANUP(TestClassCleanup)
107+
{
108+
const auto module = GetModuleHandleW(L"wsldevicehostproxystub.dll");
109+
if (module)
110+
{
111+
const auto canUnload = reinterpret_cast<HRESULT(STDAPICALLTYPE*)()>(GetProcAddress(module, "DllCanUnloadNow"));
112+
VERIFY_IS_NOT_NULL(canUnload);
113+
LogInfo("Device-host proxy unload status before COM cleanup: 0x%08x", canUnload());
114+
}
115+
CoFreeUnusedLibrariesEx(0, 0);
116+
VERIFY_IS_NULL(GetModuleHandleW(L"wsldevicehostproxystub.dll"));
117+
VERIFY_ARE_EQUAL(0, WSACleanup());
118+
return true;
119+
}
120+
99121
TEST_METHOD(FileSystemRequestsDefaultToVirtioFs)
100122
{
101123
const VmFileSystemDeviceRequest device;
@@ -260,7 +282,7 @@ class HcsVirtualMachineBackendTests
260282
CreateVhd(invalidPath);
261283

262284
auto backend = HcsVirtualMachineBackend::Create(CreateRunnableRequest());
263-
backend->Start();
285+
auto guest = StartGuest(*backend);
264286
const auto automatic = backend->AttachDisk(CreateDiskRequest(automaticPath));
265287
const auto exact = backend->AttachDisk(CreateDiskRequest(exactPath, 2));
266288
const auto replacement = backend->AttachDisk(CreateDiskRequest(replacementPath));
@@ -311,7 +333,7 @@ class HcsVirtualMachineBackendTests
311333
CreateVhd(secondPath);
312334

313335
auto firstBackend = HcsVirtualMachineBackend::Create(CreateRunnableRequest());
314-
firstBackend->Start();
336+
auto firstGuest = StartGuest(*firstBackend);
315337
const auto first = firstBackend->AttachDisk(CreateDiskRequest(firstPath, 253));
316338

317339
VERIFY_ARE_EQUAL(UINT32{253}, first.GuestAddress.Lun);
@@ -322,7 +344,7 @@ class HcsVirtualMachineBackendTests
322344

323345
// Disk IDs are local to a VM and can be reused after another VM has terminated.
324346
auto secondBackend = HcsVirtualMachineBackend::Create(CreateRunnableRequest());
325-
secondBackend->Start();
347+
auto secondGuest = StartGuest(*secondBackend);
326348
const auto second = secondBackend->AttachDisk(CreateDiskRequest(secondPath));
327349

328350
VERIFY_ARE_EQUAL(first.Id.Value, second.Id.Value);
@@ -354,7 +376,7 @@ class HcsVirtualMachineBackendTests
354376
{
355377
SKIP_TEST_ARM64();
356378
auto backend = HcsVirtualMachineBackend::Create(CreateRunnableRequest());
357-
backend->Start();
379+
auto guest = StartGuest(*backend);
358380

359381
const auto request = CreateNetworkRequest();
360382
const auto first = backend->AddNetworkAdapter(request);
@@ -372,6 +394,31 @@ class HcsVirtualMachineBackendTests
372394
backend->Terminate();
373395
}
374396

397+
TEST_METHOD(KeepsOtherVmDeviceHostsAlive)
398+
{
399+
SKIP_TEST_ARM64();
400+
const auto directory = CreateTestDirectory();
401+
auto cleanup = wil::scope_exit_log(WI_DIAGNOSTICS_INFO, [&] { std::filesystem::remove_all(directory); });
402+
403+
auto first = HcsVirtualMachineBackend::Create(CreateRunnableRequest());
404+
auto firstGuest = StartGuest(*first);
405+
first->CreateFileSystemDevice({VmVirtioFsDevice{L"first", VmVirtioFsLayout::Aggregate}});
406+
407+
auto second = HcsVirtualMachineBackend::Create(CreateRunnableRequest());
408+
auto secondGuest = StartGuest(*second);
409+
const auto device = second->CreateFileSystemDevice({VmVirtioFsDevice{L"second", VmVirtioFsLayout::Aggregate}});
410+
411+
first.reset();
412+
413+
VmFileSystemShareRequest request;
414+
request.HostPath = directory;
415+
request.Options = VmVirtioFsShareOptions{};
416+
const auto share = second->AddFileSystemShare(device.Id, request);
417+
VERIFY_ARE_EQUAL(device.Id.Value, share.Device.Value);
418+
second->RemoveFileSystemShare(share.Id);
419+
second->Terminate();
420+
}
421+
375422
TEST_METHOD(NotifiesTerminationCallbacksWithoutHoldingBackendLock)
376423
{
377424
SKIP_TEST_ARM64();
@@ -447,7 +494,7 @@ class HcsVirtualMachineBackendTests
447494
auto cleanup = wil::scope_exit_log(WI_DIAGNOSTICS_INFO, [&] { std::filesystem::remove_all(directory); });
448495

449496
auto backend = HcsVirtualMachineBackend::Create(CreateRunnableRequest());
450-
backend->Start();
497+
auto guest = StartGuest(*backend);
451498

452499
// Elevated and unelevated callers share one VM, so each elevation level gets its own device
453500
// just as WslCoreVm::AddDrvFsShare uses a separate Plan 9 port and virtio-fs tag for each.

‎test/windows/InstallerTests.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ class InstallerTests
134134
{
135135
// Note: canonical is required because msiexec seems to be unable to deal with symlinks.
136136
static int counter = 0;
137-
return std::format(L"{}\\msi-install-{}.txt", std::filesystem::canonical(g_dumpFolder).c_str(), counter++);
137+
return std::format(L"{}\\msi-install-{}-{}.txt", std::filesystem::canonical(g_dumpFolder).c_str(), GetCurrentProcessId(), counter++);
138138
}
139139

140140
bool IsInstallerMsixInstalled() const

‎test/windows/OpenVmmVirtualMachineBackendTests.cpp‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@ class OpenVmmVirtualMachineBackendTests
6666
{
6767
WSL_TEST_CLASS(OpenVmmVirtualMachineBackendTests)
6868

69+
TEST_CLASS_SETUP(TestClassSetup)
70+
{
71+
WSADATA data{};
72+
THROW_IF_WIN32_ERROR(WSAStartup(MAKEWORD(2, 2), &data));
73+
return true;
74+
}
75+
76+
TEST_CLASS_CLEANUP(TestClassCleanup)
77+
{
78+
VERIFY_ARE_EQUAL(0, WSACleanup());
79+
return true;
80+
}
81+
6982
TEST_METHOD(PreservesCallerIdentityAndBootInputs)
7083
{
7184
SKIP_TEST_ARM64();
@@ -278,7 +291,7 @@ class OpenVmmVirtualMachineBackendTests
278291
VERIFY_ARE_EQUAL(c_notSupported, OperationResult([&] { backend->AddNetworkAdapter(networkRequest); }));
279292

280293
// Consomme processes port requests once the guest has initialized its network queues.
281-
backend->Start();
294+
auto guest = StartGuest(*backend);
282295
VmPortBindingRequest bindingRequest;
283296
bindingRequest.ListenAddress.family = IpAddressFamily_V4;
284297
const auto loopbackAddress = htonl(INADDR_LOOPBACK);

‎test/windows/UnitTests.cpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,10 @@ class UnitTests
841841
const auto wslSupport =
842842
wil::CoCreateInstance<LxssUserSession, IWslSupport>(CLSCTX_LOCAL_SERVER | CLSCTX_ENABLE_CLOAKING | CLSCTX_ENABLE_AAA);
843843

844+
// REVIEW: The test process initializes COM with static cloaking, so configure this raw proxy for dynamic
845+
// cloaking to let the service impersonate the caller instead of failing with ERROR_BAD_IMPERSONATION_LEVEL.
846+
wsl::windows::common::security::ConfigureForCOMImpersonation(wslSupport.get());
847+
844848
ULONG Version;
845849
ULONG DefaultUid;
846850
wil::unique_cotaskmem_array_ptr<wil::unique_cotaskmem_ansistring> DefaultEnvironment{};

‎test/windows/VirtualMachineBackendTestHelpers.h‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "Common.h"
66
#include "IVirtualMachineBackend.h"
7+
#include "SocketChannel.h"
78

89
namespace VirtualMachineBackendTestHelpers {
910

@@ -28,10 +29,23 @@ inline VmCreateRequest CreateRunnableRequest()
2829
const auto basePath = wsl::windows::common::wslutil::GetBasePath();
2930
request.Boot.KernelPath = basePath / L"kernel";
3031
request.Boot.InitrdPath = basePath / LXSS_VM_MODE_INITRD_NAME;
31-
request.Boot.KernelCommandLine = L"panic=-1";
32+
request.Boot.KernelCommandLine = TEXT(WSL_ROOT_INIT_ENV) L"=1 panic=-1";
3233
return request;
3334
}
3435

36+
inline std::pair<wsl::shared::SocketChannel, wil::unique_socket> StartGuest(IVirtualMachineBackend& Backend)
37+
{
38+
const auto listener = Backend.CreateGuestListener({LX_INIT_UTILITY_VM_INIT_PORT});
39+
auto closeListener = wil::scope_exit([&] { Backend.CloseGuestListener(listener.Id); });
40+
Backend.Start();
41+
42+
// Keep both channels open so mini_init can wait for configuration instead of exiting.
43+
wsl::shared::SocketChannel channel{listener.Accept(30 * 1000), "BackendTest"};
44+
channel.ReceiveMessage<LX_INIT_GUEST_CAPABILITIES>(nullptr, 30 * 1000);
45+
auto notifications = listener.Accept(30 * 1000);
46+
return {std::move(channel), std::move(notifications)};
47+
}
48+
3549
template <typename Callback>
3650
HRESULT OperationResult(Callback&& Operation)
3751
{
@@ -42,7 +56,7 @@ inline void VerifyBootsAndTerminates(std::unique_ptr<IVirtualMachineBackend> Bac
4256
{
4357
auto terminationEvent = Backend->GetTerminationEvent();
4458
VERIFY_ARE_EQUAL(VmState::Created, Backend->GetState());
45-
Backend->Start();
59+
auto guest = StartGuest(*Backend);
4660
VERIFY_ARE_EQUAL(VmState::Running, Backend->GetState());
4761

4862
const auto runningResult = WaitForSingleObject(terminationEvent.get(), 100);

0 commit comments

Comments
 (0)