Skip to content

Commit

Permalink
tsan: Add basic interface
Browse files Browse the repository at this point in the history
Declare all the function tsan_{read,write}{size}(), and let them call to
the check_access function.

Signed-off-by: Chih-En Lin <shiyn.lin@gmail.com>
  • Loading branch information
linD026 committed Jun 6, 2022
1 parent bded865 commit 807ffbf
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 55 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ CFLAGS+=-D'CONFIG_GCC=y'
endif

SRC:=src/core.c
SRC+=src/tsan.c
LIB:=lib/per_cpu.c

OBJ=$(SRC:.c=.o)
Expand Down
2 changes: 1 addition & 1 deletion include/gcc-generic.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#else

#error "CONFIG_GCC seted, but doesn't support it"
#error "CONFIG_GCC set, but doesn't support it"

#endif

Expand Down
4 changes: 4 additions & 0 deletions include/ucsan/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@
})
#endif

#define __alias(symbol) __attribute__((__alias__(#symbol)))
#define _RET_IP_ (unsigned long)__builtin_return_address(0)
#define __always_inline inline __attribute__((__always_inline__))

#endif /* __COMPILER__ */
57 changes: 47 additions & 10 deletions src/core.c
Original file line number Diff line number Diff line change
@@ -1,23 +1,60 @@
#define _GNU_SOURCE
#include <ucsan/per_cpu.h>
#include <ucsan/compiler.h>
#include <uapi/ucsan.h>
#include <stdio.h>

DEFINE_PER_CPU(int, arr);
DEFINE_PER_CPU(int, arrb);
#define UCSAN_ACCESS_READ 0x0
#define UCSAN_ACCESS_WRITE 0x1
#define UCSAN_ACCESS_COMPOUND 0x2

int test_per_cpu(void)
static __always_inline void check_access(const volatile void *ptr, size_t size,
int type, unsigned long ip)
{
int *per_cpu_p;
}

per_cpu_p = per_cpu(arr);
/*
* tsan function - export to the compiler
*/

*per_cpu_p = 1234;
#define DEFINE_TSAN_READ_WRITE(size) \
void __tsan_read##size(void *ptr); \
void __tsan_read##size(void *ptr) \
{ \
check_access(ptr, size, 0, _RET_IP_); \
} \
void __tsan_unaligned_read##size(void *ptr) \
__alias(__tsan_read##size); \
void __tsan_write##size(void *ptr); \
void __tsan_write##size(void *ptr) \
{ \
check_access(ptr, size, UCSAN_ACCESS_WRITE, _RET_IP_); \
} \
void __tsan_unaligned_write##size(void *ptr) \
__alias(__tsan_write##size); \
void __tsan_read_write##size(void *ptr); \
void __tsan_read_write##size(void *ptr) \
{ \
check_access(ptr, size, \
UCSAN_ACCESS_COMPOUND | UCSAN_ACCESS_WRITE, \
_RET_IP_); \
} \
void __tsan_unaligned_read_write##size(void *ptr) \
__alias(__tsan_read_write##size);

barrier();
DEFINE_TSAN_READ_WRITE(1);
DEFINE_TSAN_READ_WRITE(2);
DEFINE_TSAN_READ_WRITE(4);
DEFINE_TSAN_READ_WRITE(8);
DEFINE_TSAN_READ_WRITE(16);

printf("per cpu: %d %d\n", *per_cpu(arr), sched_getcpu());
void __tsan_func_entry(void *call_pc)
{
}

return 0;
void __tsan_func_exit(void *p)
{
}

void __tsan_init(void)
{
}
43 changes: 0 additions & 43 deletions src/tsan.c

This file was deleted.

0 comments on commit 807ffbf

Please sign in to comment.