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() {
}
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.
// We have to rely on atexit().
atexit(helper_exit_global);
......
......@@ -233,7 +233,7 @@ void return_keytable(bthread_keytable_pool_t* pool, KeyTable* kt) {
pool->free_keytables = kt;
}
static void cleanup_pthread(void*) {
static void cleanup_pthread() {
KeyTable* kt = tls_bls.keytable;
if (kt) {
delete kt;
......@@ -446,7 +446,7 @@ int bthread_setspecific(bthread_key_t key, void* data) __THROW {
}
if (!bthread::tls_ever_created_keytable) {
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);
......
......@@ -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+=-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
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
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
all: $(TEST_BINS)
.PHONY:clean
clean:clean_bins
clean:clean_bins clean_dbg
@echo "Cleaning"
@rm -rf $(TEST_BASE_OBJS) $(TEST_BVAR_OBJS) $(TEST_BTHREAD_OBJS) \
$(TEST_BRPC_OBJS) $(TEST_PROTO_OBJS)
......@@ -161,12 +160,20 @@ clean:clean_bins
clean_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
FORCE:
.PRECIOUS: %.o
test_base:$(TEST_BASE_OBJS) ../libbrpc.dbg.a
@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
@echo "Linking $@"
......@@ -192,6 +199,3 @@ brpc_%_unittest:$(TEST_PROTO_OBJS) brpc_%_unittest.o ../libbrpc.dbg.a
@echo "Compiling $@"
@$(CXX) -c $(HDRPATHS) $(CXXFLAGS) $< -o $@
%.o:%.c
@echo "Compiling $@"
@$(CC) -c $(HDRPATHS) $(CFLAGS) $< -o $@
......@@ -10,31 +10,28 @@ namespace {
BAIDU_THREAD_LOCAL int * dummy = NULL;
const size_t NTHREAD = 8;
bool processed[NTHREAD+1];
bool deleted[NTHREAD+1];
bool register_check = false;
static bool processed[NTHREAD+1];
static bool deleted[NTHREAD+1];
static bool register_check = false;
struct YellObj {
static int nc;
static int nd;
YellObj() {
printf("Created\n");
++nc;
}
~YellObj() {
printf("Destroyed\n");
++nd;
}
};
int YellObj::nc = 0;
int YellObj::nd = 0;
void check_global_variable() {
ASSERT_TRUE(processed[NTHREAD]);
ASSERT_TRUE(deleted[NTHREAD]);
ASSERT_EQ(2, YellObj::nc);
ASSERT_EQ(2, YellObj::nd);
static void check_global_variable() {
EXPECT_TRUE(processed[NTHREAD]);
EXPECT_TRUE(deleted[NTHREAD]);
EXPECT_EQ(2, YellObj::nc);
EXPECT_EQ(2, YellObj::nd);
}
class BaiduThreadLocalTest : public ::testing::Test{
......@@ -42,7 +39,7 @@ protected:
BaiduThreadLocalTest(){
if (!register_check) {
register_check = true;
atexit(check_global_variable);
base::thread_atexit(check_global_variable);
}
};
virtual ~BaiduThreadLocalTest(){};
......@@ -99,9 +96,8 @@ TEST_F(BaiduThreadLocalTest, get_thread_local) {
}
void delete_dummy(void* arg) {
*((bool*)arg) = true;
*(bool*)arg = true;
if (dummy) {
printf("dummy(%p)=%d\n", dummy, *dummy);
delete dummy;
dummy = NULL;
} else {
......@@ -112,6 +108,7 @@ void delete_dummy(void* arg) {
void* proc_dummy(void* arg) {
bool *p = (bool*)arg;
*p = true;
EXPECT_TRUE(dummy == NULL);
dummy = new int(p - processed);
base::thread_atexit(delete_dummy, deleted + (p - processed));
return NULL;
......@@ -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() {
oss << "fun1" << std::endl;
get_oss() << "fun1" << std::endl;
}
void fun2() {
oss << "fun2" << std::endl;
get_oss() << "fun2" << std::endl;
}
void fun3(void* arg) {
oss << "fun3(" << arg << ")" << std::endl;
get_oss() << "fun3(" << arg << ")" << std::endl;
}
void fun4(void* arg) {
oss << "fun4(" << arg << ")" << std::endl;
get_oss() << "fun4(" << arg << ")" << std::endl;
}
void check_result() {
ASSERT_EQ("fun4(0)\nfun3(0x2)\nfun2\n", oss.str());
static void check_result() {
ASSERT_EQ("fun4(0)\nfun3(0x2)\nfun2\n", get_oss().str());
}
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);
}
}
} // namespace
......@@ -228,10 +228,12 @@ TEST(SecurityTest, CallocOverflow) {
EXPECT_TRUE(CallocReturnsNull(kArraySize2, kArraySize));
} else {
// It's also ok for calloc to just terminate the process.
#if defined(GTEST_HAS_DEATH_TEST)
EXPECT_DEATH(CallocReturnsNull(kArraySize, kArraySize2), "");
EXPECT_DEATH(CallocReturnsNull(kArraySize2, kArraySize), "");
#endif // GTEST_HAS_DEATH_TEST
// NOTE(gejun): base/process/memory.cc is not linked right now,
// disable following assertions on calloc
//#if defined(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