Skip to content

Commit

Permalink
Extract bundled files when IncludeAllContentForSelfExtract is set (#4…
Browse files Browse the repository at this point in the history
…2435)

* Extract bundled files when IncludeAllContentForSelfExtract is set

* Don't pass bundle probe to the runtime for netcoreapp3 compat mode

* Bundle probe should not report extracted files

This also means we can still pass bundle probe to the runtime even in full-extract case as all files will be marked as extracted in that situation (so the probe should return false all the time).
This might be benefitial as the runtime will still "know" it's single-file (we don't really use that knowledge today though).

Improve the test to actually validate locally built binaries and add validation of TPA and framework assembly loading.

* CodeBaseThrows should fail

* Normalize app base path

* Search for CoreLib in TPA

* PR feedback

* Fix bundle tests to actually test single-file bundles

The tests were basically running everything using apphost.exe - the reason the tests worked is basically a weird coincidence (the bundle was always self-contained so it had everything managed in it and all the native dlls were beside the .exe) - so from host's point of view this looked like a self-contained app - and then hostpolicy/hostfxr noticed it's also a bundle and treated it as such.

This change introduces new test baseclass to reduce duplication and standardizes the tests to mostly use self-contained publish then bundled with singlefilehost.exe. Currently they do leave hostpolicy.dll and hostfxr.dll next to the exe (they're written in .deps.json so they must exist on disk) but at runtime they won't be used since the singlefilehost.exe has them bundled in itself.

* Don't use Microsoft.NETCore.App.Internal package

From 3.0+, Microsoft.NETCore.App just chains in the appropriate runtime
packs. The .Internal package was necessary for tests to use when we were
building with a older SDK, but targeting 3.0+. We should be able to get
rid of the workaround now.

This actually causes problems with the full extraction single-file mode
because pre-5.0 and in the .Internal package, System.Private.CoreLib.dll
was not listed as a runtime assembly (only under native). As a result,
it would not end up in the TPA for a single-file bundle.

* AppWithSubDirs: add back RuntimeIdentifier, don't use .Internal package

The test assets are all restored - passing in TestTargetRid - before
the test runs. Since we do a rid-specific publish later, it needs to
be restored with the approriate rid.

Stop using .Internal package. This resulted in host binaries getting
published even for framework-dependent publishes.

* Rework the single-file API tests for better coverage

The tests now validate behavior of all of the interesting APIs in both single-file and backcompat mode.
Added the AppContext.BaseDirectory to the tests.

* Fix the reported path to .deps.json for back compat mode

The path must point to the extraction folder and not use the "fake" in-bundle path.

* Fix a test bug on Linux

Actually we should be using path separator char in the APP_CONTEXT_DEPS_FILES, but that's something we won't be able to change really (too much chance of breaking something without too much value).

Co-authored-by: vitek-karas <[email protected]>
Co-authored-by: Elinor Fung <[email protected]>
  • Loading branch information
3 people authored Sep 30, 2020
1 parent 1c4910f commit 0be66cb
Show file tree
Hide file tree
Showing 29 changed files with 599 additions and 350 deletions.
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) ||
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

0 comments on commit 0be66cb

Please sign in to comment.