Skip to content

Commit a595f9d

Browse files
committed
[Truffle] Include lowering empty arrays to null in randomisation, and make array storage checks more thorough.
1 parent f49688f commit a595f9d

File tree

3 files changed

+79
-57
lines changed

3 files changed

+79
-57
lines changed

spec/truffle/tags/core/array/concat_tags.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ fails:Array#concat is not infected by the other
66
fails:Array#concat keeps the tainted status of elements
77
fails:Array#concat is not infected untrustedness by the other
88
fails:Array#concat keeps the untrusted status of elements
9+
fails:Array#concat returns the array itself
10+
fails:Array#concat appends the elements in the other array
11+
fails:Array#concat does not loop endlessly when argument is self
12+
fails:Array#concat keeps tainted status
13+
fails:Array#concat keeps untrusted status

truffle/src/main/java/org/jruby/truffle/nodes/core/ArrayNodes.java

Lines changed: 47 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,7 @@ public int setIntegerFixnum(RubyArray array, int index, int value, UndefinedPlac
963963
}
964964

965965
store[normalisedIndex] = value;
966-
array.setSize(array.getSize() + 1);
966+
array.setStore(store, array.getSize() + 1);
967967
} else if (normalisedIndex > array.getSize()) {
968968
beyondBranch.enter();
969969
final Object[] newStore = new Object[index + 1];
@@ -1010,7 +1010,7 @@ public long setLongInIntegerFixnum(RubyArray array, int index, long value, Undef
10101010
}
10111011

10121012
store[normalisedIndex] = value;
1013-
array.setSize(array.getSize() + 1);
1013+
array.setStore(store, array.getSize() + 1);
10141014
} else if (normalisedIndex > array.getSize()) {
10151015
beyondBranch.enter();
10161016
final Object[] newStore = new Object[index + 1];
@@ -1057,7 +1057,7 @@ public long setLongFixnum(RubyArray array, int index, long value, UndefinedPlace
10571057
}
10581058

10591059
store[normalisedIndex] = value;
1060-
array.setSize(array.getSize() + 1);
1060+
array.setStore(store, array.getSize() + 1);
10611061
} else if (normalisedIndex > array.getSize()) {
10621062
beyondBranch.enter();
10631063
final Object[] newStore = new Object[index + 1];
@@ -1098,7 +1098,7 @@ public double setFloat(RubyArray array, int index, double value, UndefinedPlaceh
10981098
}
10991099

11001100
store[normalisedIndex] = value;
1101-
array.setSize(array.getSize() + 1);
1101+
array.setStore(store, array.getSize() + 1);
11021102
} else if (normalisedIndex > array.getSize()) {
11031103
beyondBranch.enter();
11041104
final Object[] newStore = new Object[index + 1];
@@ -1139,7 +1139,7 @@ public Object setObject(RubyArray array, int index, Object value, UndefinedPlace
11391139
}
11401140

11411141
store[normalisedIndex] = value;
1142-
array.setSize(array.getSize() + 1);
1142+
array.setStore(store, array.getSize() + 1);
11431143
} else if (normalisedIndex > array.getSize()) {
11441144
beyondBranch.enter();
11451145
final Object[] newStore = new Object[index + 1];
@@ -1206,7 +1206,7 @@ public RubyArray setIntegerFixnum(RubyArray array, int start, int length, RubyAr
12061206

12071207
// TODO: This is a moving overlapping memory, should we use sth else instead?
12081208
System.arraycopy(store, exclusiveEnd, store, begin, array.getSize() - exclusiveEnd);
1209-
array.setSize(array.getSize() - length);
1209+
array.setStore(store, array.getSize() - length);
12101210

12111211
return value;
12121212
} else {
@@ -1320,7 +1320,7 @@ public ClearNode(ClearNode prev) {
13201320
@Specialization
13211321
public RubyArray clear(RubyArray array) {
13221322
notDesignedForCompilation();
1323-
array.setSize(0);
1323+
array.setStore(null, 0);
13241324
return array;
13251325
}
13261326

@@ -1384,9 +1384,8 @@ public RubyArray concatIntegerFixnum(RubyArray array, RubyArray other) {
13841384
notDesignedForCompilation();
13851385

13861386
// TODO(CS): is there already space in array?
1387-
array.setStore(Arrays.copyOf((int[]) array.getStore(), array.getSize() + other.getSize()), array.getSize());
13881387
System.arraycopy(other.getStore(), 0, array.getStore(), array.getSize(), other.getSize());
1389-
array.setSize(array.getSize() + other.getSize());
1388+
array.setStore(Arrays.copyOf((int[]) array.getStore(), array.getSize() + other.getSize()), array.getSize() + other.getSize());
13901389
return array;
13911390
}
13921391

@@ -1395,9 +1394,8 @@ public RubyArray concatLongFixnum(RubyArray array, RubyArray other) {
13951394
notDesignedForCompilation();
13961395

13971396
// TODO(CS): is there already space in array?
1398-
array.setStore(Arrays.copyOf((long[]) array.getStore(), array.getSize() + other.getSize()), array.getSize());
13991397
System.arraycopy(other.getStore(), 0, array.getStore(), array.getSize(), other.getSize());
1400-
array.setSize(array.getSize() + other.getSize());
1398+
array.setStore(Arrays.copyOf((long[]) array.getStore(), array.getSize() + other.getSize()), array.getSize() + other.getSize());
14011399
return array;
14021400
}
14031401

@@ -1406,9 +1404,8 @@ public RubyArray concatDouble(RubyArray array, RubyArray other) {
14061404
notDesignedForCompilation();
14071405

14081406
// TODO(CS): is there already space in array?
1409-
array.setStore(Arrays.copyOf((double[]) array.getStore(), array.getSize() + other.getSize()), array.getSize());
14101407
System.arraycopy(other.getStore(), 0, array.getStore(), array.getSize(), other.getSize());
1411-
array.setSize(array.getSize() + other.getSize());
1408+
array.setStore(Arrays.copyOf((double[]) array.getStore(), array.getSize() + other.getSize()), array.getSize() + other.getSize());
14121409
return array;
14131410
}
14141411

@@ -1417,9 +1414,8 @@ public RubyArray concatObject(RubyArray array, RubyArray other) {
14171414
notDesignedForCompilation();
14181415

14191416
// TODO(CS): is there already space in array?
1420-
array.setStore(Arrays.copyOf((Object[]) array.getStore(), array.getSize() + other.getSize()), array.getSize());
14211417
System.arraycopy(other.getStore(), 0, array.getStore(), array.getSize(), other.getSize());
1422-
array.setSize(array.getSize() + other.getSize());
1418+
array.setStore(Arrays.copyOf((Object[]) array.getStore(), array.getSize() + other.getSize()), array.getSize() + other.getSize());
14231419
return array;
14241420
}
14251421

@@ -1476,7 +1472,7 @@ public Object deleteIntegerFixnum(VirtualFrame frame, RubyArray array, Object va
14761472
i++;
14771473
}
14781474

1479-
array.setSize(i);
1475+
array.setStore(store, i);
14801476
return found;
14811477
}
14821478

@@ -1503,7 +1499,7 @@ public Object deleteObject(VirtualFrame frame, RubyArray array, Object value) {
15031499
i++;
15041500
}
15051501

1506-
array.setSize(i);
1502+
array.setStore(store, i);
15071503
return found;
15081504
}
15091505

@@ -1535,7 +1531,7 @@ public int deleteAtIntegerFixnumInBounds(RubyArray array, int index) throws Unex
15351531
final int[] store = (int[]) array.getStore();
15361532
final int value = store[normalisedIndex];
15371533
System.arraycopy(store, normalisedIndex + 1, store, normalisedIndex, array.getSize() - normalisedIndex - 1);
1538-
array.setSize(array.getSize() - 1);
1534+
array.setStore(store, array.getSize() - 1);
15391535
return value;
15401536
}
15411537
}
@@ -1561,7 +1557,7 @@ public Object deleteAtIntegerFixnum(RubyArray array, int index) {
15611557
final int[] store = (int[]) array.getStore();
15621558
final int value = store[normalisedIndex];
15631559
System.arraycopy(store, normalisedIndex + 1, store, normalisedIndex, array.getSize() - normalisedIndex - 1);
1564-
array.setSize(array.getSize() - 1);
1560+
array.setStore(store, array.getSize() - 1);
15651561
return value;
15661562
}
15671563
}
@@ -2363,7 +2359,7 @@ public Object insert(RubyArray array, int index, Object value) {
23632359
final Object[] store = new Object[index + 1];
23642360
Arrays.fill(store, getContext().getCoreLibrary().getNilObject());
23652361
store[index] = value;
2366-
array.setSize(array.getSize() + 1);
2362+
array.setStore(store, array.getSize() + 1);
23672363
return array;
23682364
}
23692365

