Skip to content

Commit 014d75c

Browse files
authored
Fix PHOTON_BUILD_WITH_ASAN (alibaba#940)
1 parent 79228b2 commit 014d75c

2 files changed

Lines changed: 46 additions & 45 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ if (PHOTON_BUILD_WITH_ASAN)
6868
if ((NOT CMAKE_BUILD_TYPE STREQUAL "Debug") OR (NOT CMAKE_SYSTEM_NAME STREQUAL "Linux"))
6969
message(FATAL_ERROR "Wrong environment")
7070
endif ()
71+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -static-libasan")
7172
add_link_options(-fsanitize=address -static-libasan)
7273
endif ()
7374

thread/thread.cpp

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -169,51 +169,6 @@ namespace photon
169169
void* _ptr;
170170
};
171171

172-
#if defined(__has_feature)
173-
# if __has_feature(address_sanitizer) // for clang
174-
# define __SANITIZE_ADDRESS__ // GCC already sets this
175-
# endif
176-
#endif
177-
178-
#ifdef __SANITIZE_ADDRESS__
179-
extern "C" {
180-
// Check out sanitizer/asan-interface.h in compiler-rt for documentation.
181-
void __sanitizer_start_switch_fiber(void** fake_stack_save, const void* bottom,
182-
size_t size);
183-
void __sanitizer_finish_switch_fiber(void* fake_stack_save,
184-
const void** bottom_old, size_t* size_old);
185-
}
186-
187-
static void asan_start(void** save, thread* to) {
188-
void* bottom = to->buf ? to->buf : to->stackful_alloc_top;
189-
__sanitizer_start_switch_fiber(save, bottom,
190-
to->stack_size);
191-
}
192-
193-
static void asan_finish(void* save) {
194-
__sanitizer_finish_switch_fiber(save, nullptr, nullptr);
195-
}
196-
197-
#define ASAN_START() asan_finish((void*)nullptr);
198-
199-
#define ASAN_SWITCH(to) \
200-
void* __save; \
201-
asan_start(&__save, to); \
202-
DEFER({ asan_finish(__save); });
203-
204-
#define ASAN_DIE_SWITCH(to) \
205-
asan_start(nullptr, to);
206-
207-
#else
208-
#define ASAN_START(ptr)
209-
#define ASAN_SWITCH(to)
210-
#define ASAN_DIE_SWITCH(to)
211-
#endif
212-
213-
static void _asan_start() asm("_asan_start");
214-
215-
__attribute__((used)) static void _asan_start() { ASAN_START(); }
216-
217172
struct thread_list;
218173
struct thread : public intrusive_list_node<thread> {
219174
volatile vcpu_t* vcpu;
@@ -335,6 +290,51 @@ namespace photon
335290
}
336291
};
337292

293+
#if defined(__has_feature)
294+
# if __has_feature(address_sanitizer) // for clang
295+
# define __SANITIZE_ADDRESS__ // GCC already sets this
296+
# endif
297+
#endif
298+
299+
#ifdef __SANITIZE_ADDRESS__
300+
extern "C" {
301+
// Check out sanitizer/asan-interface.h in compiler-rt for documentation.
302+
void __sanitizer_start_switch_fiber(void** fake_stack_save, const void* bottom,
303+
size_t size);
304+
void __sanitizer_finish_switch_fiber(void* fake_stack_save,
305+
const void** bottom_old, size_t* size_old);
306+
}
307+
308+
static void asan_start(void** save, thread* to) {
309+
void* bottom = to->buf ? to->buf : to->stackful_alloc_top;
310+
__sanitizer_start_switch_fiber(save, bottom,
311+
to->stack_size);
312+
}
313+
314+
static void asan_finish(void* save) {
315+
__sanitizer_finish_switch_fiber(save, nullptr, nullptr);
316+
}
317+
318+
#define ASAN_START() asan_finish((void*)nullptr);
319+
320+
#define ASAN_SWITCH(to) \
321+
void* __save; \
322+
asan_start(&__save, to); \
323+
DEFER({ asan_finish(__save); });
324+
325+
#define ASAN_DIE_SWITCH(to) \
326+
asan_start(nullptr, to);
327+
328+
#else
329+
#define ASAN_START(ptr)
330+
#define ASAN_SWITCH(to)
331+
#define ASAN_DIE_SWITCH(to)
332+
#endif
333+
334+
static void _asan_start() asm("_asan_start");
335+
336+
__attribute__((used)) static void _asan_start() { ASAN_START(); }
337+
338338
#pragma GCC diagnostic push
339339
#pragma GCC diagnostic ignored "-Winvalid-offsetof"
340340
static_assert(offsetof(thread, vcpu) == offsetof(partial_thread, vcpu), "...");

0 commit comments

Comments
 (0)