Skip to content

Commit d2dfbd8

Browse files
author
Satyen Subramaniam
committed
8341178: TypeRawPtr::add_offset may be "miscompiled" due to UB
Backport-of: 0a57fe1df6f3431cfb2d5d868597c61ef6af3806
1 parent 02a45b0 commit d2dfbd8

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/hotspot/share/opto/type.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3125,8 +3125,8 @@ const TypeRawPtr *TypeRawPtr::make( enum PTR ptr ) {
31253125
return (TypeRawPtr*)(new TypeRawPtr(ptr,0))->hashcons();
31263126
}
31273127

3128-
const TypeRawPtr *TypeRawPtr::make( address bits ) {
3129-
assert( bits, "Use TypePtr for null" );
3128+
const TypeRawPtr *TypeRawPtr::make(address bits) {
3129+
assert(bits != nullptr, "Use TypePtr for null");
31303130
return (TypeRawPtr*)(new TypeRawPtr(Constant,bits))->hashcons();
31313131
}
31323132

@@ -3215,15 +3215,21 @@ const TypePtr* TypeRawPtr::add_offset(intptr_t offset) const {
32153215
case TypePtr::BotPTR:
32163216
case TypePtr::NotNull:
32173217
return this;
3218-
case TypePtr::Null:
32193218
case TypePtr::Constant: {
3220-
address bits = _bits+offset;
3221-
if ( bits == 0 ) return TypePtr::NULL_PTR;
3222-
return make( bits );
3219+
uintptr_t bits = (uintptr_t)_bits;
3220+
uintptr_t sum = bits + offset;
3221+
if (( offset < 0 )
3222+
? ( sum > bits ) // Underflow?
3223+
: ( sum < bits )) { // Overflow?
3224+
return BOTTOM;
3225+
} else if ( sum == 0 ) {
3226+
return TypePtr::NULL_PTR;
3227+
} else {
3228+
return make( (address)sum );
3229+
}
32233230
}
32243231
default: ShouldNotReachHere();
32253232
}
3226-
return nullptr; // Lint noise
32273233
}
32283234

32293235
//------------------------------eq---------------------------------------------

0 commit comments

Comments
 (0)