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
Change-Id: 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);
+        }
       }
     }
 
diff --git a/runtime/hidden_api.h b/runtime/hidden_api.h
index 0bdb5c8..2b876dc 100644
--- a/runtime/hidden_api.h
+++ b/runtime/hidden_api.h
@@ -197,7 +197,7 @@
 
   void WarnAboutAccess(AccessMethod access_method, ApiList list);
 
-  void LogAccessToEventLog(AccessMethod access_method, bool access_denied);
+  void LogAccessToEventLog(uint32_t sampled_value, AccessMethod access_method, bool access_denied);
 
   // Calls back into managed code to notify VMRuntime.nonSdkApiUsageConsumer that
   // |member| was accessed. This is usually called when an API is on the black,
diff --git a/runtime/well_known_classes.cc b/runtime/well_known_classes.cc
index 19fbf63..db90ae8 100644
--- a/runtime/well_known_classes.cc
+++ b/runtime/well_known_classes.cc
@@ -347,7 +347,7 @@
 
   dalvik_system_BaseDexClassLoader_getLdLibraryPath = CacheMethod(env, dalvik_system_BaseDexClassLoader, false, "getLdLibraryPath", "()Ljava/lang/String;");
   dalvik_system_VMRuntime_runFinalization = CacheMethod(env, dalvik_system_VMRuntime, true, "runFinalization", "(J)V");
-  dalvik_system_VMRuntime_hiddenApiUsed = CacheMethod(env, dalvik_system_VMRuntime, true, "hiddenApiUsed", "(Ljava/lang/String;Ljava/lang/String;IZ)V");
+  dalvik_system_VMRuntime_hiddenApiUsed = CacheMethod(env, dalvik_system_VMRuntime, true, "hiddenApiUsed", "(ILjava/lang/String;Ljava/lang/String;IZ)V");
   java_lang_ClassNotFoundException_init = CacheMethod(env, java_lang_ClassNotFoundException, false, "<init>", "(Ljava/lang/String;Ljava/lang/Throwable;)V");
   java_lang_ClassLoader_loadClass = CacheMethod(env, java_lang_ClassLoader, false, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;");