Merge "Load libraries in breadth-first order"
diff --git a/libc/arch-arm64/bionic/crtbegin.c b/libc/arch-arm64/bionic/crtbegin.c
index fec0b11..11ba49f 100644
--- a/libc/arch-arm64/bionic/crtbegin.c
+++ b/libc/arch-arm64/bionic/crtbegin.c
@@ -36,15 +36,10 @@
 __attribute__ ((section (".init_array")))
 void (*__INIT_ARRAY__)(void) = (void (*)(void)) -1;
 
-__attribute__ ((section (".fini_array")))
-void (*__FINI_ARRAY__)(void) = (void (*)(void)) -1;
-
-
 __LIBC_HIDDEN__ void do_arm64_start(void* raw_args) {
   structors_array_t array;
   array.preinit_array = &__PREINIT_ARRAY__;
   array.init_array = &__INIT_ARRAY__;
-  array.fini_array = &__FINI_ARRAY__;
   __libc_init(raw_args, NULL, &main, &array);
 }
 
diff --git a/libc/arch-common/bionic/crtbegin.c b/libc/arch-common/bionic/crtbegin.c
index fa9f3f3..dad265e 100644
--- a/libc/arch-common/bionic/crtbegin.c
+++ b/libc/arch-common/bionic/crtbegin.c
@@ -36,9 +36,6 @@
 __attribute__ ((section (".init_array")))
 void (*__INIT_ARRAY__)(void) = (void (*)(void)) -1;
 
-__attribute__ ((section (".fini_array")))
-void (*__FINI_ARRAY__)(void) = (void (*)(void)) -1;
-
 __LIBC_HIDDEN__
 #ifdef __i386__
 __attribute__((force_align_arg_pointer))
@@ -47,7 +44,6 @@
   structors_array_t array;
   array.preinit_array = &__PREINIT_ARRAY__;
   array.init_array = &__INIT_ARRAY__;
-  array.fini_array = &__FINI_ARRAY__;
 
   void* raw_args = (void*) ((uintptr_t) __builtin_frame_address(0) + sizeof(void*));
 #ifdef __x86_64__
diff --git a/libc/arch-mips/bionic/crtbegin.c b/libc/arch-mips/bionic/crtbegin.c
index 28e8817..33f7f40 100644
--- a/libc/arch-mips/bionic/crtbegin.c
+++ b/libc/arch-mips/bionic/crtbegin.c
@@ -36,15 +36,11 @@
 __attribute__ ((section (".init_array")))
 void (*__INIT_ARRAY__)(void) = (void (*)(void)) -1;
 
-__attribute__ ((section (".fini_array")))
-void (*__FINI_ARRAY__)(void) = (void (*)(void)) -1;
-
 
 __LIBC_HIDDEN__  void do_mips_start(void *raw_args) {
   structors_array_t array;
   array.preinit_array = &__PREINIT_ARRAY__;
   array.init_array = &__INIT_ARRAY__;
-  array.fini_array = &__FINI_ARRAY__;
 
   __libc_init(raw_args, NULL, &main, &array);
 }
diff --git a/libc/arch-mips64/bionic/crtbegin.c b/libc/arch-mips64/bionic/crtbegin.c
index 2ea31ad..fe6985d 100644
--- a/libc/arch-mips64/bionic/crtbegin.c
+++ b/libc/arch-mips64/bionic/crtbegin.c
@@ -36,15 +36,10 @@
 __attribute__ ((section (".init_array")))
 void (*__INIT_ARRAY__)(void) = (void (*)(void)) -1;
 
-__attribute__ ((section (".fini_array")))
-void (*__FINI_ARRAY__)(void) = (void (*)(void)) -1;
-
-
 __LIBC_HIDDEN__  void do_mips_start(void *raw_args) {
   structors_array_t array;
   array.preinit_array = &__PREINIT_ARRAY__;
   array.init_array = &__INIT_ARRAY__;
-  array.fini_array = &__FINI_ARRAY__;
 
   __libc_init(raw_args, NULL, &main, &array);
 }
diff --git a/libc/bionic/libc_init_common.h b/libc/bionic/libc_init_common.h
index 3032f99..ee3d32b 100644
--- a/libc/bionic/libc_init_common.h
+++ b/libc/bionic/libc_init_common.h
@@ -33,7 +33,6 @@
 typedef struct {
   void (**preinit_array)(void);
   void (**init_array)(void);
-  void (**fini_array)(void);
 } structors_array_t;
 
 __BEGIN_DECLS
diff --git a/libc/bionic/libc_init_dynamic.cpp b/libc/bionic/libc_init_dynamic.cpp
index 78125f9..3d9fa1a 100644
--- a/libc/bionic/libc_init_dynamic.cpp
+++ b/libc/bionic/libc_init_dynamic.cpp
@@ -60,6 +60,9 @@
   extern int __cxa_atexit(void (*)(void *), void *, void *);
 };
 
