Skip to content

Commit

Permalink
8286279: [vectorapi] Only check index of masked lanes if offset is ou…
Browse files Browse the repository at this point in the history
…t of array boundary for masked store

Reviewed-by: psandoz
  • Loading branch information
Xiaohong Gong committed Jun 7, 2022
1 parent 645be42 commit ef7cc21
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 37 deletions.
Expand Up @@ -3008,7 +3008,7 @@ ByteVector fromArray(VectorSpecies<Byte> species,
byte[] a, int offset,
VectorMask<Byte> m) {
ByteSpecies vsp = (ByteSpecies) species;
if (offset >= 0 && offset <= (a.length - species.length())) {
if (VectorIntrinsics.indexInRange(offset, vsp.length(), a.length)) {
return vsp.dummyVector().fromArray0(a, offset, m);
}

Expand Down Expand Up @@ -3164,7 +3164,7 @@ ByteVector fromBooleanArray(VectorSpecies<Byte> species,
boolean[] a, int offset,
VectorMask<Byte> m) {
ByteSpecies vsp = (ByteSpecies) species;
if (offset >= 0 && offset <= (a.length - species.length())) {
if (VectorIntrinsics.indexInRange(offset, vsp.length(), a.length)) {
ByteVector zero = vsp.zero();
return vsp.dummyVector().fromBooleanArray0(a, offset, m);
}
Expand Down Expand Up @@ -3352,7 +3352,7 @@ ByteVector fromMemorySegment(VectorSpecies<Byte> species,
ByteOrder bo,
VectorMask<Byte> m) {
ByteSpecies vsp = (ByteSpecies) species;
if (offset >= 0 && offset <= (ms.byteSize() - species.vectorByteSize())) {
if (VectorIntrinsics.indexInRange(offset, vsp.vectorByteSize(), ms.byteSize())) {
return vsp.dummyVector().fromMemorySegment0(ms, offset, m).maybeSwap(bo);
}

Expand Down Expand Up @@ -3424,7 +3424,9 @@ void intoArray(byte[] a, int offset,
intoArray(a, offset);
} else {
ByteSpecies vsp = vspecies();
checkMaskFromIndexSize(offset, vsp, m, 1, a.length);
if (!VectorIntrinsics.indexInRange(offset, vsp.length(), a.length)) {
checkMaskFromIndexSize(offset, vsp, m, 1, a.length);
}
intoArray0(a, offset, m);
}
}
Expand Down Expand Up @@ -3579,7 +3581,9 @@ void intoBooleanArray(boolean[] a, int offset,
intoBooleanArray(a, offset);
} else {
ByteSpecies vsp = vspecies();
checkMaskFromIndexSize(offset, vsp, m, 1, a.length);
if (!VectorIntrinsics.indexInRange(offset, vsp.length(), a.length)) {
checkMaskFromIndexSize(offset, vsp, m, 1, a.length);
}
intoBooleanArray0(a, offset, m);
}
}
Expand Down Expand Up @@ -3709,7 +3713,9 @@ void intoMemorySegment(MemorySegment ms, long offset,
throw new UnsupportedOperationException("Attempt to write a read-only segment");
}
ByteSpecies vsp = vspecies();
checkMaskFromIndexSize(offset, vsp, m, 1, ms.byteSize());
if (!VectorIntrinsics.indexInRange(offset, vsp.vectorByteSize(), ms.byteSize())) {
checkMaskFromIndexSize(offset, vsp, m, 1, ms.byteSize());
}
maybeSwap(bo).intoMemorySegment0(ms, offset, m);
}
}
Expand Down
Expand Up @@ -2805,7 +2805,7 @@ DoubleVector fromArray(VectorSpecies<Double> species,
double[] a, int offset,
VectorMask<Double> m) {
DoubleSpecies vsp = (DoubleSpecies) species;
if (offset >= 0 && offset <= (a.length - species.length())) {
if (VectorIntrinsics.indexInRange(offset, vsp.length(), a.length)) {
return vsp.dummyVector().fromArray0(a, offset, m);
}

Expand Down Expand Up @@ -3038,7 +3038,7 @@ DoubleVector fromMemorySegment(VectorSpecies<Double> species,
ByteOrder bo,
VectorMask<Double> m) {
DoubleSpecies vsp = (DoubleSpecies) species;
if (offset >= 0 && offset <= (ms.byteSize() - species.vectorByteSize())) {
if (VectorIntrinsics.indexInRange(offset, vsp.vectorByteSize(), ms.byteSize())) {
return vsp.dummyVector().fromMemorySegment0(ms, offset, m).maybeSwap(bo);
}

Expand Down Expand Up @@ -3110,7 +3110,9 @@ void intoArray(double[] a, int offset,
intoArray(a, offset);
} else {
DoubleSpecies vsp = vspecies();
checkMaskFromIndexSize(offset, vsp, m, 1, a.length);
if (!VectorIntrinsics.indexInRange(offset, vsp.length(), a.length)) {
checkMaskFromIndexSize(offset, vsp, m, 1, a.length);
}
intoArray0(a, offset, m);
}
}
Expand Down Expand Up @@ -3266,7 +3268,9 @@ void intoMemorySegment(MemorySegment ms, long offset,
throw new UnsupportedOperationException("Attempt to write a read-only segment");
}
DoubleSpecies vsp = vspecies();
checkMaskFromIndexSize(offset, vsp, m, 8, ms.byteSize());
if (!VectorIntrinsics.indexInRange(offset, vsp.vectorByteSize(), ms.byteSize())) {
checkMaskFromIndexSize(offset, vsp, m, 8, ms.byteSize());
}
maybeSwap(bo).intoMemorySegment0(ms, offset, m);
}
}
Expand Down
Expand Up @@ -2829,7 +2829,7 @@ FloatVector fromArray(VectorSpecies<Float> species,
float[] a, int offset,
VectorMask<Float> m) {
FloatSpecies vsp = (FloatSpecies) species;
if (offset >= 0 && offset <= (a.length - species.length())) {
if (VectorIntrinsics.indexInRange(offset, vsp.length(), a.length)) {
return vsp.dummyVector().fromArray0(a, offset, m);
}

Expand Down Expand Up @@ -3044,7 +3044,7 @@ FloatVector fromMemorySegment(VectorSpecies<Float> species,
ByteOrder bo,
VectorMask<Float> m) {
FloatSpecies vsp = (FloatSpecies) species;
if (offset >= 0 && offset <= (ms.byteSize() - species.vectorByteSize())) {
if (VectorIntrinsics.indexInRange(offset, vsp.vectorByteSize(), ms.byteSize())) {
return vsp.dummyVector().fromMemorySegment0(ms, offset, m).maybeSwap(bo);
}

Expand Down Expand Up @@ -3116,7 +3116,9 @@ void intoArray(float[] a, int offset,
intoArray(a, offset);
} else {
FloatSpecies vsp = vspecies();
checkMaskFromIndexSize(offset, vsp, m, 1, a.length);
if (!VectorIntrinsics.indexInRange(offset, vsp.length(), a.length)) {
checkMaskFromIndexSize(offset, vsp, m, 1, a.length);
}
intoArray0(a, offset, m);
}
}
Expand Down Expand Up @@ -3253,7 +3255,9 @@ void intoMemorySegment(MemorySegment ms, long offset,
throw new UnsupportedOperationException("Attempt to write a read-only segment");
}
FloatSpecies vsp = vspecies();
checkMaskFromIndexSize(offset, vsp, m, 4, ms.byteSize());
if (!VectorIntrinsics.indexInRange(offset, vsp.vectorByteSize(), ms.byteSize())) {
checkMaskFromIndexSize(offset, vsp, m, 4, ms.byteSize());
}
maybeSwap(bo).intoMemorySegment0(ms, offset, m);
}
}
Expand Down
Expand Up @@ -2986,7 +2986,7 @@ IntVector fromArray(VectorSpecies<Integer> species,
int[] a, int offset,
VectorMask<Integer> m) {
IntSpecies vsp = (IntSpecies) species;
if (offset >= 0 && offset <= (a.length - species.length())) {
if (VectorIntrinsics.indexInRange(offset, vsp.length(), a.length)) {
return vsp.dummyVector().fromArray0(a, offset, m);
}

Expand Down Expand Up @@ -3201,7 +3201,7 @@ IntVector fromMemorySegment(VectorSpecies<Integer> species,
ByteOrder bo,
VectorMask<Integer> m) {
IntSpecies vsp = (IntSpecies) species;
if (offset >= 0 && offset <= (ms.byteSize() - species.vectorByteSize())) {
if (VectorIntrinsics.indexInRange(offset, vsp.vectorByteSize(), ms.byteSize())) {
return vsp.dummyVector().fromMemorySegment0(ms, offset, m).maybeSwap(bo);
}

Expand Down Expand Up @@ -3273,7 +3273,9 @@ void intoArray(int[] a, int offset,
intoArray(a, offset);
} else {
IntSpecies vsp = vspecies();
checkMaskFromIndexSize(offset, vsp, m, 1, a.length);
if (!VectorIntrinsics.indexInRange(offset, vsp.length(), a.length)) {
checkMaskFromIndexSize(offset, vsp, m, 1, a.length);
}
intoArray0(a, offset, m);
}
}
Expand Down Expand Up @@ -3410,7 +3412,9 @@ void intoMemorySegment(MemorySegment ms, long offset,
throw new UnsupportedOperationException("Attempt to write a read-only segment");
}
IntSpecies vsp = vspecies();
checkMaskFromIndexSize(offset, vsp, m, 4, ms.byteSize());
if (!VectorIntrinsics.indexInRange(offset, vsp.vectorByteSize(), ms.byteSize())) {
checkMaskFromIndexSize(offset, vsp, m, 4, ms.byteSize());
}
maybeSwap(bo).intoMemorySegment0(ms, offset, m);
}
}
Expand Down
Expand Up @@ -2847,7 +2847,7 @@ LongVector fromArray(VectorSpecies<Long> species,
long[] a, int offset,
VectorMask<Long> m) {
LongSpecies vsp = (LongSpecies) species;
if (offset >= 0 && offset <= (a.length - species.length())) {
if (VectorIntrinsics.indexInRange(offset, vsp.length(), a.length)) {
return vsp.dummyVector().fromArray0(a, offset, m);
}

Expand Down Expand Up @@ -3080,7 +3080,7 @@ LongVector fromMemorySegment(VectorSpecies<Long> species,
ByteOrder bo,
VectorMask<Long> m) {
LongSpecies vsp = (LongSpecies) species;
if (offset >= 0 && offset <= (ms.byteSize() - species.vectorByteSize())) {
if (VectorIntrinsics.indexInRange(offset, vsp.vectorByteSize(), ms.byteSize())) {
return vsp.dummyVector().fromMemorySegment0(ms, offset, m).maybeSwap(bo);
}

Expand Down Expand Up @@ -3152,7 +3152,9 @@ void intoArray(long[] a, int offset,
intoArray(a, offset);
} else {
LongSpecies vsp = vspecies();
checkMaskFromIndexSize(offset, vsp, m, 1, a.length);
if (!VectorIntrinsics.indexInRange(offset, vsp.length(), a.length)) {
checkMaskFromIndexSize(offset, vsp, m, 1, a.length);
}
intoArray0(a, offset, m);
}
}
Expand Down Expand Up @@ -3308,7 +3310,9 @@ void intoMemorySegment(MemorySegment ms, long offset,
throw new UnsupportedOperationException("Attempt to write a read-only segment");
}
LongSpecies vsp = vspecies();
checkMaskFromIndexSize(offset, vsp, m, 8, ms.byteSize());
if (!VectorIntrinsics.indexInRange(offset, vsp.vectorByteSize(), ms.byteSize())) {
checkMaskFromIndexSize(offset, vsp, m, 8, ms.byteSize());
}
maybeSwap(bo).intoMemorySegment0(ms, offset, m);
}
}
Expand Down
Expand Up @@ -3009,7 +3009,7 @@ ShortVector fromArray(VectorSpecies<Short> species,
short[] a, int offset,
VectorMask<Short> m) {
ShortSpecies vsp = (ShortSpecies) species;
if (offset >= 0 && offset <= (a.length - species.length())) {
if (VectorIntrinsics.indexInRange(offset, vsp.length(), a.length)) {
return vsp.dummyVector().fromArray0(a, offset, m);
}

Expand Down Expand Up @@ -3158,7 +3158,7 @@ ShortVector fromCharArray(VectorSpecies<Short> species,
char[] a, int offset,
VectorMask<Short> m) {
ShortSpecies vsp = (ShortSpecies) species;
if (offset >= 0 && offset <= (a.length - species.length())) {
if (VectorIntrinsics.indexInRange(offset, vsp.length(), a.length)) {
return vsp.dummyVector().fromCharArray0(a, offset, m);
}

Expand Down Expand Up @@ -3351,7 +3351,7 @@ ShortVector fromMemorySegment(VectorSpecies<Short> species,
ByteOrder bo,
VectorMask<Short> m) {
ShortSpecies vsp = (ShortSpecies) species;
if (offset >= 0 && offset <= (ms.byteSize() - species.vectorByteSize())) {
if (VectorIntrinsics.indexInRange(offset, vsp.vectorByteSize(), ms.byteSize())) {
return vsp.dummyVector().fromMemorySegment0(ms, offset, m).maybeSwap(bo);
}

Expand Down Expand Up @@ -3423,7 +3423,9 @@ void intoArray(short[] a, int offset,
intoArray(a, offset);
} else {
ShortSpecies vsp = vspecies();
checkMaskFromIndexSize(offset, vsp, m, 1, a.length);
if (!VectorIntrinsics.indexInRange(offset, vsp.length(), a.length)) {
checkMaskFromIndexSize(offset, vsp, m, 1, a.length);
}
intoArray0(a, offset, m);
}
}
Expand Down Expand Up @@ -3570,7 +3572,9 @@ void intoCharArray(char[] a, int offset,
intoCharArray(a, offset);
} else {
ShortSpecies vsp = vspecies();
checkMaskFromIndexSize(offset, vsp, m, 1, a.length);
if (!VectorIntrinsics.indexInRange(offset, vsp.length(), a.length)) {
checkMaskFromIndexSize(offset, vsp, m, 1, a.length);
}
intoCharArray0(a, offset, m);
}
}
Expand Down Expand Up @@ -3695,7 +3699,9 @@ void intoMemorySegment(MemorySegment ms, long offset,
throw new UnsupportedOperationException("Attempt to write a read-only segment");
}
ShortSpecies vsp = vspecies();
checkMaskFromIndexSize(offset, vsp, m, 2, ms.byteSize());
if (!VectorIntrinsics.indexInRange(offset, vsp.vectorByteSize(), ms.byteSize())) {
checkMaskFromIndexSize(offset, vsp, m, 2, ms.byteSize());
}
maybeSwap(bo).intoMemorySegment0(ms, offset, m);
}
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -44,6 +44,11 @@ static IllegalArgumentException requireLengthFailed(int haveLength, int length)
return new IllegalArgumentException(msg);
}

@ForceInline
static boolean indexInRange(long ix, long vlen, long length) {
return ix >= 0 && ix <= (length - vlen);
}

@ForceInline
static int checkFromIndexSize(int ix, int vlen, int length) {
switch (VectorIntrinsics.VECTOR_ACCESS_OOB_CHECK) {
Expand Down
Expand Up @@ -3583,7 +3583,7 @@ public abstract class $abstractvectortype$ extends AbstractVector<$Boxtype$> {
$type$[] a, int offset,
VectorMask<$Boxtype$> m) {
$Type$Species vsp = ($Type$Species) species;
if (offset >= 0 && offset <= (a.length - species.length())) {
if (VectorIntrinsics.indexInRange(offset, vsp.length(), a.length)) {
return vsp.dummyVector().fromArray0(a, offset, m);
}

Expand Down Expand Up @@ -3804,7 +3804,7 @@ public abstract class $abstractvectortype$ extends AbstractVector<$Boxtype$> {
char[] a, int offset,
VectorMask<$Boxtype$> m) {
$Type$Species vsp = ($Type$Species) species;
if (offset >= 0 && offset <= (a.length - species.length())) {
if (VectorIntrinsics.indexInRange(offset, vsp.length(), a.length)) {
return vsp.dummyVector().fromCharArray0(a, offset, m);
}

Expand Down Expand Up @@ -3963,7 +3963,7 @@ public abstract class $abstractvectortype$ extends AbstractVector<$Boxtype$> {
boolean[] a, int offset,
VectorMask<$Boxtype$> m) {
$Type$Species vsp = ($Type$Species) species;
if (offset >= 0 && offset <= (a.length - species.length())) {
if (VectorIntrinsics.indexInRange(offset, vsp.length(), a.length)) {
$abstractvectortype$ zero = vsp.zero();
return vsp.dummyVector().fromBooleanArray0(a, offset, m);
}
Expand Down Expand Up @@ -4161,7 +4161,7 @@ public abstract class $abstractvectortype$ extends AbstractVector<$Boxtype$> {
ByteOrder bo,
VectorMask<$Boxtype$> m) {
$Type$Species vsp = ($Type$Species) species;
if (offset >= 0 && offset <= (ms.byteSize() - species.vectorByteSize())) {
if (VectorIntrinsics.indexInRange(offset, vsp.vectorByteSize(), ms.byteSize())) {
return vsp.dummyVector().fromMemorySegment0(ms, offset, m).maybeSwap(bo);
}

Expand Down Expand Up @@ -4233,7 +4233,9 @@ public abstract class $abstractvectortype$ extends AbstractVector<$Boxtype$> {
intoArray(a, offset);
} else {
$Type$Species vsp = vspecies();
checkMaskFromIndexSize(offset, vsp, m, 1, a.length);
if (!VectorIntrinsics.indexInRange(offset, vsp.length(), a.length)) {
checkMaskFromIndexSize(offset, vsp, m, 1, a.length);
}
intoArray0(a, offset, m);
}
}
Expand Down Expand Up @@ -4451,7 +4453,9 @@ public abstract class $abstractvectortype$ extends AbstractVector<$Boxtype$> {
intoCharArray(a, offset);
} else {
$Type$Species vsp = vspecies();
checkMaskFromIndexSize(offset, vsp, m, 1, a.length);
if (!VectorIntrinsics.indexInRange(offset, vsp.length(), a.length)) {
checkMaskFromIndexSize(offset, vsp, m, 1, a.length);
}
intoCharArray0(a, offset, m);
}
}
Expand Down Expand Up @@ -4613,7 +4617,9 @@ public abstract class $abstractvectortype$ extends AbstractVector<$Boxtype$> {
intoBooleanArray(a, offset);
} else {
$Type$Species vsp = vspecies();
checkMaskFromIndexSize(offset, vsp, m, 1, a.length);
if (!VectorIntrinsics.indexInRange(offset, vsp.length(), a.length)) {
checkMaskFromIndexSize(offset, vsp, m, 1, a.length);
}
intoBooleanArray0(a, offset, m);
}
}
Expand Down Expand Up @@ -4744,7 +4750,9 @@ public abstract class $abstractvectortype$ extends AbstractVector<$Boxtype$> {
throw new UnsupportedOperationException("Attempt to write a read-only segment");
}
$Type$Species vsp = vspecies();
checkMaskFromIndexSize(offset, vsp, m, $sizeInBytes$, ms.byteSize());
if (!VectorIntrinsics.indexInRange(offset, vsp.vectorByteSize(), ms.byteSize())) {
checkMaskFromIndexSize(offset, vsp, m, $sizeInBytes$, ms.byteSize());
}
maybeSwap(bo).intoMemorySegment0(ms, offset, m);
}
}
Expand Down

1 comment on commit ef7cc21

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.