1 //===-- interception_linux_test.cpp ---------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is a part of ThreadSanitizer/AddressSanitizer runtime.
10 // Tests for interception_linux.h.
11 //
12 //===----------------------------------------------------------------------===//
13
14 // Do not declare isdigit in ctype.h.
15 #define __NO_CTYPE
16
17 #include "interception/interception.h"
18
19 #include "gtest/gtest.h"
20
21 // Too slow for debug build
22 #if !SANITIZER_DEBUG
23 #if SANITIZER_LINUX
24
25 static int InterceptorFunctionCalled;
26
27 DECLARE_REAL(int, isdigit, int);
28
INTERCEPTOR(int,isdigit,int d)29 INTERCEPTOR(int, isdigit, int d) {
30 ++InterceptorFunctionCalled;
31 return d >= '0' && d <= '9';
32 }
33
34 namespace __interception {
35
TEST(Interception,InterceptFunction)36 TEST(Interception, InterceptFunction) {
37 uptr malloc_address = 0;
38 EXPECT_TRUE(InterceptFunction("malloc", &malloc_address, 0, 0));
39 EXPECT_NE(0U, malloc_address);
40 EXPECT_FALSE(InterceptFunction("malloc", &malloc_address, 0, 1));
41
42 uptr dummy_address = 0;
43 EXPECT_FALSE(InterceptFunction("dummy_doesnt_exist__", &dummy_address, 0, 0));
44 EXPECT_EQ(0U, dummy_address);
45 }
46
TEST(Interception,Basic)47 TEST(Interception, Basic) {
48 EXPECT_TRUE(INTERCEPT_FUNCTION(isdigit));
49
50 // After interception, the counter should be incremented.
51 InterceptorFunctionCalled = 0;
52 EXPECT_NE(0, isdigit('1'));
53 EXPECT_EQ(1, InterceptorFunctionCalled);
54 EXPECT_EQ(0, isdigit('a'));
55 EXPECT_EQ(2, InterceptorFunctionCalled);
56
57 // Calling the REAL function should not affect the counter.
58 InterceptorFunctionCalled = 0;
59 EXPECT_NE(0, REAL(isdigit)('1'));
60 EXPECT_EQ(0, REAL(isdigit)('a'));
61 EXPECT_EQ(0, InterceptorFunctionCalled);
62 }
63
64 } // namespace __interception
65
66 #endif // SANITIZER_LINUX
67 #endif // #if !SANITIZER_DEBUG
68