Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/actions/init-ut-make-config/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ inputs:
runs:
using: "composite"
steps:
- run: sudo apt-get update && sudo apt-get install -y clang-12 lldb-12 lld-12 libgtest-dev cmake gdb libstdc++6-11-dbg && cd /usr/src/gtest && export CC=clang-12 && export CXX=clang++-12 && sudo cmake . && sudo make -j ${{env.proc_num}} && sudo mv lib/libgtest* /usr/lib/
- run: sudo apt-get update && sudo apt-get install -y clang-12 lldb-12 lld-12 libgtest-dev cmake gdb libstdc++6-11-dbg && cd /usr/src/gtest && export CC=clang-12 && export CXX=clang++-12 && sudo cmake -DCMAKE_POLICY_VERSION_MINIMUM=3.5 . && sudo make -j ${{env.proc_num}} && sudo mv lib/libgtest* /usr/lib/
shell: bash
- run: sudo git clone https://github.com/libunwind/libunwind.git && cd libunwind && sudo git checkout tags/v1.8.1 && sudo mkdir -p /libunwind && sudo autoreconf -i && sudo CC=clang-12 CXX=clang++-12 ./configure --prefix=/libunwind && sudo make -j ${{env.proc_num}} && sudo make install
shell: bash
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/ci-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
export CC=gcc && export CXX=g++
mkdir build
cd build
cmake ..
cmake -DCMAKE_POLICY_VERSION_MINIMUM=3.5 ..
- name: compile
run: |
cd build
Expand Down Expand Up @@ -76,7 +76,7 @@ jobs:
export CC=gcc && export CXX=g++
mkdir build
cd build
cmake -DWITH_MESALINK=OFF -DWITH_GLOG=ON -DWITH_THRIFT=ON -DWITH_RDMA=ON -DWITH_DEBUG_BTHREAD_SCHE_SAFETY=ON -DWITH_DEBUG_LOCK=ON -DWITH_BTHREAD_TRACER=ON -DWITH_ASAN=ON ..
cmake -DWITH_MESALINK=OFF -DWITH_GLOG=ON -DWITH_THRIFT=ON -DWITH_RDMA=ON -DWITH_DEBUG_BTHREAD_SCHE_SAFETY=ON -DWITH_DEBUG_LOCK=ON -DWITH_BTHREAD_TRACER=ON -DWITH_ASAN=ON -DCMAKE_POLICY_VERSION_MINIMUM=3.5 ..
- name: compile
run: |
cd build
Expand Down Expand Up @@ -110,7 +110,7 @@ jobs:
export CC=clang && export CXX=clang++
mkdir build
cd build
cmake ..
cmake -DCMAKE_POLICY_VERSION_MINIMUM=3.5 ..
- name: compile
run: |
cd build
Expand Down Expand Up @@ -150,7 +150,7 @@ jobs:
export CC=clang && export CXX=clang++
mkdir build
cd build
cmake -DWITH_MESALINK=OFF -DWITH_GLOG=ON -DWITH_THRIFT=ON -DWITH_RDMA=ON -DWITH_DEBUG_BTHREAD_SCHE_SAFETY=ON -DWITH_DEBUG_LOCK=ON -DWITH_BTHREAD_TRACER=ON -DWITH_ASAN=ON ..
cmake -DWITH_MESALINK=OFF -DWITH_GLOG=ON -DWITH_THRIFT=ON -DWITH_RDMA=ON -DWITH_DEBUG_BTHREAD_SCHE_SAFETY=ON -DWITH_DEBUG_LOCK=ON -DWITH_BTHREAD_TRACER=ON -DWITH_ASAN=ON -DCMAKE_POLICY_VERSION_MINIMUM=3.5 ..
- name: compile
run: |
cd build
Expand Down
2 changes: 1 addition & 1 deletion docs/cn/sanitizers.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

