Skip to content

Instantly share code, notes, and snippets.

@sitsofe
Last active June 12, 2016 06:55
Show Gist options
  • Save sitsofe/2081422a4fbde48b739030a093ce63b9 to your computer and use it in GitHub Desktop.
Save sitsofe/2081422a4fbde48b739030a093ce63b9 to your computer and use it in GitHub Desktop.
MemorySanitizer patch for libaio
x86_64 only patch to build libaio under MemorySanitzer. Based against libaio git repo from
https://git.fedorahosted.org/cgit/libaio.git/ and using the information provided by Evgeniy Stepanov in
https://github.com/google/sanitizers/issues/688#issuecomment-225403581 .
diff --git a/src/Makefile b/src/Makefile
index eadb336..c839c9c 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -2,8 +2,8 @@ prefix=/usr
includedir=$(prefix)/include
libdir=$(prefix)/lib
-CFLAGS ?= -g -fomit-frame-pointer -O2
-CFLAGS += -nostdlib -nostartfiles -Wall -I. -fPIC
+CFLAGS ?= -g -fomit-frame-pointer -O2 -fsanitize=memory
+CFLAGS += -nostdlib -nostartfiles -Wall -I. -fPIC -fsanitize=memory
SO_CFLAGS=-shared $(CFLAGS)
L_CFLAGS=$(CFLAGS)
LINK_FLAGS=
diff --git a/src/syscall-generic.h b/src/syscall-generic.h
index 24d7c7c..ffb35e0 100644
--- a/src/syscall-generic.h
+++ b/src/syscall-generic.h
@@ -1,10 +1,16 @@
#include <errno.h>
#include <unistd.h>
#include <sys/syscall.h>
+#include <sanitizer/linux_syscall_hooks.h>
+
+#define PRE(name, ...) __sanitizer_syscall_pre_##name(__VA_ARGS__)
+#define POST(name, res, ...) __sanitizer_syscall_post_##name(res, __VA_ARGS__)
#define _body_io_syscall(sname, args...) \
{ \
+ PRE(sname, ## args); \
int ret = syscall(__NR_##sname, ## args); \
+ POST(sname, ret, ## args); \
return ret < 0 ? -errno : ret; \
}
diff --git a/src/syscall-x86_64.h b/src/syscall-x86_64.h
index 9361856..21beef2 100644
--- a/src/syscall-x86_64.h
+++ b/src/syscall-x86_64.h
@@ -1,3 +1,5 @@
+#include <sanitizer/linux_syscall_hooks.h>
+
#define __NR_io_setup 206
#define __NR_io_destroy 207
#define __NR_io_getevents 208
@@ -7,13 +9,18 @@
#define __syscall_clobber "r11","rcx","memory"
#define __syscall "syscall"
+#define PRE(name, ...) __sanitizer_syscall_pre_##name(__VA_ARGS__)
+#define POST(name, res, ...) __sanitizer_syscall_post_##name(res, __VA_ARGS__)
+
#define io_syscall1(type,fname,sname,type1,arg1) \
type fname(type1 arg1) \
{ \
long __res; \
+PRE(sname, arg1); \
__asm__ volatile (__syscall \
: "=a" (__res) \
: "0" (__NR_##sname),"D" ((long)(arg1)) : __syscall_clobber ); \
+POST(sname, __res, arg1); \
return __res; \
}
@@ -21,9 +28,11 @@ return __res; \
type fname(type1 arg1,type2 arg2) \
{ \
long __res; \
+PRE(sname, arg1, arg2); \
__asm__ volatile (__syscall \
: "=a" (__res) \
: "0" (__NR_##sname),"D" ((long)(arg1)),"S" ((long)(arg2)) : __syscall_clobber ); \
+POST(sname, __res, arg1, arg2); \
return __res; \
}
@@ -31,10 +40,12 @@ return __res; \
type fname(type1 arg1,type2 arg2,type3 arg3) \
{ \
long __res; \
+PRE(sname, arg1, arg2, arg3); \
__asm__ volatile (__syscall \
: "=a" (__res) \
: "0" (__NR_##sname),"D" ((long)(arg1)),"S" ((long)(arg2)), \
"d" ((long)(arg3)) : __syscall_clobber); \
+POST(sname, __res, arg1, arg2, arg3); \
return __res; \
}
@@ -42,10 +53,12 @@ return __res; \
type fname (type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
{ \
long __res; \
+PRE(sname, arg1, arg2, arg3, arg4); \
__asm__ volatile ("movq %5,%%r10 ;" __syscall \
: "=a" (__res) \
: "0" (__NR_##sname),"D" ((long)(arg1)),"S" ((long)(arg2)), \
"d" ((long)(arg3)),"g" ((long)(arg4)) : __syscall_clobber,"r10" ); \
+POST(sname, __res, arg1, arg2, arg3, arg4); \
return __res; \
}
@@ -54,10 +67,12 @@ return __res; \
type fname (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
{ \
long __res; \
+PRE(sname, arg1, arg2, arg3, arg4, arg5); \
__asm__ volatile ("movq %5,%%r10 ; movq %6,%%r8 ; " __syscall \
: "=a" (__res) \
: "0" (__NR_##sname),"D" ((long)(arg1)),"S" ((long)(arg2)), \
"d" ((long)(arg3)),"g" ((long)(arg4)),"g" ((long)(arg5)) : \
__syscall_clobber,"r8","r10" ); \
+POST(sname, __res, arg1, arg2, arg3, arg4, arg5); \
return __res; \
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment