Commit 83a43254 authored by gejun's avatar gejun

Patch svn r35212 r35213 & disable death test on calloc

parent d5b90801
...@@ -76,7 +76,10 @@ static void helper_exit_global() { ...@@ -76,7 +76,10 @@ static void helper_exit_global() {
} }
static void make_thread_atexit_key() { static void make_thread_atexit_key() {
pthread_key_create(&thread_atexit_key, delete_thread_exit_helper); if (pthread_key_create(&thread_atexit_key, delete_thread_exit_helper) != 0) {
fprintf(stderr, "Fail to create thread_atexit_key, abort\n");
abort();
}
// If caller is not pthread, delete_thread_exit_helper will not be called. // If caller is not pthread, delete_thread_exit_helper will not be called.
// We have to rely on atexit(). // We have to rely on atexit().
atexit(helper_exit_global); atexit(helper_exit_global);
......
...@@ -233,7 +233,7 @@ void return_keytable(bthread_keytable_pool_t* pool, KeyTable* kt) { ...@@ -233,7 +233,7 @@ void return_keytable(bthread_keytable_pool_t* pool, KeyTable* kt) {
pool->free_keytables = kt; pool->free_keytables = kt;
} }
static void cleanup_pthread(void*) { static void cleanup_pthread() {
KeyTable* kt = tls_bls.keytable; KeyTable* kt = tls_bls.keytable;
if (kt) { if (kt) {
delete kt; delete kt;
...@@ -446,7 +446,7 @@ int bthread_setspecific(bthread_key_t key, void* data) __THROW { ...@@ -446,7 +446,7 @@ int bthread_setspecific(bthread_key_t key, void* data) __THROW {
} }
if (!bthread::tls_ever_created_keytable) { if (!bthread::tls_ever_created_keytable) {
bthread::tls_ever_created_keytable = true; bthread::tls_ever_created_keytable = true;
CHECK_EQ(0, base::thread_atexit(bthread::cleanup_pthread, NULL)); CHECK_EQ(0, base::thread_atexit(bthread::cleanup_pthread));
} }
} }
return kt->set_data(key, data); return kt->set_data(key, data);
......
...@@ -5,7 +5,6 @@ include ../config.mk ...@@ -5,7 +5,6 @@ include ../config.mk
CPPFLAGS=-DBTHREAD_USE_FAST_PTHREAD_MUTEX -D__const__= -D_GNU_SOURCE -DUSE_SYMBOLIZE -DNO_TCMALLOC -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS CPPFLAGS=-DBTHREAD_USE_FAST_PTHREAD_MUTEX -D__const__= -D_GNU_SOURCE -DUSE_SYMBOLIZE -DNO_TCMALLOC -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS
CPPFLAGS+=-DUNIT_TEST -Dprivate=public -Dprotected=public -DBVAR_NOT_LINK_DEFAULT_VARIABLES CPPFLAGS+=-DUNIT_TEST -Dprivate=public -Dprotected=public -DBVAR_NOT_LINK_DEFAULT_VARIABLES
CXXFLAGS=$(CPPFLAGS) -g -pipe -Wall -W -fPIC -fstrict-aliasing -Wno-invalid-offsetof -Wno-unused-parameter -fno-omit-frame-pointer -std=c++0x CXXFLAGS=$(CPPFLAGS) -g -pipe -Wall -W -fPIC -fstrict-aliasing -Wno-invalid-offsetof -Wno-unused-parameter -fno-omit-frame-pointer -std=c++0x
CFLAGS=$(CPPFLAGS) -g -pipe -Wall -W -fPIC -fstrict-aliasing -Wno-unused-parameter -fno-omit-frame-pointer
#required by base/crc32.cc to boost performance for 10x #required by base/crc32.cc to boost performance for 10x
ifeq ($(shell test $(GCC_VERSION) -ge 40400; echo $$?),0) ifeq ($(shell test $(GCC_VERSION) -ge 40400; echo $$?),0)
...@@ -152,7 +151,7 @@ TEST_BINS = test_base test_bvar $(TEST_BTHREAD_SOURCES:.cpp=) $(TEST_BRPC_SOURCE ...@@ -152,7 +151,7 @@ TEST_BINS = test_base test_bvar $(TEST_BTHREAD_SOURCES:.cpp=) $(TEST_BRPC_SOURCE
all: $(TEST_BINS) all: $(TEST_BINS)
.PHONY:clean .PHONY:clean
clean:clean_bins clean:clean_bins clean_dbg
@echo "Cleaning" @echo "Cleaning"
@rm -rf $(TEST_BASE_OBJS) $(TEST_BVAR_OBJS) $(TEST_BTHREAD_OBJS) \ @rm -rf $(TEST_BASE_OBJS) $(TEST_BVAR_OBJS) $(TEST_BTHREAD_OBJS) \
$(TEST_BRPC_OBJS) $(TEST_PROTO_OBJS) $(TEST_BRPC_OBJS) $(TEST_PROTO_OBJS)
...@@ -161,12 +160,20 @@ clean:clean_bins ...@@ -161,12 +160,20 @@ clean:clean_bins
clean_bins: clean_bins:
@rm -rf $(TEST_BINS) @rm -rf $(TEST_BINS)
../libbrpc.dbg.a: .PHONY:clean_dbg
clean_dbg:
@$(MAKE) -C.. clean_debug
../libbrpc.dbg.a: FORCE
@$(MAKE) -C.. libbrpc.dbg.a @$(MAKE) -C.. libbrpc.dbg.a
FORCE:
.PRECIOUS: %.o
test_base:$(TEST_BASE_OBJS) ../libbrpc.dbg.a test_base:$(TEST_BASE_OBJS) ../libbrpc.dbg.a
@echo "Linking $@" @echo "Linking $@"
@$(CXX) -o $@ $(LIBPATHS) -Xlinker "-(" $^ -Wl,-Bstatic $(STATIC_LINKINGS) -Wl,-Bdynamic -Xlinker "-)" $(DYNAMIC_LINKINGS) $(CXX) -o $@ $(LIBPATHS) -Xlinker "-(" $^ -Wl,-Bstatic $(STATIC_LINKINGS) -Wl,-Bdynamic -Xlinker "-)" $(DYNAMIC_LINKINGS)
test_bvar:$(TEST_BVAR_OBJS) ../libbrpc.dbg.a test_bvar:$(TEST_BVAR_OBJS) ../libbrpc.dbg.a
@echo "Linking $@" @echo "Linking $@"
...@@ -192,6 +199,3 @@ brpc_%_unittest:$(TEST_PROTO_OBJS) brpc_%_unittest.o ../libbrpc.dbg.a ...@@ -192,6 +199,3 @@ brpc_%_unittest:$(TEST_PROTO_OBJS) brpc_%_unittest.o ../libbrpc.dbg.a
@echo "Compiling $@" @echo "Compiling $@"
@$(CXX) -c $(HDRPATHS) $(CXXFLAGS) $< -o $@ @$(CXX) -c $(HDRPATHS) $(CXXFLAGS) $< -o $@
%.o:%.c
@echo "Compiling $@"
@$(CC) -c $(HDRPATHS) $(CFLAGS) $< -o $@
...@@ -10,31 +10,28 @@ namespace { ...@@ -10,31 +10,28 @@ namespace {
BAIDU_THREAD_LOCAL int * dummy = NULL; BAIDU_THREAD_LOCAL int * dummy = NULL;
const size_t NTHREAD = 8; const size_t NTHREAD = 8;
bool processed[NTHREAD+1]; static bool processed[NTHREAD+1];
bool deleted[NTHREAD+1]; static bool deleted[NTHREAD+1];
bool register_check = false; static bool register_check = false;
struct YellObj { struct YellObj {
static int nc; static int nc;
static int nd; static int nd;
YellObj() { YellObj() {
printf("Created\n");
++nc; ++nc;
} }
~YellObj() { ~YellObj() {
printf("Destroyed\n");
++nd; ++nd;
} }
}; };
int YellObj::nc = 0; int YellObj::nc = 0;
int YellObj::nd = 0; int YellObj::nd = 0;
static void check_global_variable() {
void check_global_variable() { EXPECT_TRUE(processed[NTHREAD]);
ASSERT_TRUE(processed[NTHREAD]); EXPECT_TRUE(deleted[NTHREAD]);
ASSERT_TRUE(deleted[NTHREAD]); EXPECT_EQ(2, YellObj::nc);
ASSERT_EQ(2, YellObj::nc); EXPECT_EQ(2, YellObj::nd);
ASSERT_EQ(2, YellObj::nd);
} }
class BaiduThreadLocalTest : public ::testing::Test{ class BaiduThreadLocalTest : public ::testing::Test{
...@@ -42,7 +39,7 @@ protected: ...@@ -42,7 +39,7 @@ protected:
BaiduThreadLocalTest(){ BaiduThreadLocalTest(){
if (!register_check) { if (!register_check) {
register_check = true; register_check = true;
atexit(check_global_variable); base::thread_atexit(check_global_variable);
} }
}; };
virtual ~BaiduThreadLocalTest(){}; virtual ~BaiduThreadLocalTest(){};
...@@ -99,9 +96,8 @@ TEST_F(BaiduThreadLocalTest, get_thread_local) { ...@@ -99,9 +96,8 @@ TEST_F(BaiduThreadLocalTest, get_thread_local) {
} }
void delete_dummy(void* arg) { void delete_dummy(void* arg) {
*((bool*)arg) = true; *(bool*)arg = true;
if (dummy) { if (dummy) {
printf("dummy(%p)=%d\n", dummy, *dummy);
delete dummy; delete dummy;
dummy = NULL; dummy = NULL;
} else { } else {
...@@ -112,6 +108,7 @@ void delete_dummy(void* arg) { ...@@ -112,6 +108,7 @@ void delete_dummy(void* arg) {
void* proc_dummy(void* arg) { void* proc_dummy(void* arg) {
bool *p = (bool*)arg; bool *p = (bool*)arg;
*p = true; *p = true;
EXPECT_TRUE(dummy == NULL);
dummy = new int(p - processed); dummy = new int(p - processed);
base::thread_atexit(delete_dummy, deleted + (p - processed)); base::thread_atexit(delete_dummy, deleted + (p - processed));
return NULL; return NULL;
...@@ -139,26 +136,32 @@ TEST_F(BaiduThreadLocalTest, sanity) { ...@@ -139,26 +136,32 @@ TEST_F(BaiduThreadLocalTest, sanity) {
} }
} }
std::stringstream oss; static std::ostringstream* oss = NULL;
inline std::ostringstream& get_oss() {
if (oss == NULL) {
oss = new std::ostringstream;
}
return *oss;
}
void fun1() { void fun1() {
oss << "fun1" << std::endl; get_oss() << "fun1" << std::endl;
} }
void fun2() { void fun2() {
oss << "fun2" << std::endl; get_oss() << "fun2" << std::endl;
} }
void fun3(void* arg) { void fun3(void* arg) {
oss << "fun3(" << arg << ")" << std::endl; get_oss() << "fun3(" << arg << ")" << std::endl;
} }
void fun4(void* arg) { void fun4(void* arg) {
oss << "fun4(" << arg << ")" << std::endl; get_oss() << "fun4(" << arg << ")" << std::endl;
} }
void check_result() { static void check_result() {
ASSERT_EQ("fun4(0)\nfun3(0x2)\nfun2\n", oss.str()); ASSERT_EQ("fun4(0)\nfun3(0x2)\nfun2\n", get_oss().str());
} }
TEST_F(BaiduThreadLocalTest, call_order_and_cancel) { TEST_F(BaiduThreadLocalTest, call_order_and_cancel) {
...@@ -182,4 +185,4 @@ TEST_F(BaiduThreadLocalTest, call_order_and_cancel) { ...@@ -182,4 +185,4 @@ TEST_F(BaiduThreadLocalTest, call_order_and_cancel) {
base::thread_atexit_cancel(fun3, (void*)1); base::thread_atexit_cancel(fun3, (void*)1);
} }
} } // namespace
...@@ -228,10 +228,12 @@ TEST(SecurityTest, CallocOverflow) { ...@@ -228,10 +228,12 @@ TEST(SecurityTest, CallocOverflow) {
EXPECT_TRUE(CallocReturnsNull(kArraySize2, kArraySize)); EXPECT_TRUE(CallocReturnsNull(kArraySize2, kArraySize));
} else { } else {
// It's also ok for calloc to just terminate the process. // It's also ok for calloc to just terminate the process.
#if defined(GTEST_HAS_DEATH_TEST) // NOTE(gejun): base/process/memory.cc is not linked right now,
EXPECT_DEATH(CallocReturnsNull(kArraySize, kArraySize2), ""); // disable following assertions on calloc
EXPECT_DEATH(CallocReturnsNull(kArraySize2, kArraySize), ""); //#if defined(GTEST_HAS_DEATH_TEST)
#endif // GTEST_HAS_DEATH_TEST // EXPECT_DEATH(CallocReturnsNull(kArraySize, kArraySize2), "");
// EXPECT_DEATH(CallocReturnsNull(kArraySize2, kArraySize), "");
//#endif // GTEST_HAS_DEATH_TEST
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment