src/share/vm/utilities/copy.hpp

Mon, 03 Dec 2018 07:29:54 -0500

author
dbuck
date
Mon, 03 Dec 2018 07:29:54 -0500
changeset 9562
dee6a1ce4a0c
parent 2708
1d1603768966
child 9572
624a0741915c
permissions
-rw-r--r--

8141491: Unaligned memory access in Bits.c
Summary: Introduce alignment-safe Copy::conjoint_swap and JVM_CopySwapMemory
Reviewed-by: mikael, dholmes

duke@435 1 /*
dbuck@9562 2 * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #ifndef SHARE_VM_UTILITIES_COPY_HPP
stefank@2314 26 #define SHARE_VM_UTILITIES_COPY_HPP
stefank@2314 27
stefank@2314 28 #include "runtime/stubRoutines.hpp"
stefank@2314 29
duke@435 30 // Assembly code for platforms that need it.
duke@435 31 extern "C" {
duke@435 32 void _Copy_conjoint_words(HeapWord* from, HeapWord* to, size_t count);
duke@435 33 void _Copy_disjoint_words(HeapWord* from, HeapWord* to, size_t count);
duke@435 34
duke@435 35 void _Copy_conjoint_words_atomic(HeapWord* from, HeapWord* to, size_t count);
duke@435 36 void _Copy_disjoint_words_atomic(HeapWord* from, HeapWord* to, size_t count);
duke@435 37
duke@435 38 void _Copy_aligned_conjoint_words(HeapWord* from, HeapWord* to, size_t count);
duke@435 39 void _Copy_aligned_disjoint_words(HeapWord* from, HeapWord* to, size_t count);
duke@435 40
duke@435 41 void _Copy_conjoint_bytes(void* from, void* to, size_t count);
duke@435 42
duke@435 43 void _Copy_conjoint_bytes_atomic (void* from, void* to, size_t count);
duke@435 44 void _Copy_conjoint_jshorts_atomic(jshort* from, jshort* to, size_t count);
duke@435 45 void _Copy_conjoint_jints_atomic (jint* from, jint* to, size_t count);
duke@435 46 void _Copy_conjoint_jlongs_atomic (jlong* from, jlong* to, size_t count);
duke@435 47 void _Copy_conjoint_oops_atomic (oop* from, oop* to, size_t count);
duke@435 48
duke@435 49 void _Copy_arrayof_conjoint_bytes (HeapWord* from, HeapWord* to, size_t count);
duke@435 50 void _Copy_arrayof_conjoint_jshorts(HeapWord* from, HeapWord* to, size_t count);
duke@435 51 void _Copy_arrayof_conjoint_jints (HeapWord* from, HeapWord* to, size_t count);
duke@435 52 void _Copy_arrayof_conjoint_jlongs (HeapWord* from, HeapWord* to, size_t count);
duke@435 53 void _Copy_arrayof_conjoint_oops (HeapWord* from, HeapWord* to, size_t count);
duke@435 54 }
duke@435 55
duke@435 56 class Copy : AllStatic {
duke@435 57 public:
duke@435 58 // Block copy methods have four attributes. We don't define all possibilities.
kvn@1926 59 // alignment: aligned to BytesPerLong
duke@435 60 // arrayof: arraycopy operation with both operands aligned on the same
duke@435 61 // boundary as the first element of an array of the copy unit.
duke@435 62 // This is currently a HeapWord boundary on all platforms, except
duke@435 63 // for long and double arrays, which are aligned on an 8-byte
duke@435 64 // boundary on all platforms.
duke@435 65 // arraycopy operations are implicitly atomic on each array element.
duke@435 66 // overlap: disjoint or conjoint.
duke@435 67 // copy unit: bytes or words (i.e., HeapWords) or oops (i.e., pointers).
duke@435 68 // atomicity: atomic or non-atomic on the copy unit.
duke@435 69 //
duke@435 70 // Names are constructed thusly:
duke@435 71 //
duke@435 72 // [ 'aligned_' | 'arrayof_' ]
duke@435 73 // ('conjoint_' | 'disjoint_')
duke@435 74 // ('words' | 'bytes' | 'jshorts' | 'jints' | 'jlongs' | 'oops')
duke@435 75 // [ '_atomic' ]
duke@435 76 //
duke@435 77 // Except in the arrayof case, whatever the alignment is, we assume we can copy
kvn@1926 78 // whole alignment units. E.g., if BytesPerLong is 2x word alignment, an odd
duke@435 79 // count may copy an extra word. In the arrayof case, we are allowed to copy
duke@435 80 // only the number of copy units specified.
kvn@1958 81 //
kvn@1958 82 // All callees check count for 0.
kvn@1958 83 //
duke@435 84
duke@435 85 // HeapWords
duke@435 86
duke@435 87 // Word-aligned words, conjoint, not atomic on each word
duke@435 88 static void conjoint_words(HeapWord* from, HeapWord* to, size_t count) {
duke@435 89 assert_params_ok(from, to, LogHeapWordSize);
duke@435 90 pd_conjoint_words(from, to, count);
duke@435 91 }
duke@435 92
duke@435 93 // Word-aligned words, disjoint, not atomic on each word
duke@435 94 static void disjoint_words(HeapWord* from, HeapWord* to, size_t count) {
duke@435 95 assert_params_ok(from, to, LogHeapWordSize);
duke@435 96 assert_disjoint(from, to, count);
duke@435 97 pd_disjoint_words(from, to, count);
duke@435 98 }
duke@435 99
duke@435 100 // Word-aligned words, disjoint, atomic on each word
duke@435 101 static void disjoint_words_atomic(HeapWord* from, HeapWord* to, size_t count) {
duke@435 102 assert_params_ok(from, to, LogHeapWordSize);
duke@435 103 assert_disjoint(from, to, count);
duke@435 104 pd_disjoint_words_atomic(from, to, count);
duke@435 105 }
duke@435 106
duke@435 107 // Object-aligned words, conjoint, not atomic on each word
duke@435 108 static void aligned_conjoint_words(HeapWord* from, HeapWord* to, size_t count) {
duke@435 109 assert_params_aligned(from, to);
duke@435 110 pd_aligned_conjoint_words(from, to, count);
duke@435 111 }
duke@435 112
duke@435 113 // Object-aligned words, disjoint, not atomic on each word
duke@435 114 static void aligned_disjoint_words(HeapWord* from, HeapWord* to, size_t count) {
duke@435 115 assert_params_aligned(from, to);
duke@435 116 assert_disjoint(from, to, count);
duke@435 117 pd_aligned_disjoint_words(from, to, count);
duke@435 118 }
duke@435 119
duke@435 120 // bytes, jshorts, jints, jlongs, oops
duke@435 121
duke@435 122 // bytes, conjoint, not atomic on each byte (not that it matters)
kvn@1958 123 static void conjoint_jbytes(void* from, void* to, size_t count) {
duke@435 124 pd_conjoint_bytes(from, to, count);
duke@435 125 }
duke@435 126
duke@435 127 // bytes, conjoint, atomic on each byte (not that it matters)
kvn@1958 128 static void conjoint_jbytes_atomic(void* from, void* to, size_t count) {
duke@435 129 pd_conjoint_bytes(from, to, count);
duke@435 130 }
duke@435 131
duke@435 132 // jshorts, conjoint, atomic on each jshort
duke@435 133 static void conjoint_jshorts_atomic(jshort* from, jshort* to, size_t count) {
duke@435 134 assert_params_ok(from, to, LogBytesPerShort);
duke@435 135 pd_conjoint_jshorts_atomic(from, to, count);
duke@435 136 }
duke@435 137
duke@435 138 // jints, conjoint, atomic on each jint
duke@435 139 static void conjoint_jints_atomic(jint* from, jint* to, size_t count) {
duke@435 140 assert_params_ok(from, to, LogBytesPerInt);
duke@435 141 pd_conjoint_jints_atomic(from, to, count);
duke@435 142 }
duke@435 143
duke@435 144 // jlongs, conjoint, atomic on each jlong
duke@435 145 static void conjoint_jlongs_atomic(jlong* from, jlong* to, size_t count) {
duke@435 146 assert_params_ok(from, to, LogBytesPerLong);
duke@435 147 pd_conjoint_jlongs_atomic(from, to, count);
duke@435 148 }
duke@435 149
duke@435 150 // oops, conjoint, atomic on each oop
duke@435 151 static void conjoint_oops_atomic(oop* from, oop* to, size_t count) {
coleenp@548 152 assert_params_ok(from, to, LogBytesPerHeapOop);
duke@435 153 pd_conjoint_oops_atomic(from, to, count);
duke@435 154 }
duke@435 155
coleenp@548 156 // overloaded for UseCompressedOops
coleenp@548 157 static void conjoint_oops_atomic(narrowOop* from, narrowOop* to, size_t count) {
coleenp@548 158 assert(sizeof(narrowOop) == sizeof(jint), "this cast is wrong");
coleenp@548 159 assert_params_ok(from, to, LogBytesPerInt);
coleenp@548 160 pd_conjoint_jints_atomic((jint*)from, (jint*)to, count);
coleenp@548 161 }
coleenp@548 162
duke@435 163 // Copy a span of memory. If the span is an integral number of aligned
duke@435 164 // longs, words, or ints, copy those units atomically.
duke@435 165 // The largest atomic transfer unit is 8 bytes, or the largest power
duke@435 166 // of two which divides all of from, to, and size, whichever is smaller.
duke@435 167 static void conjoint_memory_atomic(void* from, void* to, size_t size);
duke@435 168
duke@435 169 // bytes, conjoint array, atomic on each byte (not that it matters)
kvn@1958 170 static void arrayof_conjoint_jbytes(HeapWord* from, HeapWord* to, size_t count) {
duke@435 171 pd_arrayof_conjoint_bytes(from, to, count);
duke@435 172 }
duke@435 173
duke@435 174 // jshorts, conjoint array, atomic on each jshort
duke@435 175 static void arrayof_conjoint_jshorts(HeapWord* from, HeapWord* to, size_t count) {
duke@435 176 assert_params_ok(from, to, LogBytesPerShort);
duke@435 177 pd_arrayof_conjoint_jshorts(from, to, count);
duke@435 178 }
duke@435 179
duke@435 180 // jints, conjoint array, atomic on each jint
duke@435 181 static void arrayof_conjoint_jints(HeapWord* from, HeapWord* to, size_t count) {
duke@435 182 assert_params_ok(from, to, LogBytesPerInt);
duke@435 183 pd_arrayof_conjoint_jints(from, to, count);
duke@435 184 }
duke@435 185
duke@435 186 // jlongs, conjoint array, atomic on each jlong
duke@435 187 static void arrayof_conjoint_jlongs(HeapWord* from, HeapWord* to, size_t count) {
duke@435 188 assert_params_ok(from, to, LogBytesPerLong);
duke@435 189 pd_arrayof_conjoint_jlongs(from, to, count);
duke@435 190 }
duke@435 191
duke@435 192 // oops, conjoint array, atomic on each oop
duke@435 193 static void arrayof_conjoint_oops(HeapWord* from, HeapWord* to, size_t count) {
coleenp@548 194 assert_params_ok(from, to, LogBytesPerHeapOop);
duke@435 195 pd_arrayof_conjoint_oops(from, to, count);
duke@435 196 }
duke@435 197
duke@435 198 // Known overlap methods
duke@435 199
duke@435 200 // Copy word-aligned words from higher to lower addresses, not atomic on each word
duke@435 201 inline static void conjoint_words_to_lower(HeapWord* from, HeapWord* to, size_t byte_count) {
duke@435 202 // byte_count is in bytes to check its alignment
duke@435 203 assert_params_ok(from, to, LogHeapWordSize);
duke@435 204 assert_byte_count_ok(byte_count, HeapWordSize);
duke@435 205
duke@435 206 size_t count = (size_t)round_to(byte_count, HeapWordSize) >> LogHeapWordSize;
duke@435 207 assert(to <= from || from + count <= to, "do not overwrite source data");
duke@435 208
duke@435 209 while (count-- > 0) {
duke@435 210 *to++ = *from++;
duke@435 211 }
duke@435 212 }
duke@435 213
duke@435 214 // Copy word-aligned words from lower to higher addresses, not atomic on each word
duke@435 215 inline static void conjoint_words_to_higher(HeapWord* from, HeapWord* to, size_t byte_count) {
duke@435 216 // byte_count is in bytes to check its alignment
duke@435 217 assert_params_ok(from, to, LogHeapWordSize);
duke@435 218 assert_byte_count_ok(byte_count, HeapWordSize);
duke@435 219
duke@435 220 size_t count = (size_t)round_to(byte_count, HeapWordSize) >> LogHeapWordSize;
duke@435 221 assert(from <= to || to + count <= from, "do not overwrite source data");
duke@435 222
duke@435 223 from += count - 1;
duke@435 224 to += count - 1;
duke@435 225 while (count-- > 0) {
duke@435 226 *to-- = *from--;
duke@435 227 }
duke@435 228 }
duke@435 229
dbuck@9562 230 /**
dbuck@9562 231 * Copy and *unconditionally* byte swap elements
dbuck@9562 232 *
dbuck@9562 233 * @param src address of source
dbuck@9562 234 * @param dst address of destination
dbuck@9562 235 * @param byte_count number of bytes to copy
dbuck@9562 236 * @param elem_size size of the elements to copy-swap
dbuck@9562 237 */
dbuck@9562 238 static void conjoint_swap(address src, address dst, size_t byte_count, size_t elem_size);
dbuck@9562 239
duke@435 240 // Fill methods
duke@435 241
duke@435 242 // Fill word-aligned words, not atomic on each word
duke@435 243 // set_words
duke@435 244 static void fill_to_words(HeapWord* to, size_t count, juint value = 0) {
duke@435 245 assert_params_ok(to, LogHeapWordSize);
duke@435 246 pd_fill_to_words(to, count, value);
duke@435 247 }
duke@435 248
duke@435 249 static void fill_to_aligned_words(HeapWord* to, size_t count, juint value = 0) {
duke@435 250 assert_params_aligned(to);
duke@435 251 pd_fill_to_aligned_words(to, count, value);
duke@435 252 }
duke@435 253
duke@435 254 // Fill bytes
duke@435 255 static void fill_to_bytes(void* to, size_t count, jubyte value = 0) {
duke@435 256 pd_fill_to_bytes(to, count, value);
duke@435 257 }
duke@435 258
duke@435 259 // Fill a span of memory. If the span is an integral number of aligned
duke@435 260 // longs, words, or ints, store to those units atomically.
duke@435 261 // The largest atomic transfer unit is 8 bytes, or the largest power
duke@435 262 // of two which divides both to and size, whichever is smaller.
duke@435 263 static void fill_to_memory_atomic(void* to, size_t size, jubyte value = 0);
duke@435 264
duke@435 265 // Zero-fill methods
duke@435 266
duke@435 267 // Zero word-aligned words, not atomic on each word
duke@435 268 static void zero_to_words(HeapWord* to, size_t count) {
duke@435 269 assert_params_ok(to, LogHeapWordSize);
duke@435 270 pd_zero_to_words(to, count);
duke@435 271 }
duke@435 272
duke@435 273 // Zero bytes
duke@435 274 static void zero_to_bytes(void* to, size_t count) {
duke@435 275 pd_zero_to_bytes(to, count);
duke@435 276 }
duke@435 277
duke@435 278 private:
duke@435 279 static bool params_disjoint(HeapWord* from, HeapWord* to, size_t count) {
duke@435 280 if (from < to) {
duke@435 281 return pointer_delta(to, from) >= count;
duke@435 282 }
duke@435 283 return pointer_delta(from, to) >= count;
duke@435 284 }
duke@435 285
duke@435 286 // These methods raise a fatal if they detect a problem.
duke@435 287
duke@435 288 static void assert_disjoint(HeapWord* from, HeapWord* to, size_t count) {
duke@435 289 #ifdef ASSERT
duke@435 290 if (!params_disjoint(from, to, count))
duke@435 291 basic_fatal("source and dest overlap");
duke@435 292 #endif
duke@435 293 }
duke@435 294
duke@435 295 static void assert_params_ok(void* from, void* to, intptr_t log_align) {
duke@435 296 #ifdef ASSERT
duke@435 297 if (mask_bits((uintptr_t)from, right_n_bits(log_align)) != 0)
duke@435 298 basic_fatal("not aligned");
duke@435 299 if (mask_bits((uintptr_t)to, right_n_bits(log_align)) != 0)
duke@435 300 basic_fatal("not aligned");
duke@435 301 #endif
duke@435 302 }
duke@435 303
duke@435 304 static void assert_params_ok(HeapWord* to, intptr_t log_align) {
duke@435 305 #ifdef ASSERT
duke@435 306 if (mask_bits((uintptr_t)to, right_n_bits(log_align)) != 0)
duke@435 307 basic_fatal("not word aligned");
duke@435 308 #endif
duke@435 309 }
duke@435 310 static void assert_params_aligned(HeapWord* from, HeapWord* to) {
duke@435 311 #ifdef ASSERT
kvn@1926 312 if (mask_bits((uintptr_t)from, BytesPerLong-1) != 0)
kvn@1926 313 basic_fatal("not long aligned");
kvn@1926 314 if (mask_bits((uintptr_t)to, BytesPerLong-1) != 0)
kvn@1926 315 basic_fatal("not long aligned");
duke@435 316 #endif
duke@435 317 }
duke@435 318
duke@435 319 static void assert_params_aligned(HeapWord* to) {
duke@435 320 #ifdef ASSERT
kvn@1926 321 if (mask_bits((uintptr_t)to, BytesPerLong-1) != 0)
kvn@1926 322 basic_fatal("not long aligned");
duke@435 323 #endif
duke@435 324 }
duke@435 325
duke@435 326 static void assert_byte_count_ok(size_t byte_count, size_t unit_size) {
duke@435 327 #ifdef ASSERT
duke@435 328 if ((size_t)round_to(byte_count, unit_size) != byte_count) {
duke@435 329 basic_fatal("byte count must be aligned");
duke@435 330 }
duke@435 331 #endif
duke@435 332 }
duke@435 333
duke@435 334 // Platform dependent implementations of the above methods.
stefank@2314 335 #ifdef TARGET_ARCH_x86
stefank@2314 336 # include "copy_x86.hpp"
stefank@2314 337 #endif
stefank@2314 338 #ifdef TARGET_ARCH_sparc
stefank@2314 339 # include "copy_sparc.hpp"
stefank@2314 340 #endif
stefank@2314 341 #ifdef TARGET_ARCH_zero
stefank@2314 342 # include "copy_zero.hpp"
stefank@2314 343 #endif
bobv@2508 344 #ifdef TARGET_ARCH_arm
bobv@2508 345 # include "copy_arm.hpp"
bobv@2508 346 #endif
bobv@2508 347 #ifdef TARGET_ARCH_ppc
bobv@2508 348 # include "copy_ppc.hpp"
bobv@2508 349 #endif
stefank@2314 350
duke@435 351 };
stefank@2314 352
stefank@2314 353 #endif // SHARE_VM_UTILITIES_COPY_HPP

mercurial