Revision: 6035
Author: [email protected]
Date: Wed Dec 15 08:14:29 2010
Log: Fix issue 974.

When entering a finally block in unoptimized code, we unconditionally
save the accumulator register in the stack in case it holds a return
value or an exception.  In the case of a break, continue, or falling
off the end of the try or catch block, this value is unpredictable and
not necessarily safe for GC.

Review URL: http://codereview.chromium.org/5883003
http://code.google.com/p/v8/source/detail?r=6035

Added:
 /branches/bleeding_edge/test/mjsunit/regress/regress-974.js
Modified:
 /branches/bleeding_edge/src/arm/full-codegen-arm.cc
 /branches/bleeding_edge/src/full-codegen.cc
 /branches/bleeding_edge/src/full-codegen.h
 /branches/bleeding_edge/src/ia32/full-codegen-ia32.cc
 /branches/bleeding_edge/src/x64/full-codegen-x64.cc

=======================================
--- /dev/null
+++ /branches/bleeding_edge/test/mjsunit/regress/regress-974.js Wed Dec 15 08:14:29 2010
@@ -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.
+
+// Flags: --expose-gc
+
+// Verify that GC is safe in a finally block entered by falling off the try
+// block.
+eval("(function(){try {  } catch(x) {  } finally { gc() }})")();
=======================================
--- /branches/bleeding_edge/src/arm/full-codegen-arm.cc Tue Dec 14 01:47:28 2010 +++ /branches/bleeding_edge/src/arm/full-codegen-arm.cc Wed Dec 15 08:14:29 2010
@@ -204,6 +204,11 @@
   // of the stack check table.
   masm()->CheckConstPool(true, false);
 }
+
+
+void FullCodeGenerator::ClearAccumulator() {
+  __ mov(r0, Operand(Smi::FromInt(0)));
+}


 void FullCodeGenerator::EmitStackCheck(IterationStatement* stmt) {
=======================================
--- /branches/bleeding_edge/src/full-codegen.cc Tue Dec  7 03:31:57 2010
+++ /branches/bleeding_edge/src/full-codegen.cc Wed Dec 15 08:14:29 2010
@@ -946,6 +946,11 @@
   SetStatementPosition(stmt);
   NestedStatement* current = nesting_stack_;
   int stack_depth = 0;
+  // When continuing, we clobber the unpredictable value in the accumulator
+  // with one that's safe for GC.  If we hit an exit from the try block of
+  // try...finally on our way out, we will unconditionally preserve the
+  // accumulator on the stack.
+  ClearAccumulator();
   while (!current->IsContinueTarget(stmt->target())) {
     stack_depth = current->Exit(stack_depth);
     current = current->outer();
@@ -962,6 +967,11 @@
   SetStatementPosition(stmt);
   NestedStatement* current = nesting_stack_;
   int stack_depth = 0;
+  // When breaking, we clobber the unpredictable value in the accumulator
+  // with one that's safe for GC.  If we hit an exit from the try block of
+  // try...finally on our way out, we will unconditionally preserve the
+  // accumulator on the stack.
+  ClearAccumulator();
   while (!current->IsBreakTarget(stmt->target())) {
     stack_depth = current->Exit(stack_depth);
     current = current->outer();
@@ -1235,7 +1245,10 @@
     Visit(stmt->try_block());
     __ PopTryHandler();
   }
-  // Execute the finally block on the way out.
+  // Execute the finally block on the way out.  Clobber the unpredictable
+  // value in the accumulator with one that's safe for GC.  The finally
+  // block will unconditionally preserve the accumulator on the stack.
+  ClearAccumulator();
   __ Call(&finally_entry);
 }

=======================================
--- /branches/bleeding_edge/src/full-codegen.h  Wed Dec 15 05:56:41 2010
+++ /branches/bleeding_edge/src/full-codegen.h  Wed Dec 15 08:14:29 2010
@@ -286,6 +286,10 @@

   static const InlineFunctionGenerator kInlineFunctionGenerators[];

+  // A platform-specific utility to overwrite the accumulator register
+  // with a GC-safe value.
+  void ClearAccumulator();
+
   // Compute the frame pointer relative offset for a given local or
   // parameter slot.
   int SlotOffset(Slot* slot);
=======================================
--- /branches/bleeding_edge/src/ia32/full-codegen-ia32.cc Wed Dec 15 05:56:41 2010 +++ /branches/bleeding_edge/src/ia32/full-codegen-ia32.cc Wed Dec 15 08:14:29 2010
@@ -249,6 +249,11 @@
     EmitReturnSequence();
   }
 }
+
+
+void FullCodeGenerator::ClearAccumulator() {
+  __ Set(eax, Immediate(Smi::FromInt(0)));
+}


 void FullCodeGenerator::EmitStackCheck(IterationStatement* stmt) {
=======================================
--- /branches/bleeding_edge/src/x64/full-codegen-x64.cc Tue Dec 14 01:47:28 2010 +++ /branches/bleeding_edge/src/x64/full-codegen-x64.cc Wed Dec 15 08:14:29 2010
@@ -196,6 +196,11 @@
     EmitReturnSequence();
   }
 }
+
+
+void FullCodeGenerator::ClearAccumulator() {
+  __ xor(rax, rax);
+}


 void FullCodeGenerator::EmitStackCheck(IterationStatement* stmt) {

--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

Reply via email to