Skip to content

Commit

Permalink
[cfe/ffi] Fix division by 0 on @Packed(0)
Browse files Browse the repository at this point in the history
Fixes: #45838

TEST=tests/ffi/vmspecific_static_checks_test.dart

(Looks like `dart format` also slightly changed. Committing such that
the files are in line with the formatter again.)

Change-Id: Iefd8af8c38a7490175b2e25b46cedbf85f15f17d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/197340
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Tess Strickland <sstrickl@google.com>
  • Loading branch information
dcharkes authored and commit-bot@chromium.org committed Apr 29, 2021
1 parent 427e17d commit 79331c7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
22 changes: 11 additions & 11 deletions pkg/vm/lib/transformations/ffi_definitions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,8 @@ class _FfiDefinitionTransformer extends FfiTransformer {
}
for (var dimension in dimensions) {
if (dimension < 0) {
diagnosticReporter.report(
messageNonPositiveArrayDimensions, f.fileOffset,
f.name.text.length,
f.fileUri);
diagnosticReporter.report(messageNonPositiveArrayDimensions,
f.fileOffset, f.name.text.length, f.fileUri);
success = false;
}
}
Expand Down Expand Up @@ -489,8 +487,8 @@ 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);
type = NativeTypeCfe(this, dartType,
compoundCache: compoundCache, arrayDimensions: arrayDimensions);
}
} else if (isPointerType(dartType) || isCompoundSubtype(dartType)) {
type = NativeTypeCfe(this, dartType, compoundCache: compoundCache);
Expand Down Expand Up @@ -760,7 +758,7 @@ class CompoundLayout {
abstract class NativeTypeCfe {
factory NativeTypeCfe(FfiTransformer transformer, DartType dartType,
{List<int> arrayDimensions,
Map<Class, CompoundNativeTypeCfe> compoundCache = const {}}) {
Map<Class, CompoundNativeTypeCfe> compoundCache = const {}}) {
if (transformer.isPrimitiveType(dartType)) {
final clazz = (dartType as InterfaceType).classNode;
final nativeType = transformer.getType(clazz);
Expand All @@ -783,7 +781,7 @@ abstract class NativeTypeCfe {
}
final elementType = transformer.arraySingleElementType(dartType);
final elementCfeType =
NativeTypeCfe(transformer, elementType, compoundCache: compoundCache);
NativeTypeCfe(transformer, elementType, compoundCache: compoundCache);
return ArrayNativeTypeCfe.multi(elementCfeType, arrayDimensions);
}
throw "Invalid type $dartType";
Expand Down Expand Up @@ -1102,7 +1100,9 @@ class StructNativeTypeCfe extends CompoundNativeTypeCfe {
if (packing != null && packing < alignment) {
alignment = packing;
}
offset = _alignOffset(offset, alignment);
if (alignment > 0) {
offset = _alignOffset(offset, alignment);
}
offsets.add(offset);
offset += size;
structAlignment = math.max(structAlignment, alignment);
Expand Down Expand Up @@ -1146,8 +1146,8 @@ class ArrayNativeTypeCfe implements NativeTypeCfe {

ArrayNativeTypeCfe(this.elementType, this.length);

factory ArrayNativeTypeCfe.multi(NativeTypeCfe elementType,
List<int> dimensions) {
factory ArrayNativeTypeCfe.multi(
NativeTypeCfe elementType, List<int> dimensions) {
if (dimensions.length == 1) {
return ArrayNativeTypeCfe(elementType, dimensions.single);
}
Expand Down
7 changes: 6 additions & 1 deletion tests/ffi/vmspecific_static_checks_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,11 @@ class TestStruct1606Packed extends Struct {
nestedLooselyPacked; //# 1606: compile-time error
}

@Packed(0) //# 1607: compile-time error
class TestStruct1607 extends Struct {
external Pointer<Uint8> notEmpty;
}

class TestStruct1800 extends Struct {
external Pointer<Uint8> notEmpty;

Expand All @@ -780,4 +785,4 @@ class TestStruct1802 extends Struct {

@Array.multi([2, 2, 2, 2, 2, 2, -1]) //# 1802: compile-time error
external Array<Uint8> inlineArray; //# 1802: compile-time error
}
}
7 changes: 6 additions & 1 deletion tests/ffi_2/vmspecific_static_checks_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,11 @@ class TestStruct1606Packed extends Struct {
Array<TestStruct1604> nestedLooselyPacked; //# 1606: compile-time error
}

@Packed(0) //# 1607: compile-time error
class TestStruct1607 extends Struct {
Pointer<Uint8> notEmpty;
}

class TestStruct1800 extends Struct {
Pointer<Uint8> notEmpty;

Expand All @@ -779,4 +784,4 @@ class TestStruct1802 extends Struct {

@Array.multi([2, 2, 2, 2, 2, 2, -1]) //# 1802: compile-time error
Array<Uint8> inlineArray; //# 1802: compile-time error
}
}

0 comments on commit 79331c7

Please sign in to comment.