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 <nlassert.h> 26 : 27 : #include <string.h> 28 : 29 : namespace chip { 30 : namespace FaultInjection { 31 : 32 : static nl::FaultInjection::Record sFaultRecordArray[kFault_NumItems]; 33 : static int32_t sFault_CHIPNotificationSize_Arguments[1]; 34 : static int32_t sFault_FuzzExchangeHeader_Arguments[1]; 35 : static class nl::FaultInjection::Manager sChipFaultInMgr; 36 : static const nl::FaultInjection::Name sManagerName = "chip"; 37 : static const nl::FaultInjection::Name sFaultNames[] = { 38 : "AllocExchangeContext", "DropIncomingUDPMsg", "DropOutgoingUDPMsg", "AllocBinding", "SendAlarm", 39 : "HandleAlarm", "FuzzExchangeHeaderTx", "RMPDoubleTx", "RMPSendError", "BDXBadBlockCounter", 40 : "BDXAllocTransfer", "CASEKeyConfirm", "SecMgrBusy", 41 : #if CONFIG_NETWORK_LAYER_BLE 42 : "CHIPOBLESend", 43 : #endif // CONFIG_NETWORK_LAYER_BLE 44 : }; 45 : 46 : /** 47 : * Get the singleton FaultInjection::Manager for Inet faults 48 : */ 49 7 : nl::FaultInjection::Manager & GetManager() 50 : { 51 7 : if (0 == sChipFaultInMgr.GetNumFaults()) 52 : { 53 2 : sChipFaultInMgr.Init(kFault_NumItems, sFaultRecordArray, sManagerName, sFaultNames); 54 : memset(&sFault_CHIPNotificationSize_Arguments, 0, sizeof(sFault_CHIPNotificationSize_Arguments)); 55 : memset(&sFault_FuzzExchangeHeader_Arguments, 0, sizeof(sFault_FuzzExchangeHeader_Arguments)); 56 2 : sFaultRecordArray[kFault_FuzzExchangeHeaderTx].mArguments = sFault_FuzzExchangeHeader_Arguments; 57 2 : sFaultRecordArray[kFault_FuzzExchangeHeaderTx].mLengthOfArguments = 58 : static_cast<uint8_t>(sizeof(sFault_FuzzExchangeHeader_Arguments) / sizeof(sFault_FuzzExchangeHeader_Arguments[0])); 59 : } 60 7 : return sChipFaultInMgr; 61 : } 62 : 63 : /** 64 : * Fuzz a byte of a CHIP Exchange Header 65 : * 66 : * @param[in] p Pointer to the encoded Exchange Header 67 : * @param[in] arg An index from 0 to (CHIP_FAULT_INJECTION_NUM_FUZZ_VALUES * 5 -1) 68 : * that specifies the byte to be corrupted and the value to use. 69 : */ 70 0 : DLL_EXPORT void FuzzExchangeHeader(uint8_t * p, int32_t arg) 71 : { 72 : // CHIP is little endian; this function alters the 73 : // least significant byte of the header fields. 74 0 : const uint8_t offsets[] = { 75 : 0, // flags and version 76 : 1, // MessageType 77 : 2, // ExchangeId 78 : 4, // ProfileId 79 : 8 // AckMessageCounter 80 : }; 81 0 : const uint8_t values[CHIP_FAULT_INJECTION_NUM_FUZZ_VALUES] = { 0x1, 0x2, 0xFF }; 82 0 : size_t offsetIndex = 0; 83 0 : size_t valueIndex = 0; 84 0 : size_t numOffsets = sizeof(offsets) / sizeof(offsets[0]); 85 0 : offsetIndex = static_cast<uint32_t>(arg) % (numOffsets); 86 0 : valueIndex = (static_cast<uint32_t>(arg) / numOffsets) % CHIP_FAULT_INJECTION_NUM_FUZZ_VALUES; 87 0 : p[offsetIndex] ^= values[valueIndex]; 88 0 : } 89 : 90 : } // namespace FaultInjection 91 : } // namespace chip