Home | History | Annotate | Line # | Download | only in tests
      1 //===-- interception_linux_test.cc ----------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file is a part of ThreadSanitizer/AddressSanitizer runtime.
     11 // Tests for interception_linux.h.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 // Do not declare isdigit in ctype.h.
     16 #define __NO_CTYPE
     17 
     18 #include "interception/interception.h"
     19 
     20 #include "gtest/gtest.h"
     21 
     22 // Too slow for debug build
     23 #if !SANITIZER_DEBUG
     24 #if SANITIZER_LINUX
     25 
     26 static int InterceptorFunctionCalled;
     27 
     28 DECLARE_REAL(int, isdigit, int);
     29 
     30 INTERCEPTOR(int, isdigit, int d) {
     31   ++InterceptorFunctionCalled;
     32   return d >= '0' && d <= '9';
     33 }
     34 
     35 namespace __interception {
     36 
     37 TEST(Interception, GetRealFunctionAddress) {
     38   uptr malloc_address = 0;
     39   EXPECT_TRUE(GetRealFunctionAddress("malloc", &malloc_address, 0, 0));
     40   EXPECT_NE(0U, malloc_address);
     41 
     42   uptr dummy_address = 0;
     43   EXPECT_TRUE(
     44       GetRealFunctionAddress("dummy_doesnt_exist__", &dummy_address, 0, 0));
     45   EXPECT_EQ(0U, dummy_address);
     46 }
     47 
     48 TEST(Interception, Basic) {
     49   ASSERT_TRUE(INTERCEPT_FUNCTION(isdigit));
     50 
     51   // After interception, the counter should be incremented.
     52   InterceptorFunctionCalled = 0;
     53   EXPECT_NE(0, isdigit('1'));
     54   EXPECT_EQ(1, InterceptorFunctionCalled);
     55   EXPECT_EQ(0, isdigit('a'));
     56   EXPECT_EQ(2, InterceptorFunctionCalled);
     57 
     58   // Calling the REAL function should not affect the counter.
     59   InterceptorFunctionCalled = 0;
     60   EXPECT_NE(0, REAL(isdigit)('1'));
     61   EXPECT_EQ(0, REAL(isdigit)('a'));
     62   EXPECT_EQ(0, InterceptorFunctionCalled);
     63 }
     64 
     65 }  // namespace __interception
     66 
     67 #endif  // SANITIZER_LINUX
     68 #endif  // #if !SANITIZER_DEBUG
     69