From 1aa00c95ee98318c91cc0b61adb8b7379df9973e Mon Sep 17 00:00:00 2001 From: rbiasini Date: Mon, 20 May 2019 22:13:45 -0700 Subject: [PATCH] Misra c2012 (#192) * Started making panda misra compliant: this fixes some of 12.1 warnings * Also added a regression test for strict compiler warning --- .circleci/config.yml | 33 +++++++++++++++++++++++++++++++++ board/Makefile.strict | 8 ++++++++ board/drivers/can.h | 6 ++++-- board/main.c | 16 ++++++++-------- tests/build_strict/Dockerfile | 9 +++++++++ tests/misra/Dockerfile | 6 ++++++ tests/misra/test_misra.sh | 8 ++++++++ 7 files changed, 76 insertions(+), 10 deletions(-) create mode 100644 board/Makefile.strict create mode 100644 tests/build_strict/Dockerfile create mode 100644 tests/misra/Dockerfile create mode 100755 tests/misra/test_misra.sh diff --git a/.circleci/config.yml b/.circleci/config.yml index 6b98610c507527..6ea54fdd07dcf0 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -12,6 +12,37 @@ jobs: name: Run safety test command: | docker run panda_safety /bin/bash -c "cd /panda/tests/safety; ./test.sh" + + misra-c2012: + machine: + docker_layer_caching: true + steps: + - checkout + - run: + name: Build image + command: "docker build -t panda_misra -f tests/misra/Dockerfile ." + - run: + name: Run Misra C 2012 test + command: | + mkdir /tmp/misra + docker run -v /tmp/misra:/tmp/misra panda_misra /bin/bash -c "cd /panda/tests/misra; ./test_misra.sh" + - store_artifacts: + name: Store misra test output + path: /tmp/misra/output.txt + + strict-compiler: + machine: + docker_layer_caching: true + steps: + - checkout + - run: + name: Build image + command: "docker build -t panda_strict_compiler -f tests/build_strict/Dockerfile ." + - run: + name: Build Panda with strict compiler rules + command: | + docker run panda_strict_compiler /bin/bash -c "cd /panda/board; make -f Makefile.strict clean; make -f Makefile.strict bin" + build: machine: docker_layer_caching: true @@ -54,4 +85,6 @@ workflows: main: jobs: - safety + - misra-c2012 + - strict-compiler - build diff --git a/board/Makefile.strict b/board/Makefile.strict new file mode 100644 index 00000000000000..b2ff83d3091cd0 --- /dev/null +++ b/board/Makefile.strict @@ -0,0 +1,8 @@ +PROJ_NAME = panda +CFLAGS = -g -Wall -Wextra -pedantic -Wstrict-prototypes + +CFLAGS += -mlittle-endian -mthumb -mcpu=cortex-m4 +CFLAGS += -mhard-float -DSTM32F4 -DSTM32F413xx -mfpu=fpv4-sp-d16 -fsingle-precision-constant +STARTUP_FILE = startup_stm32f413xx + +include build.mk diff --git a/board/drivers/can.h b/board/drivers/can.h index aaf3ac4e34cea7..e993bc599b0e13 100644 --- a/board/drivers/can.h +++ b/board/drivers/can.h @@ -504,15 +504,17 @@ void can_send(CAN_FIFOMailBox_TypeDef *to_push, uint8_t bus_number) { // add CAN packet to send queue // bus number isn't passed through to_push->RDTR &= 0xF; + #ifdef PANDA if (bus_number == 3 && can_num_lookup[3] == 0xFF) { - #ifdef PANDA // TODO: why uint8 bro? only int8? bitbang_gmlan(to_push); - #endif } else { + #endif can_push(can_queues[bus_number], to_push); process_can(CAN_NUM_FROM_BUS_NUM(bus_number)); + #ifdef PANDA } + #endif } } } diff --git a/board/main.c b/board/main.c index bdfdb9481752a1..820f96447704cf 100644 --- a/board/main.c +++ b/board/main.c @@ -282,7 +282,7 @@ int usb_cb_control_msg(USB_Setup_TypeDef *setup, uint8_t *resp, int hardwired) { // this is the only way to leave silent mode // and it's blocked over WiFi // Allow ELM security mode to be set over wifi. - if (hardwired || setup->b.wValue.w == SAFETY_NOOUTPUT || setup->b.wValue.w == SAFETY_ELM327) { + if (hardwired || (setup->b.wValue.w == SAFETY_NOOUTPUT) || (setup->b.wValue.w == SAFETY_ELM327)) { safety_set_mode(setup->b.wValue.w, (int16_t)setup->b.wIndex.w); switch (setup->b.wValue.w) { case SAFETY_NOOUTPUT: @@ -308,10 +308,10 @@ int usb_cb_control_msg(USB_Setup_TypeDef *setup, uint8_t *resp, int hardwired) { case 0xdd: // wValue = Can Bus Num to forward from // wIndex = Can Bus Num to forward to - if (setup->b.wValue.w < BUS_MAX && setup->b.wIndex.w < BUS_MAX && - setup->b.wValue.w != setup->b.wIndex.w) { // set forwarding + if ((setup->b.wValue.w < BUS_MAX) && (setup->b.wIndex.w < BUS_MAX) && + (setup->b.wValue.w != setup->b.wIndex.w)) { // set forwarding can_set_forwarding(setup->b.wValue.w, setup->b.wIndex.w & CAN_BUS_NUM_MASK); - } else if(setup->b.wValue.w < BUS_MAX && setup->b.wIndex.w == 0xFF){ //Clear Forwarding + } else if((setup->b.wValue.w < BUS_MAX) && (setup->b.wIndex.w == 0xFF)){ //Clear Forwarding can_set_forwarding(setup->b.wValue.w, -1); } break; @@ -502,7 +502,7 @@ void __initialize_hardware_early() { void __attribute__ ((noinline)) enable_fpu() { // enable the FPU - SCB->CPACR |= ((3UL << 10*2) | (3UL << 11*2)); + SCB->CPACR |= ((3UL << (10 * 2)) | (3UL << (11 * 2))); } int main() { @@ -520,7 +520,7 @@ int main() { // detect the revision and init the GPIOs puts("config:\n"); #ifdef PANDA - puts(revision == PANDA_REV_C ? " panda rev c\n" : " panda rev a or b\n"); + puts((revision == PANDA_REV_C) ? " panda rev c\n" : " panda rev a or b\n"); #else puts(" legacy\n"); #endif @@ -641,7 +641,7 @@ int main() { // been CLICKS clicks since we switched to CDP if ((cnt-marker) >= CLICKS) { // measure current draw, if positive and no enumeration, switch to DCP - if (!is_enumerated && current < CURRENT_THRESHOLD) { + if (!is_enumerated && (current < CURRENT_THRESHOLD)) { puts("USBP: no enumeration with current draw, switching to DCP mode\n"); set_usb_power_mode(USB_POWER_DCP); marker = cnt; @@ -693,7 +693,7 @@ int main() { for (int div_mode_loop = 0; div_mode_loop < div_mode; div_mode_loop++) { for (int fade = 0; fade < 1024; fade += 8) { - for (int i = 0; i < 128/div_mode; i++) { + for (int i = 0; i < (128/div_mode); i++) { set_led(LED_RED, 0); if (fade < 512) { delay(512-fade); } else { delay(fade-512); } set_led(LED_RED, 1); diff --git a/tests/build_strict/Dockerfile b/tests/build_strict/Dockerfile new file mode 100644 index 00000000000000..b1c75c0258ef64 --- /dev/null +++ b/tests/build_strict/Dockerfile @@ -0,0 +1,9 @@ +FROM ubuntu:16.04 + +RUN apt-get update && apt-get install -y gcc-arm-none-eabi libnewlib-arm-none-eabi python python-pip gcc g++ + +RUN pip install pycrypto==2.6.1 + +COPY . /panda + +WORKDIR /panda diff --git a/tests/misra/Dockerfile b/tests/misra/Dockerfile new file mode 100644 index 00000000000000..e63cc7e9e151ed --- /dev/null +++ b/tests/misra/Dockerfile @@ -0,0 +1,6 @@ +FROM ubuntu:16.04 + +RUN apt-get update && apt-get install -y make python python-pip git +COPY tests/safety/requirements.txt /panda/tests/safety/requirements.txt +RUN pip install -r /panda/tests/safety/requirements.txt +COPY . /panda diff --git a/tests/misra/test_misra.sh b/tests/misra/test_misra.sh new file mode 100755 index 00000000000000..5d2650dc003662 --- /dev/null +++ b/tests/misra/test_misra.sh @@ -0,0 +1,8 @@ +#!/bin/bash -e +git clone https://github.com/danmar/cppcheck.git || true +cd cppcheck +git checkout 29e5992e51ecf1ddba469c73a0eed0b28b131de5 +make -j4 +cd ../../../ +tests/misra/cppcheck/cppcheck --dump board/main.c +python tests/misra/cppcheck/addons/misra.py board/main.c.dump 2>/tmp/misra/output.txt || true