Skip to content

Commit

Permalink
Add various missing argument checks to dart:io natives.
Browse files Browse the repository at this point in the history
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/sdk#15274
Bug: dart-lang/sdk#23869
Change-Id: Ia6103e7ffceda897e475c1a3ba534c2f4b910475
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/110020
Commit-Queue: Ryan Macnak <[email protected]>
Reviewed-by: Siva Annamalai <[email protected]>
  • Loading branch information
rmacnak-google authored and [email protected] committed Jul 25, 2019
1 parent b716a4e commit ba72890
Show file tree
Hide file tree
Showing 16 changed files with 156 additions and 119 deletions.
4 changes: 4 additions & 0 deletions runtime/bin/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ static File* GetFile(Dart_NativeArguments args) {
Dart_Handle result = Dart_GetNativeInstanceField(
dart_this, kFileNativeFieldIndex, reinterpret_cast<intptr_t*>(&file));
ASSERT(!Dart_IsError(result));
if (file == NULL) {
Dart_PropagateError(Dart_NewUnhandledExceptionError(
DartUtils::NewInternalError("No native peer")));
}
return file;
}

Expand Down
6 changes: 5 additions & 1 deletion runtime/bin/namespace.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ Dart_Handle Namespace::GetNativeNamespaceArgument(Dart_NativeArguments args,
Namespace** namespc) {
Dart_Handle namespc_obj = Dart_GetNativeArgument(args, index);
if (Dart_IsError(namespc_obj)) {
Dart_PropagateError(namespc_obj);
return namespc_obj;
}
DEBUG_ASSERT(IsNamespace(namespc_obj));

Expand All @@ -129,6 +129,10 @@ Dart_Handle Namespace::GetNativeNamespaceArgument(Dart_NativeArguments args,
if (Dart_IsError(result)) {
return result;
}
if (*namespc == NULL) {
return Dart_NewUnhandledExceptionError(
DartUtils::NewInternalError("No native peer"));
}
return Dart_Null();
}

Expand Down
6 changes: 5 additions & 1 deletion runtime/bin/secure_socket_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,16 @@ const intptr_t SSLFilter::kApproximateSize =
sizeof(SSLFilter) + (2 * SSLFilter::kInternalBIOSize);

static SSLFilter* GetFilter(Dart_NativeArguments args) {
SSLFilter* filter;
SSLFilter* filter = NULL;
Dart_Handle dart_this = ThrowIfError(Dart_GetNativeArgument(args, 0));
ASSERT(Dart_IsInstance(dart_this));
ThrowIfError(Dart_GetNativeInstanceField(
dart_this, SSLFilter::kSSLFilterNativeFieldIndex,
reinterpret_cast<intptr_t*>(&filter)));
if (filter == NULL) {
Dart_PropagateError(Dart_NewUnhandledExceptionError(
DartUtils::NewInternalError("No native peer")));
}
return filter;
}

Expand Down
8 changes: 4 additions & 4 deletions runtime/bin/secure_socket_patch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ class SecureSocket {
@patch
class _SecureFilter {
@patch
factory _SecureFilter() => new _SecureFilterImpl();
factory _SecureFilter._() => new _SecureFilterImpl._();
}

@patch
@pragma("vm:entry-point")
class X509Certificate {
@patch
@pragma("vm:entry-point")
factory X509Certificate._() => new _X509CertificateImpl();
factory X509Certificate._() => new _X509CertificateImpl._();
}

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

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

Uint8List _cachedDer;
Uint8List get _der native "X509_Der";
Expand Down
8 changes: 8 additions & 0 deletions runtime/bin/security_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ SSLCertContext* SSLCertContext::GetSecurityContext(Dart_NativeArguments args) {
ThrowIfError(Dart_GetNativeInstanceField(
dart_this, SSLCertContext::kSecurityContextNativeFieldIndex,
reinterpret_cast<intptr_t*>(&context)));
if (context == NULL) {
Dart_PropagateError(Dart_NewUnhandledExceptionError(
DartUtils::NewInternalError("No native peer")));
}
return context;
}

Expand Down Expand Up @@ -644,6 +648,10 @@ static X509* GetX509Certificate(Dart_NativeArguments args) {
ThrowIfError(Dart_GetNativeInstanceField(
dart_this, SSLCertContext::kX509NativeFieldIndex,
reinterpret_cast<intptr_t*>(&certificate)));
if (certificate == NULL) {
Dart_PropagateError(Dart_NewUnhandledExceptionError(
DartUtils::NewInternalError("No native peer")));
}
return certificate;
}

Expand Down
4 changes: 4 additions & 0 deletions runtime/bin/socket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,10 @@ Socket* Socket::GetSocketIdNativeField(Dart_Handle socket_obj) {
Dart_PropagateError(err);
}
Socket* socket = reinterpret_cast<Socket*>(id);
if (socket == NULL) {
Dart_PropagateError(Dart_NewUnhandledExceptionError(
DartUtils::NewInternalError("No native peer")));
}
return socket;
}

Expand Down
4 changes: 4 additions & 0 deletions runtime/bin/sync_socket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,10 @@ Dart_Handle SynchronousSocket::GetSocketIdNativeField(
return result;
}
*socket = reinterpret_cast<SynchronousSocket*>(id);
if (*socket == NULL) {
Dart_PropagateError(Dart_NewUnhandledExceptionError(
DartUtils::NewInternalError("No native peer")));
}
return result;
}

