Skip to content

Commit d08c203

Browse files
rmacnak-googlecommit-bot@chromium.org
authored andcommitted
Add various missing argument checks to dart:io natives.
Avoid reflective access to mirrors creating invalid reflectees. Change type checks in embedding API functions to return UnhandledExceptionErrors instead of APIErrors so they are catchable by Dart. Fixes invocation_fuzz_test. Bug: dart-lang#15274 Bug: dart-lang#23869 Bug: dart-lang#37680 Change-Id: Ife3e3cb894c59620b0318e4e08947a3d1d45bab9 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/110620 Commit-Queue: Ryan Macnak <[email protected]> Reviewed-by: Siva Annamalai <[email protected]>
1 parent 650f32c commit d08c203

24 files changed

+210
-172
lines changed

runtime/bin/file.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ static File* GetFile(Dart_NativeArguments args) {
4242
Dart_Handle result = Dart_GetNativeInstanceField(
4343
dart_this, kFileNativeFieldIndex, reinterpret_cast<intptr_t*>(&file));
4444
ASSERT(!Dart_IsError(result));
45+
if (file == NULL) {
46+
Dart_PropagateError(Dart_NewUnhandledExceptionError(
47+
DartUtils::NewInternalError("No native peer")));
48+
}
4549
return file;
4650
}
4751

