-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue: #36247 Change-Id: I81f4cb1d34bc25efe6bc4c2d14615356ac52262f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/106906 Commit-Queue: Daco Harkes <dacoharkes@google.com> Auto-Submit: Daco Harkes <dacoharkes@google.com> Reviewed-by: Martin Kustermann <kustermann@google.com>
- Loading branch information
Showing
1 changed file
with
218 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
// Copyright (c) 2019, 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. | ||
// | ||
// Dart test programing for testing that optimizations do wrongly assume loads | ||
// from and stores to C memory are not aliased. | ||
// | ||
// SharedObjects=ffi_test_functions | ||
// VMOptions=--deterministic --optimization-counter-threshold=50 --enable-inlining-annotations | ||
|
||
library FfiTest; | ||
|
||
import 'dart:ffi'; | ||
|
||
import "package:expect/expect.dart"; | ||
|
||
import 'dylib_utils.dart'; | ||
|
||
void main() { | ||
for (int i = 0; i < 100; ++i) { | ||
testNonAlias(); | ||
testAliasCast(); | ||
testAliasCast2(); | ||
testAliasOffsetBy(); | ||
testAliasOffsetBy2(); | ||
testAliasElementAt(); | ||
testAliasElementAt2(); | ||
testAliasFromAddress(); | ||
testAliasFromAddress2(); | ||
testAliasFromAddressViaMemory(); | ||
testAliasFromAddressViaMemory2(); | ||
testAliasFromAddressViaNativeFunction(); | ||
testAliasFromAddressViaNativeFunction2(); | ||
testPartialOverlap(); | ||
} | ||
} | ||
|
||
void testNonAlias() { | ||
final source = allocate<Int64>(); | ||
source.store(42); | ||
final int a = source.load(); | ||
source.store(1984); | ||
// alias.load() should be re-executed, as we wrote to alias. | ||
Expect.notEquals(a, source.load<int>()); | ||
source.free(); | ||
} | ||
|
||
void testAliasCast() { | ||
final source = allocate<Int64>(); | ||
final alias = source.cast<Pointer<Int8>>().cast<Pointer<Int64>>(); | ||
source.store(42); | ||
final int a = source.load(); | ||
alias.store(1984); | ||
// source.load() should be re-executed, we wrote alias which aliases source. | ||
Expect.notEquals(a, source.load<int>()); | ||
source.free(); | ||
} | ||
|
||
void testAliasCast2() { | ||
final source = allocate<Int64>(); | ||
final alias = source.cast<Pointer<Int16>>().cast<Pointer<Int64>>(); | ||
final alias2 = source.cast<Pointer<Int8>>().cast<Pointer<Int64>>(); | ||
alias.store(42); | ||
final int a = alias.load(); | ||
alias2.store(1984); | ||
// alias.load() should be re-executed, we wrote alias2 which aliases alias. | ||
Expect.notEquals(a, alias.load<int>()); | ||
source.free(); | ||
} | ||
|
||
void testAliasOffsetBy() { | ||
final source = allocate<Int64>(count: 2); | ||
final alias = source.offsetBy(8).offsetBy(-8); | ||
source.store(42); | ||
final int a = source.load(); | ||
alias.store(1984); | ||
// source.load() should be re-executed, we wrote alias which aliases source. | ||
Expect.notEquals(a, source.load<int>()); | ||
source.free(); | ||
} | ||
|
||
void testAliasOffsetBy2() { | ||
final source = allocate<Int64>(count: 3); | ||
final alias = source.offsetBy(16).offsetBy(-16); | ||
final alias2 = source.offsetBy(8).offsetBy(-8); | ||
alias.store(42); | ||
final int a = alias.load(); | ||
alias2.store(1984); | ||
// alias.load() should be re-executed, we wrote alias2 which aliases alias. | ||
Expect.notEquals(a, alias.load<int>()); | ||
source.free(); | ||
} | ||
|
||
void testAliasElementAt() { | ||
final source = allocate<Int64>(count: 2); | ||
final alias = source.elementAt(1).elementAt(-1); | ||
source.store(42); | ||
final int a = source.load(); | ||
alias.store(1984); | ||
// source.load() should be re-executed, we wrote alias which aliases source. | ||
Expect.notEquals(a, source.load<int>()); | ||
source.free(); | ||
} | ||
|
||
void testAliasElementAt2() { | ||
final source = allocate<Int64>(count: 3); | ||
final alias = source.elementAt(2).elementAt(-2); | ||
final alias2 = source.elementAt(1).elementAt(-1); | ||
alias.store(42); | ||
final int a = alias.load(); | ||
alias2.store(1984); | ||
// alias.load() should be re-executed, we wrote alias2 which aliases alias. | ||
Expect.notEquals(a, alias.load<int>()); | ||
source.free(); | ||
} | ||
|
||
void testAliasFromAddress() { | ||
final source = allocate<Int64>(); | ||
final alias = fromAddress<Pointer<Int64>>(source.address); | ||
source.store(42); | ||
final int a = source.load(); | ||
alias.store(1984); | ||
// source.load() should be re-executed, we wrote alias which aliases source. | ||
Expect.notEquals(a, source.load<int>()); | ||
source.free(); | ||
} | ||
|
||
void testAliasFromAddress2() { | ||
final source = allocate<Int64>(); | ||
final alias = fromAddress<Pointer<Int64>>(source.address); | ||
final alias2 = fromAddress<Pointer<Int64>>(source.address); | ||
alias.store(42); | ||
final int a = alias.load(); | ||
alias2.store(1984); | ||
// alias.load() should be re-executed, we wrote alias2 which aliases alias. | ||
Expect.notEquals(a, alias.load<int>()); | ||
source.free(); | ||
} | ||
|
||
void testAliasFromAddressViaMemory() { | ||
final helper = allocate<IntPtr>(); | ||
final source = allocate<Int64>(); | ||
helper.store(source.address); | ||
final alias = fromAddress<Pointer<Int64>>(helper.load()); | ||
source.store(42); | ||
final int a = source.load(); | ||
alias.store(1984); | ||
// source.load() should be re-executed, we wrote alias which aliases source. | ||
Expect.notEquals(a, source.load<int>()); | ||
helper.free(); | ||
source.free(); | ||
} | ||
|
||
void testAliasFromAddressViaMemory2() { | ||
final helper = allocate<IntPtr>(); | ||
final source = allocate<Int64>(); | ||
helper.store(source.address); | ||
final alias = fromAddress<Pointer<Int64>>(helper.load()); | ||
final alias2 = fromAddress<Pointer<Int64>>(helper.load()); | ||
alias.store(42); | ||
final int a = alias.load(); | ||
alias2.store(1984); | ||
// alias.load() should be re-executed, we wrote alias2 which aliases alias. | ||
Expect.notEquals(a, alias.load<int>()); | ||
helper.free(); | ||
source.free(); | ||
} | ||
|
||
typedef NativeQuadOpSigned = Int64 Function(Int8, Int16, Int32, Int64); | ||
typedef QuadOp = int Function(int, int, int, int); | ||
|
||
DynamicLibrary ffiTestFunctions = dlopenPlatformSpecific("ffi_test_functions"); | ||
|
||
QuadOp intComputation = ffiTestFunctions | ||
.lookupFunction<NativeQuadOpSigned, QuadOp>("IntComputation"); | ||
|
||
void testAliasFromAddressViaNativeFunction() { | ||
final source = allocate<Int64>(); | ||
final alias = | ||
fromAddress<Pointer<Int64>>(intComputation(0, 0, 0, source.address)); | ||
source.store(42); | ||
final int a = source.load(); | ||
alias.store(1984); | ||
// source.load() should be re-executed, we wrote alias which aliases source. | ||
Expect.notEquals(a, source.load<int>()); | ||
source.free(); | ||
} | ||
|
||
void testAliasFromAddressViaNativeFunction2() { | ||
final source = allocate<Int64>(); | ||
final alias = | ||
fromAddress<Pointer<Int64>>(intComputation(0, 0, 0, source.address)); | ||
final alias2 = | ||
fromAddress<Pointer<Int64>>(intComputation(0, 0, 0, source.address)); | ||
alias.store(42); | ||
final int a = alias.load(); | ||
alias2.store(1984); | ||
// alias.load() should be re-executed, we wrote alias2 which aliases alias. | ||
Expect.notEquals(a, alias.load<int>()); | ||
source.free(); | ||
} | ||
|
||
// TODO(dacoharkes): Replace with @pragma annotations once available. | ||
const NeverInline = 'NeverInline'; | ||
|
||
@NeverInline | ||
Pointer<Int8> makeDerived(Pointer<Int64> source) => | ||
source.offsetBy(7).cast<Pointer<Int8>>(); | ||
|
||
testPartialOverlap() { | ||
final source = allocate<Int64>(count: 2); | ||
final derived = makeDerived(source); | ||
source.store(0x1122334455667788); | ||
final int value = source.load(); | ||
derived.store(0xaa); | ||
Expect.notEquals(value, source.load<int>()); | ||
source.free(); | ||
} |