From df4471c8f3ade5707a9402d57007cfba0bf1b7f9 Mon Sep 17 00:00:00 2001 From: "sgjesse@chromium.org" Date: Fri, 5 Mar 2010 13:43:53 +0000 Subject: [PATCH] Fix stack tweaking in array allocation When Array(0) or new Array(0) is handled by the generated code it is handled by the same code as Array() and new Array(). For this to work the stack is tweaked to remove the argument of value 0. However the argc was still passed as 1 if a call to the runtime system was made. When the stack is tweaked argc is also changed to 0. BUG=634 TEST=test/mjsunittest/mjsunit/regress/regress-634.js Review URL: http://codereview.chromium.org/668155 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@4038 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- src/ia32/builtins-ia32.cc | 13 ++++++++---- src/runtime.cc | 1 + test/mjsunit/regress/regress-634.js | 32 +++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 test/mjsunit/regress/regress-634.js diff --git a/src/ia32/builtins-ia32.cc b/src/ia32/builtins-ia32.cc index 888b5b8552..80e421bccd 100644 --- a/src/ia32/builtins-ia32.cc +++ b/src/ia32/builtins-ia32.cc @@ -937,13 +937,18 @@ static void ArrayNativeCode(MacroAssembler* masm, __ test(ecx, Operand(ecx)); __ j(not_zero, ¬_empty_array); - // Case above assumes there is only a single slot to drop in - // ret, but we have two. - for (int i = push_count; i >= 0; i--) { + // The single argument passed is zero, so we jump to the code above used to + // handle the case of no arguments passed. To adapt the stack for that we move + // the return address and the pushed constructor (if pushed) one stack slot up + // thereby removing the passed argument. Argc is also on the stack - at the + // bottom - and it needs to be changed from 1 to 0 to have the call into the + // runtime system work in case a GC is required. + for (int i = push_count; i > 0; i--) { __ mov(eax, Operand(esp, i * kPointerSize)); __ mov(Operand(esp, (i + 1) * kPointerSize), eax); } - __ add(Operand(esp), Immediate(kPointerSize)); + __ add(Operand(esp), Immediate(2 * kPointerSize)); // Drop two stack slots. + __ push(Immediate(0)); // Treat this as a call with argc of zero. __ jmp(&empty_array); __ bind(¬_empty_array); diff --git a/src/runtime.cc b/src/runtime.cc index 90bbaf32d6..e6eeb597aa 100644 --- a/src/runtime.cc +++ b/src/runtime.cc @@ -5742,6 +5742,7 @@ static Object* Runtime_DebugPrint(Arguments args) { } args[0]->Print(); if (args[0]->IsHeapObject()) { + PrintF("\n"); HeapObject::cast(args[0])->map()->Print(); } #else diff --git a/test/mjsunit/regress/regress-634.js b/test/mjsunit/regress/regress-634.js new file mode 100644 index 0000000000..b68e843740 --- /dev/null +++ b/test/mjsunit/regress/regress-634.js @@ -0,0 +1,32 @@ +// 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. + +for (var i = 0; i < 1000000; i++) { + a = new Array(0); + assertEquals(0, a.length); + assertEquals(0, a.length); +}