Skip to content

Commit 0a57fe1

Browse files
author
Kim Barrett
committedOct 11, 2024
8341178: TypeRawPtr::add_offset may be "miscompiled" due to UB
Reviewed-by: dlong, kvn
1 parent 1f6bd0c commit 0a57fe1

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed
 

‎src/hotspot/share/opto/type.cpp

+13-7
Original file line numberDiff line numberDiff line change
@@ -3111,8 +3111,8 @@ const TypeRawPtr *TypeRawPtr::make( enum PTR ptr ) {
31113111
return (TypeRawPtr*)(new TypeRawPtr(ptr,nullptr))->hashcons();
31123112
}
31133113

3114-
const TypeRawPtr *TypeRawPtr::make( address bits ) {
3115-
assert( bits, "Use TypePtr for null" );
3114+
const TypeRawPtr *TypeRawPtr::make(address bits) {
3115+
assert(bits != nullptr, "Use TypePtr for null");
31163116
return (TypeRawPtr*)(new TypeRawPtr(Constant,bits))->hashcons();
31173117
}
31183118

@@ -3201,15 +3201,21 @@ const TypePtr* TypeRawPtr::add_offset(intptr_t offset) const {
32013201
case TypePtr::BotPTR:
32023202
case TypePtr::NotNull:
32033203
return this;
3204-
case TypePtr::Null:
32053204
case TypePtr::Constant: {
3206-
address bits = _bits+offset;
3207-
if ( bits == 0 ) return TypePtr::NULL_PTR;
3208-
return make( bits );
3205+
uintptr_t bits = (uintptr_t)_bits;
3206+
uintptr_t sum = bits + offset;
3207+
if (( offset < 0 )
3208+
? ( sum > bits ) // Underflow?
3209+
: ( sum < bits )) { // Overflow?
3210+
return BOTTOM;
3211+
} else if ( sum == 0 ) {
3212+
return TypePtr::NULL_PTR;
3213+
} else {
3214+
return make( (address)sum );
3215+
}
32093216
}
32103217
default: ShouldNotReachHere();
32113218
}
3212-
return nullptr; // Lint noise
32133219
}
32143220

32153221
//------------------------------eq---------------------------------------------

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on Oct 11, 2024

@openjdk-notifier[bot]
Please sign in to comment.