Repository navigation
Description
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:
- Take the pointer to the function
- Convert it to a string
- Allocate two string arrays
- Place the
PINVOKE_OVERRIDEin the keys array and the pointer string in the values array - Call
monovm_initializewhich then parses the pointer value intounsigned long long, casts it touintptr_tand 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
rolfbjarne commentedon Feb 24, 2021
Another question is if converting a pointer to a string and back to a pointer again is even possible on ARM64 with pointer authentication.
[monodroid] Build the Xamarin.Android runtime for net6 (#5665)
CoffeeFlux commentedon May 3, 2021
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:
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.
grendello commentedon May 5, 2021
@CoffeeFlux Can't the array of properties be part of the strongly-typed structure, the way I proposed in OP? It would be
NULLif 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 theTPAlist 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_ASSEMBLIESAPP_PATHSAPP_NI_PATHSPLATFORM_RESOURCE_ROOTSWe will always use
PINVOKE_OVERRIDEand we might setNATIVE_DLL_SEARCH_DIRECTORIESif MonoVM takes advantage of it (but it seems that this property is not useful whenPINVOKE_OVERRIDEis used).CoffeeFlux commentedon May 5, 2021
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.
grendello commentedon May 5, 2021
@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.
[monodroid] MonoVM initialization without pointer parsing (#5935)