Skip to content
This repository was archived by the owner on Jan 27, 2019. It is now read-only.

Commit

Permalink
Fix NRE in namespace function & removing initializer data field when …
Browse files Browse the repository at this point in the history
…not all references are removed
yck1509 committed Mar 15, 2016
1 parent 1770f1f commit c5cbb03
Showing 2 changed files with 23 additions and 3 deletions.
5 changes: 4 additions & 1 deletion Confuser.Core/Project/Patterns/NamespaceFunction.cs
Original file line number Diff line number Diff line change
@@ -29,10 +29,13 @@ public override object Evaluate(IDnlibDef definition) {
if (type == null)
type = ((IMemberDef)definition).DeclaringType;

if (type == null)
return false;

while (type.IsNested)
type = type.DeclaringType;

return type != null && Regex.IsMatch(type.Namespace, ns);
return type != null && Regex.IsMatch(type.Namespace ?? "", ns);
}
}
}
21 changes: 19 additions & 2 deletions Confuser.Protections/Constants/EncodePhase.cs
Original file line number Diff line number Diff line change
@@ -215,10 +215,25 @@ void UpdateReference(CEContext moduleCtx, TypeSig valueType, List<Tuple<MethodDe
}
}

void RemoveDataFieldRefs(ConfuserContext context, HashSet<FieldDef> dataFields, HashSet<Instruction> fieldRefs) {
foreach (var type in context.CurrentModule.GetTypes())
foreach (var method in type.Methods.Where(m => m.HasBody)) {
foreach (var instr in method.Body.Instructions)
if (instr.Operand is FieldDef && !fieldRefs.Contains(instr))
dataFields.Remove((FieldDef)instr.Operand);
}

foreach (var fieldToRemove in dataFields) {
fieldToRemove.DeclaringType.Fields.Remove(fieldToRemove);
}
}

void ExtractConstants(
ConfuserContext context, ProtectionParameters parameters, CEContext moduleCtx,
Dictionary<object, List<Tuple<MethodDef, Instruction>>> ldc,
Dictionary<byte[], List<Tuple<MethodDef, Instruction>>> ldInit) {
var dataFields = new HashSet<FieldDef>();
var fieldRefs = new HashSet<Instruction>();
foreach (MethodDef method in parameters.Targets.OfType<MethodDef>().WithProgress(context.Logger)) {
if (!method.HasBody)
continue;
@@ -284,8 +299,9 @@ void ExtractConstants(
ldc.Remove(arrLen);
}

if(dataField.DeclaringType!=null)
dataField.DeclaringType.Fields.Remove(dataField);
dataFields.Add(dataField);
fieldRefs.Add(instrs[i - 1]);

var value = new byte[dataField.InitialValue.Length + 4];
value[0] = (byte)(arrLen >> 0);
value[1] = (byte)(arrLen >> 8);
@@ -328,6 +344,7 @@ void ExtractConstants(

context.CheckCancellation();
}
RemoveDataFieldRefs(context, dataFields, fieldRefs);
}

class ByteArrayComparer : IEqualityComparer<byte[]> {

0 comments on commit c5cbb03

Please sign in to comment.