Add array bound checks to code generated for SwapElements. This fixes a bug that lead to a segfault when an array was modified while it was sorted.
Review URL: http://codereview.chromium.org/5686006 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@6020 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
parent
f118f44130
commit
5f962f2eb2
@ -5592,6 +5592,12 @@ void CodeGenerator::GenerateSwapElements(ZoneList<Expression*>* args) {
|
||||
__ tst(tmp2, Operand(kSmiTagMask));
|
||||
deferred->Branch(nz);
|
||||
|
||||
// Check that both indices are valid.
|
||||
__ ldr(tmp2, FieldMemOperand(object, JSArray::kLengthOffset));
|
||||
__ cmp(tmp2, index1);
|
||||
__ cmp(tmp2, index2, hi);
|
||||
deferred->Branch(ls);
|
||||
|
||||
// Bring the offsets into the fixed array in tmp1 into index1 and
|
||||
// index2.
|
||||
__ mov(tmp2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
|
||||
|
@ -7676,6 +7676,13 @@ void CodeGenerator::GenerateSwapElements(ZoneList<Expression*>* args) {
|
||||
__ test(tmp2.reg(), Immediate(kSmiTagMask));
|
||||
deferred->Branch(not_zero);
|
||||
|
||||
// Check that both indices are valid.
|
||||
__ mov(tmp2.reg(), FieldOperand(object.reg(), JSArray::kLengthOffset));
|
||||
__ cmp(tmp2.reg(), Operand(index1.reg()));
|
||||
deferred->Branch(below_equal);
|
||||
__ cmp(tmp2.reg(), Operand(index2.reg()));
|
||||
deferred->Branch(below_equal);
|
||||
|
||||
// Bring addresses into index1 and index2.
|
||||
__ lea(index1.reg(), FixedArrayElementOperand(tmp1.reg(), index1.reg()));
|
||||
__ lea(index2.reg(), FixedArrayElementOperand(tmp1.reg(), index2.reg()));
|
||||
|
@ -3108,6 +3108,13 @@ void FullCodeGenerator::EmitSwapElements(ZoneList<Expression*>* args) {
|
||||
__ test(temp, Immediate(kSmiTagMask));
|
||||
__ j(not_zero, &slow_case);
|
||||
|
||||
// Check that both indices are valid.
|
||||
__ mov(temp, FieldOperand(object, JSArray::kLengthOffset));
|
||||
__ cmp(temp, Operand(index_1));
|
||||
__ j(below_equal, &slow_case);
|
||||
__ cmp(temp, Operand(index_2));
|
||||
__ j(below_equal, &slow_case);
|
||||
|
||||
// Bring addresses into index1 and index2.
|
||||
__ lea(index_1, CodeGenerator::FixedArrayElementOperand(elements, index_1));
|
||||
__ lea(index_2, CodeGenerator::FixedArrayElementOperand(elements, index_2));
|
||||
|
@ -6782,6 +6782,13 @@ void CodeGenerator::GenerateSwapElements(ZoneList<Expression*>* args) {
|
||||
Condition both_smi = masm()->CheckBothSmi(index1.reg(), index2.reg());
|
||||
deferred->Branch(NegateCondition(both_smi));
|
||||
|
||||
// Check that both indices are valid.
|
||||
__ movq(tmp2.reg(), FieldOperand(object.reg(), JSArray::kLengthOffset));
|
||||
__ cmpl(tmp2.reg(), index1.reg());
|
||||
deferred->Branch(below_equal);
|
||||
__ cmpl(tmp2.reg(), index2.reg());
|
||||
deferred->Branch(below_equal);
|
||||
|
||||
// Bring addresses into index1 and index2.
|
||||
__ SmiToInteger32(index1.reg(), index1.reg());
|
||||
__ lea(index1.reg(), FieldOperand(tmp1.reg(),
|
||||
|
55
test/mjsunit/regress/regress-swapelements.js
Normal file
55
test/mjsunit/regress/regress-swapelements.js
Normal file
@ -0,0 +1,55 @@
|
||||
// Copyright 2010 the V8 project authors. All rights reserved.
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
//
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following
|
||||
// disclaimer in the documentation and/or other materials provided
|
||||
// with the distribution.
|
||||
// * Neither the name of Google Inc. nor the names of its
|
||||
// contributors may be used to endorse or promote products derived
|
||||
// from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
// Check that modifying an array while sorting it does not segfault. See
|
||||
// http://code.google.com/p/chromium/issues/detail?id=66099.
|
||||
|
||||
function Item(val) {
|
||||
this.value = val;
|
||||
}
|
||||
|
||||
|
||||
var size = 23;
|
||||
var array1 = new Array(size);
|
||||
|
||||
|
||||
function myToString() {
|
||||
array1.splice(0, 1);
|
||||
return this.value.toString();
|
||||
}
|
||||
|
||||
|
||||
function test() {
|
||||
for (var i = 0; i < size; i++) {
|
||||
array1[i] = new Item(i);
|
||||
array1[i].toString = myToString;
|
||||
}
|
||||
array1.sort();
|
||||
}
|
||||
|
||||
|
||||
test();
|
Loading…
Reference in New Issue
Block a user