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