Skip to content

Commit

Permalink
Fix applying SuppressIldasmAttribute by reverting strange check
Browse files Browse the repository at this point in the history
  • Loading branch information
mv3shape authored Mar 23, 2021
1 parent 51775a0 commit b29c7f1
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 2 deletions.
14 changes: 12 additions & 2 deletions Obfuscar/Obfuscator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1512,15 +1512,25 @@ public void PostProcessing()
var module = info.Definition.MainModule;
var attribute = new TypeReference("System.Runtime.CompilerServices", "SuppressIldasmAttribute", module,
module.TypeSystem.CoreLibrary).Resolve();
if (attribute == null || attribute.Module != module.TypeSystem.CoreLibrary)
return;

if (attribute == null)
{
LogOutput($"Failed to resolve SuppressIldasmAttribute inside {module.Name}");
continue;
}

// Something like this was added in the master repo, but it doesn't work
//if (attribute == null || attribute.Module.TypeSystem.CoreLibrary.Name != module.TypeSystem.CoreLibrary.Name) return;

CustomAttribute found = module.CustomAttributes.FirstOrDefault(existing =>
existing.Constructor.DeclaringType.FullName == attribute.FullName);

//Only add if it's not there already
if (found != null)
{
LogOutput($"SuppressIldasmAttribute already exists for {module.Name}");
continue;
}

//Add one
var add = module.ImportReference(attribute.GetConstructors().FirstOrDefault(item => !item.HasParameters));
Expand Down
82 changes: 82 additions & 0 deletions ObfuscarTest/SuppressIldasmTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#region Copyright (c) 2007 Ryan Williams <[email protected]>

/// <copyright>
/// Copyright (c) 2007 Ryan Williams <[email protected]>
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy
/// of this software and associated documentation files (the "Software"), to deal
/// in the Software without restriction, including without limitation the rights
/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
/// copies of the Software, and to permit persons to whom the Software is
/// furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in
/// all copies or substantial portions of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
/// THE SOFTWARE.
/// </copyright>

#endregion

using System.IO;
using System.Linq;
using Mono.Cecil;
using Obfuscar;
using Xunit;
using System.Runtime.CompilerServices;

namespace ObfuscarTest
{
public class SuppressIldasmTests
{
Obfuscator BuildAndObfuscateAssemblies(string name)
{
string xml = string.Format(
@"<?xml version='1.0'?>" +
@"<Obfuscator>" +
@"<Var name='InPath' value='{0}' />" +
@"<Var name='OutPath' value='{1}' />" +
@"<Var name='KeepPublicApi' value='true' />" +
@"<Var name='HidePrivateApi' value='true' />" +
@"<Var name='SuppressIldasm' value='true' />" +
@"<Module file='$(InPath){2}{3}.dll' />" +
@"</Obfuscator>", TestHelper.InputPath, TestHelper.OutputPath, Path.DirectorySeparatorChar, name);

return TestHelper.BuildAndObfuscate(name, string.Empty, xml);
}

[Fact]
public void CheckSuppressIldasm()
{
Obfuscator item = BuildAndObfuscateAssemblies("AssemblyWithInterfaces");
ObfuscationMap map = item.Mapping;

string assmName = "AssemblyWithInterfaces.dll";

// We do not expect input assembly to have special attribute
using (AssemblyDefinition inAssmDef = AssemblyDefinition.ReadAssembly(
Path.Combine(TestHelper.InputPath, assmName)))
{
CustomAttribute found = inAssmDef.CustomAttributes.FirstOrDefault(existing =>
existing.Constructor.DeclaringType.Name == nameof(SuppressIldasmAttribute));
Assert.Null(found);

}

// the output assembly must have specific attribute
using (AssemblyDefinition outAssmDef = AssemblyDefinition.ReadAssembly(
Path.Combine(item.Project.Settings.OutPath, assmName)))
{
CustomAttribute found = outAssmDef.CustomAttributes.FirstOrDefault(existing =>
existing.Constructor.DeclaringType.Name == nameof(SuppressIldasmAttribute));
Assert.NotNull(found);
}
}
}
}

0 comments on commit b29c7f1

Please sign in to comment.