Skip to content

Commit b8cc3cf

Browse files
authored
fix: keep the base class of runtime-resolved types in large apps (#2111)
A type missing from the static metadata, such as the private class behind a java.util iterator, is resolved at run time and gets a node of its own. Its base class was recorded as a 16-bit node id, but those nodes were appended after the static ones plus a 1000-slot gap, so in an app whose static tree approaches the 16-bit node limit (about 64.5k nodes) the id wrapped and the type was linked to an unrelated base. The wrapper then lost every member declared on the real base class: HashMap, LinkedHashMap, TreeMap and ConcurrentHashMap iterators had no hasNext(), org.json.JSONObject.keys() had no hasNext(), and unmodifiable, synchronized and immutable collections had no iterator(). * Runtime-resolved types hold their base class as a node pointer, and node ids are 32-bit wherever a runtime-created node can appear. Those nodes are numbered from 65536 in every app, so the wide-id path runs in small apps too and the new iterator specs cover it. * Nodes created at run time go into a table the reader owns instead of being appended to MetadataTreeNode::children. The static tree is never written after it is built, which makes MetadataNode's unlocked walks of it safe: another runtime resolving a type could previously reallocate a child list mid-iteration. A type resolved at run time is consequently no longer reachable by name from its parent package or class; it only was in a runtime that had not yet materialized that parent. * Publishing a runtime-resolved node is atomic: the base class is resolved first and the lookup repeated right before the node is created, so two runtimes resolving the same type no longer each add a node. * MetadataTreeNode stays at 64 bytes on 64-bit ABIs. * ReadInterfaceImplementationTypeName returns an empty name for a runtime-resolved node rather than reading past its short record.
1 parent 98e808f commit b8cc3cf

7 files changed

Lines changed: 455 additions & 96 deletions

File tree

‎test-app/app/src/main/assets/app/mainpage.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ require("./tests/kotlin/extensions/testExtensionFunctionsSupport");
8888
require("./tests/kotlin/enums/testEnumsSupport");
8989
require("./tests/kotlin/access/testInternalLanguageFeaturesSupport");
9090
require("./tests/testPackagePrivate");
91+
require("./tests/testJavaIterators");
9192
require("./tests/kotlin/properties/testPropertiesSupport.js");
9293
require('./tests/testNativeTimers');
9394
require("./tests/testPostFrameCallback");
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
describe("Tests Java iterators", function () {
2+
function className(javaObject) {
3+
try {
4+
return javaObject.getClass().getName();
5+
} catch (e) {
6+
return "<unknown: " + e + ">";
7+
}
8+
}
9+
10+
function drain(iterator) {
11+
var context = "iterator class " + className(iterator);
12+
expect(typeof iterator.hasNext).withContext(context + " hasNext").toBe("function");
13+
expect(typeof iterator.next).withContext(context + " next").toBe("function");
14+
15+
var items = [];
16+
while (iterator.hasNext()) {
17+
items.push(iterator.next());
18+
}
19+
return items;
20+
}
21+
22+
function drainToStrings(iterator) {
23+
return drain(iterator).map(function (item) {
24+
return String(item);
25+
}).sort();
26+
}
27+
28+
function fill(collection) {
29+
collection.add("a");
30+
collection.add("b");
31+
collection.add("c");
32+
return collection;
33+
}
34+
35+
function fillMap(map) {
36+
map.put("a", "1");
37+
map.put("b", "2");
38+
map.put("c", "3");
39+
return map;
40+
}
41+
42+
var collections = {
43+
"java.util.ArrayList": function () { return fill(new java.util.ArrayList()); },
44+
"java.util.LinkedList": function () { return fill(new java.util.LinkedList()); },
45+
"java.util.Vector": function () { return fill(new java.util.Vector()); },
46+
"java.util.ArrayDeque": function () { return fill(new java.util.ArrayDeque()); },
47+
"java.util.HashSet": function () { return fill(new java.util.HashSet()); },
48+
"java.util.LinkedHashSet": function () { return fill(new java.util.LinkedHashSet()); },
49+
"java.util.TreeSet": function () { return fill(new java.util.TreeSet()); },
50+
"java.util.concurrent.CopyOnWriteArrayList": function () { return fill(new java.util.concurrent.CopyOnWriteArrayList()); },
51+
"java.util.Collections.unmodifiableList": function () { return java.util.Collections.unmodifiableList(fill(new java.util.ArrayList())); },
52+
"java.util.Collections.synchronizedList": function () { return java.util.Collections.synchronizedList(fill(new java.util.ArrayList())); },
53+
"java.util.Arrays.asList": function () {
54+
var array = Array.create(java.lang.Object, 3);
55+
array[0] = "a";
56+
array[1] = "b";
57+
array[2] = "c";
58+
return java.util.Arrays.asList(array);
59+
}
60+
};
61+
62+
Object.keys(collections).forEach(function (name) {
63+
it("iterates " + name + " with hasNext/next", function () {
64+
var collection = collections[name]();
65+
expect(drainToStrings(collection.iterator())).toEqual(["a", "b", "c"]);
66+
});
67+
});
68+
69+
var maps = {
70+
"java.util.HashMap": function () { return fillMap(new java.util.HashMap()); },
71+
"java.util.LinkedHashMap": function () { return fillMap(new java.util.LinkedHashMap()); },
72+
"java.util.TreeMap": function () { return fillMap(new java.util.TreeMap()); },
73+
"java.util.Hashtable": function () { return fillMap(new java.util.Hashtable()); },
74+
"java.util.concurrent.ConcurrentHashMap": function () { return fillMap(new java.util.concurrent.ConcurrentHashMap()); },
75+
"java.util.Collections.unmodifiableMap": function () { return java.util.Collections.unmodifiableMap(fillMap(new java.util.HashMap())); },
76+
"android.util.ArrayMap": function () { return fillMap(new android.util.ArrayMap()); }
77+
};
78+
79+
Object.keys(maps).forEach(function (name) {
80+
it("iterates the keySet of " + name, function () {
81+
expect(drainToStrings(maps[name]().keySet().iterator())).toEqual(["a", "b", "c"]);
82+
});
83+
84+
it("iterates the values of " + name, function () {
85+
expect(drainToStrings(maps[name]().values().iterator())).toEqual(["1", "2", "3"]);
86+
});
87+
88+
it("iterates the entrySet of " + name, function () {
89+
// android.util.ArrayMap reuses one entry object, so each entry is read before advancing.
90+
var iterator = maps[name]().entrySet().iterator();
91+
var entries = [];
92+
while (iterator.hasNext()) {
93+
var entry = iterator.next();
94+
entries.push(entry.getKey() + "=" + entry.getValue());
95+
}
96+
expect(entries.sort()).toEqual(["a=1", "b=2", "c=3"]);
97+
});
98+
});
99+
100+
it("iterates a java.util.ListIterator in both directions", function () {
101+
var iterator = fill(new java.util.ArrayList()).listIterator();
102+
var forward = [];
103+
while (iterator.hasNext()) {
104+
forward.push(String(iterator.next()));
105+
}
106+
var backward = [];
107+
while (iterator.hasPrevious()) {
108+
backward.push(String(iterator.previous()));
109+
}
110+
expect(forward).toEqual(["a", "b", "c"]);
111+
expect(backward).toEqual(["c", "b", "a"]);
112+
});
113+
114+
it("iterates java.util.List.of", function () {
115+
if (android.os.Build.VERSION.SDK_INT < 30) {
116+
pending("java.util.List.of needs API 30");
117+
return;
118+
}
119+
expect(drainToStrings(java.util.List.of("a", "b", "c").iterator())).toEqual(["a", "b", "c"]);
120+
});
121+
122+
it("iterates the keys of an org.json.JSONObject", function () {
123+
var json = new org.json.JSONObject('{"a": 1, "b": 2, "c": 3}');
124+
expect(drainToStrings(json.keys())).toEqual(["a", "b", "c"]);
125+
});
126+
127+
it("iterates the keys of an org.json.JSONObject built from a java.util.HashMap", function () {
128+
var json = new org.json.JSONObject(fillMap(new java.util.HashMap()));
129+
expect(json.length()).toBe(3);
130+
expect(drainToStrings(json.keys())).toEqual(["a", "b", "c"]);
131+
});
132+
133+
it("iterates the keySet of an android.os.Bundle", function () {
134+
var bundle = new android.os.Bundle();
135+
bundle.putString("a", "1");
136+
bundle.putString("b", "2");
137+
bundle.putString("c", "3");
138+
expect(drainToStrings(bundle.keySet().iterator())).toEqual(["a", "b", "c"]);
139+
});
140+
141+
it("enumerates a java.util.Vector with hasMoreElements/nextElement", function () {
142+
var enumeration = fill(new java.util.Vector()).elements();
143+
var items = [];
144+
while (enumeration.hasMoreElements()) {
145+
items.push(String(enumeration.nextElement()));
146+
}
147+
expect(items).toEqual(["a", "b", "c"]);
148+
});
149+
150+
it("iterates a Kotlin list and range", function () {
151+
var array = Array.create(java.lang.Object, 3);
152+
array[0] = "a";
153+
array[1] = "b";
154+
array[2] = "c";
155+
expect(drainToStrings(kotlin.collections.CollectionsKt.mutableListOf(array).iterator())).toEqual(["a", "b", "c"]);
156+
expect(drainToStrings(new kotlin.ranges.IntRange(1, 3).iterator())).toEqual(["1", "2", "3"]);
157+
});
158+
159+
var fixtures = {
160+
"a package-private iterator class": function () { return com.tns.tests.IteratorFixtures.packagePrivateIterator(); },
161+
"a private nested iterator class": function () { return com.tns.tests.IteratorFixtures.privateNestedIterator(); },
162+
"an anonymous iterator class": function () { return com.tns.tests.IteratorFixtures.anonymousIterator(); },
163+
"an iterator whose hasNext is inherited from a non-public base": function () { return com.tns.tests.IteratorFixtures.inheritedHasNextIterator(); },
164+
"the iterator of a lambda Iterable": function () { return com.tns.tests.IteratorFixtures.lambdaIterable().iterator(); }
165+
};
166+
167+
Object.keys(fixtures).forEach(function (name) {
168+
it("iterates " + name, function () {
169+
expect(drainToStrings(fixtures[name]())).toEqual(["a", "b", "c"]);
170+
});
171+
});
172+
173+
it("reads the elements of an array of a non-public class", function () {
174+
var array = com.tns.tests.IteratorFixtures.privateNestedIteratorArray();
175+
expect(array.length).toBe(2);
176+
expect(drainToStrings(array[0])).toEqual(["a", "b", "c"]);
177+
expect(drainToStrings(array[1])).toEqual(["a", "b", "c"]);
178+
});
179+
180+
it("iterates through a Proxy wrapping a Java iterator", function () {
181+
var iterator = new Proxy(fillMap(new java.util.HashMap()).keySet().iterator(), {});
182+
var items = [];
183+
while (iterator.hasNext()) {
184+
items.push(String(iterator.next()));
185+
}
186+
expect(items.sort()).toEqual(["a", "b", "c"]);
187+
});
188+
});
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package com.tns.tests;
2+
3+
import java.util.Iterator;
4+
import java.util.NoSuchElementException;
5+
6+
public class IteratorFixtures {
7+
private static final String[] ITEMS = {"a", "b", "c"};
8+
9+
public static Iterator<String> packagePrivateIterator() {
10+
return new PackagePrivateIterator(ITEMS);
11+
}
12+
13+
public static Iterator<String> privateNestedIterator() {
14+
return new PrivateNestedIterator(ITEMS);
15+
}
16+
17+
public static Iterator<String> anonymousIterator() {
18+
return new Iterator<String>() {
19+
private int index;
20+
21+
@Override
22+
public boolean hasNext() {
23+
return index < ITEMS.length;
24+
}
25+
26+
@Override
27+
public String next() {
28+
return ITEMS[index++];
29+
}
30+
};
31+
}
32+
33+
// hasNext() lives on a base class that does not implement Iterator, the shape
34+
// java.util.HashMap's iterators have.
35+
public static Iterator<String> inheritedHasNextIterator() {
36+
return new KeyCursor(ITEMS);
37+
}
38+
39+
public static Iterable<String> lambdaIterable() {
40+
return () -> new PackagePrivateIterator(ITEMS);
41+
}
42+
43+
public static Object[] privateNestedIteratorArray() {
44+
return new PrivateNestedIterator[] {
45+
new PrivateNestedIterator(ITEMS), new PrivateNestedIterator(ITEMS)
46+
};
47+
}
48+
49+
private static final class PrivateNestedIterator implements Iterator<String> {
50+
private final String[] items;
51+
private int index;
52+
53+
PrivateNestedIterator(String[] items) {
54+
this.items = items;
55+
}
56+
57+
@Override
58+
public boolean hasNext() {
59+
return index < items.length;
60+
}
61+
62+
@Override
63+
public String next() {
64+
if (index >= items.length) {
65+
throw new NoSuchElementException();
66+
}
67+
return items[index++];
68+
}
69+
}
70+
71+
private abstract static class Cursor {
72+
final String[] items;
73+
int index;
74+
75+
Cursor(String[] items) {
76+
this.items = items;
77+
}
78+
79+
public final boolean hasNext() {
80+
return index < items.length;
81+
}
82+
}
83+
84+
private static final class KeyCursor extends Cursor implements Iterator<String> {
85+
KeyCursor(String[] items) {
86+
super(items);
87+
}
88+
89+
@Override
90+
public String next() {
91+
return items[index++];
92+
}
93+
}
94+
}
95+
96+
class PackagePrivateIterator implements Iterator<String> {
97+
private final String[] items;
98+
private int index;
99+
100+
PackagePrivateIterator(String[] items) {
101+
this.items = items;
102+
}
103+
104+
@Override
105+
public boolean hasNext() {
106+
return index < items.length;
107+
}
108+
109+
@Override
110+
public String next() {
111+
return items[index++];
112+
}
113+
}

0 commit comments

Comments
 (0)