OSDN Git Service

Use arc4random when available to select delta for image relocation.
authorAlex Light <allight@google.com>
Thu, 19 Nov 2015 19:03:10 +0000 (11:03 -0800)
committerAlex Light <allight@google.com>
Fri, 20 Nov 2015 00:47:49 +0000 (16:47 -0800)
Bug: 25776162

Change-Id: I1c4cc992977b5d6fe782ff819903a802a40391ee

runtime/gc/space/image_space.cc
runtime/utils.cc
runtime/utils.h

index 1fe9a03..e2b2431 100644 (file)
@@ -58,10 +58,7 @@ static int32_t ChooseRelocationOffsetDelta(int32_t min_delta, int32_t max_delta)
   CHECK_ALIGNED(max_delta, kPageSize);
   CHECK_LT(min_delta, max_delta);
 
-  std::default_random_engine generator;
-  generator.seed(NanoTime() * getpid());
-  std::uniform_int_distribution<int32_t> distribution(min_delta, max_delta);
-  int32_t r = distribution(generator);
+  int32_t r = GetRandomNumber<int32_t>(min_delta, max_delta);
   if (r % 2 == 0) {
     r = RoundUp(r, kPageSize);
   } else {
index 68db7e3..7da5640 100644 (file)
 
 #include <inttypes.h>
 #include <pthread.h>
+#include <stdlib.h>
 #include <sys/stat.h>
 #include <sys/syscall.h>
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <unistd.h>
 #include <memory>
+#include <random>
 
 #include "art_field-inl.h"
 #include "art_method-inl.h"
@@ -60,6 +62,26 @@ namespace art {
 static constexpr bool kUseAddr2line = !kIsTargetBuild;
 #endif
 
+#if defined(__BIONIC__)
+struct Arc4RandomGenerator {
+  typedef uint32_t result_type;
+  uint32_t min() { return std::numeric_limits<uint32_t>::min(); }
+  uint32_t max() { return std::numeric_limits<uint32_t>::max(); }
+  uint32_t operator() { return arc4random(); }
+};
+using RNG = Arc4RandomGenerator;
+#else
+using RNG = std::random_device;
+#endif
+
+template <typename T>
+T GetRandomNumber(T min, T max) {
+  CHECK_LT(min, max);
+  std::uniform_int_distribution<T> dist(min, max);
+  RNG rng;
+  return dist(rng);
+}
+
 pid_t GetTid() {
 #if defined(__APPLE__)
   uint64_t owner;
index 3690f86..c3684e0 100644 (file)
@@ -350,6 +350,9 @@ void ParseDouble(const std::string& option,
                  double* parsed_value,
                  UsageFn Usage);
 
+template <typename T>
+T GetRandomNumber(T min, T max);
+
 }  // namespace art
 
 #endif  // ART_RUNTIME_UTILS_H_