Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1605)

Unified Diff: test/mjsunit/harmony/atomics.js

Issue 1676613002: [Atomics] Fix atomic access index validation (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: merge master Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « test/mjsunit/asm/atomics-xor.js ('k') | test/mjsunit/harmony/futex.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/mjsunit/harmony/atomics.js
diff --git a/test/mjsunit/harmony/atomics.js b/test/mjsunit/harmony/atomics.js
index 4b9c9f6c663a3951c4dfaeefab6fb211a4ffc810..bf27eb46d54f3f3420e3a75f8eac3a81aebdb7ed 100644
--- a/test/mjsunit/harmony/atomics.js
+++ b/test/mjsunit/harmony/atomics.js
@@ -60,83 +60,65 @@ var IntegerTypedArrayConstructors = [
});
})();
-function testAtomicOp(op, ia, index, expectedIndex, name) {
- for (var i = 0; i < ia.length; ++i)
- ia[i] = 22;
-
- ia[expectedIndex] = 0;
- assertEquals(0, op(ia, index, 0, 0), name);
- assertEquals(0, ia[expectedIndex], name);
-
- for (var i = 0; i < ia.length; ++i) {
- if (i == expectedIndex) continue;
- assertEquals(22, ia[i], name);
- }
-}
-
(function TestBadIndex() {
var sab = new SharedArrayBuffer(8);
var si32a = new Int32Array(sab);
var si32a2 = new Int32Array(sab, 4);
- // Non-integer indexes are converted to an integer first, so they should all
- // operate on index 0.
- [undefined, null, false, 'hi', {}].forEach(function(i) {
-
- var name = String(i);
- testAtomicOp(Atomics.compareExchange, si32a, i, 0, name);
- testAtomicOp(Atomics.load, si32a, i, 0, name);
- testAtomicOp(Atomics.store, si32a, i, 0, name);
- testAtomicOp(Atomics.add, si32a, i, 0, name);
- testAtomicOp(Atomics.sub, si32a, i, 0, name);
- testAtomicOp(Atomics.and, si32a, i, 0, name);
- testAtomicOp(Atomics.or, si32a, i, 0, name);
- testAtomicOp(Atomics.xor, si32a, i, 0, name);
- testAtomicOp(Atomics.exchange, si32a, i, 0, name);
- });
-
- // Out-of-bounds indexes should return undefined.
- // TODO(binji): Should these throw RangeError instead?
+ // Non-integer indexes should throw RangeError.
+ var nonInteger = [1.4, '1.4', NaN, -Infinity, Infinity, undefined, 'hi', {}];
+ nonInteger.forEach(function(i) {
+ assertThrows(function() { Atomics.compareExchange(si32a, i, 0); },
+ RangeError);
+ assertThrows(function() { Atomics.load(si32a, i, 0); }, RangeError);
+ assertThrows(function() { Atomics.store(si32a, i, 0); }, RangeError);
+ assertThrows(function() { Atomics.add(si32a, i, 0); }, RangeError);
+ assertThrows(function() { Atomics.sub(si32a, i, 0); }, RangeError);
+ assertThrows(function() { Atomics.and(si32a, i, 0); }, RangeError);
+ assertThrows(function() { Atomics.or(si32a, i, 0); }, RangeError);
+ assertThrows(function() { Atomics.xor(si32a, i, 0); }, RangeError);
+ assertThrows(function() { Atomics.exchange(si32a, i, 0); }, RangeError);
+ }, RangeError);
+
+ // Out-of-bounds indexes should throw RangeError.
[-1, 2, 100].forEach(function(i) {
- var name = String(i);
- assertEquals(undefined, Atomics.compareExchange(si32a, i, 0, 0), name);
- assertEquals(undefined, Atomics.load(si32a, i), name);
- assertEquals(undefined, Atomics.store(si32a, i, 0), name);
- assertEquals(undefined, Atomics.add(si32a, i, 0), name);
- assertEquals(undefined, Atomics.sub(si32a, i, 0), name);
- assertEquals(undefined, Atomics.and(si32a, i, 0), name);
- assertEquals(undefined, Atomics.or(si32a, i, 0), name);
- assertEquals(undefined, Atomics.xor(si32a, i, 0), name);
- assertEquals(undefined, Atomics.exchange(si32a, i, 0), name);
- });
-
- // Out-of-bounds indexes for offset-array
+ assertThrows(function() { Atomics.compareExchange(si32a, i, 0, 0); },
+ RangeError);
+ assertThrows(function() { Atomics.load(si32a, i); }, RangeError);
+ assertThrows(function() { Atomics.store(si32a, i, 0); }, RangeError);
+ assertThrows(function() { Atomics.add(si32a, i, 0); }, RangeError);
+ assertThrows(function() { Atomics.sub(si32a, i, 0); }, RangeError);
+ assertThrows(function() { Atomics.and(si32a, i, 0); }, RangeError);
+ assertThrows(function() { Atomics.or(si32a, i, 0); }, RangeError);
+ assertThrows(function() { Atomics.xor(si32a, i, 0); }, RangeError);
+ assertThrows(function() { Atomics.exchange(si32a, i, 0); }, RangeError);
+ }, RangeError);
+
+ // Out-of-bounds indexes for array with offset should throw RangeError.
[-1, 1, 100].forEach(function(i) {
- var name = String(i);
- assertEquals(undefined, Atomics.compareExchange(si32a2, i, 0, 0), name);
- assertEquals(undefined, Atomics.load(si32a2, i), name);
- assertEquals(undefined, Atomics.store(si32a2, i, 0), name);
- assertEquals(undefined, Atomics.add(si32a2, i, 0), name);
- assertEquals(undefined, Atomics.sub(si32a2, i, 0), name);
- assertEquals(undefined, Atomics.and(si32a2, i, 0), name);
- assertEquals(undefined, Atomics.or(si32a2, i, 0), name);
- assertEquals(undefined, Atomics.xor(si32a2, i, 0), name);
- assertEquals(undefined, Atomics.exchange(si32a2, i, 0), name);
+ assertThrows(function() { Atomics.compareExchange(si32a2, i, 0, 0); });
+ assertThrows(function() { Atomics.load(si32a2, i); }, RangeError);
+ assertThrows(function() { Atomics.store(si32a2, i, 0); }, RangeError);
+ assertThrows(function() { Atomics.add(si32a2, i, 0); }, RangeError);
+ assertThrows(function() { Atomics.sub(si32a2, i, 0); }, RangeError);
+ assertThrows(function() { Atomics.and(si32a2, i, 0); }, RangeError);
+ assertThrows(function() { Atomics.or(si32a2, i, 0); }, RangeError);
+ assertThrows(function() { Atomics.xor(si32a2, i, 0); }, RangeError);
+ assertThrows(function() { Atomics.exchange(si32a2, i, 0); }, RangeError);
});
- // Monkey-patch length and make sure these functions still return undefined.
+ // Monkey-patch length and make sure these functions still throw.
Object.defineProperty(si32a, 'length', {get: function() { return 1000; }});
[2, 100].forEach(function(i) {
- var name = String(i);
- assertEquals(undefined, Atomics.compareExchange(si32a, i, 0, 0), name);
- assertEquals(undefined, Atomics.load(si32a, i), name);
- assertEquals(undefined, Atomics.store(si32a, i, 0), name);
- assertEquals(undefined, Atomics.add(si32a, i, 0), name);
- assertEquals(undefined, Atomics.sub(si32a, i, 0), name);
- assertEquals(undefined, Atomics.and(si32a, i, 0), name);
- assertEquals(undefined, Atomics.or(si32a, i, 0), name);
- assertEquals(undefined, Atomics.xor(si32a, i, 0), name);
- assertEquals(undefined, Atomics.exchange(si32a, i, 0), name);
+ assertThrows(function() { Atomics.compareExchange(si32a, i, 0, 0); });
+ assertThrows(function() { Atomics.load(si32a, i); });
+ assertThrows(function() { Atomics.store(si32a, i, 0); });
+ assertThrows(function() { Atomics.add(si32a, i, 0); });
+ assertThrows(function() { Atomics.sub(si32a, i, 0); });
+ assertThrows(function() { Atomics.and(si32a, i, 0); });
+ assertThrows(function() { Atomics.or(si32a, i, 0); });
+ assertThrows(function() { Atomics.xor(si32a, i, 0); });
+ assertThrows(function() { Atomics.exchange(si32a, i, 0); });
});
})();
@@ -145,22 +127,52 @@ function testAtomicOp(op, ia, index, expectedIndex, name) {
var si32a = new Int32Array(sab);
var si32a2 = new Int32Array(sab, 32);
+ var testOp = function(op, ia, index, expectedIndex, name) {
+ for (var i = 0; i < ia.length; ++i)
+ ia[i] = 22;
+
+ ia[expectedIndex] = 0;
+ assertEquals(0, op(ia, index, 0, 0), name);
+ assertEquals(0, ia[expectedIndex], name);
+
+ for (var i = 0; i < ia.length; ++i) {
+ if (i == expectedIndex) continue;
+ assertEquals(22, ia[i], name);
+ }
+ };
+
+ // These values all map to index 0
+ [-0, 0, 0.0, null, false].forEach(function(i) {
+ var name = String(i);
+ [si32a, si32a2].forEach(function(array) {
+ testOp(Atomics.compareExchange, array, i, 0, name);
+ testOp(Atomics.load, array, i, 0, name);
+ testOp(Atomics.store, array, i, 0, name);
+ testOp(Atomics.add, array, i, 0, name);
+ testOp(Atomics.sub, array, i, 0, name);
+ testOp(Atomics.and, array, i, 0, name);
+ testOp(Atomics.or, array, i, 0, name);
+ testOp(Atomics.xor, array, i, 0, name);
+ testOp(Atomics.exchange, array, i, 0, name);
+ });
+ });
+
+ // These values all map to index 3
var valueOf = {valueOf: function(){ return 3;}};
var toString = {toString: function(){ return '3';}};
-
- [3, 3.5, '3', '3.5', valueOf, toString].forEach(function(i) {
+ [3, 3.0, '3', '3.0', valueOf, toString].forEach(function(i) {
var name = String(i);
[si32a, si32a2].forEach(function(array) {
- testAtomicOp(Atomics.compareExchange, array, i, 3, name);
- testAtomicOp(Atomics.load, array, i, 3, name);
- testAtomicOp(Atomics.store, array, i, 3, name);
- testAtomicOp(Atomics.add, array, i, 3, name);
- testAtomicOp(Atomics.sub, array, i, 3, name);
- testAtomicOp(Atomics.and, array, i, 3, name);
- testAtomicOp(Atomics.or, array, i, 3, name);
- testAtomicOp(Atomics.xor, array, i, 3, name);
- testAtomicOp(Atomics.exchange, array, i, 3, name);
- })
+ testOp(Atomics.compareExchange, array, i, 3, name);
+ testOp(Atomics.load, array, i, 3, name);
+ testOp(Atomics.store, array, i, 3, name);
+ testOp(Atomics.add, array, i, 3, name);
+ testOp(Atomics.sub, array, i, 3, name);
+ testOp(Atomics.and, array, i, 3, name);
+ testOp(Atomics.or, array, i, 3, name);
+ testOp(Atomics.xor, array, i, 3, name);
+ testOp(Atomics.exchange, array, i, 3, name);
+ });
});
})();
« no previous file with comments | « test/mjsunit/asm/atomics-xor.js ('k') | test/mjsunit/harmony/futex.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698