+__attribute__ ((section (".fini_array")))
+void (*__FINI_ARRAY__)(void) = (void (*)(void)) -1;
+
 // We flag the __libc_preinit function as a constructor to ensure
 // that its address is listed in libc.so's .init_array section.
 // This ensures that the function is called by the dynamic linker
@@ -79,6 +82,11 @@
   // Hooks for various libraries to let them know that we're starting up.
   malloc_debug_init();
   netdClientInit();
+
+  // The executable may have its own destructors listed in its .fini_array
+  // so we need to ensure that these are called when the program exits
+  // normally.
+  __cxa_atexit(__libc_fini, &__FINI_ARRAY__, NULL);
 }
 
 __LIBC_HIDDEN__ void __libc_postfini() {
@@ -96,19 +104,11 @@
 __noreturn void __libc_init(void* raw_args,
                             void (*onexit)(void) __unused,
                             int (*slingshot)(int, char**, char**),
-                            structors_array_t const * const structors) {
-
+                            structors_array_t const * const structors __unused) {
   KernelArgumentBlock args(raw_args);
 
   // Several Linux ABIs don't pass the onexit pointer, and the ones that
   // do never use it.  Therefore, we ignore it.
 
-  // The executable may have its own destructors listed in its .fini_array
-  // so we need to ensure that these are called when the program exits
-  // normally.
-  if (structors->fini_array) {
-    __cxa_atexit(__libc_fini,structors->fini_array,NULL);
-  }
-
   exit(slingshot(args.argc, args.argv, args.envp));
 }
diff --git a/libc/bionic/libc_init_static.cpp b/libc/bionic/libc_init_static.cpp
index bc11f3d..f2e34c2 100644
--- a/libc/bionic/libc_init_static.cpp
+++ b/libc/bionic/libc_init_static.cpp
@@ -61,6 +61,9 @@
 
 extern "C" int __cxa_atexit(void (*)(void *), void *, void *);
 
+__attribute__ ((section (".fini_array")))
+void (*__FINI_ARRAY__)(void) = (void (*)(void)) -1;
+
 static void call_array(void(**list)()) {
   // First element is -1, list is null-terminated
   while (*++list) {
@@ -99,14 +102,13 @@
   // do never use it.  Therefore, we ignore it.
 
   call_array(structors->preinit_array);
-  call_array(structors->init_array);
 
   // The executable may have its own destructors listed in its .fini_array
   // so we need to ensure that these are called when the program exits
   // normally.
-  if (structors->fini_array != NULL) {
-    __cxa_atexit(__libc_fini,structors->fini_array,NULL);
-  }
+  __cxa_atexit(__libc_fini,&__FINI_ARRAY__, NULL);
+
+  call_array(structors->init_array);
 
   exit(slingshot(args.argc, args.argv, args.envp));
 }
diff --git a/libc/bionic/ndk_cruft.cpp b/libc/bionic/ndk_cruft.cpp
index 0c72019..45ead62 100644
--- a/libc/bionic/ndk_cruft.cpp
+++ b/libc/bionic/ndk_cruft.cpp
@@ -303,4 +303,9 @@
   return strchr(str, ch);
 }
 
+// This was removed from BSD.
+extern "C" void arc4random_stir(void) {
+  // The current implementation stirs itself as needed.
+}
+
 #endif
diff --git a/tests/dlext_test.cpp b/tests/dlext_test.cpp
index 9a96b67..7bd59c8 100644
--- a/tests/dlext_test.cpp
+++ b/tests/dlext_test.cpp
@@ -260,6 +260,11 @@
 }
 
 TEST_F(DlExtRelroSharingTest, VerifyMemorySaving) {
+  if (geteuid() != 0) {
+    GTEST_LOG_(INFO) << "This test must be run as root.\n";
+    return;
+  }
+
   ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME));
   int relro_fd = open(relro_file_, O_RDONLY);
   ASSERT_NOERROR(relro_fd);
diff --git a/tests/dlfcn_test.cpp b/tests/dlfcn_test.cpp
index b8c33dd..ccfd743 100644
--- a/tests/dlfcn_test.cpp
+++ b/tests/dlfcn_test.cpp
@@ -387,4 +387,6 @@
   ASSERT_TRUE(handle1 != NULL);
   ASSERT_TRUE(handle2 != NULL);
   ASSERT_EQ(handle1, handle2);
+  dlclose(handle1);
+  dlclose(handle2);
 }
diff --git a/tests/math_cos_test.cpp b/tests/math_cos_test.cpp
index c0a2d82..4917e87 100644
--- a/tests/math_cos_test.cpp
+++ b/tests/math_cos_test.cpp
@@ -16,6 +16,8 @@
 
 #include <math.h>
 
