7047069: Array can dynamically change size when assigned to an object field

Fri, 27 May 2011 12:47:48 -0700

author
kvn
date
Fri, 27 May 2011 12:47:48 -0700
changeset 2939
b2cb497dec28
parent 2938
c76c13577460
child 2940
33e2b8f1d466

7047069: Array can dynamically change size when assigned to an object field
Summary: Fix initialization of a newly-allocated array with arraycopy
Reviewed-by: never

src/share/vm/opto/library_call.cpp file | annotate | diff | comparison | revisions
test/compiler/7047069/Test7047069.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/vm/opto/library_call.cpp	Thu May 26 16:39:34 2011 -0700
     1.2 +++ b/src/share/vm/opto/library_call.cpp	Fri May 27 12:47:48 2011 -0700
     1.3 @@ -5225,15 +5225,16 @@
     1.4  
     1.5    // Look at the alignment of the starting offsets.
     1.6    int abase = arrayOopDesc::base_offset_in_bytes(basic_elem_type);
     1.7 -  const intptr_t BIG_NEG = -128;
     1.8 -  assert(BIG_NEG + 2*abase < 0, "neg enough");
     1.9 -
    1.10 -  intptr_t src_off  = abase + ((intptr_t) find_int_con(src_offset, -1)  << scale);
    1.11 -  intptr_t dest_off = abase + ((intptr_t) find_int_con(dest_offset, -1) << scale);
    1.12 -  if (src_off < 0 || dest_off < 0)
    1.13 +
    1.14 +  intptr_t src_off_con  = (intptr_t) find_int_con(src_offset, -1);
    1.15 +  intptr_t dest_off_con = (intptr_t) find_int_con(dest_offset, -1);
    1.16 +  if (src_off_con < 0 || dest_off_con < 0)
    1.17      // At present, we can only understand constants.
    1.18      return false;
    1.19  
    1.20 +  intptr_t src_off  = abase + (src_off_con  << scale);
    1.21 +  intptr_t dest_off = abase + (dest_off_con << scale);
    1.22 +
    1.23    if (((src_off | dest_off) & (BytesPerLong-1)) != 0) {
    1.24      // Non-aligned; too bad.
    1.25      // One more chance:  Pick off an initial 32-bit word.
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/compiler/7047069/Test7047069.java	Fri May 27 12:47:48 2011 -0700
     2.3 @@ -0,0 +1,200 @@
     2.4 +/*
     2.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
     2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     2.7 + *
     2.8 + * This code is free software; you can redistribute it and/or modify it
     2.9 + * under the terms of the GNU General Public License version 2 only, as
    2.10 + * published by the Free Software Foundation.
    2.11 + *
    2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    2.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    2.15 + * version 2 for more details (a copy is included in the LICENSE file that
    2.16 + * accompanied this code).
    2.17 + *
    2.18 + * You should have received a copy of the GNU General Public License version
    2.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    2.21 + *
    2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    2.23 + * or visit www.oracle.com if you need additional information or have any
    2.24 + * questions.
    2.25 + *
    2.26 + */
    2.27 +
    2.28 +/**
    2.29 + * @test
    2.30 + * @bug 7047069
    2.31 + * @summary Array can dynamically change size when assigned to an object field
    2.32 + *
    2.33 + * @run main/othervm -Xbatch Test7047069
    2.34 + */
    2.35 +
    2.36 +import java.util.*;
    2.37 +import java.awt.geom.*;
    2.38 +
    2.39 +public class Test7047069 {
    2.40 +    static boolean verbose;
    2.41 +
    2.42 +    static final int GROW_SIZE = 24;    // Multiple of cubic & quad curve size
    2.43 +
    2.44 +    float squareflat;           // Square of the flatness parameter
    2.45 +                    // for testing against squared lengths
    2.46 +
    2.47 +    int limit;              // Maximum number of recursion levels
    2.48 +
    2.49 +    float hold[] = new float[14];   // The cache of interpolated coords
    2.50 +                    // Note that this must be long enough
    2.51 +                    // to store a full cubic segment and
    2.52 +                    // a relative cubic segment to avoid
    2.53 +                    // aliasing when copying the coords
    2.54 +                    // of a curve to the end of the array.
    2.55 +                    // This is also serendipitously equal
    2.56 +                    // to the size of a full quad segment
    2.57 +                    // and 2 relative quad segments.
    2.58 +
    2.59 +    int holdEnd;            // The index of the last curve segment
    2.60 +                    // being held for interpolation
    2.61 +
    2.62 +    int holdIndex;          // The index of the curve segment
    2.63 +                    // that was last interpolated.  This
    2.64 +                    // is the curve segment ready to be
    2.65 +                    // returned in the next call to
    2.66 +                    // currentSegment().
    2.67 +
    2.68 +    int levels[];           // The recursion level at which
    2.69 +                    // each curve being held in storage
    2.70 +                    // was generated.
    2.71 +
    2.72 +    int levelIndex;         // The index of the entry in the
    2.73 +                    // levels array of the curve segment
    2.74 +                    // at the holdIndex
    2.75 +
    2.76 +    public static void subdivide(float src[], int srcoff,
    2.77 +                                 float left[], int leftoff,
    2.78 +                                 float right[], int rightoff)
    2.79 +    {
    2.80 +        float x1 = src[srcoff + 0];
    2.81 +        float y1 = src[srcoff + 1];
    2.82 +        float ctrlx = src[srcoff + 2];
    2.83 +        float ctrly = src[srcoff + 3];
    2.84 +        float x2 = src[srcoff + 4];
    2.85 +        float y2 = src[srcoff + 5];
    2.86 +        if (left != null) {
    2.87 +            left[leftoff + 0] = x1;
    2.88 +            left[leftoff + 1] = y1;
    2.89 +        }
    2.90 +        if (right != null) {
    2.91 +            right[rightoff + 4] = x2;
    2.92 +            right[rightoff + 5] = y2;
    2.93 +        }
    2.94 +        x1 = (x1 + ctrlx) / 2f;
    2.95 +        y1 = (y1 + ctrly) / 2f;
    2.96 +        x2 = (x2 + ctrlx) / 2f;
    2.97 +        y2 = (y2 + ctrly) / 2f;
    2.98 +        ctrlx = (x1 + x2) / 2f;
    2.99 +        ctrly = (y1 + y2) / 2f;
   2.100 +        if (left != null) {
   2.101 +            left[leftoff + 2] = x1;
   2.102 +            left[leftoff + 3] = y1;
   2.103 +            left[leftoff + 4] = ctrlx;
   2.104 +            left[leftoff + 5] = ctrly;
   2.105 +        }
   2.106 +        if (right != null) {
   2.107 +            right[rightoff + 0] = ctrlx;
   2.108 +            right[rightoff + 1] = ctrly;
   2.109 +            right[rightoff + 2] = x2;
   2.110 +            right[rightoff + 3] = y2;
   2.111 +        }
   2.112 +    }
   2.113 +
   2.114 +    public static double getFlatnessSq(float coords[], int offset) {
   2.115 +        return Line2D.ptSegDistSq(coords[offset + 0], coords[offset + 1],
   2.116 +                                  coords[offset + 4], coords[offset + 5],
   2.117 +                                  coords[offset + 2], coords[offset + 3]);
   2.118 +    }
   2.119 +
   2.120 +    public Test7047069() {
   2.121 +        this.squareflat = .0001f * .0001f;
   2.122 +        holdIndex = hold.length - 6;
   2.123 +        holdEnd = hold.length - 2;
   2.124 +        hold[holdIndex + 0] = (float) (Math.random() * 100);
   2.125 +        hold[holdIndex + 1] = (float) (Math.random() * 100);
   2.126 +        hold[holdIndex + 2] = (float) (Math.random() * 100);
   2.127 +        hold[holdIndex + 3] = (float) (Math.random() * 100);
   2.128 +        hold[holdIndex + 4] = (float) (Math.random() * 100);
   2.129 +        hold[holdIndex + 5] = (float) (Math.random() * 100);
   2.130 +        levelIndex = 0;
   2.131 +        this.limit = 10;
   2.132 +        this.levels = new int[limit + 1];
   2.133 +    }
   2.134 +
   2.135 +    /*
   2.136 +     * Ensures that the hold array can hold up to (want) more values.
   2.137 +     * It is currently holding (hold.length - holdIndex) values.
   2.138 +     */
   2.139 +    void ensureHoldCapacity(int want) {
   2.140 +        if (holdIndex - want < 0) {
   2.141 +            int have = hold.length - holdIndex;
   2.142 +            int newsize = hold.length + GROW_SIZE;
   2.143 +            float newhold[] = new float[newsize];
   2.144 +            System.arraycopy(hold, holdIndex,
   2.145 +                     newhold, holdIndex + GROW_SIZE,
   2.146 +                     have);
   2.147 +            if (verbose) System.err.println("old hold = "+hold+"["+hold.length+"]");
   2.148 +            if (verbose) System.err.println("replacement hold = "+newhold+"["+newhold.length+"]");
   2.149 +            hold = newhold;
   2.150 +            if (verbose) System.err.println("new hold = "+hold+"["+hold.length+"]");
   2.151 +            if (verbose) System.err.println("replacement hold still = "+newhold+"["+newhold.length+"]");
   2.152 +            holdIndex += GROW_SIZE;
   2.153 +            holdEnd += GROW_SIZE;
   2.154 +        }
   2.155 +    }
   2.156 +
   2.157 +    private boolean next() {
   2.158 +        if (holdIndex >= holdEnd) {
   2.159 +            return false;
   2.160 +        }
   2.161 +
   2.162 +        int level = levels[levelIndex];
   2.163 +        while (level < limit) {
   2.164 +            if (getFlatnessSq(hold, holdIndex) < squareflat) {
   2.165 +                break;
   2.166 +            }
   2.167 +
   2.168 +            ensureHoldCapacity(4);
   2.169 +            subdivide(hold, holdIndex,
   2.170 +                      hold, holdIndex - 4,
   2.171 +                      hold, holdIndex);
   2.172 +            holdIndex -= 4;
   2.173 +
   2.174 +            // Now that we have subdivided, we have constructed
   2.175 +            // two curves of one depth lower than the original
   2.176 +            // curve.  One of those curves is in the place of
   2.177 +            // the former curve and one of them is in the next
   2.178 +            // set of held coordinate slots.  We now set both
   2.179 +            // curves level values to the next higher level.
   2.180 +            level++;
   2.181 +            levels[levelIndex] = level;
   2.182 +            levelIndex++;
   2.183 +            levels[levelIndex] = level;
   2.184 +        }
   2.185 +
   2.186 +        // This curve segment is flat enough, or it is too deep
   2.187 +        // in recursion levels to try to flatten any more.  The
   2.188 +        // two coordinates at holdIndex+4 and holdIndex+5 now
   2.189 +        // contain the endpoint of the curve which can be the
   2.190 +        // endpoint of an approximating line segment.
   2.191 +        holdIndex += 4;
   2.192 +        levelIndex--;
   2.193 +        return true;
   2.194 +    }
   2.195 +
   2.196 +    public static void main(String argv[]) {
   2.197 +        verbose = (argv.length > 0);
   2.198 +        for (int i = 0; i < 100000; i++) {
   2.199 +            Test7047069 st = new Test7047069();
   2.200 +            while (st.next()) {}
   2.201 +        }
   2.202 +    }
   2.203 +}

mercurial