Fix bionic-unit-tests-static crash
The crash happens because for static executables call to dlopen
results in crash. This change moves dlopen() == nullptr check
from static variable to a functions so that the dlopen crash only
affects tests calling dlopen but not the whole executable.
Also make static tests report crashes to debugerd
Test: run bionic-unit-tests-static (for arm 32 and 64)
Bug: http://b/34129417
Change-Id: I7c4d8caf2a43250234fe24496b1c95eab572769f
diff --git a/tests/Android.bp b/tests/Android.bp
index da90a92..cb69d2b 100644
--- a/tests/Android.bp
+++ b/tests/Android.bp
@@ -422,6 +422,9 @@
defaults: ["bionic_tests_defaults"],
host_supported: false,
+ srcs: [
+ "gtest_preinit_debuggerd.cpp",
+ ],
whole_static_libs: [
"libBionicTests",
"libBionicGtestMain",
@@ -435,6 +438,7 @@
"libtinyxml2",
"liblog",
"libbase",
+ "libdebuggerd_client",
],
static_executable: true,
diff --git a/tests/gtest_preinit_debuggerd.cpp b/tests/gtest_preinit_debuggerd.cpp
new file mode 100644
index 0000000..aac2e3f
--- /dev/null
+++ b/tests/gtest_preinit_debuggerd.cpp
@@ -0,0 +1,24 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "debuggerd/client.h"
+
+void __gtest_preinit() {
+ debuggerd_init(nullptr);
+}
+
+__attribute__((section(".preinit_array"), __used__))
+void (*__local_gtest_preinit)(void) = __gtest_preinit;
diff --git a/tests/wctype_test.cpp b/tests/wctype_test.cpp
index 84a0e65..1bc4128 100644
--- a/tests/wctype_test.cpp
+++ b/tests/wctype_test.cpp
@@ -29,7 +29,9 @@
// bionic's dlsym doesn't work in static binaries, so we can't access icu,
// so any unicode test case will fail.
-static bool have_dl = (dlopen("libc.so", 0) != nullptr);
+static bool have_dl() {
+ return (dlopen("libc.so", 0) != nullptr);
+}
static void TestIsWideFn(int fn(wint_t),
int fn_l(wint_t, locale_t),
@@ -37,7 +39,7 @@
const wchar_t* falses) {
UtfLocale l;
for (const wchar_t* p = trues; *p; ++p) {
- if (!have_dl && *p > 0x7f) {
+ if (!have_dl() && *p > 0x7f) {
GTEST_LOG_(INFO) << "skipping unicode test " << *p;
continue;
}
@@ -45,7 +47,7 @@
EXPECT_TRUE(fn_l(*p, l.l)) << *p;
}
for (const wchar_t* p = falses; *p; ++p) {
- if (!have_dl && *p > 0x7f) {
+ if (!have_dl() && *p > 0x7f) {
GTEST_LOG_(INFO) << "skipping unicode test " << *p;
continue;
}
@@ -107,7 +109,7 @@
EXPECT_EQ(wint_t('!'), towlower(L'!'));
EXPECT_EQ(wint_t('a'), towlower(L'a'));
EXPECT_EQ(wint_t('a'), towlower(L'A'));
- if (have_dl) {
+ if (have_dl()) {
EXPECT_EQ(wint_t(L'ç'), towlower(L'ç'));
EXPECT_EQ(wint_t(L'ç'), towlower(L'Ç'));
EXPECT_EQ(wint_t(L'δ'), towlower(L'δ'));
@@ -123,7 +125,7 @@
EXPECT_EQ(wint_t('!'), towlower_l(L'!', l.l));
EXPECT_EQ(wint_t('a'), towlower_l(L'a', l.l));
EXPECT_EQ(wint_t('a'), towlower_l(L'A', l.l));
- if (have_dl) {
+ if (have_dl()) {
EXPECT_EQ(wint_t(L'ç'), towlower_l(L'ç', l.l));
EXPECT_EQ(wint_t(L'ç'), towlower_l(L'Ç', l.l));
EXPECT_EQ(wint_t(L'δ'), towlower_l(L'δ', l.l));
@@ -138,7 +140,7 @@
EXPECT_EQ(wint_t('!'), towupper(L'!'));
EXPECT_EQ(wint_t('A'), towupper(L'a'));
EXPECT_EQ(wint_t('A'), towupper(L'A'));
- if (have_dl) {
+ if (have_dl()) {
EXPECT_EQ(wint_t(L'Ç'), towupper(L'ç'));
EXPECT_EQ(wint_t(L'Ç'), towupper(L'Ç'));
EXPECT_EQ(wint_t(L'Δ'), towupper(L'δ'));
@@ -154,7 +156,7 @@
EXPECT_EQ(wint_t('!'), towupper_l(L'!', l.l));
EXPECT_EQ(wint_t('A'), towupper_l(L'a', l.l));
EXPECT_EQ(wint_t('A'), towupper_l(L'A', l.l));
- if (have_dl) {
+ if (have_dl()) {
EXPECT_EQ(wint_t(L'Ç'), towupper_l(L'ç', l.l));
EXPECT_EQ(wint_t(L'Ç'), towupper_l(L'Ç', l.l));
EXPECT_EQ(wint_t(L'Δ'), towupper_l(L'δ', l.l));