Skip to content

Commit 8ffce8b

Browse files
committed
Preparing bundle mode for Android
1 parent be539ee commit 8ffce8b

File tree

8 files changed

+104
-16
lines changed

8 files changed

+104
-16
lines changed

src/coreclr/dlls/mscoree/exports.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ static void ConvertConfigPropertiesToUnicode(
169169
*propertyKeysWRef = propertyKeysW;
170170
*propertyValuesWRef = propertyValuesW;
171171
}
172-
#endif
172+
173+
#else // TARGET_ANDROID
173174

174175
static void ConvertConfigPropertiesToUnicode(
175176
const char** propertyKeys,
@@ -229,7 +230,7 @@ static void ConvertConfigPropertiesToUnicode(
229230
*propertyKeysWRef = propertyKeysW;
230231
*propertyValuesWRef = propertyValuesW;
231232
}
232-
233+
#endif // !TARGET_ANDROID
233234
coreclr_error_writer_callback_fn g_errorWriter = nullptr;
234235

235236
//
@@ -289,7 +290,7 @@ int android_coreclr_initialize(
289290
return HOST_E_INVALIDOPERATION;
290291
}
291292

292-
if (hostContract->bundle_probe == nullptr) { [[unlikely]]
293+
if (hostContract->android_bundle_probe == nullptr) { [[unlikely]]
293294
LogErrorToLogcat(ANDROID_LOG_FATAL, "Host contract isn't initialized properly: missing bundle probe handler.");
294295
return HOST_E_INVALIDOPERATION;
295296
}
@@ -317,7 +318,7 @@ int android_coreclr_initialize(
317318
hr = CorHost2::CreateObject(IID_ICLRRuntimeHost4, (void**)&host);
318319
IfFailRet(hr);
319320

320-
static Bundle bundle(appName, hostContract->bundle_probe);
321+
static Bundle bundle(appName, hostContract->android_bundle_probe);
321322
Bundle::AppBundle = &bundle;
322323

323324
// This will take ownership of propertyKeysWTemp and propertyValuesWTemp
@@ -349,6 +350,7 @@ int android_coreclr_initialize(
349350
}
350351
return hr;
351352
}
353+
352354
#endif // TARGET_ANDROID
353355

354356
//
@@ -378,6 +380,7 @@ int coreclr_initialize(
378380
void** hostHandle,
379381
unsigned int* domainId)
380382
{
383+
#if !defined(TARGET_ANDROID)
381384
HRESULT hr;
382385

383386
LPCWSTR* propertyKeysW;
@@ -482,6 +485,9 @@ int coreclr_initialize(
482485
#endif
483486
}
484487
return hr;
488+
#else // TARGET_ANDROID
489+
return HOST_E_INVALIDOPERATION;
490+
#endif
485491
}
486492

487493
//

src/coreclr/hosts/inc/coreclrhost.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,11 @@ CORECLR_HOSTING_API(coreclr_execute_assembly,
169169
//
170170
// Callback types used by the hosts
171171
//
172+
#if defined(TARGET_ANDROID)
173+
using BundleProbeFn = bool(const char* path, void** data_start, int64_t* size);
174+
#else
172175
typedef bool(CORECLR_CALLING_CONVENTION BundleProbeFn)(const char* path, int64_t* offset, int64_t* size, int64_t* compressedSize);
176+
#endif
173177
typedef const void* (CORECLR_CALLING_CONVENTION PInvokeOverrideFn)(const char* libraryName, const char* entrypointName);
174178

175179

src/coreclr/inc/bundle.h

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,37 @@ class Bundle;
1818
struct BundleFileLocation
1919
{
2020
INT64 Size;
21+
#if defined(TARGET_ANDROID)
22+
void* DataStart;
23+
constexpr static INT64 Offset = 0;
24+
constexpr static INT64 UncompresedSize = 0;
25+
#else
2126
INT64 Offset;
2227
INT64 UncompresedSize;
28+
#endif
2329

2430
BundleFileLocation()
25-
{
31+
{
2632
LIMITED_METHOD_CONTRACT;
2733

2834
Size = 0;
29-
Offset = 0;
35+
#if defined(TARGET_ANDROID)
36+
DataStart = INVALID_HANDLE_VALUE;
37+
#else
38+
Offset = 0;
3039
UncompresedSize = 0;
40+
#endif
3141
}
3242

3343
static BundleFileLocation Invalid() { LIMITED_METHOD_CONTRACT; return BundleFileLocation(); }
3444

3545
const SString &Path() const;
36-
46+
#if defined(TARGET_ANDROID)
47+
const SString &AppName() const;
48+
bool IsValid() const { LIMITED_METHOD_CONTRACT; return DataStart != nullptr; }
49+
#else // TARGET_ANDROID
3750
bool IsValid() const { LIMITED_METHOD_CONTRACT; return Offset != 0; }
51+
#endif // !TARGET_ANDROID
3852
};
3953

4054
class Bundle
@@ -51,12 +65,14 @@ class Bundle
5165
static BundleFileLocation ProbeAppBundle(const SString& path, bool pathIsBundleRelative = false);
5266

5367
private:
54-
68+
#if defined(TARGET_ANDROID)
69+
SString m_appName;
70+
#endif
5571
SString m_path; // The path to single-file executable
5672
BundleProbeFn *m_probe;
5773

5874
SString m_basePath; // The prefix to denote a path within the bundle
59-
COUNT_T m_basePathLength;
75+
COUNT_T m_basePathLength = 0;
6076
};
6177

6278
#endif // _BUNDLE_H_

src/coreclr/vm/bundle.cpp

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,28 @@ const SString &BundleFileLocation::Path() const
3030
return Bundle::AppBundle->Path();
3131
}
3232

33+
#if defined(TARGET_ANDROID)
34+
const SString &BundleFileLocation::AppName() const
35+
{
36+
LIMITED_METHOD_CONTRACT;
37+
38+
_ASSERTE(IsValid());
39+
_ASSERTE(Bundle::AppBundle != nullptr);
40+
41+
return Bundle::AppBundle->Path();
42+
}
43+
#endif
44+
3345
Bundle::Bundle(LPCSTR bundlePath, BundleProbeFn *probe)
3446
{
3547
STANDARD_VM_CONTRACT;
3648

3749
_ASSERTE(probe != nullptr);
38-
39-
m_path.SetUTF8(bundlePath);
4050
m_probe = probe;
51+
#if defined(TARGET_ANDROID)
52+
m_appName.SetUTF8(bundlePath);
53+
#else
54+
m_path.SetUTF8(bundlePath);
4155

4256
// The bundle-base path is the directory containing the single-file bundle.
4357
// When the Probe() function searches within the bundle, it masks out the basePath from the assembly-path (if found).
@@ -47,6 +61,7 @@ Bundle::Bundle(LPCSTR bundlePath, BundleProbeFn *probe)
4761
size_t baseLen = pos - bundlePath + 1; // Include DIRECTORY_SEPARATOR_CHAR_A in m_basePath
4862
m_basePath.SetUTF8(bundlePath, (COUNT_T)baseLen);
4963
m_basePathLength = (COUNT_T)baseLen;
64+
#endif // !TARGET_ANDROID
5065
}
5166

5267
BundleFileLocation Bundle::Probe(const SString& path, bool pathIsBundleRelative) const
@@ -64,6 +79,13 @@ BundleFileLocation Bundle::Probe(const SString& path, bool pathIsBundleRelative)
6479
pathBuffer.SetAndConvertToUTF8(path.GetUnicode());
6580
LPCSTR utf8Path(pathBuffer.GetUTF8());
6681

82+
#if defined(TARGET_ANDROID)
83+
// On Android we always strip away the assembly path, if any
84+
LPCSTR pos = strrchr(utf8Path, DIRECTORY_SEPARATOR_CHAR_A);
85+
if (pos != nullptr) {
86+
utf8Path = pos + 1; // one past the last directory separator char
87+
}
88+
#else // TARGET_ANDROID
6789
if (!pathIsBundleRelative)
6890
{
6991
#ifdef TARGET_UNIX
@@ -80,10 +102,13 @@ BundleFileLocation Bundle::Probe(const SString& path, bool pathIsBundleRelative)
80102
return loc;
81103
}
82104
}
83-
105+
#endif // !TARGET_ANDROID
84106
INT64 fileSize = 0;
85107
INT64 compressedSize = 0;
86108

109+
#if defined(TARGET_ANDROID)
110+
m_probe(utf8Path, &loc.DataStart, &loc.Size);
111+
#else
87112
m_probe(utf8Path, &loc.Offset, &fileSize, &compressedSize);
88113

89114
if (compressedSize)
@@ -96,7 +121,7 @@ BundleFileLocation Bundle::Probe(const SString& path, bool pathIsBundleRelative)
96121
loc.Size = fileSize;
97122
loc.UncompresedSize = 0;
98123
}
99-
124+
#endif
100125
return loc;
101126
}
102127

