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
61 changes: 57 additions & 4 deletions dev/MRTCore/mrt/Core/src/MRM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@

#include "MRM.h"

#include <FrameworkUdk/Containment.h>

#include <memory>
#include <string>

// Bug 63048673: Restore MrmGetFilePathFromName always-succeed contract (return S_OK with a
// best-effort path when no PRI file exists) instead of failing with ERROR_FILE_NOT_FOUND.
#define WINAPPSDK_CHANGEID_63048673 63048673

using namespace Microsoft::Resources;

Expand Down Expand Up @@ -969,6 +976,10 @@ STDAPI MrmGetFilePathFromName(_In_opt_ PCWSTR filename, _Outptr_ PWSTR* filePath
{
*filePath = nullptr;

// When disabled via RuntimeCompatibilityOptions, retain the pre-fix behavior of failing with
// ERROR_FILE_NOT_FOUND when no PRI file exists.
const bool fallbackChangeEnabled = WinAppSdk::Containment::IsChangeEnabled<WINAPPSDK_CHANGEID_63048673>();

wil::unique_cotaskmem_string exeDir;
RETURN_IF_FAILED(wil::GetModuleFileNameW(nullptr, exeDir));
PCWSTR exeName = wil::find_last_path_segment(exeDir.get());
Expand All @@ -980,6 +991,11 @@ STDAPI MrmGetFilePathFromName(_In_opt_ PCWSTR filename, _Outptr_ PWSTR* filePath
RETURN_IF_FAILED(SizeTAdd(exeDirCount, 1, &exeDirCount));
RETURN_IF_FAILED(PathCchRemoveFileSpec(exeDir.get(), exeDirCount));

// The ParentPathForFileName pass truncates exeDir in place to reach the parent folder, so
// preserve the original module directory here for the DefaultFallback pass, which must return
// a path under the module directory (the documented behavior).
std::wstring moduleDir(exeDir.get());

enum SearchPass
{
exeDirForFileName,
Expand All @@ -988,6 +1004,7 @@ STDAPI MrmGetFilePathFromName(_In_opt_ PCWSTR filename, _Outptr_ PWSTR* filePath
BaseDirForModulePri,
exeDirForResourcesPri,
exeDirForModulePri,
DefaultFallback,
Final,
};
SearchPass searchStart = SearchPass::exeDirForFileName;
Expand Down Expand Up @@ -1030,7 +1047,10 @@ STDAPI MrmGetFilePathFromName(_In_opt_ PCWSTR filename, _Outptr_ PWSTR* filePath
case SearchPass::ParentPathForFileName:
// move to parent folder of previous search
RETURN_IF_FAILED(PathCchRemoveFileSpec(searchDir, searchDirCount));
pass = SearchPass::Final;
// If the change is enabled and the parent search misses, fall through to
// DefaultFallback so we still return a best-effort path (the documented
// always-succeed contract). The loop's increment lands pass on DefaultFallback.
pass = fallbackChangeEnabled ? SearchPass(SearchPass::DefaultFallback - 1) : SearchPass::Final;
break;

// Search, given no FileName
Expand All @@ -1050,7 +1070,38 @@ STDAPI MrmGetFilePathFromName(_In_opt_ PCWSTR filename, _Outptr_ PWSTR* filePath
break;
case SearchPass::exeDirForModulePri:
searchFilename = modulePriFileName;
pass = SearchPass::Final;
// If the change is enabled, fall through to DefaultFallback (the next pass) on a
// miss so we still return a best-effort path; otherwise stop after this pass.
if (!fallbackChangeEnabled)
{
pass = SearchPass::Final;
}
break;
case SearchPass::DefaultFallback:
// No file was found in any prior pass. Return a best-effort path even though it
// doesn't exist, matching the documented contract: the provided filename (if any)
// or resources.pri, under the base directory (if set) otherwise the module (exe)
// directory. Callers (e.g. ResourceManager) tolerate a non-existent path.
if (filename == nullptr || *filename == L'\0')
{
if (baseDir)
{
searchDir = baseDir.get();
searchDirCount = baseDirCount;
}
else
{
searchDir = moduleDir.data();
searchDirCount = exeDirCount;
}
searchFilename = ResourcesPriFileName;
}
else
{
searchDir = moduleDir.data();
searchDirCount = exeDirCount;
searchFilename = filename;
}
break;
}

Expand All @@ -1074,13 +1125,15 @@ STDAPI MrmGetFilePathFromName(_In_opt_ PCWSTR filename, _Outptr_ PWSTR* filePath
PATHCCH_ALLOW_LONG_PATHS));

DWORD attributes = GetFileAttributes(outputPath.get());
if ((attributes != INVALID_FILE_ATTRIBUTES) && !(attributes & FILE_ATTRIBUTE_DIRECTORY))
if ((pass == SearchPass::DefaultFallback) ||
((attributes != INVALID_FILE_ATTRIBUTES) && !(attributes & FILE_ATTRIBUTE_DIRECTORY)))
{
// The file exists. Done.
// The file exists, or this is the unconditional best-effort fallback pass. Done.
*filePath = outputPath.release();
return S_OK;
}
}

// Only reached when the change is disabled (the DefaultFallback pass is skipped).
return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
}
2 changes: 2 additions & 0 deletions dev/MRTCore/mrt/Core/src/MRM.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
<Import Project="$(NugetPackageDirectory)\Microsoft.FrameworkUdk\$(MicrosoftFrameworkUdkPackageVersion)\build\native\Microsoft.FrameworkUdk.targets" Condition="Exists('$(NugetPackageDirectory)\Microsoft.FrameworkUdk\$(MicrosoftFrameworkUdkPackageVersion)\build\native\Microsoft.FrameworkUdk.targets')" />
<Import Project="$(NugetPackageDirectory)\Microsoft.Windows.ImplementationLibrary.$(MicrosoftWindowsImplementationLibraryVersion)\build\native\Microsoft.Windows.ImplementationLibrary.targets" Condition="Exists('$(NugetPackageDirectory)\Microsoft.Windows.ImplementationLibrary.$(MicrosoftWindowsImplementationLibraryVersion)\build\native\Microsoft.Windows.ImplementationLibrary.targets')" />
<Import Project="$(NugetPackageDirectory)\Microsoft.SourceLink.Common.$(MicrosoftSourceLinkCommonVersion)\build\Microsoft.SourceLink.Common.targets" Condition="Exists('$(NugetPackageDirectory)\Microsoft.SourceLink.Common.$(MicrosoftSourceLinkCommonVersion)\build\Microsoft.SourceLink.Common.targets')" />
<Import Project="$(NugetPackageDirectory)\Microsoft.Build.Tasks.Git.$(MicrosoftBuildTasksGitVersion)\build\Microsoft.Build.Tasks.Git.targets" Condition="Exists('$(NugetPackageDirectory)\Microsoft.Build.Tasks.Git.$(MicrosoftBuildTasksGitVersion)\build\Microsoft.Build.Tasks.Git.targets')" />
Expand All @@ -122,6 +123,7 @@
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('$(NugetPackageDirectory)\Microsoft.FrameworkUdk\$(MicrosoftFrameworkUdkPackageVersion)\build\native\Microsoft.FrameworkUdk.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(NugetPackageDirectory)\Microsoft.FrameworkUdk\$(MicrosoftFrameworkUdkPackageVersion)\build\native\Microsoft.FrameworkUdk.targets'))" />
<Error Condition="!Exists('$(NugetPackageDirectory)\Microsoft.Windows.ImplementationLibrary.$(MicrosoftWindowsImplementationLibraryVersion)\build\native\Microsoft.Windows.ImplementationLibrary.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(NugetPackageDirectory)\Microsoft.Windows.ImplementationLibrary.$(MicrosoftWindowsImplementationLibraryVersion)\build\native\Microsoft.Windows.ImplementationLibrary.targets'))" />
<Error Condition="!Exists('$(NugetPackageDirectory)\Microsoft.SourceLink.Common.$(MicrosoftSourceLinkCommonVersion)\build\Microsoft.SourceLink.Common.props')" Text="$([System.String]::Format('$(ErrorText)', '$(NugetPackageDirectory)\Microsoft.SourceLink.Common.$(MicrosoftSourceLinkCommonVersion)\build\Microsoft.SourceLink.Common.props'))" />
<Error Condition="!Exists('$(NugetPackageDirectory)\Microsoft.SourceLink.Common.$(MicrosoftSourceLinkCommonVersion)\build\Microsoft.SourceLink.Common.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(NugetPackageDirectory)\Microsoft.SourceLink.Common.$(MicrosoftSourceLinkCommonVersion)\build\Microsoft.SourceLink.Common.targets'))" />
Expand Down
57 changes: 56 additions & 1 deletion dev/MRTCore/mrt/Core/unittests/MrmTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
#include <Windows.h>
#include <WexTestClass.h>
#include "..\src\MRM.h"
#include <FrameworkUdk/Containment.h>

// Change ID that gates the MrmGetFilePathFromName always-succeed fallback (see MRM.cpp). Defined
// here (mirroring the inline definition in MRM.cpp) so the disabled-behavior test can name it.
#define WINAPPSDK_CHANGEID_63048673 63048673

using namespace WEX::Common;
using namespace WEX::TestExecution;
Expand Down Expand Up @@ -685,8 +690,20 @@ class BasicTest
MrmFreeResource(path);

VERIFY_ARE_EQUAL(MrmGetFilePathFromName(L"something.pri", &path), S_OK);
// Even if the file doesn't exist, we will still return a path. This is for those don't use PRI for resource.
// Even if the file doesn't exist, we still return a best-effort path so apps without a PRI
// file can still create a ResourceManager. The path is under the module (exe) directory.
VERIFY_IS_NOT_NULL(wcsstr(path, L"something.pri"));
{
wchar_t moduleFilePath[MAX_PATH];
VERIFY_ARE_NOT_EQUAL(0u, GetModuleFileNameW(nullptr, moduleFilePath, ARRAYSIZE(moduleFilePath)));
PWSTR lastSeparator = wcsrchr(moduleFilePath, L'\\');
VERIFY_IS_NOT_NULL(lastSeparator);
lastSeparator[1] = L'\0'; // Trim to "<moduleDir>\"
wchar_t expectedPath[MAX_PATH];
VERIFY_ARE_EQUAL(0, wcscpy_s(expectedPath, ARRAYSIZE(expectedPath), moduleFilePath));
VERIFY_ARE_EQUAL(0, wcscat_s(expectedPath, ARRAYSIZE(expectedPath), L"something.pri"));
VERIFY_ARE_EQUAL(CSTR_EQUAL, CompareStringOrdinal(path, -1, expectedPath, -1, TRUE));
}
MrmFreeResource(path);

VERIFY_ARE_EQUAL(MrmGetFilePathFromName(nullptr, &path), S_OK);
Expand All @@ -713,4 +730,42 @@ class BasicTest

wchar_t previousWorkingDirectory[MAX_PATH];
};

