--- old/src/hotspot/share/ci/ciMethod.cpp 2019-05-31 17:24:09.966120054 +0800 +++ new/src/hotspot/share/ci/ciMethod.cpp 2019-05-31 17:24:09.746135784 +0800 @@ -462,6 +462,26 @@ // ------------------------------------------------------------------ +// ciMethod::saturated_add +// +// Update profile counters with saturating addition +// Wil check and handle the overflow condition +template +int ciMethod::saturated_add(L a, R b) { + jlong src1 = a; + jlong src2 = b; + jlong sum = src1 + src2; + 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 +522,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(call->receiver_count(i), epsilon); if (rcount == 0) rcount = 1; // Should be valid value - receivers_count_total += rcount; + receivers_count_total = saturated_add(receivers_count_total, rcount); // Add the receiver to result data. result.add_receiver(receiver, rcount); // If we extend profiling to record methods, @@ -534,7 +554,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(count, receivers_count_total); } } result._count = count;