Skip to content

Extend preinitialization interpreter to support calling string::Length #78680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;

using ILCompiler.DependencyAnalysis;

Expand Down Expand Up @@ -402,6 +403,7 @@ private Status TryScanMethod(MethodIL methodIL, Value[] parameters, Stack<Method
break;

case ILOpcode.call:
case ILOpcode.callvirt:
{
MethodDesc method = (MethodDesc)methodIL.GetObject(reader.ReadILToken());
MethodSignature methodSig = method.Signature;
Expand All @@ -427,6 +429,13 @@ private Status TryScanMethod(MethodIL methodIL, Value[] parameters, Stack<Method
methodParams[i] = stack.PopIntoLocation(GetArgType(method, i));
}

if (opcode == ILOpcode.callvirt)
{
// Only support non-virtual methods for now + we don't emulate NRE on null this
if (method.IsVirtual || methodParams[0] == null)
return Status.Fail(methodIL.OwningMethod, opcode);
}

Value retVal;
if (!method.IsIntrinsic || !TryHandleIntrinsicCall(method, methodParams, out retVal))
{
Expand Down Expand Up @@ -598,6 +607,11 @@ private Status TryScanMethod(MethodIL methodIL, Value[] parameters, Stack<Method
return Status.Fail(methodIL.OwningMethod, opcode, "Reference field");
}

if (field.FieldType.IsByRef)
{
return Status.Fail(methodIL.OwningMethod, opcode, "Byref field");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not strictly necessary because we would throw/catch exception later, but ideally we should only do throw/catch on invalid programs.

}

var settableInstance = instance.Value as IHasInstanceFields;
if (settableInstance == null)
{
Expand Down Expand Up @@ -2259,28 +2273,65 @@ public override void WriteFieldData(ref ObjectDataBuilder builder, NodeFactory f
public override ReferenceTypeValue ToForeignInstance(int baseInstructionCounter) => this;
}

private sealed class StringInstance : ReferenceTypeValue
private sealed class StringInstance : ReferenceTypeValue, IHasInstanceFields
{
private readonly string _value;
private readonly byte[] _value;

private string ValueAsString
{
get
{
FieldDesc firstCharField = Type.GetField("_firstChar");
int startOffset = firstCharField.Offset.AsInt;
int length = _value.Length - startOffset - sizeof(char) /* terminating null */;
return new string(MemoryMarshal.Cast<byte, char>(
((ReadOnlySpan<byte>)_value).Slice(startOffset, length)));
}
}
public StringInstance(TypeDesc stringType, string value)
: base(stringType)
{
_value = value;
_value = ConstructStringInstance(stringType, value);
}

private static byte[] ConstructStringInstance(TypeDesc stringType, ReadOnlySpan<char> value)
{
int pointerSize = stringType.Context.Target.PointerSize;
var bytes = new byte[
pointerSize /* MethodTable */
+ sizeof(int) /* length */
+ (value.Length * sizeof(char)) /* bytes */
+ sizeof(char) /* null terminator */];

FieldDesc lengthField = stringType.GetField("_stringLength");
Debug.Assert(lengthField.FieldType.IsWellKnownType(WellKnownType.Int32)
&& lengthField.Offset.AsInt == pointerSize);
new FieldAccessor(bytes).SetField(lengthField, ValueTypeValue.FromInt32(value.Length));

FieldDesc firstCharField = stringType.GetField("_firstChar");
Debug.Assert(firstCharField.FieldType.IsWellKnownType(WellKnownType.Char)
&& firstCharField.Offset.AsInt == pointerSize + sizeof(int) /* length */);

value.CopyTo(MemoryMarshal.Cast<byte, char>(((Span<byte>)bytes).Slice(firstCharField.Offset.AsInt)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would not work for little-endian/big-endian cross-compilation, but it is not the first problem of this kind in the aot interpreter.


return bytes;
}

public override void WriteFieldData(ref ObjectDataBuilder builder, NodeFactory factory)
{
builder.EmitPointerReloc(factory.SerializedStringObject(_value));
builder.EmitPointerReloc(factory.SerializedStringObject(ValueAsString));
}

public override bool GetRawData(NodeFactory factory, out object data)
{
data = factory.SerializedStringObject(_value);
data = factory.SerializedStringObject(ValueAsString);
return true;
}

public override ReferenceTypeValue ToForeignInstance(int baseInstructionCounter) => this;
Value IHasInstanceFields.GetField(FieldDesc field) => new FieldAccessor(_value).GetField(field);
void IHasInstanceFields.SetField(FieldDesc field, Value value) => ThrowHelper.ThrowInvalidProgramException();
ByRefValue IHasInstanceFields.GetFieldAddress(FieldDesc field) => new FieldAccessor(_value).GetFieldAddress(field);
}

#pragma warning disable CA1852
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ private static int Main()
TestGCInteraction.Run();
TestDuplicatedFields.Run();
TestInstanceDelegate.Run();
TestStringFields.Run();
#else
Console.WriteLine("Preinitialization is disabled in multimodule builds for now. Skipping test.");
#endif
Expand Down Expand Up @@ -913,6 +914,37 @@ public static void Run()
}
}

class TestStringFields
{
class ClassAccessingLength
{
public static int Length = "Hello".Length;
}

class ClassAccessingNull
{
public static int Length;
static ClassAccessingNull()
{
string myNull = null;
try
{
Length = myNull.Length;
}
catch (Exception) { }
}
}

public static void Run()
{
Assert.IsPreinitialized(typeof(ClassAccessingLength));
Assert.AreEqual(5, ClassAccessingLength.Length);

Assert.IsLazyInitialized(typeof(ClassAccessingNull));
Assert.AreEqual(0, ClassAccessingNull.Length);
}
}

static class Assert
{
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2070:UnrecognizedReflectionPattern",
Expand Down