From 6c6d440ebbe81fe203ef46b9f240ea5b4d3de142 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Tue, 1 Nov 2022 09:37:50 +0100 Subject: [PATCH] =?UTF-8?q?=D0=A1/wasm:=20don't=20include=20references=20m?= =?UTF-8?q?etadata=20resource=20into=20GC=20stack=20roots=20and=20into=20c?= =?UTF-8?q?lass=20layout=20for=20GC.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/teavm/backend/c/CTarget.java | 1 - .../backend/c/generate/ClassGenerator.java | 1 + .../teavm/model/lowlevel/Characteristics.java | 29 +++++++++++++++++++ .../lowlevel/LowLevelNullCheckFilter.java | 7 +++-- .../model/lowlevel/NativePointerFinder.java | 2 +- 5 files changed, 36 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/org/teavm/backend/c/CTarget.java b/core/src/main/java/org/teavm/backend/c/CTarget.java index 8cdf140ce..0883b340f 100644 --- a/core/src/main/java/org/teavm/backend/c/CTarget.java +++ b/core/src/main/java/org/teavm/backend/c/CTarget.java @@ -374,7 +374,6 @@ public class CTarget implements TeaVMTarget, TeaVMCHost { TagRegistry tagRegistry = !incremental ? new TagRegistry(classes, hierarchy) : null; Decompiler decompiler = new Decompiler(classes, new HashSet<>(), controller.isFriendlyToDebugger()); - Characteristics characteristics = new Characteristics(controller.getUnprocessedClassSource()); NameProvider nameProvider = new NameProviderWithSpecialNames(rawNameProvider, controller.getUnprocessedClassSource()); diff --git a/core/src/main/java/org/teavm/backend/c/generate/ClassGenerator.java b/core/src/main/java/org/teavm/backend/c/generate/ClassGenerator.java index d54fd64c6..93910b293 100644 --- a/core/src/main/java/org/teavm/backend/c/generate/ClassGenerator.java +++ b/core/src/main/java/org/teavm/backend/c/generate/ClassGenerator.java @@ -1131,6 +1131,7 @@ public class ClassGenerator { String className = ((ValueType.Object) type).getClassName(); return !context.getCharacteristics().isStructure(className) && !context.getCharacteristics().isFunction(className) + && !context.getCharacteristics().isResource(className) && !className.equals(Address.class.getName()); } else { return type instanceof ValueType.Array; diff --git a/core/src/main/java/org/teavm/model/lowlevel/Characteristics.java b/core/src/main/java/org/teavm/model/lowlevel/Characteristics.java index 002e0d5fe..c1c8d034c 100644 --- a/core/src/main/java/org/teavm/model/lowlevel/Characteristics.java +++ b/core/src/main/java/org/teavm/model/lowlevel/Characteristics.java @@ -32,6 +32,7 @@ public class Characteristics { private ObjectByteMap isStructure = new ObjectByteHashMap<>(); private ObjectByteMap isStaticInit = new ObjectByteHashMap<>(); private ObjectByteMap isFunction = new ObjectByteHashMap<>(); + private ObjectByteMap isResource = new ObjectByteHashMap<>(); private ObjectByteMap isManaged = new ObjectByteHashMap<>(); public Characteristics(ClassReaderSource classSource) { @@ -56,6 +57,34 @@ public class Characteristics { return result != 0; } + public boolean isResource(String className) { + byte result = isResource.getOrDefault(className, (byte) -1); + if (result < 0) { + if (className.equals("org/teavm/platform/metadata/Resource")) { + result = 1; + } else { + ClassReader cls = classSource.get(className); + if (cls != null) { + if (cls.getParent() != null) { + result = isResource(cls.getParent()) ? (byte) 1 : 0; + } + if (result == 0) { + for (String itf : cls.getInterfaces()) { + if (isResource(itf)) { + result = 1; + break; + } + } + } + } else { + result = 0; + } + } + isResource.put(className, result); + } + return result != 0; + } + public boolean isStaticInit(String className) { byte result = isStaticInit.getOrDefault(className, (byte) -1); if (result < 0) { diff --git a/core/src/main/java/org/teavm/model/lowlevel/LowLevelNullCheckFilter.java b/core/src/main/java/org/teavm/model/lowlevel/LowLevelNullCheckFilter.java index acaa87625..9c9ef6130 100644 --- a/core/src/main/java/org/teavm/model/lowlevel/LowLevelNullCheckFilter.java +++ b/core/src/main/java/org/teavm/model/lowlevel/LowLevelNullCheckFilter.java @@ -28,11 +28,14 @@ public class LowLevelNullCheckFilter implements NullCheckFilter { @Override public boolean apply(FieldReference field) { - return !characteristics.isStructure(field.getClassName()); + return !characteristics.isStructure(field.getClassName()) + && !characteristics.isStructure(field.getClassName()); } @Override public boolean apply(MethodReference method) { - return characteristics.isManaged(method.getClassName()) && characteristics.isManaged(method); + return characteristics.isManaged(method.getClassName()) + && characteristics.isManaged(method) + && !characteristics.isResource(method.getClassName()); } } diff --git a/core/src/main/java/org/teavm/model/lowlevel/NativePointerFinder.java b/core/src/main/java/org/teavm/model/lowlevel/NativePointerFinder.java index 280100fa4..176e57968 100644 --- a/core/src/main/java/org/teavm/model/lowlevel/NativePointerFinder.java +++ b/core/src/main/java/org/teavm/model/lowlevel/NativePointerFinder.java @@ -120,6 +120,6 @@ public class NativePointerFinder { } String className = ((ValueType.Object) type).getClassName(); return characteristics.isStructure(className) || className.equals(Address.class.getName()) - || characteristics.isFunction(className); + || characteristics.isFunction(className) || characteristics.isResource(className); } }