forked from xamarin/xamarin-macios
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[linker] Remove non-bitcode compatible code, and show a warning.
Remove code not currently compatible with bitcode and replace it with an exception instead (otherwise we'll assert at runtime). Also show a warning when we detect this. This is quite helpful when looking at watch device test runs to filter out failures we already know about. This fixes point #2 in xamarin#4763.
- Loading branch information
1 parent
510bb0b
commit be9355c
Showing
10 changed files
with
303 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
using ObjCRuntime; | ||
|
||
using NUnit.Framework; | ||
|
||
namespace LinkSdk { | ||
[TestFixture] | ||
public class BitcodeTest { | ||
[Test] | ||
public void FilterClauseTest () | ||
{ | ||
var supported = true; | ||
#if __WATCHOS__ | ||
if (Runtime.Arch == Arch.DEVICE) | ||
supported = false; | ||
#endif | ||
if (supported) { | ||
Assert.AreEqual (0, FilterClause (), "Filter me"); | ||
} else { | ||
Assert.Throws<NotSupportedException> (() => FilterClause (), "Filter me not supported"); | ||
} | ||
} | ||
|
||
// A method with a filter clause | ||
// mtouch will show a warning for this method (MT2105) when building for watchOS device. This is expected. | ||
static int FilterClause () | ||
{ | ||
try { | ||
throw new Exception ("FilterMe"); | ||
} catch (Exception e) when (e.Message == "FilterMe") { | ||
return 0; | ||
} catch { | ||
return 1; | ||
} | ||
} | ||
|
||
[Test] | ||
public void FaultClauseTest () | ||
{ | ||
// First assert that the method that is supposed to have a fault clause actually has a fault clause. | ||
// This is somewhat complicated because it's not the method we call that has the fault clause, but a generated method in a generated class. | ||
// This is because we only have an indirect way of making csc produce a fault clause | ||
var enumeratorType = GetType ().GetNestedTypes (BindingFlags.NonPublic).First ((v) => v.Name.Contains ($"<{nameof (FaultClause)}>")); | ||
var method = enumeratorType.GetMethod ("MoveNext", BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public); | ||
var body = method.GetMethodBody (); | ||
Assert.IsTrue (body.ExceptionHandlingClauses.Any ((v) => v.Flags == ExceptionHandlingClauseOptions.Fault), "Any fault clauses"); | ||
|
||
// Then assert that the method can be called successfully. | ||
var rv = FaultClause ().ToArray (); | ||
Assert.AreEqual (1, rv.Count (), "Count"); | ||
Assert.AreEqual (1, rv [0], "Item 1"); | ||
|
||
} | ||
|
||
// The C# compiler uses fault clauses for 'using' blocks inside iterators. | ||
static IEnumerable<int> FaultClause () | ||
{ | ||
using (var d = new Disposable ()) { | ||
yield return 1; | ||
} | ||
} | ||
class Disposable : IDisposable { | ||
void IDisposable.Dispose () { } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
tools/linker/MonoTouch.Tuner/RemoveBitcodeIncompatibleCodeStep.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// Copyright 2012-2014, 2016 Xamarin Inc. All rights reserved. | ||
using System.Collections.Generic; | ||
using Mono.Tuner; | ||
using Mono.Cecil; | ||
using Mono.Cecil.Cil; | ||
using Mono.Linker.Steps; | ||
|
||
using Xamarin.Bundler; | ||
using Xamarin.Linker; | ||
using Mono.Linker; | ||
using System; | ||
|
||
namespace MonoTouch.Tuner { | ||
|
||
public class RemoveBitcodeIncompatibleCodeStep : BaseStep { | ||
|
||
LinkerOptions Options; | ||
MethodDefinition nse_ctor_def; | ||
Dictionary<ModuleDefinition, MethodReference> nse_ctors; | ||
|
||
public RemoveBitcodeIncompatibleCodeStep (LinkerOptions options) | ||
{ | ||
Options = options; | ||
} | ||
|
||
protected override void ProcessAssembly (AssemblyDefinition assembly) | ||
{ | ||
foreach (var type in assembly.MainModule.Types) | ||
ProcessType (type); | ||
} | ||
|
||
void ProcessType (TypeDefinition type) | ||
{ | ||
if (type.HasNestedTypes) { | ||
foreach (var nestedType in type.NestedTypes) | ||
ProcessType (nestedType); | ||
} | ||
|
||
if (type.HasMethods) { | ||
foreach (var method in type.Methods) | ||
ProcessMethod (method); | ||
} | ||
|
||
} | ||
|
||
void ProcessMethod (MethodDefinition method) | ||
{ | ||
if (!method.HasBody) | ||
return; | ||
|
||
var body = method.Body; | ||
if (!body.HasExceptionHandlers) | ||
return; | ||
|
||
var anyFilterClauses = false; | ||
foreach (var eh in body.ExceptionHandlers) { | ||
if (eh.HandlerType == ExceptionHandlerType.Filter) { | ||
anyFilterClauses = true; | ||
ErrorHelper.Show (ErrorHelper.CreateWarning (Options.Application, 2105, method, $"The method {method.DeclaringType.FullName}.{method.Name} contains a '{eh.HandlerType}' exception clause, which is currently not supported when compiling for bitcode. This method will throw an exception if called.")); | ||
break; | ||
} | ||
} | ||
if (!anyFilterClauses) | ||
return; | ||
|
||
body = new MethodBody (method); | ||
var il = body.GetILProcessor (); | ||
il.Emit (OpCodes.Ldstr, "This method contains IL not supported when compiled to bitcode."); | ||
if (nse_ctor_def == null) { | ||
var corlib = Context.GetAssembly ("mscorlib"); | ||
var nse = corlib.MainModule.GetType ("System", "NotSupportedException"); | ||
foreach (var ctor in nse.GetConstructors ()) { | ||
if (!ctor.HasParameters) | ||
continue; | ||
var parameters = ctor.Parameters; | ||
if (parameters.Count != 1) | ||
continue; | ||
if (!parameters [0].ParameterType.Is ("System", "String")) | ||
continue; | ||
nse_ctor_def = ctor; | ||
break; | ||
} | ||
nse_ctors = new Dictionary<ModuleDefinition, MethodReference> (); | ||
} | ||
MethodReference nse_ctor; | ||
if (!nse_ctors.TryGetValue (method.Module, out nse_ctor)) { | ||
nse_ctors [method.Module] = nse_ctor = method.Module.ImportReference (nse_ctor_def); | ||
|
||
// We're processing all assemblies, not linked assemblies, so | ||
// make sure we're saving any changes to non-linked assemblies as well. | ||
var assembly = method.Module.Assembly; | ||
var action = Annotations.GetAction (assembly); | ||
switch (action) { | ||
case AssemblyAction.Link: | ||
case AssemblyAction.Save: | ||
break; | ||
default: | ||
Annotations.SetAction (assembly, AssemblyAction.Save); | ||
break; | ||
} | ||
} | ||
il.Emit (OpCodes.Newobj, nse_ctor); | ||
il.Emit (OpCodes.Throw); | ||
method.Body = body; | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters