8250844: Make sure {type,obj}ArrayOopDesc accessors check the bounds

Reviewed-by: rrich, coleenp
This commit is contained in:
Aleksey Shipilev 2020-08-02 16:58:14 +02:00
parent aab365f746
commit ddb726d4a0
2 changed files with 28 additions and 2 deletions

View File

@ -35,21 +35,23 @@ inline HeapWord* objArrayOopDesc::base() const { return (HeapWord*) arrayOopDesc
inline HeapWord* objArrayOopDesc::base_raw() const { return (HeapWord*) arrayOopDesc::base_raw(T_OBJECT); } inline HeapWord* objArrayOopDesc::base_raw() const { return (HeapWord*) arrayOopDesc::base_raw(T_OBJECT); }
template <class T> T* objArrayOopDesc::obj_at_addr(int index) const { template <class T> T* objArrayOopDesc::obj_at_addr(int index) const {
assert(is_within_bounds(index), "index out of bounds"); assert(is_within_bounds(index), "index %d out of bounds %d", index, length());
return &((T*)base())[index]; return &((T*)base())[index];
} }
template <class T> T* objArrayOopDesc::obj_at_addr_raw(int index) const { template <class T> T* objArrayOopDesc::obj_at_addr_raw(int index) const {
assert(is_within_bounds(index), "index out of bounds"); assert(is_within_bounds(index), "index %d out of bounds %d", index, length());
return &((T*)base_raw())[index]; return &((T*)base_raw())[index];
} }
inline oop objArrayOopDesc::obj_at(int index) const { inline oop objArrayOopDesc::obj_at(int index) const {
assert(is_within_bounds(index), "index %d out of bounds %d", index, length());
ptrdiff_t offset = UseCompressedOops ? obj_at_offset<narrowOop>(index) : obj_at_offset<oop>(index); ptrdiff_t offset = UseCompressedOops ? obj_at_offset<narrowOop>(index) : obj_at_offset<oop>(index);
return HeapAccess<IS_ARRAY>::oop_load_at(as_oop(), offset); return HeapAccess<IS_ARRAY>::oop_load_at(as_oop(), offset);
} }
inline void objArrayOopDesc::obj_at_put(int index, oop value) { inline void objArrayOopDesc::obj_at_put(int index, oop value) {
assert(is_within_bounds(index), "index %d out of bounds %d", index, length());
ptrdiff_t offset = UseCompressedOops ? obj_at_offset<narrowOop>(index) : obj_at_offset<oop>(index); ptrdiff_t offset = UseCompressedOops ? obj_at_offset<narrowOop>(index) : obj_at_offset<oop>(index);
HeapAccess<IS_ARRAY>::oop_store_at(as_oop(), offset, value); HeapAccess<IS_ARRAY>::oop_store_at(as_oop(), offset, value);
} }

View File

@ -90,91 +90,111 @@ inline jdouble* typeArrayOopDesc::double_at_addr(int which) const {
} }
inline jbyte typeArrayOopDesc::byte_at(int which) const { inline jbyte typeArrayOopDesc::byte_at(int which) const {
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
ptrdiff_t offset = element_offset<jbyte>(which); ptrdiff_t offset = element_offset<jbyte>(which);
return HeapAccess<IS_ARRAY>::load_at(as_oop(), offset); return HeapAccess<IS_ARRAY>::load_at(as_oop(), offset);
} }
inline void typeArrayOopDesc::byte_at_put(int which, jbyte contents) { inline void typeArrayOopDesc::byte_at_put(int which, jbyte contents) {
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
ptrdiff_t offset = element_offset<jbyte>(which); ptrdiff_t offset = element_offset<jbyte>(which);
HeapAccess<IS_ARRAY>::store_at(as_oop(), offset, contents); HeapAccess<IS_ARRAY>::store_at(as_oop(), offset, contents);
} }
inline jboolean typeArrayOopDesc::bool_at(int which) const { inline jboolean typeArrayOopDesc::bool_at(int which) const {
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
ptrdiff_t offset = element_offset<jboolean>(which); ptrdiff_t offset = element_offset<jboolean>(which);
return HeapAccess<IS_ARRAY>::load_at(as_oop(), offset); return HeapAccess<IS_ARRAY>::load_at(as_oop(), offset);
} }
inline void typeArrayOopDesc::bool_at_put(int which, jboolean contents) { inline void typeArrayOopDesc::bool_at_put(int which, jboolean contents) {
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
ptrdiff_t offset = element_offset<jboolean>(which); ptrdiff_t offset = element_offset<jboolean>(which);
HeapAccess<IS_ARRAY>::store_at(as_oop(), offset, jboolean(contents & 1)); HeapAccess<IS_ARRAY>::store_at(as_oop(), offset, jboolean(contents & 1));
} }
inline jchar typeArrayOopDesc::char_at(int which) const { inline jchar typeArrayOopDesc::char_at(int which) const {
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
ptrdiff_t offset = element_offset<jchar>(which); ptrdiff_t offset = element_offset<jchar>(which);
return HeapAccess<IS_ARRAY>::load_at(as_oop(), offset); return HeapAccess<IS_ARRAY>::load_at(as_oop(), offset);
} }
inline void typeArrayOopDesc::char_at_put(int which, jchar contents) { inline void typeArrayOopDesc::char_at_put(int which, jchar contents) {
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
ptrdiff_t offset = element_offset<jchar>(which); ptrdiff_t offset = element_offset<jchar>(which);
HeapAccess<IS_ARRAY>::store_at(as_oop(), offset, contents); HeapAccess<IS_ARRAY>::store_at(as_oop(), offset, contents);
} }
inline jint typeArrayOopDesc::int_at(int which) const { inline jint typeArrayOopDesc::int_at(int which) const {
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
ptrdiff_t offset = element_offset<jint>(which); ptrdiff_t offset = element_offset<jint>(which);
return HeapAccess<IS_ARRAY>::load_at(as_oop(), offset); return HeapAccess<IS_ARRAY>::load_at(as_oop(), offset);
} }
inline void typeArrayOopDesc::int_at_put(int which, jint contents) { inline void typeArrayOopDesc::int_at_put(int which, jint contents) {
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
ptrdiff_t offset = element_offset<jint>(which); ptrdiff_t offset = element_offset<jint>(which);
HeapAccess<IS_ARRAY>::store_at(as_oop(), offset, contents); HeapAccess<IS_ARRAY>::store_at(as_oop(), offset, contents);
} }
inline jshort typeArrayOopDesc::short_at(int which) const { inline jshort typeArrayOopDesc::short_at(int which) const {
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
ptrdiff_t offset = element_offset<jshort>(which); ptrdiff_t offset = element_offset<jshort>(which);
return HeapAccess<IS_ARRAY>::load_at(as_oop(), offset); return HeapAccess<IS_ARRAY>::load_at(as_oop(), offset);
} }
inline void typeArrayOopDesc::short_at_put(int which, jshort contents) { inline void typeArrayOopDesc::short_at_put(int which, jshort contents) {
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
ptrdiff_t offset = element_offset<jshort>(which); ptrdiff_t offset = element_offset<jshort>(which);
HeapAccess<IS_ARRAY>::store_at(as_oop(), offset, contents); HeapAccess<IS_ARRAY>::store_at(as_oop(), offset, contents);
} }
inline jushort typeArrayOopDesc::ushort_at(int which) const { inline jushort typeArrayOopDesc::ushort_at(int which) const {
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
ptrdiff_t offset = element_offset<jushort>(which); ptrdiff_t offset = element_offset<jushort>(which);
return HeapAccess<IS_ARRAY>::load_at(as_oop(), offset); return HeapAccess<IS_ARRAY>::load_at(as_oop(), offset);
} }
inline void typeArrayOopDesc::ushort_at_put(int which, jushort contents) { inline void typeArrayOopDesc::ushort_at_put(int which, jushort contents) {
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
ptrdiff_t offset = element_offset<jushort>(which); ptrdiff_t offset = element_offset<jushort>(which);
HeapAccess<IS_ARRAY>::store_at(as_oop(), offset, contents); HeapAccess<IS_ARRAY>::store_at(as_oop(), offset, contents);
} }
inline jlong typeArrayOopDesc::long_at(int which) const { inline jlong typeArrayOopDesc::long_at(int which) const {
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
ptrdiff_t offset = element_offset<jlong>(which); ptrdiff_t offset = element_offset<jlong>(which);
return HeapAccess<IS_ARRAY>::load_at(as_oop(), offset); return HeapAccess<IS_ARRAY>::load_at(as_oop(), offset);
} }
inline void typeArrayOopDesc::long_at_put(int which, jlong contents) { inline void typeArrayOopDesc::long_at_put(int which, jlong contents) {
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
ptrdiff_t offset = element_offset<jlong>(which); ptrdiff_t offset = element_offset<jlong>(which);
HeapAccess<IS_ARRAY>::store_at(as_oop(), offset, contents); HeapAccess<IS_ARRAY>::store_at(as_oop(), offset, contents);
} }
inline jfloat typeArrayOopDesc::float_at(int which) const { inline jfloat typeArrayOopDesc::float_at(int which) const {
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
ptrdiff_t offset = element_offset<jfloat>(which); ptrdiff_t offset = element_offset<jfloat>(which);
return HeapAccess<IS_ARRAY>::load_at(as_oop(), offset); return HeapAccess<IS_ARRAY>::load_at(as_oop(), offset);
} }
inline void typeArrayOopDesc::float_at_put(int which, jfloat contents) { inline void typeArrayOopDesc::float_at_put(int which, jfloat contents) {
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
ptrdiff_t offset = element_offset<jfloat>(which); ptrdiff_t offset = element_offset<jfloat>(which);
HeapAccess<IS_ARRAY>::store_at(as_oop(), offset, contents); HeapAccess<IS_ARRAY>::store_at(as_oop(), offset, contents);
} }
inline jdouble typeArrayOopDesc::double_at(int which) const { inline jdouble typeArrayOopDesc::double_at(int which) const {
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
ptrdiff_t offset = element_offset<jdouble>(which); ptrdiff_t offset = element_offset<jdouble>(which);
return HeapAccess<IS_ARRAY>::load_at(as_oop(), offset); return HeapAccess<IS_ARRAY>::load_at(as_oop(), offset);
} }
inline void typeArrayOopDesc::double_at_put(int which, jdouble contents) { inline void typeArrayOopDesc::double_at_put(int which, jdouble contents) {
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
ptrdiff_t offset = element_offset<jdouble>(which); ptrdiff_t offset = element_offset<jdouble>(which);
HeapAccess<IS_ARRAY>::store_at(as_oop(), offset, contents); HeapAccess<IS_ARRAY>::store_at(as_oop(), offset, contents);
} }
inline jbyte typeArrayOopDesc::byte_at_acquire(int which) const { inline jbyte typeArrayOopDesc::byte_at_acquire(int which) const {
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
ptrdiff_t offset = element_offset<jbyte>(which); ptrdiff_t offset = element_offset<jbyte>(which);
return HeapAccess<MO_ACQUIRE | IS_ARRAY>::load_at(as_oop(), offset); return HeapAccess<MO_ACQUIRE | IS_ARRAY>::load_at(as_oop(), offset);
} }
inline void typeArrayOopDesc::release_byte_at_put(int which, jbyte contents) { inline void typeArrayOopDesc::release_byte_at_put(int which, jbyte contents) {
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
ptrdiff_t offset = element_offset<jbyte>(which); ptrdiff_t offset = element_offset<jbyte>(which);
HeapAccess<MO_RELEASE | IS_ARRAY>::store_at(as_oop(), offset, contents); HeapAccess<MO_RELEASE | IS_ARRAY>::store_at(as_oop(), offset, contents);
} }
@ -184,19 +204,23 @@ inline void typeArrayOopDesc::release_byte_at_put(int which, jbyte contents) {
// casting // casting
#ifdef _LP64 #ifdef _LP64
inline Symbol* typeArrayOopDesc::symbol_at(int which) const { inline Symbol* typeArrayOopDesc::symbol_at(int which) const {
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
ptrdiff_t offset = element_offset<jlong>(which); ptrdiff_t offset = element_offset<jlong>(which);
return (Symbol*)(jlong) HeapAccess<IS_ARRAY>::load_at(as_oop(), offset); return (Symbol*)(jlong) HeapAccess<IS_ARRAY>::load_at(as_oop(), offset);
} }
inline void typeArrayOopDesc::symbol_at_put(int which, Symbol* contents) { inline void typeArrayOopDesc::symbol_at_put(int which, Symbol* contents) {
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
ptrdiff_t offset = element_offset<jlong>(which); ptrdiff_t offset = element_offset<jlong>(which);
HeapAccess<IS_ARRAY>::store_at(as_oop(), offset, (jlong)contents); HeapAccess<IS_ARRAY>::store_at(as_oop(), offset, (jlong)contents);
} }
#else #else
inline Symbol* typeArrayOopDesc::symbol_at(int which) const { inline Symbol* typeArrayOopDesc::symbol_at(int which) const {
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
ptrdiff_t offset = element_offset<jint>(which); ptrdiff_t offset = element_offset<jint>(which);
return (Symbol*)(jint) HeapAccess<IS_ARRAY>::load_at(as_oop(), offset); return (Symbol*)(jint) HeapAccess<IS_ARRAY>::load_at(as_oop(), offset);
} }
inline void typeArrayOopDesc::symbol_at_put(int which, Symbol* contents) { inline void typeArrayOopDesc::symbol_at_put(int which, Symbol* contents) {
assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
ptrdiff_t offset = element_offset<jint>(which); ptrdiff_t offset = element_offset<jint>(which);
HeapAccess<IS_ARRAY>::store_at(as_oop(), offset, (jint)contents); HeapAccess<IS_ARRAY>::store_at(as_oop(), offset, (jint)contents);
} }