@@ -2381,7 +2377,7 @@ public Object insert(RubyArray array, int index, int value) {
23812377
} else {
23822378
System.arraycopy(store, normalisedIndex, store, normalisedIndex + 1, array.getSize() - normalisedIndex);
23832379
store[normalisedIndex] = value;
2384-
array.setSize(array.getSize() + 1);
2380+
array.setStore(store, array.getSize() + 1);
23852381
}
23862382

23872383
return array;
@@ -3075,8 +3071,9 @@ public int popIntegerFixnumInBounds(RubyArray array) throws UnexpectedResultExce
30753071
if (CompilerDirectives.injectBranchProbability(CompilerDirectives.UNLIKELY_PROBABILITY, array.getSize() == 0)) {
30763072
throw new UnexpectedResultException(getContext().getCoreLibrary().getNilObject());
30773073
} else {
3078-
final int value = ((int[]) array.getStore())[array.getSize() - 1];
3079-
array.setSize(array.getSize() - 1);
3074+
final int[] store = ((int[]) array.getStore());
3075+
final int value = store[array.getSize() - 1];
3076+
array.setStore(store, array.getSize() - 1);
30803077
return value;
30813078
}
30823079
}
@@ -3086,8 +3083,9 @@ public Object popIntegerFixnum(RubyArray array) {
30863083
if (CompilerDirectives.injectBranchProbability(CompilerDirectives.UNLIKELY_PROBABILITY, array.getSize() == 0)) {
30873084
return getContext().getCoreLibrary().getNilObject();
30883085
} else {
3089-
final int value = ((int[]) array.getStore())[array.getSize() - 1];
3090-
array.setSize(array.getSize() - 1);
3086+
final int[] store = ((int[]) array.getStore());
3087+
final int value = store[array.getSize() - 1];
3088+
array.setStore(store, array.getSize() - 1);
30913089
return value;
30923090
}
30933091
}
@@ -3097,8 +3095,9 @@ public long popLongFixnumInBounds(RubyArray array) throws UnexpectedResultExcept
30973095
if (CompilerDirectives.injectBranchProbability(CompilerDirectives.UNLIKELY_PROBABILITY, array.getSize() == 0)) {
30983096
throw new UnexpectedResultException(getContext().getCoreLibrary().getNilObject());
30993097
} else {
3100-
final long value = ((long[]) array.getStore())[array.getSize() - 1];
3101-
array.setSize(array.getSize() - 1);
3098+
final long[] store = ((long[]) array.getStore());
3099+
final long value = store[array.getSize() - 1];
3100+
array.setStore(store, array.getSize() - 1);
31023101
return value;
31033102
}
31043103
}
@@ -3108,8 +3107,9 @@ public Object popLongFixnum(RubyArray array) {
31083107
if (CompilerDirectives.injectBranchProbability(CompilerDirectives.UNLIKELY_PROBABILITY, array.getSize() == 0)) {
31093108
return getContext().getCoreLibrary().getNilObject();
31103109
} else {
3111-
final long value = ((long[]) array.getStore())[array.getSize() - 1];
3112-
array.setSize(array.getSize() - 1);
3110+
final long[] store = ((long[]) array.getStore());
3111+
final long value = store[array.getSize() - 1];
3112+
array.setStore(store, array.getSize() - 1);
31133113
return value;
31143114
}
31153115
}
@@ -3119,8 +3119,9 @@ public double popFloatInBounds(RubyArray array) throws UnexpectedResultException
31193119
if (CompilerDirectives.injectBranchProbability(CompilerDirectives.UNLIKELY_PROBABILITY, array.getSize() == 0)) {
31203120
throw new UnexpectedResultException(getContext().getCoreLibrary().getNilObject());
31213121
} else {
3122-
final double value = ((double[]) array.getStore())[array.getSize() - 1];
3123-
array.setSize(array.getSize() - 1);
3122+
final double[] store = ((double[]) array.getStore());
3123+
final double value = store[array.getSize() - 1];
3124+
array.setStore(store, array.getSize() - 1);
31243125
return value;
31253126
}
31263127
}
@@ -3130,8 +3131,9 @@ public Object popFloat(RubyArray array) {
31303131
if (CompilerDirectives.injectBranchProbability(CompilerDirectives.UNLIKELY_PROBABILITY, array.getSize() == 0)) {
31313132
return getContext().getCoreLibrary().getNilObject();
31323133
} else {
3133-
final double value = ((double[]) array.getStore())[array.getSize() - 1];
3134-
array.setSize(array.getSize() - 1);
3134+
final double[] store = ((double[]) array.getStore());
3135+
final double value = store[array.getSize() - 1];
3136+
array.setStore(store, array.getSize() - 1);
31353137
return value;
31363138
}
31373139
}
@@ -3141,8 +3143,9 @@ public Object popObject(RubyArray array) {
31413143
if (CompilerDirectives.injectBranchProbability(CompilerDirectives.UNLIKELY_PROBABILITY, array.getSize() == 0)) {
31423144
return getContext().getCoreLibrary().getNilObject();
31433145
} else {
3144-
final Object value = ((Object[]) array.getStore())[array.getSize() - 1];
3145-
array.setSize(array.getSize() - 1);
3146+
final Object[] store = ((Object[]) array.getStore());
3147+
final Object value = store[array.getSize() - 1];
3148+
array.setStore(store, array.getSize() - 1);
31463149
return value;
31473150
}
31483151
}
@@ -3222,11 +3225,10 @@ public RubyArray pushIntegerFixnumSingleIntegerFixnum(RubyArray array, Object...
32223225
if (store.length < newSize) {
32233226
extendBranch.enter();
32243227
store = Arrays.copyOf(store, ArrayUtils.capacity(store.length, newSize));
3225-
array.setStore(store, array.getSize());
32263228
}
32273229

32283230
store[oldSize] = (int) values[0];
3229-
array.setSize(newSize);
3231+
array.setStore(store, newSize);
32303232
return array;
32313233
}
32323234

