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

Extract bundled files when IncludeAllContentForSelfExtract is set #42435

Merged
merged 19 commits into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from 17 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
123 changes: 3 additions & 120 deletions src/coreclr/src/binder/applicationcontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,74 +127,6 @@ namespace BINDER_SPACE
return hr;
}

HRESULT GetNextPath(SString& paths, SString::Iterator& startPos, SString& outPath)
{
HRESULT hr = S_OK;

bool wrappedWithQuotes = false;

// Skip any leading spaces or path separators
while (paths.Skip(startPos, W(' ')) || paths.Skip(startPos, PATH_SEPARATOR_CHAR_W)) {}

if (startPos == paths.End())
{
// No more paths in the string and we just skipped over some white space
outPath.Set(W(""));
return S_FALSE;
}

// Support paths being wrapped with quotations
if (paths.Skip(startPos, W('\"')))
{
wrappedWithQuotes = true;
}

SString::Iterator iEnd = startPos; // Where current path ends
SString::Iterator iNext; // Where next path starts
if (wrappedWithQuotes)
{
if (paths.Find(iEnd, W('\"')))
{
iNext = iEnd;
// Find where the next path starts - there should be a path separator right after the closing quotation mark
if (paths.Find(iNext, PATH_SEPARATOR_CHAR_W))
{
iNext++;
}
else
{
iNext = paths.End();
}
}
else
{
// There was no terminating quotation mark - that's bad
GO_WITH_HRESULT(E_INVALIDARG);
}
}
else if (paths.Find(iEnd, PATH_SEPARATOR_CHAR_W))
{
iNext = iEnd + 1;
}
else
{
iNext = iEnd = paths.End();
}

// Skip any trailing spaces
while (iEnd[-1] == W(' '))
{
iEnd--;
}

_ASSERTE(startPos < iEnd);

outPath.Set(paths, startPos, iEnd);
startPos = iNext;
Exit:
return hr;
}

