Make support for thread-unsafe C functions optional.
One of the aspects of CloudABI is that it aims to help you write code
that is thread-safe out of the box. This is very important if you want
to write libraries that are easy to reuse. For CloudABI we decided to
not provide the thread-unsafe functions. So far this is working out
pretty well, as thread-unsafety issues are detected really early on.
The following patch adds a knob to libc++,
_LIBCPP_HAS_NO_THREAD_UNSAFE_C_FUNCTIONS, that can be set to disable
thread-unsafe functions that can easily be avoided in practice. The
following functions are not thread-safe:
- <clocale>: locale handles should be preferred over setlocale().
- <cstdlib>: mbrlen(), mbrtowc() and wcrtomb() should be preferred over
their non-restartable counterparts.
- <ctime>: asctime(), ctime(), gmtime() and localtime() are not
thread-safe. The first two are also deprecated by POSIX.
Differential Revision: http://reviews.llvm.org/D8703
Reviewed by: marshall
llvm-svn: 240527
diff --git a/libcxx/test/std/depr/depr.c.headers/stdlib_h.pass.cpp b/libcxx/test/std/depr/depr.c.headers/stdlib_h.pass.cpp
index 98b19c6..47e1527 100644
--- a/libcxx/test/std/depr/depr.c.headers/stdlib_h.pass.cpp
+++ b/libcxx/test/std/depr/depr.c.headers/stdlib_h.pass.cpp
@@ -71,12 +71,14 @@
static_assert((std::is_same<decltype(div(0,0)), div_t>::value), "");
static_assert((std::is_same<decltype(ldiv(0L,0L)), ldiv_t>::value), "");
static_assert((std::is_same<decltype(lldiv(0LL,0LL)), lldiv_t>::value), "");
- static_assert((std::is_same<decltype(mblen("",0)), int>::value), "");
wchar_t* pw = 0;
const wchar_t* pwc = 0;
char* pc = 0;
+#ifndef _LIBCPP_HAS_NO_THREAD_UNSAFE_C_FUNCTIONS
+ static_assert((std::is_same<decltype(mblen("",0)), int>::value), "");
static_assert((std::is_same<decltype(mbtowc(pw,"",0)), int>::value), "");
static_assert((std::is_same<decltype(wctomb(pc,L' ')), int>::value), "");
+#endif
static_assert((std::is_same<decltype(mbstowcs(pw,"",0)), size_t>::value), "");
static_assert((std::is_same<decltype(wcstombs(pc,pwc,0)), size_t>::value), "");
}
diff --git a/libcxx/test/std/depr/depr.c.headers/string_h.pass.cpp b/libcxx/test/std/depr/depr.c.headers/string_h.pass.cpp
index fc7f65d..afc784f 100644
--- a/libcxx/test/std/depr/depr.c.headers/string_h.pass.cpp
+++ b/libcxx/test/std/depr/depr.c.headers/string_h.pass.cpp
@@ -41,7 +41,9 @@
static_assert((std::is_same<decltype(strrchr(cp, 0)), char*>::value), "");
static_assert((std::is_same<decltype(strspn(cpc, cpc)), size_t>::value), "");
static_assert((std::is_same<decltype(strstr(cp, cpc)), char*>::value), "");
+#ifndef _LIBCPP_HAS_NO_THREAD_UNSAFE_C_FUNCTIONS
static_assert((std::is_same<decltype(strtok(cp, cpc)), char*>::value), "");
+#endif
static_assert((std::is_same<decltype(memset(vp, 0, s)), void*>::value), "");
static_assert((std::is_same<decltype(strerror(0)), char*>::value), "");
static_assert((std::is_same<decltype(strlen(cpc)), size_t>::value), "");
diff --git a/libcxx/test/std/language.support/support.runtime/cstdlib.pass.cpp b/libcxx/test/std/language.support/support.runtime/cstdlib.pass.cpp
index e14e70e..0739491 100644
--- a/libcxx/test/std/language.support/support.runtime/cstdlib.pass.cpp
+++ b/libcxx/test/std/language.support/support.runtime/cstdlib.pass.cpp
@@ -75,12 +75,14 @@
static_assert((std::is_same<decltype(std::div(0LL,0LL)), std::lldiv_t>::value), "");
static_assert((std::is_same<decltype(std::ldiv(0L,0L)), std::ldiv_t>::value), "");
static_assert((std::is_same<decltype(std::lldiv(0LL,0LL)), std::lldiv_t>::value), "");
- static_assert((std::is_same<decltype(std::mblen("",0)), int>::value), "");
wchar_t* pw = 0;
const wchar_t* pwc = 0;
char* pc = 0;
+#ifndef _LIBCPP_HAS_NO_THREAD_UNSAFE_C_FUNCTIONS
+ static_assert((std::is_same<decltype(std::mblen("",0)), int>::value), "");
static_assert((std::is_same<decltype(std::mbtowc(pw,"",0)), int>::value), "");
static_assert((std::is_same<decltype(std::wctomb(pc,L' ')), int>::value), "");
+#endif
static_assert((std::is_same<decltype(std::mbstowcs(pw,"",0)), std::size_t>::value), "");
static_assert((std::is_same<decltype(std::wcstombs(pc,pwc,0)), std::size_t>::value), "");
}
diff --git a/libcxx/test/std/language.support/support.runtime/ctime.pass.cpp b/libcxx/test/std/language.support/support.runtime/ctime.pass.cpp
index 495d6eb..8c5d281 100644
--- a/libcxx/test/std/language.support/support.runtime/ctime.pass.cpp
+++ b/libcxx/test/std/language.support/support.runtime/ctime.pass.cpp
@@ -30,10 +30,12 @@
static_assert((std::is_same<decltype(std::difftime(t,t)), double>::value), "");
static_assert((std::is_same<decltype(std::mktime(&tm)), std::time_t>::value), "");
static_assert((std::is_same<decltype(std::time(&t)), std::time_t>::value), "");
+#ifndef _LIBCPP_HAS_NO_THREAD_UNSAFE_C_FUNCTIONS
static_assert((std::is_same<decltype(std::asctime(&tm)), char*>::value), "");
static_assert((std::is_same<decltype(std::ctime(&t)), char*>::value), "");
static_assert((std::is_same<decltype(std::gmtime(&t)), std::tm*>::value), "");
static_assert((std::is_same<decltype(std::localtime(&t)), std::tm*>::value), "");
+#endif
char* c1 = 0;
const char* c2 = 0;
static_assert((std::is_same<decltype(std::strftime(c1,s,c2,&tm)), std::size_t>::value), "");
diff --git a/libcxx/test/std/localization/c.locales/clocale.pass.cpp b/libcxx/test/std/localization/c.locales/clocale.pass.cpp
index 3b3e933..a90725b 100644
--- a/libcxx/test/std/localization/c.locales/clocale.pass.cpp
+++ b/libcxx/test/std/localization/c.locales/clocale.pass.cpp
@@ -12,6 +12,8 @@
#include <clocale>
#include <type_traits>
+#ifndef _LIBCPP_HAS_NO_THREAD_UNSAFE_C_FUNCTIONS
+
#ifndef LC_ALL
#error LC_ALL not defined
#endif
@@ -36,6 +38,8 @@
#error LC_TIME not defined
#endif
+#endif // !_LIBCPP_HAS_NO_THREAD_UNSAFE_C_FUNCTIONS
+
#ifndef NULL
#error NULL not defined
#endif
@@ -43,6 +47,8 @@
int main()
{
std::lconv lc;
+#ifndef _LIBCPP_HAS_NO_THREAD_UNSAFE_C_FUNCTIONS
static_assert((std::is_same<decltype(std::setlocale(0, "")), char*>::value), "");
+#endif
static_assert((std::is_same<decltype(std::localeconv()), std::lconv*>::value), "");
}
diff --git a/libcxx/test/std/strings/c.strings/cstring.pass.cpp b/libcxx/test/std/strings/c.strings/cstring.pass.cpp
index 5ed7e6c..20f4050 100644
--- a/libcxx/test/std/strings/c.strings/cstring.pass.cpp
+++ b/libcxx/test/std/strings/c.strings/cstring.pass.cpp
@@ -46,7 +46,9 @@
static_assert((std::is_same<decltype(std::strspn(cpc, cpc)), std::size_t>::value), "");
// static_assert((std::is_same<decltype(std::strstr(cpc, cpc)), const char*>::value), "");
static_assert((std::is_same<decltype(std::strstr(cp, cpc)), char*>::value), "");
+#ifndef _LIBCPP_HAS_NO_THREAD_UNSAFE_C_FUNCTIONS
static_assert((std::is_same<decltype(std::strtok(cp, cpc)), char*>::value), "");
+#endif
static_assert((std::is_same<decltype(std::memset(vp, 0, s)), void*>::value), "");
static_assert((std::is_same<decltype(std::strerror(0)), char*>::value), "");
static_assert((std::is_same<decltype(std::strlen(cpc)), std::size_t>::value), "");
diff --git a/libcxx/test/std/utilities/date.time/asctime.thread-unsafe.fail.cpp b/libcxx/test/std/utilities/date.time/asctime.thread-unsafe.fail.cpp
new file mode 100644
index 0000000..3a9749e
--- /dev/null
+++ b/libcxx/test/std/utilities/date.time/asctime.thread-unsafe.fail.cpp
@@ -0,0 +1,18 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: libcpp-has-no-thread-unsafe-c-functions
+
+#include <ctime>
+
+int main() {
+ // asctime is not thread-safe.
+ std::time_t t = 0;
+ std::asctime(&t);
+}
diff --git a/libcxx/test/std/utilities/date.time/ctime.thread-unsafe.fail.cpp b/libcxx/test/std/utilities/date.time/ctime.thread-unsafe.fail.cpp
new file mode 100644
index 0000000..cd246c6
--- /dev/null
+++ b/libcxx/test/std/utilities/date.time/ctime.thread-unsafe.fail.cpp
@@ -0,0 +1,18 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: libcpp-has-no-thread-unsafe-c-functions
+
+#include <ctime>
+
+int main() {
+ // ctime is not thread-safe.
+ std::time_t t = 0;
+ std::ctime(&t);
+}
diff --git a/libcxx/test/std/utilities/date.time/gmtime.thread-unsafe.fail.cpp b/libcxx/test/std/utilities/date.time/gmtime.thread-unsafe.fail.cpp
new file mode 100644
index 0000000..a6debcb
--- /dev/null
+++ b/libcxx/test/std/utilities/date.time/gmtime.thread-unsafe.fail.cpp
@@ -0,0 +1,18 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: libcpp-has-no-thread-unsafe-c-functions
+
+#include <ctime>
+
+int main() {
+ // gmtime is not thread-safe.
+ std::time_t t = 0;
+ std::gmtime(&t);
+}
diff --git a/libcxx/test/std/utilities/date.time/localtime.thread-unsafe.fail.cpp b/libcxx/test/std/utilities/date.time/localtime.thread-unsafe.fail.cpp
new file mode 100644
index 0000000..c9e55c8
--- /dev/null
+++ b/libcxx/test/std/utilities/date.time/localtime.thread-unsafe.fail.cpp
@@ -0,0 +1,18 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: libcpp-has-no-thread-unsafe-c-functions
+
+#include <ctime>
+
+int main() {
+ // localtime is not thread-safe.
+ std::time_t t = 0;
+ std::localtime(&t);
+}