src/coreclr/vm/peimage.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,14 @@ HANDLE PEImage::GetFileHandle()
850850
if (m_hFile == INVALID_HANDLE_VALUE)
851851
{
852852
#if !defined(DACCESS_COMPILE)
853-
EEFileLoadException::Throw(GetPathToLoad(), hr);
853+
EEFileLoadException::Throw(
854+
#if defined(TARGET_ANDROID)
855+
AndroidGetAppName(),
856+
#else
857+
GetPathToLoad(),
858+
#endif
859+
hr
860+
);
854861
#else // defined(DACCESS_COMPILE)
855862
ThrowHR(hr);
856863
#endif // !defined(DACCESS_COMPILE)
@@ -869,6 +876,9 @@ HRESULT PEImage::TryOpenFile(bool takeLock)
869876
return S_OK;
870877

871878
ErrorModeHolder mode{};
879+
#if defined(TARGET_ANDROID)
880+
m_hFile = AndroidGetDataStart ();
881+
#else
872882
m_hFile=WszCreateFile((LPCWSTR)GetPathToLoad(),
873883
GENERIC_READ
874884
#if TARGET_WINDOWS
@@ -881,7 +891,7 @@ HRESULT PEImage::TryOpenFile(bool takeLock)
881891
OPEN_EXISTING,
882892
FILE_ATTRIBUTE_NORMAL,
883893
NULL);
884-
894+
#endif
885895
if (m_hFile != INVALID_HANDLE_VALUE)
886896
return S_OK;
887897

src/coreclr/vm/peimage.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,12 @@ class PEImage final
133133
PTR_PEImageLayout GetFlatLayout();
134134

135135
const SString& GetPath();
136+
#if defined(TARGET_ANDROID)
137+
const HANDLE AndroidGetDataStart();
138+
const SString& AndroidGetAppName();
139+
#else
136140
const SString& GetPathToLoad();
141+
#endif
137142
LPCWSTR GetPathForErrorMessages() { return GetPath(); }
138143

139144
BOOL IsFile();

src/coreclr/vm/peimage.inl

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,25 @@ inline const SString &PEImage::GetPath()
3232
return m_path;
3333
}
3434

