[vm/ffi] Fix CFE crash on missing Array sizes

Closes: https://github.com/dart-lang/sdk/issues/46085

TEST=tests/ffi/regress_46085_test.dart

Change-Id: I04e05baccb6eb490ef32702677e3d5c1bb815560
Cq-Include-Trybots: luci.dart.try:vm-kernel-nnbd-linux-debug-x64-try
Fixed: 46085
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/201264
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Clement Skau <cskau@google.com>
This commit is contained in:
Daco Harkes 2021-05-26 11:02:22 +00:00 committed by commit-bot@chromium.org
parent e817b2d05a
commit 1d38c98b69
3 changed files with 50 additions and 5 deletions

View file

@ -568,8 +568,13 @@ class _FfiDefinitionTransformer extends FfiTransformer {
final sizeAnnotations = _getArraySizeAnnotations(m).toList();
if (sizeAnnotations.length == 1) {
final arrayDimensions = sizeAnnotations.single;
type = NativeTypeCfe(this, dartType,
compoundCache: compoundCache, arrayDimensions: arrayDimensions);
arrayDimensions.length;
if (this.arrayDimensions(dartType) == arrayDimensions.length) {
type = NativeTypeCfe(this, dartType,
compoundCache: compoundCache, arrayDimensions: arrayDimensions);
} else {
type = InvalidNativeTypeCfe("Invalid array dimensions.");
}
}
} else if (isPointerType(dartType) || isCompoundSubtype(dartType)) {
type = NativeTypeCfe(this, dartType, compoundCache: compoundCache);
@ -888,7 +893,6 @@ class _FfiDefinitionTransformer extends FfiTransformer {
.whereType<IntConstant>()
.map((e) => e.value)
.toList();
assert(result.length > 0);
return result;
}
}
@ -982,12 +986,15 @@ abstract class NativeTypeCfe {
if (compoundCache.containsKey(clazz)) {
return compoundCache[clazz];
} else {
throw "$clazz not found in compoundCache";
throw "Class '$clazz' not found in compoundCache.";
}
}
if (transformer.isArrayType(dartType)) {
if (arrayDimensions == null) {
throw "Must have array dimensions for ArrayType";
throw "Must have array dimensions for ArrayType.";
}
if (arrayDimensions.length == 0) {
throw "Must have a size for this array dimension.";
}
final elementType = transformer.arraySingleElementType(dartType);
final elementCfeType =

View file

@ -0,0 +1,19 @@
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "dart:ffi";
class MyStruct extends Struct {
external Pointer<Int8> notEmpty;
@Array.multi([]) //# 01: compile-time error
external Array<Int16> a0; //# 01: compile-time error
@Array.multi([1]) //# 02: compile-time error
external Array<Array<Int16>> a1; //# 02: compile-time error
}
void main() {
MyStruct? ms = null;
}

View file

@ -0,0 +1,19 @@
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "dart:ffi";
class MyStruct extends Struct {
Pointer<Int8> notEmpty;
@Array.multi([]) //# 01: compile-time error
Array<Int16> a0; //# 01: compile-time error
@Array.multi([1]) //# 02: compile-time error
Array<Array<Int16>> a1; //# 02: compile-time error
}
void main() {
MyStruct ms = null;
}