Fix broken return code of M_INITIALIZE_GWP_ASAN.

When calling android_mallopt using M_INITIALIZE_GWP_ASAN, nothing
was being returned. Fix this, add a test, and also refactor the
code a bit so dynamic and static share the same code.

Test: Unit tests pass in dynamic and static versions.
Test: Passed using both jemalloc and scudo.
Change-Id: Ibe54b6ccabdbd44d2378892e793df393978bc02b
diff --git a/libc/bionic/gwp_asan_wrappers.cpp b/libc/bionic/gwp_asan_wrappers.cpp
index 6eb1749..8c51347 100644
--- a/libc/bionic/gwp_asan_wrappers.cpp
+++ b/libc/bionic/gwp_asan_wrappers.cpp
@@ -277,3 +277,14 @@
 bool DispatchIsGwpAsan(const MallocDispatch* dispatch) {
   return dispatch == &gwp_asan_dispatch;
 }
+
+bool EnableGwpAsan(bool force_init) {
+  if (GwpAsanInitialized) {
+    return true;
+  }
+
+  bool ret_value;
+  __libc_globals.mutate(
+      [&](libc_globals* globals) { ret_value = MaybeInitGwpAsan(globals, force_init); });
+  return ret_value;
+}
diff --git a/libc/bionic/gwp_asan_wrappers.h b/libc/bionic/gwp_asan_wrappers.h
index a39d50b..c568681 100644
--- a/libc/bionic/gwp_asan_wrappers.h
+++ b/libc/bionic/gwp_asan_wrappers.h
@@ -32,6 +32,9 @@
 #include <private/bionic_malloc_dispatch.h>
 #include <stddef.h>
 
+// Enable GWP-ASan, used by android_mallopt.
+bool EnableGwpAsan(bool force_init);
+
 // Hooks for libc to possibly install GWP-ASan.
 bool MaybeInitGwpAsanFromLibc(libc_globals* globals);
 
diff --git a/libc/bionic/malloc_common.cpp b/libc/bionic/malloc_common.cpp
index c91efa0..38168ee 100644
--- a/libc/bionic/malloc_common.cpp
+++ b/libc/bionic/malloc_common.cpp
@@ -330,9 +330,8 @@
       errno = EINVAL;
       return false;
     }
-    __libc_globals.mutate([&](libc_globals* globals) {
-      return MaybeInitGwpAsan(globals, *reinterpret_cast<bool*>(arg));
-    });
+
+    return EnableGwpAsan(*reinterpret_cast<bool*>(arg));
   }
   errno = ENOTSUP;
   return false;
diff --git a/libc/bionic/malloc_common_dynamic.cpp b/libc/bionic/malloc_common_dynamic.cpp
index 31d1e69..1f58fda 100644
--- a/libc/bionic/malloc_common_dynamic.cpp
+++ b/libc/bionic/malloc_common_dynamic.cpp
@@ -530,9 +530,8 @@
       errno = EINVAL;
       return false;
     }
-    __libc_globals.mutate([&](libc_globals* globals) {
-      return MaybeInitGwpAsan(globals, *reinterpret_cast<bool*>(arg));
-    });
+
+    return EnableGwpAsan(*reinterpret_cast<bool*>(arg));
   }
   // Try heapprofd's mallopt, as it handles options not covered here.
   return HeapprofdMallopt(opcode, arg, arg_size);