@@ -3245,13 +3247,11 @@ public RubyArray pushIntegerFixnum(RubyArray array, Object... values) {
32453247
store = ArrayUtils.box(oldStore);
32463248
}
32473249

3248-
array.setStore(store, oldSize);
3249-
32503250
for (int n = 0; n < values.length; n++) {
32513251
store[oldSize + n] = values[n];
32523252
}
32533253

3254-
array.setSize(newSize);
3254+
array.setStore(store, newSize);
32553255
return array;
32563256
}
32573257

@@ -3265,11 +3265,10 @@ public RubyArray pushLongFixnumSingleIntegerFixnum(RubyArray array, Object... va
32653265
if (store.length < newSize) {
32663266
extendBranch.enter();
32673267
store = Arrays.copyOf(store, ArrayUtils.capacity(store.length, newSize));
3268-
array.setStore(store, array.getSize());
32693268
}
32703269

32713270
store[oldSize] = (long) (int) values[0];
3272-
array.setSize(newSize);
3271+
array.setStore(store, newSize);
32733272
return array;
32743273
}
32753274

@@ -3283,11 +3282,10 @@ public RubyArray pushLongFixnumSingleLongFixnum(RubyArray array, Object... value
32833282
if (store.length < newSize) {
32843283
extendBranch.enter();
32853284
store = Arrays.copyOf(store, ArrayUtils.capacity(store.length, newSize));
3286-
array.setStore(store, array.getSize());
32873285
}
32883286

32893287
store[oldSize] = (long) values[0];
3290-
array.setSize(newSize);
3288+
array.setStore(store, newSize);
32913289
return array;
32923290
}
32933291