HRESULT ApplicationContext::SetupBindingPaths(SString &sTrustedPlatformAssemblies,
SString &sPlatformResourceRoots,
SString &sAppPaths,
Expand All @@ -221,64 +153,15 @@ namespace BINDER_SPACE
for (SString::Iterator i = sTrustedPlatformAssemblies.Begin(); i != sTrustedPlatformAssemblies.End(); )
{
SString fileName;
SString simpleName;
bool isNativeImage = false;
HRESULT pathResult = S_OK;
IF_FAIL_GO(pathResult = GetNextPath(sTrustedPlatformAssemblies, i, fileName));
IF_FAIL_GO(pathResult = GetNextTPAPath(sTrustedPlatformAssemblies, i, /*dllOnly*/ false, fileName, simpleName, isNativeImage));
if (pathResult == S_FALSE)
{
break;
}

#ifndef CROSSGEN_COMPILE
if (Path::IsRelative(fileName))
{
GO_WITH_HRESULT(E_INVALIDARG);
}
#endif

// Find the beginning of the simple name
SString::Iterator iSimpleNameStart = fileName.End();

if (!fileName.FindBack(iSimpleNameStart, DIRECTORY_SEPARATOR_CHAR_W))
{
iSimpleNameStart = fileName.Begin();
}
else
{
// Advance past the directory separator to the first character of the file name
iSimpleNameStart++;
}

if (iSimpleNameStart == fileName.End())
{
GO_WITH_HRESULT(E_INVALIDARG);
}

SString simpleName;
bool isNativeImage = false;

// GCC complains if we create SStrings inline as part of a function call
SString sNiDll(W(".ni.dll"));
SString sNiExe(W(".ni.exe"));
SString sDll(W(".dll"));
SString sExe(W(".exe"));

if (fileName.EndsWithCaseInsensitive(sNiDll) ||
fileName.EndsWithCaseInsensitive(sNiExe))
{
simpleName.Set(fileName, iSimpleNameStart, fileName.End() - 7);
isNativeImage = true;
}
else if (fileName.EndsWithCaseInsensitive(sDll) ||
fileName.EndsWithCaseInsensitive(sExe))
{
simpleName.Set(fileName, iSimpleNameStart, fileName.End() - 4);
}
else
{
// Invalid filename
GO_WITH_HRESULT(E_INVALIDARG);
}

const SimpleNameToFileNameMapEntry *pExistingEntry = m_pTrustedPlatformAssemblyMap->LookupPtr(simpleName.GetUnicode());

if (pExistingEntry != nullptr)
Expand Down
58 changes: 52 additions & 6 deletions src/coreclr/src/binder/assemblybinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "utils.hpp"
#include "variables.hpp"
#include "stringarraylist.h"
#include "configuration.h"

#define APP_DOMAIN_LOCKED_UNLOCKED 0x02
#define APP_DOMAIN_LOCKED_CONTEXT 0x04
Expand Down Expand Up @@ -401,14 +402,59 @@ namespace BINDER_SPACE
sCoreLib.Set(systemDirectory);
CombinePath(sCoreLib, sCoreLibName, sCoreLib);

IF_FAIL_GO(AssemblyBinder::GetAssembly(sCoreLib,
TRUE /* fIsInGAC */,
fBindToNativeImage,
&pSystemAssembly,
NULL /* szMDAssemblyPath */,
bundleFileLocation));
hr = AssemblyBinder::GetAssembly(sCoreLib,
TRUE /* fIsInGAC */,
fBindToNativeImage,
&pSystemAssembly,
NULL /* szMDAssemblyPath */,
bundleFileLocation);
BinderTracing::PathProbed(sCoreLib, pathSource, hr);

if (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
{
// Try to find corelib in the TPA
StackSString sCoreLibSimpleName(CoreLibName_W);
StackSString sTrustedPlatformAssemblies = Configuration::GetKnobStringValue(W("TRUSTED_PLATFORM_ASSEMBLIES"));
sTrustedPlatformAssemblies.Normalize();

bool found = false;
for (SString::Iterator i = sTrustedPlatformAssemblies.Begin(); i != sTrustedPlatformAssemblies.End(); )
{
SString fileName;
SString simpleName;
bool isNativeImage = false;
HRESULT pathResult = S_OK;
IF_FAIL_GO(pathResult = GetNextTPAPath(sTrustedPlatformAssemblies, i, /*dllOnly*/ true, fileName, simpleName, isNativeImage));
if (pathResult == S_FALSE)
{
break;
}

if (simpleName.EqualsCaseInsensitive(sCoreLibSimpleName))
{
sCoreLib = fileName;
found = true;
break;
}
}

if (!found)
{
GO_WITH_HRESULT(HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND));
}

hr = AssemblyBinder::GetAssembly(sCoreLib,
TRUE /* fIsInGAC */,
fBindToNativeImage,
&pSystemAssembly,
NULL /* szMDAssemblyPath */,
bundleFileLocation);

BinderTracing::PathProbed(sCoreLib, BinderTracing::PathSource::ApplicationAssemblies, hr);
}

IF_FAIL_GO(hr);

*ppSystemAssembly = pSystemAssembly.Extract();

Exit:
Expand Down
3 changes: 3 additions & 0 deletions src/coreclr/src/binder/inc/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ namespace BINDER_SPACE
SBuffer &publicKeyTokenBLOB);

BOOL IsFileNotFound(HRESULT hr);

HRESULT GetNextPath(SString& paths, SString::Iterator& startPos, SString& outPath);
HRESULT GetNextTPAPath(SString& paths, SString::Iterator& startPos, bool dllOnly, SString& outPath, SString& simpleName, bool& isNativeImage);
};

#endif
137 changes: 137 additions & 0 deletions src/coreclr/src/binder/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

#include "strongnameinternal.h"
#include "corpriv.h"
#include "clr/fs/path.h"
using namespace clr::fs;

