Skip to content

[net6] monovm_initialize should allow an alternative to properties for initialization #48416

Closed

Description

@grendello

Background and Motivation

monovm_initialize takes a list of properties (specified as two arrays of keys and values) and parses them in order to set certain parameters for VM initialization.
This parsing not only adds an initialization overhead but also makes the initialization awkward and insecure in certain cases. For instance, the PINVOKE_OVERRIDE
property is assumed to be an integer, which is to be converted to a pointer to a callback function. This requires the host application to:

  1. Take the pointer to the function
  2. Convert it to a string
  3. Allocate two string arrays
  4. Place the PINVOKE_OVERRIDE in the keys array and the pointer string in the values array
  5. Call monovm_initialize which then parses the pointer value into unsigned long long, casts it to uintptr_t and then casts it to the expected callback type.

Not only the process is unnecessarily verbose and wasteful, but it is also potentially insecure - should there be any problem with pointer->string->integer->pointer conversion
or should there be a discrepancy between the signature of the callback in the host versus the one expected by the VM, the developer will get NO warning about the problem until
a mysterious crash during application execution.

Other properties, for instance those specifying a list of paths (e.g. TRUSTED_PLATFORM_ASSEMBLIES or APP_PATHS) also require that the host first constructs the array
(with the indicated separators) only for it to be deconstructed (with memory allocation for each path extracted from the list) moments later. This not only makes the process longer,
it also uses more memory than necessary (which is important especially on mobile or embedded devices).

Proposed API

I understand that the above pattern is preferred by the dotnet runtime and that it will most likely remain available, but I suggest that an alternative initialization function is added which
takes a struct with properly typed and declared members that can be filled with all the necessary information for the known properties. The struct could include an optional key:value
array to pass properties that aren't standard and thus aren't included in the structure definition. Sample structure and init function declarations:

struct _MonoVmInitProperty
{
   const char* name;
   const char* value;
};

struct _MonoVmInit
{
   size_t trusted_platform_assemblies_count;
   char** trusted_platform_assemblies; // Or remove the count member above and require this array to be NULL-terminated
   PInvokeOverrideFn pinvoke_override_cb;
   bool system_globalization_invariant;

   size_t other_properties_count;
   struct _MonoVmInitProperty* other_properties;
};

int monovm_initialize_fast (struct _MonoVmInit *init_data);

Activity

  1. added
    api-suggestion
    on Feb 17, 2021
  2. ghost added
    untriaged
    on Feb 17, 2021
  3. added and removed
    api-suggestion
    untriaged
    on Feb 17, 2021
  4. added this to the 6.0.0 milestone on Feb 17, 2021
  5. self-assigned this
    on Feb 17, 2021
  6. rolfbjarne commented on Feb 24, 2021

    @rolfbjarne
    Member

    Another question is if converting a pointer to a string and back to a pointer again is even possible on ARM64 with pointer authentication.

  7. CoffeeFlux commented on May 3, 2021

    @CoffeeFlux
    Contributor

    A couple things to note here:

    Rather than matching the proposed API exactly, it seems like the intent is to have the runtime do as little as possible during startup. This means more parsing work will need to be done on X-A's end. As an example, the format we parse the TPA list into is:

    typedef struct {
    	uint32_t assembly_count;
    	char **basenames; /* Foo.dll */
    	uint32_t *basename_lens;
    	char **assembly_filepaths; /* /blah/blah/blah/Foo.dll */
    } MonoCoreTrustedPlatformAssemblies;

    Similarly, we will need to also get the full array of properties, like what you would pass to monovm_initialize, at the end. Otherwise, we will be forced to convert these better-typed alternatives back into the string representation so that we can pass them up to AppContext. The situation here will hopefully improve over time if we add lazy fetching in AppContext, but for now this will be required.

    Does that sound reasonable? If so I can have a sample PR up soon.

  8. grendello commented on May 5, 2021

    @grendello
    ContributorAuthor

    @CoffeeFlux Can't the array of properties be part of the strongly-typed structure, the way I proposed in OP? It would be NULL if no properties outside those in the struct are used. Regarding your proposal above, it makes perfect sense. One note, however: Xamarin.Android won't set/use any of the path properties (like the TPA list in your sample). This is because assemblies aren't extracted from the APK on Android and there is no filesystem path leading to them. In effect, MonoVM must go through XA and XA mmaps the assembly data from the APK directly, uncompresses it (they are stored in the APK but we compress them, by default, with LZ4) and hands the pointer back to MonoVM. Consequentially, none of the properties below will be set by XA:

    • TRUSTED_PLATFORM_ASSEMBLIES
    • APP_PATHS
    • APP_NI_PATHS
    • PLATFORM_RESOURCE_ROOTS

    We will always use PINVOKE_OVERRIDE and we might set NATIVE_DLL_SEARCH_DIRECTORIES if MonoVM takes advantage of it (but it seems that this property is not useful when PINVOKE_OVERRIDE is used).

  9. CoffeeFlux commented on May 5, 2021

    @CoffeeFlux
    Contributor

    Ah right, I forgot you aren't going to actually use the TPA list. That's somewhat awkward, since we use that list to identify the framework (which has different loader behavior)... I guess I'll PR this to start and we can figure that out next.

  10. ghost added
    in-pr
    on May 5, 2021
  11. grendello commented on May 5, 2021

    @grendello
    ContributorAuthor

    @CoffeeFlux in XA all assemblies are equal, so the only way we could tell framework assemblies apart from the non-framework ones would be by name (at least as things stand now). Alternatively, we could rearrange the layout in the APK and put the framework assemblies in a different folder and base the decision on that (we would have to let MonoVM know which assembly is a framework one somehow, though). Not sure if it's worth it, but if you think it is - we need to make the decision in the nearest future so that there's time to hash all the kinks out and implement that on both MonoVM and XA (possibly XI too?) sides.

  12. ghost removed
    in-pr
    on May 10, 2021
  13. ghost locked as resolved and limited conversation to collaborators on Jun 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions