Pass sampled value to hidden api access logger

The hidden api access logger can use multiple logging methods, each with
its own sampling rate. ART will only be aware of the maximum sampling
rate, and it will be up to the logger to choose which logging method or
methods will be chosen.

Test: m
Bug: 119217680

(cherry picked from commit 5746f795e254fc74ab54ba4e59bc0c5e994efb03)

Change-Id: Ida8d494cb280cb94cd27adf1264776d3e9306471
Merged-In: I888e55c76ea7a032c35e880a981d1fc9dd4ba6b6
diff --git a/runtime/hidden_api.cc b/runtime/hidden_api.cc
index 1279997..95289b2 100644
--- a/runtime/hidden_api.cc
+++ b/runtime/hidden_api.cc
@@ -173,7 +173,9 @@
   return member_name_ == other.member_name_ && type_signature_ == other.type_signature_;
 }
 
-void MemberSignature::LogAccessToEventLog(AccessMethod access_method, bool access_denied) {
+void MemberSignature::LogAccessToEventLog(uint32_t sampled_value,
+                                          AccessMethod access_method,
+                                          bool access_denied) {
 #ifdef ART_TARGET_ANDROID
   if (access_method == AccessMethod::kLinking || access_method == AccessMethod::kNone) {
     // Linking warnings come from static analysis/compilation of the bytecode
@@ -202,13 +204,18 @@
     LOG(ERROR) << "Unable to allocate string for hidden api method signature";
   }
   env->CallStaticVoidMethod(WellKnownClasses::dalvik_system_VMRuntime,
-      WellKnownClasses::dalvik_system_VMRuntime_hiddenApiUsed, package_str.get(),
-      signature_jstr.get(), static_cast<jint>(access_method), access_denied);
+      WellKnownClasses::dalvik_system_VMRuntime_hiddenApiUsed,
+      sampled_value,
+      package_str.get(),
+      signature_jstr.get(),
+      static_cast<jint>(access_method),
+      access_denied);
   if (env->ExceptionCheck()) {
     env->ExceptionClear();
     LOG(ERROR) << "Unable to report hidden api usage";
   }
 #else
+  UNUSED(sampled_value);
   UNUSED(access_method);
   UNUSED(access_denied);
 #endif
@@ -394,9 +401,11 @@
       uint32_t eventLogSampleRate = runtime->GetHiddenApiEventLogSampleRate();
       // Assert that RAND_MAX is big enough, to ensure sampling below works as expected.
       static_assert(RAND_MAX >= 0xffff, "RAND_MAX too small");
-      if (eventLogSampleRate != 0 &&
-          (static_cast<uint32_t>(std::rand()) & 0xffff) < eventLogSampleRate) {
-        member_signature.LogAccessToEventLog(access_method, deny_access);
+      if (eventLogSampleRate != 0) {
+        const uint32_t sampled_value = static_cast<uint32_t>(std::rand()) & 0xffff;
+        if (sampled_value < eventLogSampleRate) {
+          member_signature.LogAccessToEventLog(sampled_value, access_method, deny_access);
+        }
       }
     }