Expand Down
42 changes: 21 additions & 21 deletions runtime/bin/sync_socket_patch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class _NativeSynchronousSocket extends _NativeSynchronousSocketNativeWrapper {
var address = it.current;
var socket = new _NativeSynchronousSocket();
socket.localAddress = address;
var result = socket.nativeCreateConnectSync(address._in_addr, port);
var result = socket._nativeCreateConnectSync(address._in_addr, port);
if (result is OSError) {
// Keep first error, if present.
if (error == null) {
Expand All @@ -117,7 +117,7 @@ class _NativeSynchronousSocket extends _NativeSynchronousSocketNativeWrapper {
}

InternetAddress get address => localAddress;
int get available => nativeAvailable();
int get available => _nativeAvailable();

int get port {
if (localPort != 0) {
Expand All @@ -126,7 +126,7 @@ class _NativeSynchronousSocket extends _NativeSynchronousSocketNativeWrapper {
if (isClosed) {
throw const SocketException.closed();
}
var result = nativeGetPort();
var result = _nativeGetPort();
if (result is OSError) {
throw result;
}
Expand All @@ -137,7 +137,7 @@ class _NativeSynchronousSocket extends _NativeSynchronousSocketNativeWrapper {
if (isClosed) {
throw const SocketException.closed();
}
var result = nativeGetRemotePeer();
var result = _nativeGetRemotePeer();
if (result is OSError) {
throw result;
}
Expand All @@ -149,7 +149,7 @@ class _NativeSynchronousSocket extends _NativeSynchronousSocketNativeWrapper {
if (isClosed) {
throw const SocketException.closed();
}
var result = nativeGetRemotePeer();
var result = _nativeGetRemotePeer();
if (result is OSError) {
throw result;
}
Expand All @@ -158,7 +158,7 @@ class _NativeSynchronousSocket extends _NativeSynchronousSocketNativeWrapper {

void closeSync() {
if (!isClosed) {
nativeCloseSync();
_nativeCloseSync();
_SocketResourceInfo.SocketClosed(resourceInfo);
isClosed = true;
}
Expand Down Expand Up @@ -209,7 +209,7 @@ class _NativeSynchronousSocket extends _NativeSynchronousSocketNativeWrapper {
if (end == start) {
return 0;
}
var result = nativeReadInto(buffer, start, (end - start));
var result = _nativeReadInto(buffer, start, (end - start));
if (result is OSError) {
throw new SocketException("readIntoSync failed", osError: result);
}
Expand All @@ -229,7 +229,7 @@ class _NativeSynchronousSocket extends _NativeSynchronousSocketNativeWrapper {
if (len == 0) {
return null;
}
var result = nativeRead(len);
var result = _nativeRead(len);
if (result is OSError) {
throw result;
}
Expand Down Expand Up @@ -275,7 +275,7 @@ class _NativeSynchronousSocket extends _NativeSynchronousSocketNativeWrapper {
if (isClosedWrite) {
closeSync();
} else {
nativeShutdownRead();
_nativeShutdownRead();
}
isClosedRead = true;
}
Expand All @@ -287,7 +287,7 @@ class _NativeSynchronousSocket extends _NativeSynchronousSocketNativeWrapper {
if (isClosedRead) {
closeSync();
} else {
nativeShutdownWrite();
_nativeShutdownWrite();
}
isClosedWrite = true;
}
Expand All @@ -313,7 +313,7 @@ class _NativeSynchronousSocket extends _NativeSynchronousSocketNativeWrapper {

_BufferAndStart bufferAndStart =
_ensureFastAndSerializableByteData(buffer, start, end);
var result = nativeWrite(bufferAndStart.buffer, bufferAndStart.start,
var result = _nativeWrite(bufferAndStart.buffer, bufferAndStart.start,
end - (start - bufferAndStart.start));
if (result is OSError) {
throw new SocketException("writeFromSync failed", osError: result);
Expand All @@ -333,17 +333,17 @@ class _NativeSynchronousSocket extends _NativeSynchronousSocketNativeWrapper {
// Native method declarations.
static _nativeLookupRequest(host, int type)
native "SynchronousSocket_LookupRequest";
nativeCreateConnectSync(host, int port)
_nativeCreateConnectSync(host, int port)
native "SynchronousSocket_CreateConnectSync";
nativeAvailable() native "SynchronousSocket_Available";
nativeCloseSync() native "SynchronousSocket_CloseSync";
int nativeGetPort() native "SynchronousSocket_GetPort";
List nativeGetRemotePeer() native "SynchronousSocket_GetRemotePeer";
nativeRead(int len) native "SynchronousSocket_Read";
nativeReadInto(List<int> buffer, int offset, int bytes)
_nativeAvailable() native "SynchronousSocket_Available";
_nativeCloseSync() native "SynchronousSocket_CloseSync";
int _nativeGetPort() native "SynchronousSocket_GetPort";
List _nativeGetRemotePeer() native "SynchronousSocket_GetRemotePeer";
_nativeRead(int len) native "SynchronousSocket_Read";
_nativeReadInto(List<int> buffer, int offset, int bytes)
native "SynchronousSocket_ReadList";
nativeShutdownRead() native "SynchronousSocket_ShutdownRead";
nativeShutdownWrite() native "SynchronousSocket_ShutdownWrite";
nativeWrite(List<int> buffer, int offset, int bytes)
_nativeShutdownRead() native "SynchronousSocket_ShutdownRead";
_nativeShutdownWrite() native "SynchronousSocket_ShutdownWrite";
_nativeWrite(List<int> buffer, int offset, int bytes)
native "SynchronousSocket_WriteList";
}
Loading

0 comments on commit ba72890

Please sign in to comment.