Skip to content

Commit 736203f

Browse files
Add exit-time destructors test
1 parent b6ec06b commit 736203f

File tree

3 files changed

+90
-6
lines changed

3 files changed

+90
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ test/httplib.cc
1616
test/httplib.h
1717
test/test
1818
test/server_fuzzer
19+
test/test_no_exit_time_dtors
1920
test/test_proxy
2021
test/test_split
2122
test/test.xcodeproj/xcuser*

test/Makefile

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ ZLIB_SUPPORT = -DCPPHTTPLIB_ZLIB_SUPPORT -lz
1818
BROTLI_DIR = $(PREFIX)/opt/brotli
1919
BROTLI_SUPPORT = -DCPPHTTPLIB_BROTLI_SUPPORT -I$(BROTLI_DIR)/include -L$(BROTLI_DIR)/lib -lbrotlicommon -lbrotlienc -lbrotlidec
2020

21-
TEST_ARGS = gtest/src/gtest-all.cc gtest/src/gtest_main.cc -Igtest -Igtest/include $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) -pthread -lcurl
21+
TEST_ARGS = gtest_main.o gtest-all.o -Igtest -Igtest/include $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) -pthread -lcurl
2222

2323
# By default, use standalone_fuzz_target_runner.
2424
# This runner does no fuzzing, but simply executes the inputs
@@ -33,19 +33,30 @@ REALPATH = $(shell which grealpath 2>/dev/null || which realpath 2>/dev/null)
3333
STYLE_CHECK_FILES = $(filter-out httplib.h httplib.cc, \
3434
$(wildcard example/*.h example/*.cc fuzzing/*.h fuzzing/*.cc *.h *.cc ../httplib.h))
3535

36-
all : test test_split
36+
all : test test_no_exit_time_dtors test_split
3737
./test
38+
GTEST_FILTER="ExitTimeDtorsTest.*" ./test_no_exit_time_dtors
3839

3940
proxy : test_proxy
4041
./test_proxy
4142

42-
test : test.cc include_httplib.cc ../httplib.h Makefile cert.pem
43+
gtest-all.o : gtest/src/gtest-all.cc
44+
$(CXX) -c -o $@ $(CXXFLAGS) -Igtest -Igtest/include $<
45+
46+
gtest_main.o : gtest/src/gtest_main.cc
47+
$(CXX) -c -o $@ $(CXXFLAGS) -Igtest -Igtest/include $<
48+
49+
test : gtest_main.o gtest-all.o test.cc include_httplib.cc ../httplib.h Makefile cert.pem
4350
$(CXX) -o $@ -I.. $(CXXFLAGS) test.cc include_httplib.cc $(TEST_ARGS)
4451
@file $@
4552

53+
test_no_exit_time_dtors : gtest_main.o gtest-all.o test.cc ../httplib.h Makefile cert.pem
54+
$(CXX) -o $@ -I.. $(CXXFLAGS) -DCPPHTTPLIB_NO_EXIT_TIME_DESTRUCTORS \
55+
$(if $(findstring clang,$(CXX)),-Wexit-time-destructors -Werror=exit-time-destructors) test.cc $(TEST_ARGS)
56+
4657
# Note: The intention of test_split is to verify that it works to compile and
4758
# link the split httplib.h, so there is normally no need to execute it.
48-
test_split : test.cc ../httplib.h httplib.cc Makefile cert.pem
59+
test_split : gtest_main.o gtest-all.o test.cc ../httplib.h httplib.cc Makefile cert.pem
4960
$(CXX) -o $@ $(CXXFLAGS) test.cc httplib.cc $(TEST_ARGS)
5061

5162
check_abi:
@@ -73,7 +84,7 @@ style_check: $(STYLE_CHECK_FILES)
7384
echo "All files are properly formatted."; \
7485
fi
7586

76-
test_proxy : test_proxy.cc ../httplib.h Makefile cert.pem
87+
test_proxy : gtest_main.o gtest-all.o test_proxy.cc ../httplib.h Makefile cert.pem
7788
$(CXX) -o $@ -I.. $(CXXFLAGS) test_proxy.cc $(TEST_ARGS)
7889

7990
# Runs server_fuzzer.cc based on value of $(LIB_FUZZING_ENGINE).
@@ -98,5 +109,5 @@ cert.pem:
98109
./gen-certs.sh
99110

100111
clean:
101-
rm -rf test test_split test_proxy server_fuzzer *.pem *.0 *.o *.1 *.srl httplib.h httplib.cc _build* *.dSYM
112+
rm -rf test test_no_exit_time_dtors test_split test_proxy server_fuzzer *.pem *.0 *.o *.1 *.srl httplib.h httplib.cc _build* *.dSYM
102113

test/test.cc

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8470,3 +8470,75 @@ TEST(ClientInThreadTest, Issue2068) {
84708470
t.join();
84718471
}
84728472
}
8473+
8474+
#if defined(__SANITIZE_ADDRESS__)
8475+
#define ASAN_ENABLED 1
8476+
#else
8477+
#if defined(__has_feature)
8478+
#if __has_feature(address_sanitizer)
8479+
#define ASAN_ENABLED 1
8480+
#else
8481+
#define ASAN_ENABLED 0
8482+
#endif
8483+
#else
8484+
#define ASAN_ENABLED 0
8485+
#endif
8486+
#endif
8487+
8488+
// No death tests on Windows
8489+
#ifndef _WIN32
8490+
bool KilledByAbortOrSegfault(int exit_status) {
8491+
return
8492+
#if ASAN_ENABLED
8493+
// For ASan in some environments
8494+
(WIFEXITED(exit_status) && WEXITSTATUS(exit_status) == 1) ||
8495+
#endif
8496+
(WIFSIGNALED(exit_status) &&
8497+
(WTERMSIG(exit_status) == SIGABRT || WTERMSIG(exit_status) == SIGSEGV));
8498+
}
8499+
8500+
Server *issue2097_svr = nullptr;
8501+
std::thread *issue2097_svr_thread = nullptr;
8502+
8503+
TEST(ExitTimeDtorsTest, Issue2097) {
8504+
GTEST_FLAG_SET(death_test_style, "threadsafe");
8505+
ASSERT_EXIT(
8506+
{
8507+
issue2097_svr = new Server();
8508+
std::atexit([]() {
8509+
// Wait a bit before stopping server to simulate delayed exit
8510+
std::this_thread::sleep_for(std::chrono::milliseconds(200));
8511+
issue2097_svr->stop();
8512+
issue2097_svr_thread->join();
8513+
});
8514+
8515+
issue2097_svr_thread = new std::thread([]() {
8516+
issue2097_svr->Get(
8517+
"/hi", [](const Request & /*req*/, httplib::Response &res) {
8518+
res.set_content("Quack", "text/plain");
8519+
});
8520+
8521+
issue2097_svr->listen(HOST, PORT);
8522+
});
8523+
8524+
std::thread cli_thread([]() {
8525+
Client cli(HOST, PORT);
8526+
while (true) {
8527+
auto res = cli.Get("/hi");
8528+
std::this_thread::sleep_for(std::chrono::milliseconds(1));
8529+
}
8530+
});
8531+
8532+
std::thread([]() {
8533+
std::this_thread::sleep_for(std::chrono::milliseconds(200));
8534+
std::exit(42);
8535+
}).join();
8536+
},
8537+
#ifdef CPPHTTPLIB_NO_EXIT_TIME_DESTRUCTORS
8538+
::testing::ExitedWithCode(42),
8539+
#else
8540+
KilledByAbortOrSegfault,
8541+
#endif
8542+
"");
8543+
}
8544+
#endif

0 commit comments

Comments
 (0)