Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| 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))); | ||
Member 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 | ||
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.