// Verifies the runtime-compatibility containment behavior of the MrmGetFilePathFromName fallback
// (WINAPPSDK_CHANGEID_63048673). When an app disables the change, the API must revert to its
// previous behavior of failing with ERROR_FILE_NOT_FOUND for a non-existent PRI file, instead of
// returning S_OK with a best-effort path.
//
// This runs with IsolationLevel=Method so it executes in its own process: WinAppSdk::Containment::
// IsChangeEnabled<>() caches its result in a process-lifetime function-local static, so testing the
// disabled state must not share a process with BasicTest::GetFilePath (which exercises the enabled
// default state and would otherwise poison, or be poisoned by, the cache).
class ContainmentDisabledTest
{
public:
BEGIN_TEST_CLASS(ContainmentDisabledTest)
TEST_CLASS_PROPERTY(L"IsolationLevel", L"Method")
END_TEST_CLASS()

TEST_METHOD(GetFilePathFallbackDisabled)
{
// Disable the fallback change at the FrameworkUdk containment worker. MrmGetFilePathFromName
// consults it via IsChangeEnabled<WINAPPSDK_CHANGEID_63048673>(). disabledChanges must be
// sorted ascending; a single element is trivially sorted.
const UINT32 disabledChanges[] = { WINAPPSDK_CHANGEID_63048673 };
WinAppSdk::Containment::WinAppSDKRuntimeConfiguration config{};
config.disabledChanges = disabledChanges;
config.disabledChangesCount = ARRAYSIZE(disabledChanges);
VERIFY_SUCCEEDED(WinAppSdk::Containment::SetConfiguration(&config));

// Confirm the worker now reports the change as disabled.
VERIFY_IS_FALSE((WinAppSdk::Containment::IsChangeEnabled<WINAPPSDK_CHANGEID_63048673>()));

// With the change disabled, a non-existent PRI file must fail with ERROR_FILE_NOT_FOUND
// (the pre-fix behavior) rather than returning S_OK with a best-effort path.
wchar_t* path = nullptr;
VERIFY_ARE_EQUAL(HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), MrmGetFilePathFromName(L"something.pri", &path));
VERIFY_IS_NULL(path);
}
};
} // namespace UnitTest
4 changes: 4 additions & 0 deletions dev/MRTCore/mrt/Core/unittests/MrmUnitTest.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@
<ClCompile Include="MrmTests.cpp" />
<CopyFileToFolders Include="$(BaseOutputPath)MRM\mrm.dll" DestinationFolders="$(TargetDir)" TreatOutputAsContent="true" />
<CopyFileToFolders Include="$(BaseOutputPath)MRM\mrm.pdb" DestinationFolders="$(TargetDir)" TreatOutputAsContent="true" />
<!-- mrm.dll now links the FrameworkUdk Containment worker (Microsoft.Internal.FrameworkUdk.dll); deploy it alongside for the test host to load. -->
<CopyFileToFolders Include="$(BaseOutputPath)MRM\Microsoft.Internal.FrameworkUdk.dll" DestinationFolders="$(TargetDir)" TreatOutputAsContent="true" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(MRTCoreRoot)\mrt\Core\src\MRM.vcxproj">
Expand Down Expand Up @@ -204,12 +206,14 @@
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
<Import Project="$(NugetPackageDirectory)\Microsoft.FrameworkUdk\$(MicrosoftFrameworkUdkPackageVersion)\build\native\Microsoft.FrameworkUdk.targets" Condition="Exists('$(NugetPackageDirectory)\Microsoft.FrameworkUdk\$(MicrosoftFrameworkUdkPackageVersion)\build\native\Microsoft.FrameworkUdk.targets')" />
<Import Project="$(NugetPackageDirectory)\Microsoft.Taef.$(MicrosoftTaefVersion)\build\Microsoft.Taef.targets" Condition="Exists('$(NugetPackageDirectory)\Microsoft.Taef.$(MicrosoftTaefVersion)\build\Microsoft.Taef.targets')" />
</ImportGroup>
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('$(NugetPackageDirectory)\Microsoft.FrameworkUdk\$(MicrosoftFrameworkUdkPackageVersion)\build\native\Microsoft.FrameworkUdk.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(NugetPackageDirectory)\Microsoft.FrameworkUdk\$(MicrosoftFrameworkUdkPackageVersion)\build\native\Microsoft.FrameworkUdk.targets'))" />
<Error Condition="!Exists('$(NugetPackageDirectory)\Microsoft.Taef.$(MicrosoftTaefVersion)\build\Microsoft.Taef.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(NugetPackageDirectory)\Microsoft.Taef.$(MicrosoftTaefVersion)\build\Microsoft.Taef.targets'))" />
</Target>
<Target Name="CopyFiles" AfterTargets="AfterBuild">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,23 @@ HRESULT GetDefaultPriFile(winrt::hstring& filePath)
isPackaged = AppModel::Identity::IsPackagedProcess();
hr = GetDefaultPriFileForCurentModule(isPackaged, filePath);

