Add coverity example (#870)

* Add coverity example

* Update for CI

* Fix for CI 2

* Update kernel_misra.config

* Rename coverity example to coverity

* Update FreeRTOSConfig.h for coverity project

* Update MISRA.md

* Move coverity config to coverity_misra.config

* Update coverity misra config

* Add README.md file

* Update FreeRTOSConfig.h for coverity

* Fix uncrustify and spell

* Update README.md for relative link path

Update README.md for relative link path

* Update README.md for relative link 2

* Update MISRA.md for relateive path

* Fix for format

* Update coverity_misra.config

* Update configuration folder

* Update README.md for link

* Code review suggestions

Signed-off-by: Gaurav Aggarwal <aggarg@amazon.com>

---------

Signed-off-by: Gaurav Aggarwal <aggarg@amazon.com>
Co-authored-by: Ubuntu <ubuntu@ip-172-31-34-245.ap-northeast-1.compute.internal>
Co-authored-by: Rahul Kar <118818625+kar-rahul-aws@users.noreply.github.com>
Co-authored-by: Soren Ptak <ptaksoren@gmail.com>
Co-authored-by: Gaurav-Aggarwal-AWS <33462878+aggarg@users.noreply.github.com>
Co-authored-by: Gaurav Aggarwal <aggarg@amazon.com>
diff --git a/.github/scripts/kernel_checker.py b/.github/scripts/kernel_checker.py
index 9251c86..3373cd5 100755
--- a/.github/scripts/kernel_checker.py
+++ b/.github/scripts/kernel_checker.py
@@ -62,7 +62,8 @@
     '.bat',
     '.sh',
     '.txt',
-    '.cmake'
+    '.cmake',
+    '.config'
 ]
 
 KERNEL_ASM_EXTENSIONS = [
diff --git a/MISRA.md b/MISRA.md
index 734a512..8d501e0 100644
--- a/MISRA.md
+++ b/MISRA.md
@@ -8,8 +8,8 @@
 deviates from some MISRA rules. The specific deviations, suppressed inline,
 are listed below.
 
-Additionally, [MISRA configuration](#misra-configuration) contains project
-wide deviations.
+Additionally, [MISRA configuration file](examples/coverity/coverity_misra.config)
+contains project wide deviations.
 
 ### Suppressed with Coverity Comments
 To find the violation references in the source files run grep on the source code
@@ -116,39 +116,3 @@
  - The Standard Library function snprintf is used in vTaskListTasks and
    vTaskGetRunTimeStatistics APIs, both of which are utility functions only and
    are not considered part of core kernel implementation.
-
-### MISRA configuration
-
-Copy below content to `misra.conf` to run Coverity on FreeRTOS-Kernel.
-
-```
-// MISRA C-2012 Rules
-{
-    version : "2.0",
-    standard : "c2012",
-    title: "Coverity MISRA Configuration",
-    deviations : [
-        // Disable the following rules.
-        {
-            deviation: "Directive 4.8",
-            reason: "HeapRegion_t and HeapStats_t are used only in heap files but declared in portable.h which is included in multiple source files. As a result, these definitions appear in multiple source files where they are not used."
-        },
-        {
-            deviation: "Directive 4.9",
-            reason: "FreeRTOS-Kernel is optimised to work on small micro-controllers. To achieve that, function-like macros are used."
-        },
-        {
-            deviation: "Rule 1.2",
-            reason: "The __attribute__ tags are used via macros which are defined in port files."
-        },
-        {
-            deviation: "Rule 3.1",
-            reason: "We post HTTP links in code comments which contain // inside comments blocks."
-        },
-        {
-            deviation: "Rule 8.7",
-            reason: "API functions are not used by the library outside of the files they are defined; however, they must be externally visible in order to be used by an application."
-        }
-    ]
-}
-```
diff --git a/examples/cmake_example/main.c b/examples/cmake_example/main.c
index bf1717e..4825f2d 100644
--- a/examples/cmake_example/main.c
+++ b/examples/cmake_example/main.c
@@ -33,17 +33,16 @@
  * https://freertos.org/FreeRTOS-quick-start-guide.html
  */
 
+/* FreeRTOS includes. */
 #include <FreeRTOS.h>
 #include <task.h>
 #include <queue.h>
 #include <timers.h>
 #include <semphr.h>
 
+/* Standard includes. */
 #include <stdio.h>
 
-static StaticTask_t exampleTaskTCB;
-static StackType_t exampleTaskStack[ configMINIMAL_STACK_SIZE ];
-
 void exampleTask( void * parameters )
 {
     /* Unused parameters. */
@@ -55,9 +54,13 @@
         vTaskDelay( 100 ); /* delay 100 ticks */
     }
 }
+/*-----------------------------------------------------------*/
 
 void main( void )
 {
+    static StaticTask_t exampleTaskTCB;
+    static StackType_t exampleTaskStack[ configMINIMAL_STACK_SIZE ];
+
     printf( "Example FreeRTOS Project\n" );
 
     xTaskCreateStatic( exampleTask,
@@ -65,8 +68,8 @@
                        configMINIMAL_STACK_SIZE,
                        NULL,
                        configMAX_PRIORITIES - 1,
-                       exampleTaskStack,
-                       &exampleTaskTCB );
+                       &( exampleTaskStack[ 0 ] ),
+                       &( exampleTaskTCB ) );
 
     /* Start the scheduler. */
     vTaskStartScheduler();
@@ -76,6 +79,7 @@
         /* Should not reach here. */
     }
 }
