1 //===-- interception_linux_test.cc ----------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of ThreadSanitizer/AddressSanitizer runtime.
11 // Tests for interception_linux.h.
13 //===----------------------------------------------------------------------===//
15 // Do not declare isdigit in ctype.h.
18 #include "interception/interception.h"
20 #include "gtest/gtest.h"
22 // Too slow for debug build
26 static int InterceptorFunctionCalled
;
28 DECLARE_REAL(int, isdigit
, int);
30 INTERCEPTOR(int, isdigit
, int d
) {
31 ++InterceptorFunctionCalled
;
32 return d
>= '0' && d
<= '9';
35 namespace __interception
{
37 TEST(Interception
, GetRealFunctionAddress
) {
38 uptr malloc_address
= 0;
39 EXPECT_TRUE(GetRealFunctionAddress("malloc", &malloc_address
, 0, 0));
40 EXPECT_NE(0U, malloc_address
);
42 uptr dummy_address
= 0;
44 GetRealFunctionAddress("dummy_doesnt_exist__", &dummy_address
, 0, 0));
45 EXPECT_EQ(0U, dummy_address
);
48 TEST(Interception
, Basic
) {
49 ASSERT_TRUE(INTERCEPT_FUNCTION(isdigit
));
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
);
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
);
65 } // namespace __interception
67 #endif // SANITIZER_LINUX
68 #endif // #if !SANITIZER_DEBUG