[Sanitizer] Fix fopencookie() interceptor to work with null hook functions.
authorAlexey Samsonov <vonosmas@gmail.com>
Tue, 14 Jul 2015 20:13:42 +0000 (20:13 +0000)
committerAlexey Samsonov <vonosmas@gmail.com>
Tue, 14 Jul 2015 20:13:42 +0000 (20:13 +0000)
llvm-svn: 242193

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

index 9b75a30..a7772b7 100644 (file)
@@ -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;
 }
index e9616d5..e5b8f93 100644 (file)
@@ -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);
 }