35+
#if defined(TARGET_ANDROID)
36+
inline const HANDLE PEImage::AndroidGetDataStart()
37+
{
38+
LIMITED_METHOD_CONTRACT;
39+
return m_bundleFileLocation.DataStart;
40+
}
41+
42+
inline const SString& PEImage::AndroidGetAppName()
43+
{
44+
return m_bundleFileLocation.AppName();
45+
}
46+
#else
3547
inline const SString& PEImage::GetPathToLoad()
3648
{
3749
LIMITED_METHOD_DAC_CONTRACT;
3850

3951
return IsInBundle() ? m_bundleFileLocation.Path() : m_path;
4052
}
53+
#endif // !TARGET_ANDROID
4154

4255
inline INT64 PEImage::GetOffset() const
4356
{
@@ -98,8 +111,11 @@ inline const SString &PEImage::GetModuleFileNameHintForDAC()
98111
inline BOOL PEImage::IsFile()
99112
{
100113
WRAPPER_NO_CONTRACT;
101-
114+
#if defined(TARGET_ANDROID)
115+
return false;
116+
#else
102117
return !GetPathToLoad().IsEmpty();
118+
#endif
103119
}
104120

105121
//

src/native/corehost/host_runtime_contract.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ struct host_runtime_contract
3838
size_t value_buffer_size,
3939
void* contract_context);
4040

41+
#if defined(TARGET_ANDROID)
42+
bool(HOST_CONTRACT_CALLTYPE* android_bundle_probe)(
43+
const char* path,
44+
void **data_start,
45+
int64_t* size);
46+
#endif
4147
// Probe an app bundle for `path`. Sets its location (`offset`, `size`) in the bundle if found.
4248
// Returns true if found, false otherwise.
4349
bool(HOST_CONTRACT_CALLTYPE* bundle_probe)(

0 commit comments

Comments
 (0)