Skip to content

Commit

Permalink
Add FieldRva initialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
Washi1337 committed Mar 20, 2024
1 parent c77651d commit 8cb57ac
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using AsmResolver;
using AsmResolver.DotNet;
using AsmResolver.DotNet.Signatures;

Expand Down Expand Up @@ -109,7 +110,7 @@ public TypeInitializerResult HandleInitialization(CilThread thread, ITypeDescrip

// "Call" the constructor.
initialization.ConstructorCalled = true;

// Actually find the constructor and call it if it is there.
var cctor = definition.GetStaticConstructor();
if (cctor is not null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using AsmResolver;
using AsmResolver.DotNet;
using AsmResolver.DotNet.Signatures;
using Echo.Memory;
Expand Down Expand Up @@ -51,6 +52,13 @@ public long GetFieldAddress(IFieldDescriptor field)
{
var layout = _valueFactory.GetTypeValueMemoryLayout(field.Signature!.FieldType);
address = _heap.Allocate(layout.Size, true);

if (field.Resolve() is { IsStatic: true, HasFieldRva: true, FieldRva: IReadableSegment data } def)
{
field = def;
_heap.GetChunkSpan(address).Write(data.WriteIntoArray());
}

_fields.Add(field, address);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Linq;
using System.Text;
using AsmResolver;
using AsmResolver.DotNet;
using AsmResolver.PE.DotNet.Cil;
using AsmResolver.PE.DotNet.Metadata.Tables.Rows;
Expand Down Expand Up @@ -80,4 +82,20 @@ public void ReadStaticInt32WithInitializer()
Assert.False(slot.IsNull);
Assert.Equal(ClassWithInitializer.Field, Encoding.Unicode.GetString(slot.ReadStringData().Bits));
}

[Fact]
public void ReadStaticInt32WithFieldRva()
{
var field = new FieldDefinition(
"StaticInt32WithFieldRva",
FieldAttributes.Static | FieldAttributes.HasFieldRva,
ModuleFixture.MockModule.CorLibTypeFactory.Int32
);
field.FieldRva = new DataSegment(BitConverter.GetBytes(1337));

var result = Dispatcher.Dispatch(Context, new CilInstruction(CilOpCodes.Ldsfld, field));

Assert.True(result.IsSuccess);
Assert.Equal(1337, Assert.Single(Context.CurrentFrame.EvaluationStack).Contents.AsSpan().I32);
}
}

0 comments on commit 8cb57ac

Please sign in to comment.