--- old/src/hotspot/share/ci/ciMethod.cpp 2019-05-30 13:31:19.862262285 +0800 +++ new/src/hotspot/share/ci/ciMethod.cpp 2019-05-30 13:31:19.630262150 +0800 @@ -462,6 +462,23 @@ // ------------------------------------------------------------------ +// ciMethod::saturated_add_int +// +// Update profile counters with saturating addition +// Wil check and handle the overflow condition +int ciMethod::saturated_add_int(int a, int b) { + jlong sum = a + b; + if (sum > max_jint) { + sum = max_jint; + } else if (sum < min_jint) { + sum = min_jint; + } + + return (int)sum; +} + + +// ------------------------------------------------------------------ // ciMethod::call_profile_at_bci // // Get the ciCallProfile for the invocation of this method. @@ -502,9 +519,9 @@ for (uint i = 0; i < call->row_limit(); i++) { ciKlass* receiver = call->receiver(i); if (receiver == NULL) continue; - int rcount = call->receiver_count(i) + epsilon; + int rcount = saturated_add_int(call->receiver_count(i), epsilon); if (rcount == 0) rcount = 1; // Should be valid value - receivers_count_total += rcount; + receivers_count_total = saturated_add_int(receivers_count_total, rcount); // Add the receiver to result data. result.add_receiver(receiver, rcount); // If we extend profiling to record methods, @@ -534,7 +551,7 @@ // do nothing. Otherwise, increase count to be the sum of all // receiver's counts. if (count >= 0) { - count += receivers_count_total; + count = saturated_add_int(count, receivers_count_total); } } result._count = count;