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 #include "interception/interception.h"
15
16 #include "gtest/gtest.h"
17
18 // Too slow for debug build
19 #if !SANITIZER_DEBUG
20 #if SANITIZER_LINUX
21
22 static int InterceptorFunctionCalled;
23
24 DECLARE_REAL(int, isdigit, int);
25
INTERCEPTOR(int,isdigit,int d)26 INTERCEPTOR(int, isdigit, int d) {
27 ++InterceptorFunctionCalled;
28 return d >= '0' && d <= '9';
29 }
30
31 namespace __interception {
32
TEST(Interception,GetRealFunctionAddress)33 TEST(Interception, GetRealFunctionAddress) {
34 uptr expected_malloc_address = (uptr)(void*)&malloc;
35 uptr malloc_address = 0;
36 EXPECT_TRUE(GetRealFunctionAddress("malloc", &malloc_address, 0, 0));
37 EXPECT_EQ(expected_malloc_address, malloc_address);
38
39 uptr dummy_address = 0;
40 EXPECT_TRUE(
41 GetRealFunctionAddress("dummy_doesnt_exist__", &dummy_address, 0, 0));
42 EXPECT_EQ(0U, dummy_address);
43 }
44
TEST(Interception,Basic)45 TEST(Interception, Basic) {
46 ASSERT_TRUE(INTERCEPT_FUNCTION(isdigit));
47
48 // After interception, the counter should be incremented.
49 InterceptorFunctionCalled = 0;
50 EXPECT_NE(0, isdigit('1'));
51 EXPECT_EQ(1, InterceptorFunctionCalled);
52 EXPECT_EQ(0, isdigit('a'));
53 EXPECT_EQ(2, InterceptorFunctionCalled);
54
55 // Calling the REAL function should not affect the counter.
56 InterceptorFunctionCalled = 0;
57 EXPECT_NE(0, REAL(isdigit)('1'));
58 EXPECT_EQ(0, REAL(isdigit)('a'));
59 EXPECT_EQ(0, InterceptorFunctionCalled);
60 }
61
62 } // namespace __interception
63
64 #endif // SANITIZER_LINUX
65 #endif // #if !SANITIZER_DEBUG
66