+/*-----------------------------------------------------------*/
 
 void vApplicationStackOverflowHook( TaskHandle_t xTask,
                                     char * pcTaskName )
@@ -85,3 +89,4 @@
     ( void ) xTask;
     ( void ) pcTaskName;
 }
+/*-----------------------------------------------------------*/
diff --git a/examples/coverity/CMakeLists.txt b/examples/coverity/CMakeLists.txt
new file mode 100644
index 0000000..b453865
--- /dev/null
+++ b/examples/coverity/CMakeLists.txt
@@ -0,0 +1,39 @@
+cmake_minimum_required(VERSION 3.15)
+
+project(coverity)
+
+set(FREERTOS_KERNEL_PATH "../../")
+FILE(GLOB FREERTOS_KERNEL_SOURCE ${FREERTOS_KERNEL_PATH}*.c)
+
+# Coverity incorrectly infers the type of pdTRUE and pdFALSE as boolean because
+# of their names. This generates multiple false positive warnings about type
+# mismatch. Replace pdTRUE with pdPASS and pdFALSE with pdFAIL to avoid these
+# false positive warnings. This workaround will not be needed after Coverity
+# fixes the issue of incorrectly inferring the type of pdTRUE and pdFALSE as
+# boolean.
+add_custom_target(fix_source ALL
+                  COMMAND sed -i -b -e 's/pdFALSE/pdFAIL/g' -e 's/pdTRUE/pdPASS/g' ${FREERTOS_KERNEL_SOURCE}
+                  DEPENDS ${FREERTOS_KERNEL_SOURCE})
+
+# Add the freertos_config for FreeRTOS-Kernel.
+add_library(freertos_config INTERFACE)
+
+target_include_directories(freertos_config
+                           INTERFACE
+                           ./)
+
+# Select the heap. Values between 1-5 will pick a heap.
+set(FREERTOS_HEAP "3" CACHE STRING "" FORCE)
+
+# Select the FreeRTOS port.
+set(FREERTOS_PORT "TEMPLATE" CACHE STRING "" FORCE)
+
+# Add the FreeRTOS-Kernel subdirectory.
+add_subdirectory(${FREERTOS_KERNEL_PATH} FreeRTOS-Kernel)
+
+add_executable(${PROJECT_NAME}
+               ../cmake_example/main.c)
+
+add_dependencies(${PROJECT_NAME} fix_source)
+
+target_link_libraries(${PROJECT_NAME} freertos_kernel freertos_config)
diff --git a/examples/coverity/FreeRTOSConfig.h b/examples/coverity/FreeRTOSConfig.h
new file mode 100644
index 0000000..4d95787
--- /dev/null
+++ b/examples/coverity/FreeRTOSConfig.h
@@ -0,0 +1,135 @@
+/*
+ * FreeRTOS Kernel <DEVELOPMENT BRANCH>
+ * Copyright (C) 2021 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
+ *
+ * SPDX-License-Identifier: MIT
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
+ * this software and associated documentation files (the "Software"), to deal in
+ * the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+ * the Software, and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+ * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+ * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+ * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * https://www.FreeRTOS.org
+ * https://github.com/FreeRTOS
+ *
+ */
+
+#ifndef FREERTOS_CONFIG_H
+#define FREERTOS_CONFIG_H
+
+/******************************************************************************/
+/* Hardware description related definitions. **********************************/
+/******************************************************************************/
+
+#define configCPU_CLOCK_HZ    ( ( unsigned long ) 20000000 )
+
+/******************************************************************************/
+/* Scheduling behaviour related definitions. **********************************/
+/******************************************************************************/
+
+#define configTICK_RATE_HZ                         ( 100U )
+#define configUSE_PREEMPTION                       1
+#define configUSE_TIME_SLICING                     1
+#define configUSE_PORT_OPTIMISED_TASK_SELECTION    0
+#define configUSE_TICKLESS_IDLE                    1
+#define configMAX_PRIORITIES                       5U
+#define configMINIMAL_STACK_SIZE                   128U
+#define configMAX_TASK_NAME_LEN                    4U
+#define configTICK_TYPE_WIDTH_IN_BITS              TICK_TYPE_WIDTH_64_BITS
+#define configIDLE_SHOULD_YIELD                    1
+#define configTASK_NOTIFICATION_ARRAY_ENTRIES      1U
+#define configQUEUE_REGISTRY_SIZE                  0U
+#define configENABLE_BACKWARD_COMPATIBILITY        1
+#define configNUM_THREAD_LOCAL_STORAGE_POINTERS    0
+#define configSTACK_DEPTH_TYPE                     size_t
+#define configMESSAGE_BUFFER_LENGTH_TYPE           size_t
+#define configUSE_NEWLIB_REENTRANT                 0
+
+/******************************************************************************/
+/* Software timer related definitions. ****************************************/
+/******************************************************************************/
+
+#define configUSE_TIMERS                1
+#define configTIMER_TASK_PRIORITY       ( configMAX_PRIORITIES - 1U )
+#define configTIMER_TASK_STACK_DEPTH    configMINIMAL_STACK_SIZE
+#define configTIMER_QUEUE_LENGTH        10U
+
+/******************************************************************************/
+/* Memory allocation related definitions. *************************************/
+/******************************************************************************/
+
+#define configSUPPORT_STATIC_ALLOCATION              1
+#define configSUPPORT_DYNAMIC_ALLOCATION             1
+#define configTOTAL_HEAP_SIZE                        4096U
+#define configAPPLICATION_ALLOCATED_HEAP             1
+#define configSTACK_ALLOCATION_FROM_SEPARATE_HEAP    0
+#define configUSE_MINI_LIST_ITEM                     0
+
+/******************************************************************************/
+/* Interrupt nesting behaviour configuration. *********************************/
+/******************************************************************************/
+
+#define configKERNEL_INTERRUPT_PRIORITY          0U
+#define configMAX_SYSCALL_INTERRUPT_PRIORITY     0U
+#define configMAX_API_CALL_INTERRUPT_PRIORITY    0U
+
+/******************************************************************************/
+/* Hook and callback function related definitions. ****************************/
+/******************************************************************************/
+
+#define configUSE_IDLE_HOOK                   0
+#define configUSE_TICK_HOOK                   0
+#define configUSE_MALLOC_FAILED_HOOK          0
+#define configUSE_DAEMON_TASK_STARTUP_HOOK    0
+#define configCHECK_FOR_STACK_OVERFLOW        0
+
+/******************************************************************************/
+/* Run time and task stats gathering related definitions. *********************/
+/******************************************************************************/
+
+#define configGENERATE_RUN_TIME_STATS           0
+#define configUSE_TRACE_FACILITY                0
+#define configUSE_STATS_FORMATTING_FUNCTIONS    0
+#define configKERNEL_PROVIDED_STATIC_MEMORY     1
+
+/******************************************************************************/
+/* Definitions that include or exclude functionality. *************************/
+/******************************************************************************/
+
+#define configUSE_TASK_NOTIFICATIONS           1
+#define configUSE_MUTEXES                      1
+#define configUSE_RECURSIVE_MUTEXES            1
+#define configUSE_COUNTING_SEMAPHORES          1
+#define configUSE_QUEUE_SETS                   1
+#define configUSE_APPLICATION_TASK_TAG         1
+#define INCLUDE_vTaskPrioritySet               1
+#define INCLUDE_uxTaskPriorityGet              1
+#define INCLUDE_vTaskDelete                    1
+#define INCLUDE_vTaskSuspend                   1
+#define INCLUDE_xResumeFromISR                 1
+#define INCLUDE_vTaskDelayUntil                1
+#define INCLUDE_vTaskDelay                     1
+#define INCLUDE_xTaskGetSchedulerState         1
+#define INCLUDE_xTaskGetCurrentTaskHandle      1
+#define INCLUDE_uxTaskGetStackHighWaterMark    1
+#define INCLUDE_xTaskGetIdleTaskHandle         1
+#define INCLUDE_eTaskGetState                  1
+#define INCLUDE_xEventGroupSetBitFromISR       1
+#define INCLUDE_xTimerPendFunctionCall         1
+#define INCLUDE_xTaskAbortDelay                1
+#define INCLUDE_xTaskGetHandle                 1
+#define INCLUDE_xTaskResumeFromISR             1
+
+#endif /* FREERTOS_CONFIG_H */
diff --git a/examples/coverity/README.md b/examples/coverity/README.md
new file mode 100644
index 0000000..0daed7b
--- /dev/null
+++ b/examples/coverity/README.md
@@ -0,0 +1,58 @@
+# MISRA Compliance for FreeRTOS-Kernel
+FreeRTOS-Kernel is MISRA C:2012 compliant. This directory contains a project to
+run [Synopsys Coverity](https://www.synopsys.com/software-integrity/security-testing/static-analysis-sast.html)
+for checking MISRA compliance.
+
+> **Note**
+Coverity version 2022.6.1 incorrectly infers the type of `pdTRUE` and `pdFALSE`
+as boolean because of their names, resulting in multiple false positive warnings
+about type mismatch. We replace `pdTRUE` with `pdPASS` and `pdFALSE` with
+`pdFAIL` to avoid these false positive warnings. This workaround will not be
+needed after Coverity fixes the issue of incorrectly inferring the type of
+`pdTRUE` and `pdFALSE` as boolean.
+
+Deviations from the MISRA C:2012 guidelines are documented in
+[MISRA.md](../../MISRA.md) and [coverity_misra.config](coverity_misra.config)
+files.
+
+## Getting Started
+### Prerequisites
+Coverity can be run on any platform mentioned [here](https://sig-docs.synopsys.com/polaris/topics/c_coverity-compatible-platforms.html).
+The following are the prerequisites to generate coverity report:
+
+1. CMake version > 3.13.0 (You can check whether you have this by typing `cmake --version`).
+2. GCC compiler.
+    - See download and installation instructions [here](https://gcc.gnu.org/install/).
+3. Clone the repo using the following command:
+    - `git clone https://github.com/FreeRTOS/FreeRTOS-Kernel.git ./FreeRTOS-Kernel`
+
+### Generating Report
+Go to the root directory of the FreeRTOS-Kernel repo and run the following
+commands in a terminal:
+1. Update the compiler configuration in Coverity:
+  ~~~
+  cov-configure --force --compiler cc --comptype gcc
+  ~~~
+2. Create the build files using CMake in a `build` directory:
+  ~~~
+  cmake -B build -S examples/coverity
+  ~~~
+3. Build the (pseudo) application:
+  ~~~
+  cd build/
+  cov-build --emit-complementary-info --dir cov-out make
+  ~~~
+4. Go to the Coverity output directory (`cov-out`) and begin Coverity static
+   analysis:
+  ~~~
+  cd cov-out/
+  cov-analyze --dir ./cov-out \
+    --coding-standard-config ../examples/coverity/coverity_misra.config \
+    --tu-pattern "file('.*/FreeRTOS/Source/[A-Za-z_]*\.c')
+  ~~~
+5. Generate the HTML report:
+  ~~~
+  cov-format-errors --dir ./cov-out --html-output html-output
+  ~~~
+
+HTML report should now be generated in a directory named `html-output`.
diff --git a/examples/coverity/coverity_misra.config b/examples/coverity/coverity_misra.config
new file mode 100644
index 0000000..101b200
--- /dev/null
+++ b/examples/coverity/coverity_misra.config
@@ -0,0 +1,97 @@
+// MISRA C-2012 Rules
+
+{
+    version : "2.0",
+    standard : "c2012",
+    title: "Coverity MISRA Configuration",
+    deviations : [
+        // Disable the following rules.
+        {
+            deviation: "Rule 3.1",
+            reason: "We post HTTP links in code comments which contain // inside comments blocks."
+        },
+        {
+            deviation: "Rule 14.4",
+            reason: "do while( 0 ) pattern is used in macros to prevent extra semi-colon."
+        },
+
+        // Disable the following advisory rules and directives.
+        {
+            deviation: "Directive 4.4",
+            reason: "Code snippet is used in comment to help explanation."
+        },
+        {
+            deviation: "Directive 4.5",
+            reason: "Allow names that MISRA considers ambiguous."
+        },
+        {
+            deviation: "Directive 4.6",
+            reason: "Allow port to use primitive type with typedefs."
+        },
+        {
+            deviation: "Directive 4.8",
+            reason: "HeapRegion_t and HeapStats_t are used only in heap files but declared in portable.h which is included in multiple source files. As a result, these definitions appear in multiple source files where they are not used."
+        },
+        {
+            deviation: "Directive 4.9",
+            reason: "FreeRTOS-Kernel is optimised to work on small micro-controllers. To achieve that, function-like macros are used."
+        },
+        {
+            deviation: "Rule 2.3",
+            reason: "FreeRTOS defines types which is used in application."
+        },
+        {
+            deviation: "Rule 2.4",
+            reason: "Allow to define unused tag."
+        },
+        {
+            deviation: "Rule 2.5",
+            reason: "Allow to define unused macro."
+        },
+        {
+            deviation: "Rule 5.9",
+            reason: "Allow to define identifier with the same name in structure and global variable."
+        },
+        {
+            deviation: "Rule 8.7",
+            reason: "API functions are not used by the library outside of the files they are defined; however, they must be externally visible in order to be used by an application."
+        },
+        {
+            deviation: "Rule 8.9",
+            reason: "Allow to object to be defined in wider scope for debug purpose."
+        },
+        {
+            deviation: "Rule 8.13",
+            reason: "Allow to not to use const-qualified type for callback function."
+        },
+        {
+            deviation: "Rule 11.4",
+            reason: "Allow to convert between a pointer to object and an interger type for stack alignment."
+        },
+        {
+            deviation: "Rule 15.4",
+            reason: "Allow to use multiple break statements in a loop."
+        },
+        {
+            deviation: "Rule 15.5",
+            reason: "Allow to use multiple points of exit."
+        },
+        {
+            deviation: "Rule 17.8",
+            reason: "Allow to update the parameters of a function."
+        },
+        {
+            deviation: "Rule 18.4",
+            reason: "Allow to use pointer arithmetic."
+        },
+        {
+            deviation: "Rule 19.2",
+            reason: "Allow to use union."
+        },
+        {
+            deviation: "Rule 20.5",
+            reason: "Allow to use #undef for MPU wrappers."
+        }
+    ]
+}
+