ASan提供了[对协程的支持](https://reviews.llvm.org/D20913)。 在bthread创建、切换、销毁时,让ASan知道当前bthread的栈信息,主要用于维护[fake stack](https://github.com/google/sanitizers/wiki/AddressSanitizerUseAfterReturn)。

bRPC中启用ASan的方法:给config_brpc.sh增加`--with-glog`选项、给cmake增加`-DWITH_GLOG=ON`选项或者给bazel增加`--define with_asan=true`选项。
bRPC中启用ASan的方法:给config_brpc.sh增加`--with-asan`选项、给cmake增加`-DWITH_ASAN=ON`选项或者给bazel增加`--define with_asan=true`选项。

另外需要注意的是,ASan没法检测非ASan分配内存或者对象池复用内存。所以我们封装了两个宏,让ASan知道内存块是否能被使用。在非ASan环境下,这两个宏什么也不做,没有开销。

Expand Down
12 changes: 5 additions & 7 deletions src/bthread/stack_inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,8 @@ BUTIL_FORCE_INLINE void FinishSwitchFiber(void* fake_stack_save) {

class ScopedASanFiberSwitcher {
public:
ScopedASanFiberSwitcher(StackStorage& storage, bool ending) {
// If bthread will be quit here, pass NULL as `fake_stack_save',
// so that ASan knows it can destroy the fake stack.
StartSwitchFiber(ending ? NULL : &_fake_stack, storage);
ScopedASanFiberSwitcher(StackStorage& next_storage) {
StartSwitchFiber(&_fake_stack, next_storage);
}

~ScopedASanFiberSwitcher() {
Expand All @@ -92,16 +90,16 @@ class ScopedASanFiberSwitcher {
#define BTHREAD_ASAN_UNPOISON_MEMORY_REGION(storage) \
::bthread::internal::ASanUnpoisonMemoryRegion(storage)

#define BTHREAD_SCOPED_ASAN_FIBER_SWITCHER(storage, ending) \
::bthread::internal::ScopedASanFiberSwitcher switcher(storage, ending)
#define BTHREAD_SCOPED_ASAN_FIBER_SWITCHER(storage) \
::bthread::internal::ScopedASanFiberSwitcher switcher(storage)

} // namespace internal
#else

// If ASan are used, the annotations should be no-ops.
#define BTHREAD_ASAN_POISON_MEMORY_REGION(storage) ((void)(storage))
#define BTHREAD_ASAN_UNPOISON_MEMORY_REGION(storage) ((void)(storage))
#define BTHREAD_SCOPED_ASAN_FIBER_SWITCHER(storage, ending) ((void)(storage), (void)(ending))
#define BTHREAD_SCOPED_ASAN_FIBER_SWITCHER(storage) ((void)(storage))

#endif // BUTIL_USE_ASAN

Expand Down
3 changes: 1 addition & 2 deletions src/bthread/task_group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -716,8 +716,7 @@ void TaskGroup::sched_to(TaskGroup** pg, TaskMeta* next_meta, bool cur_ending) {
g->_control->_task_tracer.set_status(TASK_STATUS_JUMPING, next_meta);
#endif // BRPC_BTHREAD_TRACER
{
BTHREAD_SCOPED_ASAN_FIBER_SWITCHER(
cur_meta->stack->storage, cur_ending);
BTHREAD_SCOPED_ASAN_FIBER_SWITCHER(next_meta->stack->storage);
jump_stack(cur_meta->stack, next_meta->stack);
}
// probably went to another group, need to assign g again.
Expand Down
7 changes: 5 additions & 2 deletions test/brpc_load_balancer_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <gtest/gtest.h>
#include "bthread/bthread.h"
#include "gperftools_helper.h"
#include "butil/compiler_specific.h"
#include "butil/containers/doubly_buffered_data.h"
#include "brpc/describable.h"
#include "brpc/socket.h"
Expand Down Expand Up @@ -1155,6 +1156,7 @@ TEST_F(LoadBalancerTest, revived_from_all_failed_sanity) {
}
}

#ifndef BUTIL_USE_ASAN
class EchoServiceImpl : public test::EchoService {
public:
EchoServiceImpl()
Expand Down Expand Up @@ -1251,14 +1253,14 @@ TEST_F(LoadBalancerTest, revived_from_all_failed_intergrated) {
}

butil::EndPoint point(butil::IP_ANY, 7777);
brpc::Server server;
EchoServiceImpl service;
brpc::Server server;
ASSERT_EQ(0, server.AddService(&service, brpc::SERVER_DOESNT_OWN_SERVICE));
ASSERT_EQ(0, server.Start(point, NULL));

butil::EndPoint point2(butil::IP_ANY, 7778);
brpc::Server server2;
EchoServiceImpl service2;
brpc::Server server2;
ASSERT_EQ(0, server2.AddService(&service2, brpc::SERVER_DOESNT_OWN_SERVICE));
ASSERT_EQ(0, server2.Start(point2, NULL));

Expand Down Expand Up @@ -1287,6 +1289,7 @@ TEST_F(LoadBalancerTest, revived_from_all_failed_intergrated) {
bthread_usleep(500000 /* sleep longer than timeout of channel */);
ASSERT_EQ(0, num_failed.load(butil::memory_order_relaxed));
}
#endif // BUTIL_USE_ASAN

TEST_F(LoadBalancerTest, la_selection_too_long) {
brpc::GlobalInitializeOrDie();
Expand Down
2 changes: 2 additions & 0 deletions test/bthread_cond_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ static void launch_many_bthreads() {
}

TEST(CondTest, too_many_bthreads_from_pthread) {
bthread_setconcurrency(16);
launch_many_bthreads();
}

Expand All @@ -454,6 +455,7 @@ static void* run_launch_many_bthreads(void*) {
}

TEST(CondTest, too_many_bthreads_from_bthread) {
bthread_setconcurrency(16);
bthread_t th;
ASSERT_EQ(0, bthread_start_urgent(&th, NULL, run_launch_many_bthreads, NULL));
bthread_join(th, NULL);
Expand Down
2 changes: 1 addition & 1 deletion test/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ test_bins="test_butil test_bvar bthread*unittest brpc*unittest"
for test_bin in $test_bins; do
test_num=$((test_num + 1))
>&2 echo "[runtest] $test_bin"
ASAN_OPTIONS="detect_leaks=0" ./$test_bin
ASAN_OPTIONS="detect_leaks=0:detect_stack_use_after_return=1" ./$test_bin
# If ASan abort without detailed call stack of new/delete,
# try to disable fast_unwind_on_malloc, which would be a performance killer.
# ASAN_OPTIONS="fast_unwind_on_malloc=0:detect_leaks=0" ./$test_bin
Expand Down
2 changes: 1 addition & 1 deletion test/security_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#endif
#include "butil/compiler_specific.h"

#ifdef BUTIL_USE_ASAN
#ifndef BUTIL_USE_ASAN

using std::nothrow;
using std::numeric_limits;
Expand Down
Loading