namespace BINDER_SPACE
{
Expand Down Expand Up @@ -115,4 +117,139 @@ namespace BINDER_SPACE
{
return RuntimeFileNotFound(hr);
}

HRESULT GetNextPath(SString& paths, SString::Iterator& startPos, SString& outPath)
{
HRESULT hr = S_OK;

bool wrappedWithQuotes = false;

// Skip any leading spaces or path separators
while (paths.Skip(startPos, W(' ')) || paths.Skip(startPos, PATH_SEPARATOR_CHAR_W)) {}

if (startPos == paths.End())
{
// No more paths in the string and we just skipped over some white space
outPath.Set(W(""));
return S_FALSE;
}

// Support paths being wrapped with quotations
if (paths.Skip(startPos, W('\"')))
{
wrappedWithQuotes = true;
}

SString::Iterator iEnd = startPos; // Where current path ends
SString::Iterator iNext; // Where next path starts
if (wrappedWithQuotes)
{
if (paths.Find(iEnd, W('\"')))
{
iNext = iEnd;
// Find where the next path starts - there should be a path separator right after the closing quotation mark
if (paths.Find(iNext, PATH_SEPARATOR_CHAR_W))
{
iNext++;
}
else
{
iNext = paths.End();
}
}
else
{
// There was no terminating quotation mark - that's bad
GO_WITH_HRESULT(E_INVALIDARG);
}
}
else if (paths.Find(iEnd, PATH_SEPARATOR_CHAR_W))
{
iNext = iEnd + 1;
}
else
{
iNext = iEnd = paths.End();
}

// Skip any trailing spaces
while (iEnd[-1] == W(' '))
{
iEnd--;
}

_ASSERTE(startPos < iEnd);

outPath.Set(paths, startPos, iEnd);
startPos = iNext;
Exit:
return hr;
}

HRESULT GetNextTPAPath(SString& paths, SString::Iterator& startPos, bool dllOnly, SString& outPath, SString& simpleName, bool& isNativeImage)
{
HRESULT hr = S_OK;
isNativeImage = false;

HRESULT pathResult = S_OK;
IF_FAIL_GO(pathResult = GetNextPath(paths, startPos, outPath));
if (pathResult == S_FALSE)
{
return S_FALSE;
}

#ifndef CROSSGEN_COMPILE
if (Path::IsRelative(outPath))
{
GO_WITH_HRESULT(E_INVALIDARG);
}
#endif

{
// Find the beginning of the simple name
SString::Iterator iSimpleNameStart = outPath.End();

if (!outPath.FindBack(iSimpleNameStart, DIRECTORY_SEPARATOR_CHAR_W))
{
iSimpleNameStart = outPath.Begin();
}
else
{
// Advance past the directory separator to the first character of the file name
iSimpleNameStart++;
}

if (iSimpleNameStart == outPath.End())
{
GO_WITH_HRESULT(E_INVALIDARG);
}

// GCC complains if we create SStrings inline as part of a function call
SString sNiDll(W(".ni.dll"));
SString sNiExe(W(".ni.exe"));
SString sDll(W(".dll"));
SString sExe(W(".exe"));

if (!dllOnly && (outPath.EndsWithCaseInsensitive(sNiDll) ||
Copy link
Member

Choose a reason for hiding this comment

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

Why do we exclude the .ni.dll here? I wonder if that's correct.

Copy link
Member

Choose a reason for hiding this comment

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

The dllOnly is currently only set to true when searching for System.Private.CoreLib (in the added fallback logic), so I believe the handling of it was more targeted to avoiding extra work in that scenario of just looking for .dll and not .ni.dll. For longer term / 6.0, I expect it will be refactored away per #42435 (comment).

outPath.EndsWithCaseInsensitive(sNiExe)))
{
simpleName.Set(outPath, iSimpleNameStart, outPath.End() - 7);
isNativeImage = true;
}
else if (outPath.EndsWithCaseInsensitive(sDll) ||
(!dllOnly && outPath.EndsWithCaseInsensitive(sExe)))
{
simpleName.Set(outPath, iSimpleNameStart, outPath.End() - 4);
}
else
{
// Invalid filename
GO_WITH_HRESULT(E_INVALIDARG);
}
}

Exit:
return hr;
}

};
7 changes: 5 additions & 2 deletions src/installer/corehost/cli/bundle/file_entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ bool file_entry_t::is_valid() const
static_cast<file_type_t>(m_type) < file_type_t::__last;
}

file_entry_t file_entry_t::read(reader_t &reader)
file_entry_t file_entry_t::read(reader_t &reader, bool force_extraction)
{
// First read the fixed-sized portion of file-entry
const file_entry_fixed_t* fixed_data = reinterpret_cast<const file_entry_fixed_t*>(reader.read_direct(sizeof(file_entry_fixed_t)));
file_entry_t entry(fixed_data);
file_entry_t entry(fixed_data, force_extraction);

if (!entry.is_valid())
{
Expand All @@ -35,6 +35,9 @@ file_entry_t file_entry_t::read(reader_t &reader)

bool file_entry_t::needs_extraction() const
{
if (m_force_extraction)
return true;

switch (m_type)
{
case file_type_t::deps_json:
Expand Down
Loading