Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
4 changes: 0 additions & 4 deletions src/coreclr/inc/clrconfigvalues.h
Original file line number Diff line number Diff line change
Expand Up @@ -579,11 +579,7 @@ RETAIL_CONFIG_DWORD_INFO(INTERNAL_CreateDumpDiagnostics, W("CreateDumpDiagnostic
/// R2R
///
RETAIL_CONFIG_STRING_INFO(INTERNAL_NativeImageSearchPaths, W("NativeImageSearchPaths"), "Extra search paths for native composite R2R images")
#if defined(TARGET_IOS) || defined(TARGET_TVOS) || defined(TARGET_MACCATALYST)
RETAIL_CONFIG_DWORD_INFO(EXTERNAL_ReadyToRun, W("ReadyToRun"), 0, "Enable/disable use of ReadyToRun native code") // Off by default for Apple mobile
#else
RETAIL_CONFIG_DWORD_INFO(EXTERNAL_ReadyToRun, W("ReadyToRun"), 1, "Enable/disable use of ReadyToRun native code") // On by default for CoreCLR
#endif // defined(TARGET_IOS) || defined(TARGET_TVOS) || defined(TARGET_MACCATALYST)
RETAIL_CONFIG_STRING_INFO(EXTERNAL_ReadyToRunExcludeList, W("ReadyToRunExcludeList"), "List of assemblies that cannot use Ready to Run images")
RETAIL_CONFIG_STRING_INFO(EXTERNAL_ReadyToRunLogFile, W("ReadyToRunLogFile"), "Name of file to log success/failure of using Ready to Run images")

Expand Down
15 changes: 15 additions & 0 deletions src/mono/msbuild/apple/build/AppleBuild.InTree.targets
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,19 @@
</ItemGroup>
<Message Text="Used runtime pack: %(ResolvedRuntimePack.PackageDirectory)" Importance="high" />
</Target>

<!-- Override the ResolveReadyToRunCompilers target to use the in-build crossgen2.
This needs to be imported after SDK targets, so any project requiring this should set AfterMicrosoftNETSdkTargets -->
<Target Name="ResolveReadyToRunCompilers" Condition="'$(PublishReadyToRun)' == true" DependsOnTargets="ResolveRuntimeFilesFromLocalBuild">
<PropertyGroup>
<Crossgen2Path>$([MSBuild]::NormalizePath('$(Crossgen2InBuildDir)', 'crossgen2$(ExeSuffix)'))</Crossgen2Path>
</PropertyGroup>

<ItemGroup>
<Crossgen2Tool Include="$(Crossgen2Path)"
TargetArch="$(TargetArchitecture)"
TargetOS="$(TargetOS)"
PerfmapFormatVersion="$(PublishReadyToRunPerfmapFormatVersion)"/>
</ItemGroup>
</Target>
</Project>
2 changes: 1 addition & 1 deletion src/mono/msbuild/apple/build/AppleBuild.targets
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@
<_MonoLLVMPath Condition="'$(_MonoLLVMPath)' == '' and '$(MonoEnableLLVM)' == 'true'">$([System.IO.Path]::GetDirectoryName("$(_CompilerBinaryPath)"))</_MonoLLVMPath>
</PropertyGroup>

<MonoAOTCompiler Condition="'$(RunAOTCompilation)' == 'true'"
<MonoAOTCompiler Condition="'$(RunAOTCompilation)' == 'true' and '$(UseMonoRuntime)' != 'false'"
AotModulesTablePath="$(_AotModuleTablePath)"
AotModulesTableLanguage="ObjC"
Assemblies="@(_AotInputAssemblies)"
Expand Down
2 changes: 2 additions & 0 deletions src/mono/sample/iOS/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ APP_SANDBOX?=false
STRIP_DEBUG_SYMBOLS?=false # only used when measuring SOD via build-appbundle make target
USE_MONO_RUNTIME?=true
AOT?=true
R2R?=false

#If DIAGNOSTIC_PORTS is enabled, @(RuntimeComponents) must also include 'diagnostics_tracing'.
#If @(RuntimeComponents) includes 'diagnostics_tracing', DIAGNOSTIC_PORTS is optional.
Expand Down Expand Up @@ -44,6 +45,7 @@ build-appbundle: clean appbuilder
/p:DeployAndRun=false \
/p:RunAOTCompilation=$(AOT) \
/p:UseMonoRuntime=$(USE_MONO_RUNTIME) \
/p:PublishReadyToRun=$(R2R) \
/bl

run: clean appbuilder
Expand Down
25 changes: 21 additions & 4 deletions src/mono/sample/iOS/Program.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,32 @@
<BuildAppBundleDependsOnTargets Condition="'$(ArchiveTests)' == 'true'">Publish</BuildAppBundleDependsOnTargets>
</PropertyGroup>

<PropertyGroup Condition="'$(PublishReadyToRun)' == 'true' and '$(UseMonoRuntime)' != 'true'">
<PublishReadyToRunComposite>true</PublishReadyToRunComposite>
<PublishReadyToRunContainerFormat>macho</PublishReadyToRunContainerFormat>
</PropertyGroup>

<ItemGroup>
<RuntimeComponents Condition="'$(DiagnosticPorts)' != ''" Include="diagnostics_tracing" />
<EnvironmentVariables Condition="'$(UseMonoRuntime)' != 'true'" Include="DOTNET_Interpreter=%2A%21%2A" />
<EnvironmentVariables Condition="'$(UseMonoRuntime)' != 'true'" Include="DOTNET_InterpMode=3" />
<EnvironmentVariables Condition="'$(UseMonoRuntime)' != 'true'" Include="DOTNET_ReadyToRun=0" />
</ItemGroup>

<ItemGroup Condition="'$(UseMonoRuntime)' != 'true'">
<EnvironmentVariables Include="DOTNET_Interpreter=%2A%21%2A" />
<EnvironmentVariables Condition="'$(PublishReadyToRun)' == 'true'" Include="DOTNET_InterpMode=1" />
<EnvironmentVariables Condition="'$(PublishReadyToRun)' == 'true'" Include="DOTNET_ReadyToRun=1" />
<EnvironmentVariables Condition="'$(PublishReadyToRun)' != 'true'" Include="DOTNET_InterpMode=3" />
<EnvironmentVariables Condition="'$(PublishReadyToRun)' != 'true'" Include="DOTNET_ReadyToRun=0" />
</ItemGroup>

<!-- Use the repo's CrossGen props/targets.
This can be removed once we upstream Mach-O support in the targets to the SDK -->
<Import Project="$(Crossgen2SdkOverridePropsPath)" Condition="'$(PublishReadyToRun)' == 'true' and '$(Crossgen2SdkOverridePropsPath)' != ''" />
<Import Project="$(MonoProjectRoot)\msbuild\apple\build\AppleBuild.props" />
<Import Project="$(MonoProjectRoot)\msbuild\apple\build\AppleBuild.InTree.targets" />
<PropertyGroup>
<!-- Import after SDK targets in order to override R2R-related targets in the SDK -->
<AfterMicrosoftNETSdkTargets Condition="'$(PublishReadyToRun)' == 'true' and '$(Crossgen2SdkOverrideTargetsPath)' != ''">$(Crossgen2SdkOverrideTargetsPath)</AfterMicrosoftNETSdkTargets>
<AfterMicrosoftNETSdkTargets>$(AfterMicrosoftNETSdkTargets);$(MonoProjectRoot)\msbuild\apple\build\AppleBuild.InTree.targets</AfterMicrosoftNETSdkTargets>
</PropertyGroup>

<Target Name="BuildAppBundle" AfterTargets="$(BuildAppBundleAfterTargets)" DependsOnTargets="$(BuildAppBundleDependsOnTargets)"/>
<Target Name="_SetAppleGenerateAppBundleProps" Condition="'$(TargetOS)' != 'ios' and '$(ArchiveTests)' != 'true'" BeforeTargets="_AppleGenerateAppBundle">
Expand Down
1 change: 1 addition & 0 deletions src/tasks/AppleAppBuilder/AppleAppBuilder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<ItemGroup>
<EmbeddedResource Include="Templates\*.*" />
<EmbeddedResource Include="$(CoreClrProjectRoot)hosts\inc\coreclrhost.h" Link="Templates\coreclrhost.h" />
<EmbeddedResource Include="$(SharedNativeRoot)corehost\host_runtime_contract.h" Link="Templates\host_runtime_contract.h" />
</ItemGroup>
<ItemGroup>
<Compile Include="AppleAppBuilder.cs" />
Expand Down
6 changes: 6 additions & 0 deletions src/tasks/AppleAppBuilder/Templates/CMakeLists.txt.template
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ add_custom_command(
if ls -1 $CODESIGNING_FOLDER_PATH/*.dylib 2>/dev/null | grep -q .\; then
codesign --force --sign \"$SIGN_IDENTITY\" --timestamp $CODESIGNING_FOLDER_PATH/*.dylib\;
fi\;
if ls -1 $CODESIGNING_FOLDER_PATH/Contents/Resources/*.r2r.dylib 2>/dev/null | grep -q .\; then
codesign --force --sign \"$SIGN_IDENTITY\" --timestamp $CODESIGNING_FOLDER_PATH/Contents/Resources/*.r2r.dylib\;
fi\;
if ls -1 $CODESIGNING_FOLDER_PATH/*.r2r.dylib 2>/dev/null | grep -q .\; then
codesign --force --sign \"$SIGN_IDENTITY\" --timestamp $CODESIGNING_FOLDER_PATH/*.r2r.dylib\;
fi\;
fi\;
fi
)
Expand Down
106 changes: 101 additions & 5 deletions src/tasks/AppleAppBuilder/Templates/runtime-coreclr.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#import <Foundation/Foundation.h>
#include "coreclrhost.h"
#include "host_runtime_contract.h"
#include <TargetConditionals.h>
#import <os/log.h>
#include <sys/stat.h>
Expand All @@ -15,6 +16,10 @@

#define APPLE_RUNTIME_IDENTIFIER "//%APPLE_RUNTIME_IDENTIFIER%"

#define MAX_LOADED_NATIVE_CODE_COUNT 64 // Arbitrarily 'large enough' number
static void* loaded_native_code_handles[MAX_LOADED_NATIVE_CODE_COUNT];
static int loaded_native_code_handle_count = 0;

const char *
get_bundle_path (void)
{
Expand Down Expand Up @@ -68,7 +73,7 @@
return strdup([joined UTF8String]);
}

void*
const void*
pinvoke_override (const char *libraryName, const char *entrypointName)
{
if (!strcmp (libraryName, "__Internal"))
Expand All @@ -79,6 +84,83 @@
return NULL;
}

#include <mach-o/dyld.h>
#include <mach-o/loader.h>
size_t get_image_size(void* base_address)
{
uint32_t image_count = _dyld_image_count();
for (uint32_t i = 0; i < image_count; ++i)
{
const struct mach_header_64* header = (const struct mach_header_64*)_dyld_get_image_header(i);
if ((const void*)header != base_address)
continue;

const struct load_command* cmd = (const struct load_command*)((const char*)header + sizeof(struct mach_header_64));

size_t image_size = 0;
for (uint32_t j = 0; j < header->ncmds; ++j)
{
if (cmd->cmd == LC_SEGMENT_64)
{
const struct segment_command_64* seg = (const struct segment_command_64*)cmd;
size_t end_addr = (size_t)(seg->vmaddr + seg->vmsize);
if (end_addr > image_size)
image_size = end_addr;
}

cmd = (const struct load_command*)((const char*)cmd + cmd->cmdsize);
}

return image_size;
}
}

bool get_native_code_data(const struct host_runtime_contract_native_code_context* context, struct host_runtime_contract_native_code_data* data)
{
if (!context || !data || !context->assembly_path || !context->owner_composite_name)
return false;

// Look for the owner composite R2R image in the same directory as the assembly
char r2r_path[PATH_MAX];
const char *last_slash = strrchr(context->assembly_path, '/');
size_t dir_len = last_slash ? (size_t)(last_slash - context->assembly_path) : 0;
if (dir_len >= sizeof(r2r_path) - 1)
return false;

strncpy(r2r_path, context->assembly_path, dir_len);
int written = snprintf(r2r_path + dir_len, sizeof(r2r_path) - dir_len, "/%s", context->owner_composite_name);
if (written <= 0 || (size_t)written >= sizeof(r2r_path) - dir_len)
return false;

assert(loaded_native_code_handle_count < MAX_LOADED_NATIVE_CODE_COUNT);
void* handle = dlopen(r2r_path, RTLD_LAZY | RTLD_LOCAL);
if (handle == NULL)
return false;

loaded_native_code_handles[loaded_native_code_handle_count] = handle;
loaded_native_code_handle_count++;

void* r2r_header = dlsym(handle, "RTR_HEADER");
if (r2r_header == NULL)
{
dlclose(handle);
return false;
}

Dl_info info;
if (dladdr(r2r_header, &info) == 0)
{
dlclose(handle);
return false;
}

data->size = sizeof(struct host_runtime_contract_native_code_data);
data->r2r_header_ptr = r2r_header;
data->image_size = get_image_size(info.dli_fbase);
data->image_base = info.dli_fbase;
return true;
}

void
mono_ios_runtime_init (void)
{
Expand Down Expand Up @@ -116,15 +198,21 @@
#endif
assert (res > 0);

char pinvoke_override_addr [16];
sprintf (pinvoke_override_addr, "%p", &pinvoke_override);
struct host_runtime_contract host_contract = {
.size = sizeof(struct host_runtime_contract),
.pinvoke_override = &pinvoke_override,
.get_native_code_data = &get_native_code_data
};

char contract_str[19]; // 0x + 16 hex digits + '\0'
snprintf(contract_str, 19, "0x%zx", (size_t)(&host_contract));

// TODO: set TRUSTED_PLATFORM_ASSEMBLIES, APP_PATHS and NATIVE_DLL_SEARCH_DIRECTORIES
const char *appctx_keys [] = {
"RUNTIME_IDENTIFIER",
"APP_CONTEXT_BASE_DIRECTORY",
"TRUSTED_PLATFORM_ASSEMBLIES",
"PINVOKE_OVERRIDE",
"HOST_RUNTIME_CONTRACT",
#if !defined(INVARIANT_GLOBALIZATION)
"ICU_DAT_FILE_PATH"
#endif
Expand All @@ -133,7 +221,7 @@
APPLE_RUNTIME_IDENTIFIER,
bundle,
compute_trusted_platform_assemblies(),
pinvoke_override_addr,
contract_str,
#if !defined(INVARIANT_GLOBALIZATION)
icu_dat_path
#endif
Expand All @@ -160,5 +248,13 @@

free_managed_args (&managed_argv, argi);

// Close all loaded handles after the app runs
for (int i = 0; i < loaded_native_code_handle_count; ++i)
{
if (loaded_native_code_handles[i] != NULL) {
dlclose(loaded_native_code_handles[i]);
}
}

exit (res);
}
2 changes: 2 additions & 0 deletions src/tasks/AppleAppBuilder/Xcode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,8 @@ public string GenerateCMake(
{
File.WriteAllText(Path.Combine(binDir, "coreclrhost.h"),
Utils.GetEmbeddedResource("coreclrhost.h"));
File.WriteAllText(Path.Combine(binDir, "host_runtime_contract.h"),
Utils.GetEmbeddedResource("host_runtime_contract.h"));

// NOTE: Library mode is not supported yet
File.WriteAllText(Path.Combine(binDir, "runtime.m"),
Expand Down
Loading