[compiler-rt] r242193 - [Sanitizer] Fix fopencookie() interceptor to work with null hook functions.

Alexey Samsonov vonosmas at gmail.com
Tue Jul 14 13:13:43 PDT 2015


Author: samsonov
Date: Tue Jul 14 15:13:42 2015
New Revision: 242193

URL: http://llvm.org/viewvc/llvm-project?rev=242193&view=rev
Log:
[Sanitizer] Fix fopencookie() interceptor to work with null hook functions.

Modified:
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc
    compiler-rt/trunk/test/msan/Linux/fopencookie.cc

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc?rev=242193&r1=242192&r2=242193&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc Tue Jul 14 15:13:42 2015
@@ -4965,29 +4965,31 @@ struct WrappedCookie {
 static uptr wrapped_read(void *cookie, char *buf, uptr size) {
   COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
   WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
-  return wrapped_cookie->real_io_funcs.read(wrapped_cookie->real_cookie, buf,
-                                            size);
+  __sanitizer_cookie_io_read real_read = wrapped_cookie->real_io_funcs.read;
+  return real_read ? real_read(wrapped_cookie->real_cookie, buf, size) : 0;
 }
 
 static uptr wrapped_write(void *cookie, const char *buf, uptr size) {
   COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
   WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
-  return wrapped_cookie->real_io_funcs.write(wrapped_cookie->real_cookie, buf,
-                                             size);
+  __sanitizer_cookie_io_write real_write = wrapped_cookie->real_io_funcs.write;
+  return real_write ? real_write(wrapped_cookie->real_cookie, buf, size) : size;
 }
 
 static int wrapped_seek(void *cookie, u64 *offset, int whence) {
   COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
   COMMON_INTERCEPTOR_INITIALIZE_RANGE(offset, sizeof(*offset));
   WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
-  return wrapped_cookie->real_io_funcs.seek(wrapped_cookie->real_cookie, offset,
-                                            whence);
+  __sanitizer_cookie_io_seek real_seek = wrapped_cookie->real_io_funcs.seek;
+  return real_seek ? real_seek(wrapped_cookie->real_cookie, offset, whence)
+                   : -1;
 }
 
 static int wrapped_close(void *cookie) {
   COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
   WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
-  int res = wrapped_cookie->real_io_funcs.close(wrapped_cookie->real_cookie);
+  __sanitizer_cookie_io_close real_close = wrapped_cookie->real_io_funcs.close;
+  int res = real_close ? real_close(wrapped_cookie->real_cookie) : 0;
   InternalFree(wrapped_cookie);
   return res;
 }

Modified: compiler-rt/trunk/test/msan/Linux/fopencookie.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/msan/Linux/fopencookie.cc?rev=242193&r1=242192&r2=242193&view=diff
==============================================================================
--- compiler-rt/trunk/test/msan/Linux/fopencookie.cc (original)
+++ compiler-rt/trunk/test/msan/Linux/fopencookie.cc Tue Jul 14 15:13:42 2015
@@ -56,4 +56,10 @@ int main() {
   fread(buf, 50, 1, f);
   fwrite(buf, 50, 1, f);
   fclose(f);
+
+  f = fopencookie(cookie, "rw", {nullptr, nullptr, nullptr, nullptr});
+  fseek(f, 100, SEEK_SET);
+  fread(buf, 50, 1, f);
+  fwrite(buf, 50, 1, f);
+  fclose(f);
 }





More information about the llvm-commits mailing list