runtime/bin/namespace.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ Dart_Handle Namespace::GetNativeNamespaceArgument(Dart_NativeArguments args,
119119
Namespace** namespc) {
120120
Dart_Handle namespc_obj = Dart_GetNativeArgument(args, index);
121121
if (Dart_IsError(namespc_obj)) {
122-
Dart_PropagateError(namespc_obj);
122+
return namespc_obj;
123123
}
124124
DEBUG_ASSERT(IsNamespace(namespc_obj));
125125

@@ -129,6 +129,10 @@ Dart_Handle Namespace::GetNativeNamespaceArgument(Dart_NativeArguments args,
129129
if (Dart_IsError(result)) {
130130
return result;
131131
}
132+
if (*namespc == NULL) {
133+
return Dart_NewUnhandledExceptionError(
134+
DartUtils::NewInternalError("No native peer"));
135+
}
132136
return Dart_Null();
133137
}
134138

runtime/bin/secure_socket_filter.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,16 @@ const intptr_t SSLFilter::kApproximateSize =
3838
sizeof(SSLFilter) + (2 * SSLFilter::kInternalBIOSize);
3939

4040
static SSLFilter* GetFilter(Dart_NativeArguments args) {
41-
SSLFilter* filter;
41+
SSLFilter* filter = NULL;
4242
Dart_Handle dart_this = ThrowIfError(Dart_GetNativeArgument(args, 0));
4343
ASSERT(Dart_IsInstance(dart_this));
4444
ThrowIfError(Dart_GetNativeInstanceField(
4545
dart_this, SSLFilter::kSSLFilterNativeFieldIndex,
4646
reinterpret_cast<intptr_t*>(&filter)));
47+
if (filter == NULL) {
48+
Dart_PropagateError(Dart_NewUnhandledExceptionError(
49+
DartUtils::NewInternalError("No native peer")));
50+
}
4751
return filter;
4852
}
4953

runtime/bin/secure_socket_patch.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ class SecureSocket {
1414
@patch
1515
class _SecureFilter {
1616
@patch
17-
factory _SecureFilter() => new _SecureFilterImpl();
17+
factory _SecureFilter._() => new _SecureFilterImpl._();
1818
}
1919

2020
@patch
2121
@pragma("vm:entry-point")
2222
class X509Certificate {
2323
@patch
2424
@pragma("vm:entry-point")
25-
factory X509Certificate._() => new _X509CertificateImpl();
25+
factory X509Certificate._() => new _X509CertificateImpl._();
2626
}
2727

2828
class _SecureSocket extends _Socket implements SecureSocket {
@@ -75,7 +75,7 @@ class _SecureFilterImpl extends NativeFieldWrapperClass1
7575
@pragma("vm:entry-point")
7676
static final int ENCRYPTED_SIZE = 10 * 1024;
7777

78-
_SecureFilterImpl() {
78+
_SecureFilterImpl._() {
7979
buffers = new List<_ExternalBuffer>(_RawSecureSocket.bufferCount);
8080
for (int i = 0; i < _RawSecureSocket.bufferCount; ++i) {
8181
buffers[i] = new _ExternalBuffer(
@@ -206,7 +206,7 @@ class _X509CertificateImpl extends NativeFieldWrapperClass1
206206
implements X509Certificate {
207207
// The native field must be set manually on a new object, in native code.
208208
// This is done by WrappedX509 in secure_socket.cc.
209-
_X509CertificateImpl();
209+
_X509CertificateImpl._();
210210

211211
Uint8List _cachedDer;
212212
Uint8List get _der native "X509_Der";

runtime/bin/security_context.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ SSLCertContext* SSLCertContext::GetSecurityContext(Dart_NativeArguments args) {
8686
ThrowIfError(Dart_GetNativeInstanceField(
8787
dart_this, SSLCertContext::kSecurityContextNativeFieldIndex,
8888
reinterpret_cast<intptr_t*>(&context)));
89+
if (context == NULL) {
90+
Dart_PropagateError(Dart_NewUnhandledExceptionError(
91+
DartUtils::NewInternalError("No native peer")));
92+
}
8993
return context;
9094
}
9195

@@ -644,6 +648,10 @@ static X509* GetX509Certificate(Dart_NativeArguments args) {
644648
ThrowIfError(Dart_GetNativeInstanceField(
645649
dart_this, SSLCertContext::kX509NativeFieldIndex,
646650
reinterpret_cast<intptr_t*>(&certificate)));
651+
if (certificate == NULL) {
652+
Dart_PropagateError(Dart_NewUnhandledExceptionError(
653+
DartUtils::NewInternalError("No native peer")));
654+
}
647655
return certificate;
648656
}
649657

runtime/bin/socket.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,10 @@ Socket* Socket::GetSocketIdNativeField(Dart_Handle socket_obj) {
11061106
Dart_PropagateError(err);
11071107
}
11081108
Socket* socket = reinterpret_cast<Socket*>(id);
1109+
if (socket == NULL) {
1110+
Dart_PropagateError(Dart_NewUnhandledExceptionError(
1111+
DartUtils::NewInternalError("No native peer")));
1112+
}
11091113
return socket;
11101114
}
11111115

runtime/bin/sync_socket.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,10 @@ Dart_Handle SynchronousSocket::GetSocketIdNativeField(
351351
return result;
352352
}
353353
*socket = reinterpret_cast<SynchronousSocket*>(id);
354+
if (*socket == NULL) {
355+
Dart_PropagateError(Dart_NewUnhandledExceptionError(
356+
DartUtils::NewInternalError("No native peer")));
357+
}
354358
return result;
355359
}
356360

runtime/bin/sync_socket_patch.dart

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class _NativeSynchronousSocket extends _NativeSynchronousSocketNativeWrapper {
9191
var address = it.current;
9292
var socket = new _NativeSynchronousSocket();
9393
socket.localAddress = address;
94-
var result = socket.nativeCreateConnectSync(address._in_addr, port);
94+
var result = socket._nativeCreateConnectSync(address._in_addr, port);
9595
if (result is OSError) {
9696
// Keep first error, if present.
9797
if (error == null) {
@@ -117,7 +117,7 @@ class _NativeSynchronousSocket extends _NativeSynchronousSocketNativeWrapper {
117117
}
118118

119119
InternetAddress get address => localAddress;
120-
int get available => nativeAvailable();
120+
int get available => _nativeAvailable();
121121

122122
int get port {
123123
if (localPort != 0) {
@@ -126,7 +126,7 @@ class _NativeSynchronousSocket extends _NativeSynchronousSocketNativeWrapper {
126126
if (isClosed) {
127127
throw const SocketException.closed();
128128
}
129-
var result = nativeGetPort();
129+
var result = _nativeGetPort();
130130
if (result is OSError) {
131131
throw result;
132132
}
@@ -137,7 +137,7 @@ class _NativeSynchronousSocket extends _NativeSynchronousSocketNativeWrapper {
137137
if (isClosed) {
138138
throw const SocketException.closed();
139139
}
140-
var result = nativeGetRemotePeer();
140+
var result = _nativeGetRemotePeer();
141141
if (result is OSError) {
142142
throw result;
143143
}
@@ -149,7 +149,7 @@ class _NativeSynchronousSocket extends _NativeSynchronousSocketNativeWrapper {
149149
if (isClosed) {
150150
throw const SocketException.closed();
151151
}
152-
var result = nativeGetRemotePeer();
152+
var result = _nativeGetRemotePeer();
153153
if (result is OSError) {
154154
throw result;
155155
}
@@ -158,7 +158,7 @@ class _NativeSynchronousSocket extends _NativeSynchronousSocketNativeWrapper {
158158

159159
void closeSync() {
160160
if (!isClosed) {
161-
nativeCloseSync();
161+
_nativeCloseSync();
162162
_SocketResourceInfo.SocketClosed(resourceInfo);
163163
isClosed = true;
164164
}
@@ -209,7 +209,7 @@ class _NativeSynchronousSocket extends _NativeSynchronousSocketNativeWrapper {
209209
if (end == start) {
210210
return 0;
211211
}
212-
var result = nativeReadInto(buffer, start, (end - start));
212+
var result = _nativeReadInto(buffer, start, (end - start));
213213
if (result is OSError) {
214214
throw new SocketException("readIntoSync failed", osError: result);
215215
}
@@ -229,7 +229,7 @@ class _NativeSynchronousSocket extends _NativeSynchronousSocketNativeWrapper {
229229
if (len == 0) {
230230
return null;
231231
}
232-
var result = nativeRead(len);
232+
var result = _nativeRead(len);
233233
if (result is OSError) {
234234
throw result;
235235
}
@@ -275,7 +275,7 @@ class _NativeSynchronousSocket extends _NativeSynchronousSocketNativeWrapper {
275275
if (isClosedWrite) {
276276
closeSync();
277277
} else {
278-
nativeShutdownRead();
278+
_nativeShutdownRead();
279279
}
280280
isClosedRead = true;
281281
}
@@ -287,7 +287,7 @@ class _NativeSynchronousSocket extends _NativeSynchronousSocketNativeWrapper {
287287
if (isClosedRead) {
288288
closeSync();
289289
} else {
290-
nativeShutdownWrite();
290+
_nativeShutdownWrite();
291291
}
292292
isClosedWrite = true;
293293
}
@@ -313,7 +313,7 @@ class _NativeSynchronousSocket extends _NativeSynchronousSocketNativeWrapper {
313313

314314
_BufferAndStart bufferAndStart =
315315
_ensureFastAndSerializableByteData(buffer, start, end);
316-
var result = nativeWrite(bufferAndStart.buffer, bufferAndStart.start,
316+
var result = _nativeWrite(bufferAndStart.buffer, bufferAndStart.start,
317317
end - (start - bufferAndStart.start));
318318
if (result is OSError) {
319319
throw new SocketException("writeFromSync failed", osError: result);
@@ -333,17 +333,17 @@ class _NativeSynchronousSocket extends _NativeSynchronousSocketNativeWrapper {
333333
// Native method declarations.
334334
static _nativeLookupRequest(host, int type)
335335
native "SynchronousSocket_LookupRequest";
336-
nativeCreateConnectSync(host, int port)
336+
_nativeCreateConnectSync(host, int port)
337337
native "SynchronousSocket_CreateConnectSync";
338-
nativeAvailable() native "SynchronousSocket_Available";
339-
nativeCloseSync() native "SynchronousSocket_CloseSync";
340-
int nativeGetPort() native "SynchronousSocket_GetPort";
341-
List nativeGetRemotePeer() native "SynchronousSocket_GetRemotePeer";
342-
nativeRead(int len) native "SynchronousSocket_Read";
343-
nativeReadInto(List<int> buffer, int offset, int bytes)
338+
_nativeAvailable() native "SynchronousSocket_Available";
339+
_nativeCloseSync() native "SynchronousSocket_CloseSync";
340+
int _nativeGetPort() native "SynchronousSocket_GetPort";
341+
List _nativeGetRemotePeer() native "SynchronousSocket_GetRemotePeer";
342+
_nativeRead(int len) native "SynchronousSocket_Read";
343+
_nativeReadInto(List<int> buffer, int offset, int bytes)
344344
native "SynchronousSocket_ReadList";
345-
nativeShutdownRead() native "SynchronousSocket_ShutdownRead";
346-
nativeShutdownWrite() native "SynchronousSocket_ShutdownWrite";
347-
nativeWrite(List<int> buffer, int offset, int bytes)
345+
_nativeShutdownRead() native "SynchronousSocket_ShutdownRead";
346+
_nativeShutdownWrite() native "SynchronousSocket_ShutdownWrite";
347+
_nativeWrite(List<int> buffer, int offset, int bytes)
348348
native "SynchronousSocket_WriteList";
349349
}

runtime/lib/array.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class _List<E> extends FixedLengthListBase<E> {
9696
end = RangeError.checkValidRange(start, end, this.length);
9797
int length = end - start;
9898
if (length == 0) return <E>[];
99-
var result = new _GrowableList<E>.withData(_slice(start, length, false));
99+
var result = new _GrowableList<E>._withData(_slice(start, length, false));
100100
result._setLength(length);
101101
return result;
102102
}
@@ -137,11 +137,11 @@ class _List<E> extends FixedLengthListBase<E> {
137137
if (length > 0) {
138138
var result = _slice(0, length, !growable);
139139
if (growable) {
140-
result = new _GrowableList<E>.withData(result).._setLength(length);
140+
result = new _GrowableList<E>._withData(result).._setLength(length);
141141
}
142142
return result;
143143
}
144-
// _GrowableList.withData must not be called with empty list.
144+
// _GrowableList._withData must not be called with empty list.
145145
return growable ? <E>[] : new List<E>(0);
146146
}
147147
}
@@ -177,7 +177,7 @@ class _ImmutableList<E> extends UnmodifiableListBase<E> {
177177
for (int i = 0; i < length; i++) {
178178
list[i] = this[start + i];
179179
}
180-
var result = new _GrowableList<E>.withData(list);
180+
var result = new _GrowableList<E>._withData(list);
181181
result._setLength(length);
182182
return result;
183183
}
@@ -221,7 +221,7 @@ class _ImmutableList<E> extends UnmodifiableListBase<E> {
221221
list[i] = this[i];
222222
}
223223
if (!growable) return list;
224-
var result = new _GrowableList<E>.withData(list);
224+
var result = new _GrowableList<E>._withData(list);
225225
result._setLength(length);
226226
return result;
227227
}

runtime/lib/array_patch.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class List<E> {
7070
if (elements.isEmpty) {
7171
return new _GrowableList<E>(0);
7272
}
73-
var result = new _GrowableList<E>.withData(elements);
73+
var result = new _GrowableList<E>._withData(elements);
7474
result._setLength(elements.length);
7575
return result;
7676
}

0 commit comments

Comments
 (0)