Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2020 Project CHIP Authors
4 : * Copyright (c) 2016-2017 Nest Labs, Inc.
5 : *
6 : * Licensed under the Apache License, Version 2.0 (the "License");
7 : * you may not use this file except in compliance with the License.
8 : * You may obtain a copy of the License at
9 : *
10 : * http://www.apache.org/licenses/LICENSE-2.0
11 : *
12 : * Unless required by applicable law or agreed to in writing, software
13 : * distributed under the License is distributed on an "AS IS" BASIS,
14 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 : * See the License for the specific language governing permissions and
16 : * limitations under the License.
17 : */
18 :
19 : /**
20 : * @file
21 : * Implementation of the fault-injection utilities for CHIP.
22 : */
23 : #include "CHIPFaultInjection.h"
24 :
25 : #include <string.h>
26 :
27 : namespace chip {
28 : namespace FaultInjection {
29 :
30 : static nl::FaultInjection::Record sFaultRecordArray[kFault_NumItems];
31 : static int32_t sFault_CHIPNotificationSize_Arguments[1];
32 : static int32_t sFault_FuzzExchangeHeader_Arguments[1];
33 : static class nl::FaultInjection::Manager sChipFaultInMgr;
34 : static const nl::FaultInjection::Name sManagerName = "chip";
35 : static const nl::FaultInjection::Name sFaultNames[] = {
36 : "AllocExchangeContext",
37 : "DropIncomingUDPMsg",
38 : "DropOutgoingUDPMsg",
39 : "AllocBinding",
40 : "SendAlarm",
41 : "HandleAlarm",
42 : "FuzzExchangeHeaderTx",
43 : "RMPDoubleTx",
44 : "RMPSendError",
45 : "BDXBadBlockCounter",
46 : "BDXAllocTransfer",
47 : "SecMgrBusy",
48 : "IMInvoke_SeparateResponses",
49 : "IMInvoke_SeparateResponsesInvertResponseOrder",
50 : "IMInvoke_SkipSecondResponse",
51 : #if CONFIG_NETWORK_LAYER_BLE
52 : "CHIPOBLESend",
53 : #endif // CONFIG_NETWORK_LAYER_BLE
54 : "CASEServerBusy",
55 : };
56 :
57 : /**
58 : * Get the singleton FaultInjection::Manager for Inet faults
59 : */
60 114 : nl::FaultInjection::Manager & GetManager()
61 : {
62 114 : if (0 == sChipFaultInMgr.GetNumFaults())
63 : {
64 7 : sChipFaultInMgr.Init(kFault_NumItems, sFaultRecordArray, sManagerName, sFaultNames);
65 : memset(&sFault_CHIPNotificationSize_Arguments, 0, sizeof(sFault_CHIPNotificationSize_Arguments));
66 : memset(&sFault_FuzzExchangeHeader_Arguments, 0, sizeof(sFault_FuzzExchangeHeader_Arguments));
67 7 : sFaultRecordArray[kFault_FuzzExchangeHeaderTx].mArguments = sFault_FuzzExchangeHeader_Arguments;
68 7 : sFaultRecordArray[kFault_FuzzExchangeHeaderTx].mLengthOfArguments =
69 : static_cast<uint8_t>(sizeof(sFault_FuzzExchangeHeader_Arguments) / sizeof(sFault_FuzzExchangeHeader_Arguments[0]));
70 : }
71 114 : return sChipFaultInMgr;
72 : }
73 :
74 : /**
75 : * Fuzz a byte of a CHIP Exchange Header
76 : *
77 : * @param[in] p Pointer to the encoded Exchange Header
78 : * @param[in] arg An index from 0 to (CHIP_FAULT_INJECTION_NUM_FUZZ_VALUES * 5 -1)
79 : * that specifies the byte to be corrupted and the value to use.
80 : */
81 0 : DLL_EXPORT void FuzzExchangeHeader(uint8_t * p, int32_t arg)
82 : {
83 : // CHIP is little endian; this function alters the
84 : // least significant byte of the header fields.
85 0 : const uint8_t offsets[] = {
86 : 0, // flags and version
87 : 1, // MessageType
88 : 2, // ExchangeId
89 : 4, // ProfileId
90 : 8 // AckMessageCounter
91 : };
92 0 : const uint8_t values[CHIP_FAULT_INJECTION_NUM_FUZZ_VALUES] = { 0x1, 0x2, 0xFF };
93 0 : size_t offsetIndex = 0;
94 0 : size_t valueIndex = 0;
95 0 : size_t numOffsets = sizeof(offsets) / sizeof(offsets[0]);
96 0 : offsetIndex = static_cast<uint32_t>(arg) % (numOffsets);
97 0 : valueIndex = (static_cast<uint32_t>(arg) / numOffsets) % CHIP_FAULT_INJECTION_NUM_FUZZ_VALUES;
98 0 : p[offsetIndex] ^= values[valueIndex];
99 0 : }
100 :
101 : } // namespace FaultInjection
102 : } // namespace chip
|