-
Notifications
You must be signed in to change notification settings - Fork 5k
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Runtime.InteropServices; | ||
|
||
using ILCompiler.DependencyAnalysis; | ||
|
||
|
@@ -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; | ||
|
@@ -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)) | ||
{ | ||
|
@@ -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"); | ||
} | ||
|
||
var settableInstance = instance.Value as IHasInstanceFields; | ||
if (settableInstance == null) | ||
{ | ||
|
@@ -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))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.