Skip to content

Commit

Permalink
Merge branch 'main' into ChangingLanguageSet
Browse files Browse the repository at this point in the history
  • Loading branch information
mandel-macaque committed May 12, 2021
2 parents a07a3d0 + 6eb60b9 commit d53c057
Show file tree
Hide file tree
Showing 27 changed files with 621 additions and 249 deletions.
11 changes: 11 additions & 0 deletions runtime/coreclr-bridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,17 @@
return rv;
}

MonoType *
mono_reflection_type_get_type (MonoReflectionType *reftype)
{
// MonoType and MonoReflectionType are identical in CoreCLR (both are actually MonoObjects).
// However, we're returning a retained object, so we need to retain here.
MonoType *rv = reftype;
xamarin_mono_object_retain (rv);
LOG_CORECLR (stderr, "%s (%p) => %p\n", __func__, reftype, rv);
return rv;
}

int
mono_jit_exec (MonoDomain * domain, MonoAssembly * assembly, int argc, const char** argv)
{
Expand Down
4 changes: 3 additions & 1 deletion runtime/exports.t4
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,9 @@

new Export ("MonoType *", "mono_reflection_type_get_type",
"MonoReflectionType *", "reftype"
),
) {
HasCoreCLRBridgeFunction = true,
},
#endregion

#region metadata/metadata.h
Expand Down
7 changes: 5 additions & 2 deletions runtime/runtime.m
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,11 @@ void xamarin_framework_peer_unlock ()

MonoReflectionType *nullable_type = (MonoReflectionType *) xamarin_gchandle_unwrap (nullable_type_handle);

if (element_type != NULL && nullable_type != NULL)
*element_type = mono_class_from_mono_type (mono_reflection_type_get_type (nullable_type));
if (element_type != NULL && nullable_type != NULL) {
MonoType *mono_type = mono_reflection_type_get_type (nullable_type);
*element_type = mono_class_from_mono_type (mono_type);
xamarin_mono_object_release (&mono_type);
}