@@ -3301,14 +3299,13 @@ public RubyArray pushObject(RubyArray array, Object... values) {
33013299
if (store.length < newSize) {
33023300
extendBranch.enter();
33033301
store = Arrays.copyOf(store, ArrayUtils.capacity(store.length, newSize));
3304-
array.setStore(store, oldSize);
33053302
}
33063303

33073304
for (int n = 0; n < values.length; n++) {
33083305
store[oldSize + n] = values[n];
33093306
}
33103307

3311-
array.setSize(newSize);
3308+
array.setStore(store, newSize);
33123309
return array;
33133310
}
33143311

@@ -3355,7 +3352,7 @@ public RubyArray pushIntegerFixnumIntegerFixnum(RubyArray array, int value) {
33553352
}
33563353

33573354
store[oldSize] = value;
3358-
array.setSize(newSize);
3355+
array.setStore(store, newSize);
33593356
return array;
33603357
}
33613358

@@ -3392,7 +3389,7 @@ public RubyArray pushObjectObject(RubyArray array, Object value) {
33923389
}
33933390

33943391
store[oldSize] = value;
3395-
array.setSize(newSize);
3392+
array.setStore(store, newSize);
33963393
return array;
33973394
}
33983395

@@ -3522,7 +3519,7 @@ public Object rejectInPlaceObject(VirtualFrame frame, RubyArray array, RubyProc
35223519
i++;
35233520
}
35243521

3525-
array.setSize(i);
3522+
array.setStore(store, i);
35263523
return array;
35273524
}
35283525

@@ -3543,7 +3540,7 @@ public ReplaceNode(ReplaceNode prev) {
35433540
public RubyArray replace(RubyArray array, RubyArray other) {
35443541
notDesignedForCompilation();
35453542

3546-
array.setSize(0);
3543+
array.setStore(null, 0);
35473544
return array;
35483545
}
35493546

0 commit comments

Comments
 (0)