[Tizen] Add coreclr_preload_assembly to CoreCLR host API
authorGleb Balykov <g.balykov@samsung.com>
Fri, 16 Sep 2022 09:21:23 +0000 (12:21 +0300)
committerGleb Balykov <g.balykov@samsung.com>
Tue, 27 Sep 2022 12:50:22 +0000 (15:50 +0300)
commit93273eafd9fcc920369248be1610b4cde0d387d0
tree5003957059349af6ab4109b8231e831e40097fe0
parentb146588f91d1afe7f41069c73e95e7d7e05be31b
[Tizen] Add coreclr_preload_assembly to CoreCLR host API

This API allows to preload ni.dll (both simple and composite)

Restrictions:
- single file bundle can't be preloaded using this API
- images with huge pages (i.e. images with alignment > os page size) can't be preloaded using this API

!This patch should be rebased with caution, see comment for MAPApplyBaseRelocationsPreloadedPEFile!

Co-authored by k.baladurin@samsung.com

----

Simple change to tests coreclr preloading api on corerun (note the extra / for non-SPC dlls, it is needed because TPA list in corerun is created with extra / after dir name, i.e. "/home/user/Dev/runtime2/overlay_x64//System.Runtime.ni.dll" will be in tpa list, and argument of coreclr_preload_assembly must match it exactly). TPA list in dotnet/launcher is created without extra /.

How to test:
0. build runtime in debug
1. generate R2R ni.dll
2. run some app under gdb and stop on coreclr_preload_assembly_func
3. check that coreclr_preload_assembly_func correctly mmaps all files loaded in below patch (both using gdb in code and /proc/<pid>/smaps)
4. stop on PAL_LOADLoadPEFile and check that all ni.dll are found in preloaded cache instead of new load (also check that loaded base address doesn't change both in code and in /proc/<pid>/smaps)

diff --git a/src/coreclr/hosts/corerun/corerun.cpp b/src/coreclr/hosts/corerun/corerun.cpp
index b3f2c867c30..33d446d1a32 100644
--- a/src/coreclr/hosts/corerun/corerun.cpp
+++ b/src/coreclr/hosts/corerun/corerun.cpp
@@ -288,6 +288,17 @@ static int run(const configuration& config)
         return -1;
     }

+    coreclr_preload_assembly_ptr coreclr_preload_assembly_func = nullptr;
+    if (!try_get_export(coreclr_mod, "coreclr_preload_assembly", (void**)&coreclr_preload_assembly_func))
+    {
+        return -1;
+    }
+
+
+    coreclr_preload_assembly_func("/home/user/Dev/runtime2/overlay_x64/System.Private.CoreLib.dll");
+    coreclr_preload_assembly_func("/home/user/Dev/runtime2/overlay_x64//System.Runtime.ni.dll");
+    coreclr_preload_assembly_func("/home/user/Dev/runtime2/overlay_x64//System.IO.ni.dll");
+
     // Construct CoreCLR properties.
     pal::string_utf8_t tpa_list_utf8 = pal::convert_to_utf8(std::move(tpa_list));
     pal::string_utf8_t app_path_utf8 = pal::convert_to_utf8(std::move(app_path));
14 files changed:
src/coreclr/dlls/mscoree/mscorwks_ntdef.src
src/coreclr/dlls/mscoree/mscorwks_unixexports.src
src/coreclr/dlls/mscoree/unixinterface.cpp
src/coreclr/hosts/inc/coreclrhost.h
src/coreclr/inc/corhost.h
src/coreclr/pal/inc/pal.h
src/coreclr/pal/src/include/pal/dbgmsg.h
src/coreclr/pal/src/include/pal/map.hpp
src/coreclr/pal/src/include/pal/module.h
src/coreclr/pal/src/loader/module.cpp
src/coreclr/pal/src/map/map.cpp
src/coreclr/vm/corhost.cpp
src/coreclr/vm/peimagelayout.cpp
src/coreclr/vm/peimagelayout.h