+#include <fenv.h>
+
 #include <gtest/gtest.h>
 
 #if defined(__BIONIC__)
@@ -5634,6 +5636,7 @@
 
 TEST(math_cos, cos_intel) {
 #if defined(__BIONIC__)
+  fesetenv(FE_DFL_ENV);
   for (size_t i = 0; i < sizeof(g_cos_intel_data)/sizeof(cos_intel_data_t); i++) {
     EXPECT_DOUBLE_EQ(g_cos_intel_data[i].expected, cos(g_cos_intel_data[i].call_data)) << "Failed on element " << i;
   }
diff --git a/tests/math_cosf_test.cpp b/tests/math_cosf_test.cpp
index ea95ff3..8520c1d 100644
--- a/tests/math_cosf_test.cpp
+++ b/tests/math_cosf_test.cpp
@@ -16,6 +16,8 @@
 
 #include <math.h>
 
+#include <fenv.h>
+
 #include <gtest/gtest.h>
 
 #if defined(__BIONIC__)
@@ -4346,6 +4348,7 @@
 
 TEST(math_cosf, cosf_intel) {
 #if defined(__BIONIC__)
+  fesetenv(FE_DFL_ENV);
   for (size_t i = 0; i < sizeof(g_cosf_intel_data)/sizeof(cosf_intel_data_t); i++) {
     EXPECT_FLOAT_EQ(g_cosf_intel_data[i].expected, cosf(g_cosf_intel_data[i].call_data)) << "Failed on element " << i;
   }
diff --git a/tests/math_exp_test.cpp b/tests/math_exp_test.cpp
index beb2584..c9c6ad5 100644
--- a/tests/math_exp_test.cpp
+++ b/tests/math_exp_test.cpp
@@ -16,6 +16,8 @@
 
 #include <math.h>
 
+#include <fenv.h>
+
 #include <gtest/gtest.h>
 
 #if defined(__BIONIC__)
@@ -1966,6 +1968,7 @@
 
 TEST(math_exp, exp_intel) {
 #if defined(__BIONIC__)
+  fesetenv(FE_DFL_ENV);
   for (size_t i = 0; i < sizeof(g_exp_intel_data)/sizeof(exp_intel_data_t); i++) {
     EXPECT_DOUBLE_EQ(g_exp_intel_data[i].expected, exp(g_exp_intel_data[i].call_data)) << "Failed on element " << i;
   }
diff --git a/tests/math_expf_test.cpp b/tests/math_expf_test.cpp
index 257aa26..30bc946 100644
--- a/tests/math_expf_test.cpp
+++ b/tests/math_expf_test.cpp
@@ -16,6 +16,8 @@
 
 #include <math.h>
 
+#include <fenv.h>
+
 #include <gtest/gtest.h>
 
 #if defined(__BIONIC__)
@@ -1430,6 +1432,7 @@
 
 TEST(math_expf, expf_intel) {
 #if defined(__BIONIC__)
+  fesetenv(FE_DFL_ENV);
   for (size_t i = 0; i < sizeof(g_expf_intel_data)/sizeof(expf_intel_data_t); i++) {
     EXPECT_FLOAT_EQ(g_expf_intel_data[i].expected, expf(g_expf_intel_data[i].call_data)) << "Failed on element " << i;
   }
diff --git a/tests/math_log_test.cpp b/tests/math_log_test.cpp
index da2a848..4f136a7 100644
--- a/tests/math_log_test.cpp
+++ b/tests/math_log_test.cpp
@@ -16,6 +16,8 @@
 
 #include <math.h>
 
+#include <fenv.h>
+
 #include <gtest/gtest.h>
 
 #if defined(__BIONIC__)
@@ -1666,6 +1668,7 @@
 
 TEST(math_log, log_intel) {
 #if defined(__BIONIC__)
+  fesetenv(FE_DFL_ENV);
   for (size_t i = 0; i < sizeof(g_log_intel_data)/sizeof(log_intel_data_t); i++) {
     EXPECT_DOUBLE_EQ(g_log_intel_data[i].expected, log(g_log_intel_data[i].call_data)) << "Failed on element " << i;
   }
diff --git a/tests/math_logf_test.cpp b/tests/math_logf_test.cpp
index e5d0921..ca02095 100644
--- a/tests/math_logf_test.cpp
+++ b/tests/math_logf_test.cpp
@@ -16,6 +16,8 @@
 
 #include <math.h>
 
+#include <fenv.h>
+
 #include <gtest/gtest.h>
 
 #if defined(__BIONIC__)
@@ -1318,6 +1320,7 @@
 
 TEST(math_logf, logf_intel) {
 #if defined(__BIONIC__)
+  fesetenv(FE_DFL_ENV);
   for (size_t i = 0; i < sizeof(g_logf_intel_data)/sizeof(logf_intel_data_t); i++) {
     EXPECT_FLOAT_EQ(g_logf_intel_data[i].expected, logf(g_logf_intel_data[i].call_data)) << "Failed on element " << i;
   }
diff --git a/tests/math_pow_test.cpp b/tests/math_pow_test.cpp
index c185424..a4caa36 100644
--- a/tests/math_pow_test.cpp
+++ b/tests/math_pow_test.cpp
@@ -16,6 +16,8 @@
 
 #include <math.h>
 
+#include <fenv.h>
+
 #include <gtest/gtest.h>
 
 #if defined(__BIONIC__)
@@ -3291,6 +3293,7 @@
 
 TEST(math_pow, pow_intel) {
 #if defined(__BIONIC__)
+  fesetenv(FE_DFL_ENV);
   for (size_t i = 0; i < sizeof(g_pow_intel_data)/sizeof(pow_intel_data_t); i++) {
     EXPECT_DOUBLE_EQ(g_pow_intel_data[i].expected, pow(g_pow_intel_data[i].x_call_data, g_pow_intel_data[i].y_call_data)) << "Failed on element " << i;
   }
diff --git a/tests/math_powf_test.cpp b/tests/math_powf_test.cpp
index f77b23a..7272644 100644
--- a/tests/math_powf_test.cpp
+++ b/tests/math_powf_test.cpp
@@ -16,6 +16,8 @@
 
 #include <math.h>
 
+#include <fenv.h>
+
 #include <gtest/gtest.h>
 
 #if defined(__BIONIC__)
@@ -2779,6 +2781,7 @@
 
 TEST(math_powf, powf_intel) {
 #if defined(__BIONIC__)
+  fesetenv(FE_DFL_ENV);
   for (size_t i = 0; i < sizeof(g_powf_intel_data)/sizeof(powf_intel_data_t); i++) {
     EXPECT_FLOAT_EQ(g_powf_intel_data[i].expected, powf(g_powf_intel_data[i].x_call_data, g_powf_intel_data[i].y_call_data)) << "Failed on element " << i;
   }
diff --git a/tests/math_sin_test.cpp b/tests/math_sin_test.cpp
index ffa4340..509642c 100644
--- a/tests/math_sin_test.cpp
+++ b/tests/math_sin_test.cpp
@@ -16,6 +16,8 @@
 
 #include <math.h>
 
+#include <fenv.h>
+
 #include <gtest/gtest.h>
 
 #if defined(__BIONIC__)
@@ -5786,6 +5788,7 @@
 
 TEST(math_sin, sin_intel) {
 #if defined(__BIONIC__)
+  fesetenv(FE_DFL_ENV);
   for (size_t i = 0; i < sizeof(g_sin_intel_data)/sizeof(sin_intel_data_t); i++) {
     EXPECT_DOUBLE_EQ(g_sin_intel_data[i].expected, sin(g_sin_intel_data[i].call_data)) << "Failed on element " << i;
   }
diff --git a/tests/math_sincos_test.cpp b/tests/math_sincos_test.cpp
index f2e30de..4093fb9 100644
--- a/tests/math_sincos_test.cpp
+++ b/tests/math_sincos_test.cpp
@@ -18,6 +18,8 @@
 
 #include <math.h>
 
+#include <fenv.h>
+
 #include <gtest/gtest.h>
 
 #if defined(__BIONIC__)
@@ -4778,6 +4780,7 @@
 
 TEST(math_sincos, sincos_intel) {
 #if defined(__BIONIC__)
+  fesetenv(FE_DFL_ENV);
   for (size_t i = 0; i < sizeof(g_sincos_intel_data)/sizeof(sincos_intel_data_t); i++) {
    double dsin, dcos;
    sincos(g_sincos_intel_data[i].call_data, &dsin, &dcos);
diff --git a/tests/math_sincosf_test.cpp b/tests/math_sincosf_test.cpp
index c1a32c9..d30f72e 100644
--- a/tests/math_sincosf_test.cpp
+++ b/tests/math_sincosf_test.cpp
@@ -16,6 +16,8 @@
 
 #include <math.h>
 
+#include <fenv.h>
+
 #include <gtest/gtest.h>
 
 #if defined(__BIONIC__)
@@ -4646,6 +4648,7 @@
 
 TEST(math_sincosf, sincosf_intel) {
 #if defined(__BIONIC__)
+  fesetenv(FE_DFL_ENV);
   for (size_t i = 0; i < sizeof(g_sincosf_intel_data)/sizeof(sincosf_intel_data_t); i++) {
    float fsin, fcos;
    sincosf(g_sincosf_intel_data[i].call_data, &fsin, &fcos);
diff --git a/tests/math_sinf_test.cpp b/tests/math_sinf_test.cpp
index bb1e2c9..0c2e6f1 100644
--- a/tests/math_sinf_test.cpp
+++ b/tests/math_sinf_test.cpp
@@ -16,6 +16,8 @@
 
 #include <math.h>
 
+#include <fenv.h>
+
 #include <gtest/gtest.h>
 
 #if defined(__BIONIC__)
@@ -4386,6 +4388,7 @@
 
 TEST(math_sinf, sinf_intel) {
 #if defined(__BIONIC__)
+  fesetenv(FE_DFL_ENV);
   for (size_t i = 0; i < sizeof(g_sinf_intel_data)/sizeof(sinf_intel_data_t); i++) {
     EXPECT_FLOAT_EQ(g_sinf_intel_data[i].expected, sinf(g_sinf_intel_data[i].call_data)) << "Failed on element " << i;
   }
diff --git a/tests/math_tan_test.cpp b/tests/math_tan_test.cpp
index 6862019..e555ef3 100644
--- a/tests/math_tan_test.cpp
+++ b/tests/math_tan_test.cpp
@@ -16,6 +16,8 @@
 
 #include <math.h>
 
+#include <fenv.h>
+
 #include <gtest/gtest.h>
 
 #if defined(__BIONIC__)
@@ -5194,6 +5196,7 @@
 
 TEST(math_tan, tan_intel) {
 #if defined(__BIONIC__)
+  fesetenv(FE_DFL_ENV);
   for (size_t i = 0; i < sizeof(g_tan_intel_data)/sizeof(tan_intel_data_t); i++) {
     EXPECT_DOUBLE_EQ(g_tan_intel_data[i].expected, tan(g_tan_intel_data[i].call_data)) << "Failed on element " << i;
   }
diff --git a/tests/math_tanf_test.cpp b/tests/math_tanf_test.cpp
index 9319046..db04116 100644
--- a/tests/math_tanf_test.cpp
+++ b/tests/math_tanf_test.cpp
@@ -16,6 +16,8 @@
 
 #include <math.h>
 
+#include <fenv.h>
+
 #include <gtest/gtest.h>
 
 #if defined(__BIONIC__)
@@ -4450,6 +4452,7 @@
 
 TEST(math_tanf, tanf_intel) {
 #if defined(__BIONIC__)
+  fesetenv(FE_DFL_ENV);
   for (size_t i = 0; i < sizeof(g_tanf_intel_data)/sizeof(tanf_intel_data_t); i++) {
     EXPECT_FLOAT_EQ(g_tanf_intel_data[i].expected, tanf(g_tanf_intel_data[i].call_data)) << "Failed on element " << i;
   }
diff --git a/tests/pthread_test.cpp b/tests/pthread_test.cpp
index 6557738..bbac7fe 100644
--- a/tests/pthread_test.cpp
+++ b/tests/pthread_test.cpp
@@ -105,6 +105,7 @@
   ASSERT_EQ(99, WEXITSTATUS(status));
 
   ASSERT_EQ(expected, pthread_getspecific(key));
+  ASSERT_EQ(0, pthread_key_delete(key));
 }
 
 static void* DirtyKeyFn(void* key) {
@@ -132,6 +133,7 @@
   ASSERT_EQ(nullptr, result); // Not ~0!
 
   ASSERT_EQ(0, munmap(stack, stack_size));
+  ASSERT_EQ(0, pthread_key_delete(key));
 }
 
 static void* IdFn(void* arg) {
diff --git a/tests/string_test.cpp b/tests/string_test.cpp
index 7db8e21..ad0040a 100644
--- a/tests/string_test.cpp
+++ b/tests/string_test.cpp
@@ -137,8 +137,7 @@
   ASSERT_STREQ("Unknown signal 1001", strsignal1001);
 }
 
-// TODO: where did these numbers come from?
-#define POS_ITER    10
+// TODO: where did this number come from?
 #define ITER        500
 
 // For every length we want to test, vary and change alignment
@@ -147,8 +146,9 @@
 // These tests contributed by Intel Corporation.
 // TODO: make these tests more intention-revealing and less random.
 template<class Character>
-struct StringTestState {
-  StringTestState(size_t MAX_LEN) : MAX_LEN(MAX_LEN) {
+class StringTestState {
+ public:
+  StringTestState(size_t MAX_LEN) : MAX_LEN(MAX_LEN), align1_index_(0), align2_index_(0) {
     int max_alignment = 64;
 
     // TODO: fix the tests to not sometimes use twice their specified "MAX_LEN".
@@ -167,15 +167,30 @@
     free(glob_ptr2);
   }
 
-  void NewIteration() {
-    int alignments[] = { 24, 32, 16, 48, 1, 2, 3, 0, 5, 11 };
-    int usable_alignments = 10;
-    int align1 = alignments[random() % (usable_alignments - 1)];
-    int align2 = alignments[random() % (usable_alignments - 1)];
+  void BeginIterations() {
+    align1_index_ = 0;
+    align2_index_ = 0;
 
-    ptr = glob_ptr + align1;
-    ptr1 = glob_ptr1 + align1;
-    ptr2 = glob_ptr2 + align2;
+    ResetPointers();
+  }
+
+  bool HasNextIteration() {
+    return (align1_index_ != (alignments_size - 1) || align2_index_ != (alignments_size - 1));
+  }
+
+  void NextIteration() {
+    if (align1_index_ == (alignments_size - 1) && align2_index_ == (alignments_size - 1)) {
+      return;
+    }
+
+    if (align1_index_ == (alignments_size - 1)) {
+      align1_index_ = 0;
+      align2_index_++;
+    } else {
+      align1_index_++;
+    }
+
+    ResetPointers();
   }
 
   const size_t MAX_LEN;
@@ -184,7 +199,10 @@
   int len[ITER + 1];
 
  private:
+  static size_t alignments[];
+  static size_t alignments_size;
   Character *glob_ptr, *glob_ptr1, *glob_ptr2;
+  size_t align1_index_, align2_index_;
 
   // Calculate input lengths and fill state.len with them.
   // Test small lengths with more density than big ones. Manually push
@@ -201,19 +219,33 @@
     }
     len[n++] = MAX_LEN;
   }
+
+  void ResetPointers() {
+    if (align1_index_ == alignments_size || align2_index_ == alignments_size) {
+      ptr = ptr1 = ptr2 = nullptr;
+    } else {
+      ptr = glob_ptr + alignments[align1_index_];
+      ptr1 = glob_ptr1 + alignments[align1_index_];
+      ptr2 = glob_ptr2 + alignments[align2_index_];
+    }
+  }
 };
 
+template<class Character>
+size_t StringTestState<Character>::alignments[] = { 24, 32, 16, 48, 0, 1, 2, 3, 4, 5, 6, 7, 11 };
+
+template<class Character>
+size_t StringTestState<Character>::alignments_size = sizeof(alignments)/sizeof(size_t);
+
 TEST(string, strcat) {
   StringTestState<char> state(SMALL);
   for (size_t i = 1; i < state.n; i++) {
-    for (size_t j = 0; j < POS_ITER; j++) {
-      state.NewIteration();
-
+    for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
       memset(state.ptr2, '\2', state.MAX_LEN);
       state.ptr2[state.MAX_LEN - 1] = '\0';
       memcpy(state.ptr, state.ptr2, 2 * state.MAX_LEN);
 
-      memset(state.ptr1, random() & 255, state.len[i]);
+      memset(state.ptr1, 'L', state.len[i]);
       state.ptr1[random() % state.len[i]] = '\0';
       state.ptr1[state.len[i] - 1] = '\0';
 
@@ -386,13 +418,11 @@
 }
 
 TEST(string, strchr) {
-  int seek_char = random() & 255;
+  int seek_char = 'R';
 
   StringTestState<char> state(SMALL);
   for (size_t i = 1; i < state.n; i++) {
-    for (size_t j = 0; j < POS_ITER; j++) {
-      state.NewIteration();
-
+    for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
       if (~seek_char > 0) {
         memset(state.ptr1, ~seek_char, state.len[i]);
       } else {
@@ -421,9 +451,7 @@
 TEST(string, strcmp) {
   StringTestState<char> state(SMALL);
   for (size_t i = 1; i < state.n; i++) {
-    for (size_t j = 0; j < POS_ITER; j++) {
-      state.NewIteration();
-
+    for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
       memset(state.ptr1, 'v', state.MAX_LEN);
       memset(state.ptr2, 'n', state.MAX_LEN);
       state.ptr1[state.len[i] - 1] = '\0';
@@ -457,9 +485,7 @@
 
 TEST(string, stpcpy) {
   StringTestState<char> state(SMALL);
-  for (size_t j = 0; j < POS_ITER; j++) {
-    state.NewIteration();
-
+  for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
     size_t pos = random() % state.MAX_LEN;
 
     memset(state.ptr1, '\2', pos);
@@ -483,9 +509,7 @@
 
 TEST(string, strcpy) {
   StringTestState<char> state(SMALL);
-  for (size_t j = 0; j < POS_ITER; j++) {
-    state.NewIteration();
-
+  for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
     size_t pos = random() % state.MAX_LEN;
 
     memset(state.ptr1, '\2', pos);
@@ -511,9 +535,7 @@
 #if defined(__BIONIC__)
   StringTestState<char> state(SMALL);
   for (size_t i = 0; i < state.n; i++) {
-    for (size_t j = 0; j < POS_ITER; j++) {
-      state.NewIteration();
-
+    for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
       memset(state.ptr2, '\2', state.MAX_LEN + state.len[i]);
       state.ptr2[state.MAX_LEN - 1] = '\0';
       memcpy(state.ptr, state.ptr2, state.MAX_LEN + state.len[i]);
@@ -541,13 +563,8 @@
 TEST(string, strlcpy) {
 #if defined(__BIONIC__)
   StringTestState<char> state(SMALL);
-  for (size_t j = 0; j < POS_ITER; j++) {
-    state.NewIteration();
-
-    int rand = random() & 255;
-    if (rand < 1) {
-      rand = 1;
-    }
+  for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
+    int rand = 'O';
     memset(state.ptr1, rand, state.MAX_LEN);
 
     size_t pos = random() % state.MAX_LEN;
@@ -556,7 +573,7 @@
     }
     memcpy(state.ptr, state.ptr1, state.MAX_LEN);
 
-    memset(state.ptr2, random() & 255, state.MAX_LEN);
+    memset(state.ptr2, 'I', state.MAX_LEN);
     memcpy(state.ptr + state.MAX_LEN, state.ptr2, state.MAX_LEN);
 
     if (pos > state.MAX_LEN - 1) {
@@ -578,14 +595,12 @@
 TEST(string, strncat) {
   StringTestState<char> state(SMALL);
   for (size_t i = 1; i < state.n; i++) {
-    for (size_t j = 0; j < POS_ITER; j++) {
-      state.NewIteration();
-
+    for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
       memset(state.ptr2, '\2', state.MAX_LEN);
       state.ptr2[state.MAX_LEN - 1] = '\0';
       memcpy(state.ptr, state.ptr2, 2 * state.MAX_LEN);
 
-      memset(state.ptr1, random() & 255, state.len[i]);
+      memset(state.ptr1, 'I', state.len[i]);
       state.ptr1[random() % state.len[i]] = '\0';
       state.ptr1[state.len[i] - 1] = '\0';
 
@@ -604,9 +619,7 @@
 TEST(string, strncmp) {
   StringTestState<char> state(SMALL);
   for (size_t i = 1; i < state.n; i++) {
-    for (size_t j = 0; j < POS_ITER; j++) {
-      state.NewIteration();
-
+    for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
       memset(state.ptr1, 'v', state.MAX_LEN);
       memset(state.ptr2, 'n', state.MAX_LEN);
       state.ptr1[state.len[i] - 1] = '\0';
@@ -640,12 +653,8 @@
 
 TEST(string, stpncpy) {
   StringTestState<char> state(SMALL);
-  for (size_t j = 0; j < ITER; j++) {
-    state.NewIteration();
-
-    // Choose a random value to fill the string, except \0 (string terminator),
-    // or \1 (guarantees it's different from anything in ptr2).
-    memset(state.ptr1, (random() % 254) + 2, state.MAX_LEN);
+  for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
+    memset(state.ptr1, 'J', state.MAX_LEN);
     // Choose a random size for our src buffer.
     size_t ptr1_len = random() % state.MAX_LEN;
     state.ptr1[ptr1_len] = '\0';
@@ -679,12 +688,10 @@
 
 TEST(string, strncpy) {
   StringTestState<char> state(SMALL);
-  for (size_t j = 0; j < ITER; j++) {
-    state.NewIteration();
-
+  for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
     // Choose a random value to fill the string, except \0 (string terminator),
     // or \1 (guarantees it's different from anything in ptr2).
-    memset(state.ptr1, (random() % 254) + 2, state.MAX_LEN);
+    memset(state.ptr1, 'K', state.MAX_LEN);
     // Choose a random size for our src buffer.
     size_t ptr1_len = random() % state.MAX_LEN;
     state.ptr1[ptr1_len] = '\0';
@@ -717,12 +724,10 @@
 }
 
 TEST(string, strrchr) {
-  int seek_char = random() & 255;
+  int seek_char = 'M';
   StringTestState<char> state(SMALL);
   for (size_t i = 1; i < state.n; i++) {
-    for (size_t j = 0; j < POS_ITER; j++) {
-      state.NewIteration();
-
+    for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
       if (~seek_char > 0) {
         memset(state.ptr1, ~seek_char, state.len[i]);
       } else {
@@ -749,12 +754,10 @@
 }
 
 TEST(string, memchr) {
-  int seek_char = random() & 255;
+  int seek_char = 'N';
   StringTestState<char> state(SMALL);
   for (size_t i = 0; i < state.n; i++) {
-    for (size_t j = 0; j < POS_ITER; j++) {
-      state.NewIteration();
-
+    for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
       memset(state.ptr1, ~seek_char, state.len[i]);
 
       int pos = random() % state.MAX_LEN;
@@ -780,12 +783,10 @@
 }
 
 TEST(string, memrchr) {
-  int seek_char = random() & 255;
+  int seek_char = 'P';
   StringTestState<char> state(SMALL);
   for (size_t i = 0; i < state.n; i++) {
-    for (size_t j = 0; j < POS_ITER; j++) {
-      state.NewIteration();
-
+    for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
       memset(state.ptr1, ~seek_char, state.len[i]);
 
       int pos = random() % state.MAX_LEN;
@@ -805,11 +806,9 @@
 TEST(string, memcmp) {
   StringTestState<char> state(SMALL);
   for (size_t i = 0; i < state.n; i++) {
-    for (size_t j = 0; j < POS_ITER; j++) {
-      state.NewIteration();
-
-      int c1 = random() & 0xff;
-      int c2 = random() & 0xff;
+    for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
+      int c1 = 'A';
+      int c2 = 'N';
       memset(state.ptr1, c1, state.MAX_LEN);
       memset(state.ptr2, c1, state.MAX_LEN);
 
@@ -828,9 +827,7 @@
   StringTestState<wchar_t> state(SMALL);
 
   for (size_t i = 0; i < state.n; i++) {
-    for (size_t j = 0; j < POS_ITER; j++) {
-      state.NewIteration();
-
+    for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
       long long mask = ((long long) 1 << 8 * sizeof(wchar_t)) - 1;
       int c1 = rand() & mask;
       int c2 = rand() & mask;
@@ -850,11 +847,9 @@
 
 TEST(string, memcpy) {
   StringTestState<char> state(LARGE);
-  int rand = random() & 255;
+  int rand = 4;
   for (size_t i = 0; i < state.n - 1; i++) {
-    for (size_t j = 0; j < POS_ITER; j++) {
-      state.NewIteration();
-
+    for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
       size_t pos = random() % (state.MAX_LEN - state.len[i]);
 
       memset(state.ptr1, rand, state.len[i]);
@@ -872,11 +867,9 @@
 
 TEST(string, memset) {
   StringTestState<char> state(LARGE);
-  char ch = random () & 255;
+  char ch = 'P';
   for (size_t i = 0; i < state.n - 1; i++) {
-    for (size_t j = 0; j < POS_ITER; j++) {
-      state.NewIteration();
-
+    for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
       memset(state.ptr1, ~ch, state.MAX_LEN);
       memcpy(state.ptr2, state.ptr1, state.MAX_LEN);
 
@@ -895,14 +888,12 @@
 TEST(string, memmove) {
   StringTestState<char> state(LARGE);
   for (size_t i = 0; i < state.n - 1; i++) {
-    for (size_t j = 0; j < POS_ITER; j++) {
-      state.NewIteration();
-
-      memset(state.ptr1, random() & 255, 2 * state.MAX_LEN);
+    for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
+      memset(state.ptr1, 'Q', 2 * state.MAX_LEN);
 
       size_t pos = random() % (state.MAX_LEN - state.len[i]);
 
-      memset(state.ptr1, random() & 255, state.len[i]);
+      memset(state.ptr1, 'R', state.len[i]);
       memcpy(state.ptr2, state.ptr1, 2 * state.MAX_LEN);
       memcpy(state.ptr, state.ptr1, state.len[i]);
       memcpy(state.ptr1 + pos, state.ptr, state.len[i]);
@@ -928,8 +919,8 @@
 
   for (int i = 0; i < 5; i++) {
     char* ptr2 = glob_ptr2 + alignments[i];
-    memset(ptr1, random() & 255, 2 * len);
-    memset(ptr1, random() & 255, len);
+    memset(ptr1, 'S', 2 * len);
+    memset(ptr1, 'T', len);
     memcpy(ptr2, ptr1, 2 * len);
     memcpy(ptr, ptr1, len);
     memcpy(ptr1 + pos, ptr, len);
@@ -995,11 +986,9 @@
 TEST(string, bcopy) {
   StringTestState<char> state(LARGE);
   for (size_t i = 0; i < state.n; i++) {
-    for (size_t j = 0; j < POS_ITER; j++) {
-      state.NewIteration();
-
-      memset(state.ptr1, random() & 255, state.MAX_LEN);
-      memset(state.ptr1 + state.MAX_LEN, random() & 255, state.MAX_LEN);
+    for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
+      memset(state.ptr1, '4', state.MAX_LEN);
+      memset(state.ptr1 + state.MAX_LEN, 'a', state.MAX_LEN);
       memcpy(state.ptr2, state.ptr1, 2 * state.MAX_LEN);
 
       size_t start = random() % (2 * state.MAX_LEN - state.len[i]);
@@ -1013,10 +1002,8 @@
 
 TEST(string, bzero) {
   StringTestState<char> state(LARGE);
-  for (size_t j = 0; j < ITER; j++) {
-    state.NewIteration();
-
-    memset(state.ptr1, random() & 255, state.MAX_LEN);
+  for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
+    memset(state.ptr1, 'R', state.MAX_LEN);
 
     size_t start = random() % state.MAX_LEN;
     size_t end = start + random() % (state.MAX_LEN - start);