From e61d726979b3e709a3c5f23b6461913e8fe3eb53 Mon Sep 17 00:00:00 2001 From: SingleAccretion Date: Sun, 21 Jan 2024 23:01:38 +0300 Subject: [PATCH] Enable writeable data for WASM This is going upstream. --- .../Common/src/Internal/Runtime/MethodTable.cs | 5 ++--- .../Compiler/DependencyAnalysis/EETypeNode.cs | 13 +++++++++---- .../aot/ILCompiler.LLVM/CodeGen/LLVMObjectWriter.cs | 12 ++++++++++++ .../Preinitialization/Preinitialization.cs | 2 -- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/coreclr/nativeaot/Common/src/Internal/Runtime/MethodTable.cs b/src/coreclr/nativeaot/Common/src/Internal/Runtime/MethodTable.cs index 106b5d1c2242..e24feb5536de 100644 --- a/src/coreclr/nativeaot/Common/src/Internal/Runtime/MethodTable.cs +++ b/src/coreclr/nativeaot/Common/src/Internal/Runtime/MethodTable.cs @@ -190,8 +190,7 @@ internal static bool SupportsWritableData { get { - // For now just key this off of SupportsRelativePointer to avoid this on both CppCodegen and WASM. - return SupportsRelativePointers; + return true; } } @@ -1184,7 +1183,7 @@ internal void* WritableData uint offset = GetFieldOffset(EETypeField.ETF_WritableData); - if (!IsDynamicType) + if (!IsDynamicType && SupportsRelativePointers) return (void*)GetField(offset).Value; else return (void*)GetField(offset).Value; diff --git a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/EETypeNode.cs b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/EETypeNode.cs index e136b1057cf1..48ee9cbc18a2 100644 --- a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/EETypeNode.cs +++ b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/EETypeNode.cs @@ -103,8 +103,7 @@ public EETypeNode(NodeFactory factory, TypeDesc type) factory.TypeSystemContext.EnsureLoadableType(type); } - public static bool SupportsWritableData(TargetDetails target) - => target.SupportsRelativePointers; + public static bool SupportsWritableData(TargetDetails target) => true; public static bool SupportsFrozenRuntimeTypeInstances(TargetDetails target) => SupportsWritableData(target); @@ -1125,11 +1124,17 @@ protected void OutputWritableData(NodeFactory factory, ref ObjectDataBuilder obj { if (_writableDataNode != null) { - objData.EmitReloc(_writableDataNode, RelocType.IMAGE_REL_BASED_RELPTR32); + if (factory.Target.SupportsRelativePointers) + objData.EmitReloc(_writableDataNode, RelocType.IMAGE_REL_BASED_RELPTR32); + else + objData.EmitPointerReloc(_writableDataNode); } else if (SupportsWritableData(factory.Target)) { - objData.EmitInt(0); + if (factory.Target.SupportsRelativePointers) + objData.EmitInt(0); + else + objData.EmitZeroPointer(); } } diff --git a/src/coreclr/tools/aot/ILCompiler.LLVM/CodeGen/LLVMObjectWriter.cs b/src/coreclr/tools/aot/ILCompiler.LLVM/CodeGen/LLVMObjectWriter.cs index c145c72524e5..2653826f475e 100644 --- a/src/coreclr/tools/aot/ILCompiler.LLVM/CodeGen/LLVMObjectWriter.cs +++ b/src/coreclr/tools/aot/ILCompiler.LLVM/CodeGen/LLVMObjectWriter.cs @@ -248,6 +248,8 @@ private void EmitObjectNode(ObjectNode node, ObjectData nodeContents) if (nextRelocValid) { Relocation reloc = nodeContents.Relocs[relocIndex]; + Debug.Assert(IsSupportedRelocType(node, reloc.RelocType), $"{reloc.RelocType} in {node} not supported"); + long delta; fixed (void* location = &data[reloc.Offset]) { @@ -313,6 +315,16 @@ private void EmitObjectNode(ObjectNode node, ObjectData nodeContents) } } + private static bool IsSupportedRelocType(ObjectNode node, RelocType type) + { + if (node is StackTraceMethodMappingNode) + { + // Stack trace metadata uses relative pointers, but is currently unused. + return true; + } + return type is RelocType.IMAGE_REL_BASED_HIGHLOW; + } + private void EmitSymbolDef(LLVMValueRef baseSymbol, ReadOnlySpan symbolIdentifier, int offsetFromBaseSymbol) { LLVMValueRef symbolAddress = baseSymbol; diff --git a/src/tests/nativeaot/SmokeTests/Preinitialization/Preinitialization.cs b/src/tests/nativeaot/SmokeTests/Preinitialization/Preinitialization.cs index 62c6f5211a94..5252e1f2d58c 100644 --- a/src/tests/nativeaot/SmokeTests/Preinitialization/Preinitialization.cs +++ b/src/tests/nativeaot/SmokeTests/Preinitialization/Preinitialization.cs @@ -55,9 +55,7 @@ private static int Main() TestReadOnlySpan.Run(); TestStaticInterfaceMethod.Run(); TestConstrainedCall.Run(); -#if !CODEGEN_WASM TestTypeHandles.Run(); -#endif TestIsValueType.Run(); TestIndirectLoads.Run(); TestInitBlock.Run();