Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

[single-exe] Run from bundle on Windows #26904

Merged
merged 2 commits into from
Sep 27, 2019

Commits on Sep 26, 2019

  1. [single-exe] Run from bundle on Windows

    This commit has two main changes:
    * Add a simplified `corebundle` host on Windows, similar to `unixcorebundle`
    * Enable assemblies embedded within single-exe bundles to be loaded directly from the bundle.
    
    The CoreCLR native binaries are not linked into the corebundle host yet, so there need to be located separately on disk beside the single-exe app.
    
    `corebundle` host:
    Add a new CoreBundle host – the prototype host for single-file self-contained apps on Windows
    This code is adapted from coreconsole, with a few changes:
    * This host uses the coreclr_initialize() interface similar to AppHost, unlike other Windows hosts in `coreclr\src\hosts`
    * The path computations are done in UTF8 (similar to unixcorebundle) and not Unicode
    * Calls bundle-handling logic, and sets up appropriate runtime properties.
    
    `app_bundle` abstraction:
    * Added the `app_bundle_t` class to represent the bundle for the currently executing application
    * This class is the bundle-processing module's interface with the outside world, and shares code common to `corebundle` and `unixcorebundle`
    
    Bundle handling code:
    * Move `bundle` directory to `coreclr/src/hosts` so that it can be shared by `corebundle` and `unixcorebundle`
    * Remove dummy tracing code and trace files
    * Add Windows PAL definitions so that bundle handling works on the Windows host
    * Remove some unnecessary PAL definitions
    
    Loading the contents of the bundle:
    * IL assemblies are loaded directly from the bundle using `FlatImageLayout`
    * R2R assemblies are loaded through `ConvertedImageLayout` which involves copying sections to appropriate offsets.
    
    Limitations:
    
    There are a few shortcomings to running single-exe apps on Windows, as compared to Linux.
    The Windows mapping routines have the following limitations:
    * [CreateFileMapping](https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createfilemappinga) has no option to create a mapping for a part of the file (no offset argument).
        This means that:
        * We cannot use the (SEC_IMAGE) attribute to perform automatic section-wise loading (circumventinc alignment requirements) of bundled assemblies directly.
        * Instead we need to map each section independently.
    * [MapViewOfFile]( https://docs.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-mapviewoffileex) can only map parts of a file aligned at [memory allocation granularity]( https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/ns-sysinfoapi-system_info), which is 64KB.
        This means that:
        * Each section within the assemblies should be aligned at 64KB – which is not guaranteed by the crossgen compiler.
            * We therefore map the entire assembly and perform an in-memory copy of the sections to appropriate offsets.
            * This behavior is achieved by using `ConvertedImageLayout`
        * In order to memory-map one embedded assembly at a time, the assemblies in the bundle must be aligned at 64KB boundaries.
            * A prototype bundler with this behavior for testing purposes is available in this branch: https://github.com/swaroop-sridhar/core-setup/tree/single-exe
            * This change is strictly not necessary while copying out individual sections after memory-map (because we can align-down and map)
            * But this change is closer to the final no-copy solution.
    
    In the long term, the solution to the mapping problem would involve considerations such as:
    * Use the new crossgen AOT compiler to compile all assemblies in a version bubble into one PE assembly, with a few aligned sections.
    * Create the big-assembly into the host with proper alignment, so that the single-exe bundle can be loaded without copies at run time.
    swaroop-sridhar committed Sep 26, 2019
    Configuration menu
    Copy the full SHA
    6720c1a View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    c748263 View commit details
    Browse the repository at this point in the history