bool is_nullable = nullable_type != NULL;
xamarin_mono_object_release (&nullable_type);
Expand Down
4 changes: 3 additions & 1 deletion runtime/trampolines-invoke.m
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,9 @@
if (desc->bindas [i + 1].original_type_handle != INVALID_GCHANDLE) {
MonoReflectionType *original_type = (MonoReflectionType *) xamarin_gchandle_get_target (desc->bindas [i + 1].original_type_handle);
ADD_TO_MONOOBJECT_RELEASE_LIST (original_type);
arg_ptrs [i + mofs] = xamarin_generate_conversion_to_managed ((id) arg, mono_reflection_type_get_type (original_type), p, method, &exception_gchandle, (void *) INVALID_TOKEN_REF, (void **) &free_list, (void **) &release_list);
MonoType *original_mono_type = mono_reflection_type_get_type (original_type);
ADD_TO_MONOOBJECT_RELEASE_LIST (original_mono_type);
arg_ptrs [i + mofs] = xamarin_generate_conversion_to_managed ((id) arg, original_mono_type, p, method, &exception_gchandle, (void *) INVALID_TOKEN_REF, (void **) &free_list, (void **) &release_list);
if (exception_gchandle != INVALID_GCHANDLE)
goto exception_handling;
ofs++;
Expand Down
23 changes: 22 additions & 1 deletion src/Foundation/NSObject2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,27 @@ internal Flags FlagsInternal {
[DllImport ("__Internal")]
static extern void xamarin_release_managed_ref (IntPtr handle, bool user_type);

#if NET
static void RegisterToggleRefMonoVM (NSObject obj, IntPtr handle, bool isCustomType)
{
// We need this indirection for CoreCLR, otherwise JITting RegisterToggleReference will throw System.Security.SecurityException: ECall methods must be packaged into a system module.
RegisterToggleRef (obj, handle, isCustomType);
}
#endif

static void RegisterToggleReference (NSObject obj, IntPtr handle, bool isCustomType)
{
#if NET
if (Runtime.IsCoreCLR) {
Runtime.RegisterToggleReferenceCoreCLR (obj, handle, isCustomType);
} else {
RegisterToggleRefMonoVM (obj, handle, isCustomType);
}
#else
RegisterToggleRef (obj, handle, isCustomType);
#endif
}

#if !XAMCORE_3_0
public static bool IsNewRefcountEnabled ()
{
Expand All @@ -245,7 +266,7 @@ internal void MarkDirty (bool allowCustomTypes)
return;

IsRegisteredToggleRef = true;
RegisterToggleRef (this, Handle, allowCustomTypes);
RegisterToggleReference (this, Handle, allowCustomTypes);
}

private void InitializeObject (bool alloced) {
Expand Down
6 changes: 6 additions & 0 deletions src/ObjCRuntime/Runtime.CoreCLR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ static void log_coreclr (string message)
NSLog (message);
}

internal static void RegisterToggleReferenceCoreCLR (NSObject obj, IntPtr handle, bool isCustomType)
{
// This requires https://github.com/dotnet/runtime/pull/52146 to be merged and packages available.
Console.WriteLine ("Not implemented: RegisterToggleReferenceCoreCLR");
}

// Returns a retained MonoObject. Caller must release.
static IntPtr FindAssembly (IntPtr assembly_name)
{
Expand Down
7 changes: 7 additions & 0 deletions src/ObjCRuntime/Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,13 @@ internal bool IsCoreCLR {

internal static unsafe InitializationOptions* options;

#if NET
[BindingImpl (BindingImplOptions.Optimizable)]
internal unsafe static bool IsCoreCLR {
get { return options->IsCoreCLR; }
}
#endif

[BindingImpl (BindingImplOptions.Optimizable)]
public static bool DynamicRegistrationSupported {
get {
Expand Down
10 changes: 10 additions & 0 deletions tests/common/TestRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,16 @@ public static void AssertDevice ()
#endif
}

public static void AssertNotVirtualMachine ()
{
#if MONOMAC
// enviroment variable set by the CI when running on a VM
var vmVendor = Environment.GetEnvironmentVariable ("VM_VENDOR");
if (!string.IsNullOrEmpty (vmVendor))
NUnit.Framework.Assert.Ignore ($"This test only runs on device. Found vm vendor: {vmVendor}");
#endif
}

// This function checks if the current Xcode version is exactly (neither higher nor lower) the requested one.
public static bool CheckExactXcodeVersion (int major, int minor, int beta = 0)
{
Expand Down
3 changes: 3 additions & 0 deletions tests/linker/ios/link all/PreserveTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ public void PreserveTypeWithoutMembers ()
}

[Test]
#if NET
[Ignore ("This feature is not supported by dotnet's ILLink -> https://github.com/xamarin/xamarin-macios/issues/8900")]
#endif
public void PreserveTypeWithCustomAttribute ()
{
var t = Type.GetType ("LinkAll.Attributes.MemberWithCustomAttribute" + WorkAroundLinkerHeuristics);
Expand Down
4 changes: 2 additions & 2 deletions tools/devops/automation/build-pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ stages:
- template: templates/build/stage.yml
parameters:
vsdropsPrefix: ${{ variables.vsdropsPrefix }}
runTests: ${{ parameters.runTests }}
runDeviceTests: ${{ parameters.runDeviceTests }}
runTests: ${{ and(parameters.runTests, ne(variables['Build.Reason'], 'Schedule'))}}
runDeviceTests: ${{ and(parameters.runDeviceTests, ne(variables['Build.Reason'], 'Schedule')) }}
keyringPass: $(xma-password)
gitHubToken: ${{ variables['GitHub.Token'] }}
xqaCertPass: $(xqa--certificates--password)
Expand Down
2 changes: 1 addition & 1 deletion tools/devops/automation/templates/build/configure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ steps:
inputs:
locProj: '$(Build.SourcesDirectory)\Localize\LocProject.json'
outDir: '$(Build.ArtifactStagingDirectory)'
isCreatePrSelected: eq(variables['Build.Reason'], 'Schedule')
isCreatePrSelected: eq(variables.isSchedule, 'True')
packageSourceAuth: patAuth
patVariable: '$(OneLocBuild--PAT)'
isAutoCompletePrSelected: false
Expand Down
1 change: 1 addition & 0 deletions tools/devops/automation/templates/build/stage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ jobs:

variables:
isMain: $[eq(variables['Build.SourceBranch'], 'refs/heads/main')]
isScheduled: $[eq(variables['Build.Reason'], 'Schedule')]

steps:
- template: configure.yml
Expand Down
3 changes: 2 additions & 1 deletion tools/dotnet-linker/ApplyPreserveAttributeBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public override SubStepTargets Targets {
| SubStepTargets.Field
| SubStepTargets.Method
| SubStepTargets.Property
| SubStepTargets.Event;
| SubStepTargets.Event
| SubStepTargets.Assembly;
}
}

Expand Down
35 changes: 23 additions & 12 deletions tools/dotnet-linker/SetupStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ public List<IStep> Steps {
}
}

List<IMarkHandler> _markHandlers;
List<IMarkHandler> MarkHandlers {
get {
if (_markHandlers == null) {
var pipeline = typeof (LinkContext).GetProperty ("Pipeline").GetGetMethod ().Invoke (Context, null);
_markHandlers = (List<IMarkHandler>) pipeline.GetType ().GetProperty ("MarkHandlers").GetValue (pipeline);
}
return _markHandlers;
}
}

void InsertBefore (IStep step, string stepName)
{
for (int i = 0; i < Steps.Count; i++) {
Expand Down Expand Up @@ -65,28 +76,28 @@ protected override void TryProcess ()
// the final decision to remove/keep the dynamic registrar must be done before the linking step
InsertBefore (new RegistrarRemovalTrackingStep (), "MarkStep");

var pre_dynamic_dependency_lookup_substeps = new DotNetSubStepDispatcher ();
InsertBefore (pre_dynamic_dependency_lookup_substeps, "MarkStep");

var prelink_substeps = new DotNetSubStepDispatcher ();
InsertBefore (prelink_substeps, "MarkStep");
var pre_mark_substeps = new DotNetSubStepDispatcher ();
InsertBefore (pre_mark_substeps, "MarkStep");

var post_sweep_substeps = new DotNetSubStepDispatcher ();
InsertAfter (post_sweep_substeps, "SweepStep");

if (Configuration.LinkMode != LinkMode.None) {
pre_dynamic_dependency_lookup_substeps.Add (new PreserveBlockCodeSubStep ());
MarkHandlers.Add (new PreserveBlockCodeHandler ());

// We need to run the ApplyPreserveAttribute step even we're only linking sdk assemblies, because even
// though we know that sdk assemblies will never have Preserve attributes, user assemblies may have
// [assembly: LinkSafe] attributes, which means we treat them as sdk assemblies and those may have
// Preserve attributes.
prelink_substeps.Add (new ApplyPreserveAttribute ());
prelink_substeps.Add (new OptimizeGeneratedCodeSubStep ());
prelink_substeps.Add (new MarkNSObjects ());
prelink_substeps.Add (new PreserveSmartEnumConversionsSubStep ());
prelink_substeps.Add (new CollectUnmarkedMembersSubStep ());
prelink_substeps.Add (new StoreAttributesStep ());
MarkHandlers.Add (new DotNetMarkAssemblySubStepDispatcher (new ApplyPreserveAttribute ()));
MarkHandlers.Add (new OptimizeGeneratedCodeHandler ());
MarkHandlers.Add (new DotNetMarkAssemblySubStepDispatcher (new MarkNSObjects ()));
MarkHandlers.Add (new PreserveSmartEnumConversionsHandler ());

// This step could be run after Mark to avoid tracking all members:
// https://github.com/xamarin/xamarin-macios/issues/11447
pre_mark_substeps.Add (new CollectUnmarkedMembersSubStep ());
pre_mark_substeps.Add (new StoreAttributesStep ());

post_sweep_substeps.Add (new RemoveAttributesStep ());
}
Expand Down
18 changes: 18 additions & 0 deletions tools/dotnet-linker/Steps/ConfigurationAwareMarkHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;

using Xamarin.Bundler;

namespace Xamarin.Linker {
public abstract class ConfigurationAwareMarkHandler : ExceptionalMarkHandler {
protected override void Report (Exception exception)
{
LinkerConfiguration.Report (context, exception);
}

protected void Report (List<Exception> exceptions)
{
LinkerConfiguration.Report (context, exceptions);
}
}
}
11 changes: 11 additions & 0 deletions tools/dotnet-linker/Steps/DotNetMarkAssemblyDispatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Mono.Linker.Steps;

namespace Xamarin.Linker.Steps {
// MarkSubStepsDispatcher is abstract, so create a subclass we can instantiate.
// Can be removed when we update to the preview4 linker, which makes MarkSubStepsDispatcher non-abstract.
class DotNetMarkAssemblySubStepDispatcher : MarkSubStepsDispatcher {
public DotNetMarkAssemblySubStepDispatcher (params BaseSubStep[] subSteps) : base (subSteps)
{
}
}
}
121 changes: 121 additions & 0 deletions tools/dotnet-linker/Steps/ExceptionalMarkHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Copyright 2016 Xamarin Inc.

using System;
using Mono.Cecil;
using Mono.Tuner;
using Xamarin.Bundler;

using Xamarin.Tuner;

using Mono.Linker;
using Mono.Linker.Steps;

namespace Xamarin.Linker {

// Similar to ExceptionalSubStep, but this only runs for marked members
// that were registered for handling by the subclass.
public abstract class ExceptionalMarkHandler : IMarkHandler
{
public abstract void Initialize (LinkContext context, MarkContext markContext);

public virtual void Initialize (LinkContext context)
{
this.context = context;
}

protected DerivedLinkContext LinkContext => Configuration.DerivedLinkContext;

protected LinkContext context { get; private set; }

protected AnnotationStore Annotations => context.Annotations;
protected LinkerConfiguration Configuration => LinkerConfiguration.GetInstance (context);

protected Profile Profile => Configuration.Profile;

public void ProcessAssembly (AssemblyDefinition assembly)
{
try {
Process (assembly);
} catch (Exception e) {
Report (Fail (assembly, e));
}
}

public void ProcessType (TypeDefinition type)
{
try {
Process (type);
} catch (Exception e) {
Report (Fail (type, e));
}
}

public void ProcessField (FieldDefinition field)
{
try {
Process (field);
} catch (Exception e) {
Report (Fail (field, e));
}
}

public void ProcessMethod (MethodDefinition method)
{
try {
Process (method);
} catch (Exception e) {
Report (Fail (method, e));
}
}

// state-aware versions to be subclassed

protected virtual void Process (AssemblyDefinition assembly)
{
}

protected virtual void Process (TypeDefinition type)
{
}

protected virtual void Process (FieldDefinition field)
{
}

protected virtual void Process (MethodDefinition method)
{
}

// failure overrides, with defaults

protected virtual Exception Fail (AssemblyDefinition assembly, Exception e)
{
return ErrorHelper.CreateError (ErrorCode, e, Errors.MX_ExceptionalSubSteps, Name, assembly?.FullName);
}

protected virtual Exception Fail (TypeDefinition type, Exception e)
{
return ErrorHelper.CreateError (ErrorCode | 1, e, Errors.MX_ExceptionalSubSteps, Name, type?.FullName);
}

protected virtual Exception Fail (FieldDefinition field, Exception e)
{
return ErrorHelper.CreateError (ErrorCode | 2, e, Errors.MX_ExceptionalSubSteps, Name, field?.FullName);
}

protected virtual Exception Fail (MethodDefinition method, Exception e)
{
return ErrorHelper.CreateError (ErrorCode | 3, e, Errors.MX_ExceptionalSubSteps, Name, method?.FullName);
}
protected virtual void Report (Exception e)
{
throw e;
}

// abstracts

protected abstract string Name { get; }

protected abstract int ErrorCode { get; }
}
}
Loading

0 comments on commit d53c057

Please sign in to comment.