Skip to content

Commit f005bd5

Browse files
rmacnak-googlecommit-bot@chromium.org
authored andcommitted
[vm] Harden access to unchecked natives.
Bug: #37234 Change-Id: I567b3fa177e89db50345e174a07c98b10c53f102 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/105721 Commit-Queue: Ryan Macnak <[email protected]> Reviewed-by: Régis Crelier <[email protected]> Reviewed-by: Siva Annamalai <[email protected]>
1 parent fa28d5b commit f005bd5

File tree

9 files changed

+196
-83
lines changed

9 files changed

+196
-83
lines changed

runtime/lib/function.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
@pragma("vm:entry-point")
88
class _Closure implements Function {
9+
factory _Closure._uninstantiable() {
10+
throw "Unreachable";
11+
}
12+
913
bool operator ==(Object other) native "Closure_equals";
1014

1115
int get hashCode {

runtime/lib/integers.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,8 +480,9 @@ abstract class _IntegerImplementation implements int {
480480
@pragma("vm:entry-point")
481481
class _Smi extends _IntegerImplementation {
482482
factory _Smi._uninstantiable() {
483-
throw new UnsupportedError("_Smi can only be allocated by the VM");
483+
throw "Unreachable";
484484
}
485+
485486
int get hashCode => this;
486487
int get _identityHashCode => this;
487488
@pragma("vm:exact-result-type", "dart:core#_Smi")
@@ -682,8 +683,9 @@ class _Smi extends _IntegerImplementation {
682683
@pragma("vm:entry-point")
683684
class _Mint extends _IntegerImplementation {
684685
factory _Mint._uninstantiable() {
685-
throw new UnsupportedError("_Mint can only be allocated by the VM");
686+
throw "Unreachable";
686687
}
688+
687689
int get hashCode => this;
688690
int get _identityHashCode => this;
689691
@pragma("vm:non-nullable-result-type")

runtime/lib/lib_prefix.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
// This type corresponds to the VM-internal class LibraryPrefix.
88
@pragma("vm:entry-point")
99
class _LibraryPrefix {
10+
factory _LibraryPrefix._uninstantiable() {
11+
throw "Unreachable";
12+
}
13+
1014
bool _load() native "LibraryPrefix_load";
1115
Object _loadError() native "LibraryPrefix_loadError";
1216
bool isLoaded() native "LibraryPrefix_isLoaded";

runtime/lib/mirror_reference.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
@pragma("vm:entry-point")
88
class _MirrorReference {
99
factory _MirrorReference._uninstantiable() {
10-
throw new UnsupportedError("class _MirrorReference cannot be instantiated");
10+
throw "Unreachable";
1111
}
1212

1313
bool operator ==(other) native "MirrorReference_equals";

runtime/lib/regexp_patch.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ class _RegExpHashValue {
148148
}
149149

150150
class _RegExpMatch implements RegExpMatch {
151-
_RegExpMatch(this._regexp, this.input, this._match);
151+
_RegExpMatch._(this._regexp, this.input, this._match);
152152

153153
int get start => _start(0);
154154
int get end => _end(0);
@@ -222,7 +222,7 @@ class _RegExp implements RegExp {
222222
if (match == null) {
223223
return null;
224224
}
225-
return new _RegExpMatch(this, str, match);
225+
return new _RegExpMatch._(this, str, match);
226226
}
227227

228228
Iterable<RegExpMatch> allMatches(String string, [int start = 0]) {
@@ -242,7 +242,7 @@ class _RegExp implements RegExp {
242242
}
243243
List<int> list = _ExecuteMatchSticky(string, start);
244244
if (list == null) return null;
245-
return new _RegExpMatch(this, string, list);
245+
return new _RegExpMatch._(this, string, list);
246246
}
247247

248248
bool hasMatch(String str) {
@@ -379,7 +379,7 @@ class _AllMatchesIterator implements Iterator<RegExpMatch> {
379379
if (_nextIndex <= _str.length) {
380380
var match = _re._ExecuteMatch(_str, _nextIndex);
381381
if (match != null) {
382-
_current = new _RegExpMatch(_re, _str, match);
382+
_current = new _RegExpMatch._(_re, _str, match);
383383
_nextIndex = _current.end;
384384
if (_nextIndex == _current.start) {
385385
// Zero-width match. Advance by one more, unless the regexp

runtime/lib/string_patch.dart

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -945,10 +945,7 @@ abstract class _StringBase implements String {
945945

946946
@pragma("vm:entry-point")
947947
class _OneByteString extends _StringBase {
948-
factory _OneByteString._uninstantiable() {
949-
throw new UnsupportedError(
950-
"_OneByteString can only be allocated by the VM");
951-
}
948+
factory _OneByteString._uninstantiable() { throw "Unreachable"; }
952949

953950
@pragma("vm:exact-result-type", "dart:core#_Smi")
954951
int get hashCode native "String_getHashCode";
@@ -1254,10 +1251,7 @@ class _OneByteString extends _StringBase {
12541251

12551252
@pragma("vm:entry-point")
12561253
class _TwoByteString extends _StringBase {
1257-
factory _TwoByteString._uninstantiable() {
1258-
throw new UnsupportedError(
1259-
"_TwoByteString can only be allocated by the VM");
1260-
}
1254+
factory _TwoByteString._uninstantiable() { throw "Unreachable"; }
12611255

12621256
static String _allocateFromTwoByteList(List<int> list, int start, int end)
12631257
native "TwoByteString_allocateFromTwoByteList";
@@ -1277,10 +1271,7 @@ class _TwoByteString extends _StringBase {
12771271

12781272
@pragma("vm:entry-point")
12791273
class _ExternalOneByteString extends _StringBase {
1280-
factory _ExternalOneByteString._uninstantiable() {
1281-
throw new UnsupportedError(
1282-
"_ExternalOneByteString can only be allocated by the VM");
1283-
}
1274+
factory _ExternalOneByteString._uninstantiable() { throw "Unreachable"; }
12841275

12851276
bool _isWhitespace(int codeUnit) {
12861277
return _StringBase._isOneByteWhitespace(codeUnit);
@@ -1296,10 +1287,7 @@ class _ExternalOneByteString extends _StringBase {
12961287

12971288
@pragma("vm:entry-point")
12981289
class _ExternalTwoByteString extends _StringBase {
1299-
factory _ExternalTwoByteString._uninstantiable() {
1300-
throw new UnsupportedError(
1301-
"_ExternalTwoByteString can only be allocated by the VM");
1302-
}
1290+
factory _ExternalTwoByteString._uninstantiable() { throw "Unreachable"; }
13031291

13041292
bool _isWhitespace(int codeUnit) {
13051293
return _StringBase._isTwoByteWhitespace(codeUnit);

runtime/lib/type_patch.dart

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,26 @@ abstract class _AbstractType implements Type {
1414
// Equivalent of RawType.
1515
@pragma("vm:entry-point")
1616
class _Type extends _AbstractType {
17+
factory _Type._uninstantiable() {
18+
throw "Unreachable";
19+
}
20+
1721
@pragma("vm:exact-result-type", "dart:core#_Smi")
1822
int get hashCode native "Type_getHashCode";
1923
}
2024

2125
// Equivalent of RawTypeRef.
2226
@pragma("vm:entry-point")
23-
class _TypeRef extends _AbstractType {}
27+
class _TypeRef extends _AbstractType {
28+
factory _TypeRef._uninstantiable() {
29+
throw "Unreachable";
30+
}
31+
}
2432

2533
// Equivalent of RawTypeParameter.
2634
@pragma("vm:entry-point")
27-
class _TypeParameter extends _AbstractType {}
35+
class _TypeParameter extends _AbstractType {
36+
factory _TypeParameter._uninstantiable() {
37+
throw "Unreachable";
38+
}
39+
}

0 commit comments

Comments
 (0)