[gardening] Fix benign nullptr-dereference reported by UBSAN

Fixes https://github.com/dart-lang/sdk/issues/47341

TEST=Fixes service_2/break_on_function_many_child_isolates_test/service in UBSAN

Change-Id: I4de303e99604b9ffeca36a8e62bbaac421b1e7ca
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/215547
Reviewed-by: Alexander Aprelev <aam@google.com>
Commit-Queue: Martin Kustermann <kustermann@google.com>
diff --git a/runtime/vm/compiler/jit/compiler.cc b/runtime/vm/compiler/jit/compiler.cc
index c9f4f86..7c89806 100644
--- a/runtime/vm/compiler/jit/compiler.cc
+++ b/runtime/vm/compiler/jit/compiler.cc
@@ -243,7 +243,7 @@
     // immediately, causing an infinite compilation loop. The compiler raises
     // the threshold for functions with breakpoints, so we drop the unoptimized
     // to force it to be recompiled.
-    if (thread->isolate()->CanOptimizeImmediately()) {
+    if (CanOptimizeImmediately()) {
       function.ClearCode();
     }
     return false;
diff --git a/runtime/vm/compiler/jit/compiler.h b/runtime/vm/compiler/jit/compiler.h
index 456c70b..528a964 100644
--- a/runtime/vm/compiler/jit/compiler.h
+++ b/runtime/vm/compiler/jit/compiler.h
@@ -76,6 +76,15 @@
   // The result for a function may change if debugging gets turned on/off.
   static bool CanOptimizeFunction(Thread* thread, const Function& function);
 
+#if !defined(PRODUCT)
+  // Whether it's possible for unoptimized code to optimize immediately on entry
+  // (can happen with random or very low optimization counter thresholds)
+  static bool CanOptimizeImmediately() {
+    return FLAG_optimization_counter_threshold < 2 ||
+           FLAG_randomize_optimization_counter;
+  }
+#endif
+
   // Generates code for given function without optimization and sets its code
   // field.
   //
diff --git a/runtime/vm/isolate.h b/runtime/vm/isolate.h
index 21cd81c..cb402bd 100644
--- a/runtime/vm/isolate.h
+++ b/runtime/vm/isolate.h
@@ -1334,13 +1334,6 @@
     UpdateIsolateFlagsBit<IsKernelIsolateBit>(value);
   }
 
-  // Whether it's possible for unoptimized code to optimize immediately on entry
-  // (can happen with random or very low optimization counter thresholds)
-  bool CanOptimizeImmediately() const {
-    return FLAG_optimization_counter_threshold < 2 ||
-           FLAG_randomize_optimization_counter;
-  }
-
   const DispatchTable* dispatch_table() const {
     return group()->dispatch_table();
   }