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