// Sparse-packaged apps have identity but deploy PRI files as loose files;
// fall back to unpackaged discovery which also searches for "[modulename].pri".
if (isPackaged && IsResourceNotFound(hr))
// Sparse-packaged apps have identity but deploy PRI files as loose files; fall back to
// unpackaged discovery which also searches for "[modulename].pri". MrmGetFilePathFromName
// may now return S_OK with a best-effort (possibly non-existent) path, so gate the fallback
// on whether the packaged lookup actually resolved to an existing file rather than solely on
// IsResourceNotFound(hr).
if (isPackaged && (IsResourceNotFound(hr) || (SUCCEEDED(hr) && FAILED(CheckFile(filePath.c_str())))))
{
HRESULT hrFallback = GetDefaultPriFileForCurentModule(false, filePath);
if (SUCCEEDED(hrFallback))
winrt::hstring fallbackPath;
HRESULT hrFallback = GetDefaultPriFileForCurentModule(false, fallbackPath);
// Only adopt the unpackaged result if it resolves to an existing file; otherwise keep
// the packaged best-effort path so ResourceManager still receives a path (and handles
// the not-found case gracefully).
if (SUCCEEDED(hrFallback) && SUCCEEDED(CheckFile(fallbackPath.c_str())))
{
filePath = fallbackPath;
hr = hrFallback;
}
// If the fallback also fails, preserve the original HRESULT (not the fallback's)
// for backward compatibility so callers checking for specific errors (e.g., ERROR_FILE_NOT_FOUND) are not broken.
}

return hr;
Expand Down
Loading