[BUG] CBMC: operator precedence turns the pxUDPPayloadBuffer_to_NetworkBuffer harness assumption into assume(false)

Open Beginner friendly
#1,389 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
1/5
Estimated time
Under an hour
Newbie friendliness
95/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
c
Domain
testing

Research direction

Open test/cbmc/proofs/IPUtils/pxUDPPayloadBuffer_to_NetworkBuffer/pxUDPPayloadBuffer_to_NetworkBuffer_harness.c at line 88 and check the operator precedence in the IP type assumption. Add parentheses so the mask is evaluated before each equality, then run the reproduction commands in the issue and confirm CBMC reports SUCCESS, 94 properties, and coverage after the assumption as SATISFIED.

Written by the indexing model from the issue text.

Description

bug

Describe the bug

In the CBMC proof IPUtils/pxUDPPayloadBuffer_to_NetworkBuffer, the IP-type assumption on harness line 88 is always false, because == binds tighter than &. CBMC therefore never reaches the call to pxUDPPayloadBuffer_to_NetworkBuffer() on line 90, so none of the proof's 94 properties is checked on an actual execution of the function, yet make cbmc reports SUCCESS. Only the proof is affected, and it still passes after the fix below.

pxUDPPayloadBuffer_to_NetworkBuffer_harness.c, line 88:

__CPROVER_assume( ( ( *pucIPType ) & 0xf0 == ipTYPE_IPv4 ) || ( ( *pucIPType ) & 0xf0 == ipTYPE_IPv6 ) );

This is ( *pucIPType & ( 0xf0 == 0x40 ) ) || ( *pucIPType & ( 0xf0 == 0x60 ) ), that is *pucIPType & 0 || *pucIPType & 0, which is always 0. gcc -Wparentheses warns about both & operands on this line. The library code masks first (FreeRTOS_IP_Utils.c, line 748).

Target

Not target-specific: CBMC proof only.

Host

Ubuntu 22.04 (x86-64), CBMC 6.3.1 (the version pinned in .github/workflows/ci.yml), 32-bit glibc headers from libc6-dev-i386. main at b86ad086e9870099bb0ac18a75dcb7f6dd508cc7 (V4.4.2).

To Reproduce

The cbmc line uses the proof's own CBMCFLAGS.

git clone https://github.com/FreeRTOS/FreeRTOS-Plus-TCP.git
cd FreeRTOS-Plus-TCP
git checkout b86ad086e9870099bb0ac18a75dcb7f6dd508cc7
git submodule update --init --checkout test/FreeRTOS-Kernel
cd test/cbmc/proofs
python3 prepare.py
cd IPUtils/pxUDPPayloadBuffer_to_NetworkBuffer
make goto
make -s cbmc; grep '<cprover-status>' cbmc.xml; grep -c '<result ' cbmc.xml
cbmc --32 --object-bits 8 --unwind 1 --nondet-static --cover assume pxUDPPayloadBuffer_to_NetworkBuffer.goto | grep 'line 88'

Last lines of the output:

<cprover-status>SUCCESS</cprover-status>
94
[harness.coverage.11] file pxUDPPayloadBuffer_to_NetworkBuffer_harness.c line 88 function harness assert(false) before assume(((signed int)*pucIPType & (signed int)((unsigned int)0xF0 == 0x40u)) != 0 || ((signed int)*pucIPType & (signed int)((unsigned int)0xF0 == 0x60u)) != 0): SATISFIED
[harness.coverage.12] file pxUDPPayloadBuffer_to_NetworkBuffer_harness.c line 88 function harness assert(false) after assume(((signed int)*pucIPType & (signed int)((unsigned int)0xF0 == 0x40u)) != 0 || ((signed int)*pucIPType & (signed int)((unsigned int)0xF0 == 0x60u)) != 0): FAILED

before ...: SATISFIED together with after ...: FAILED means that CBMC reaches the assumption but the assumption never holds.

Expected behavior

The point after the assumption is reachable (after ...: SATISFIED), so pxUDPPayloadBuffer_to_NetworkBuffer() is actually checked. With the patch below, the same commands print SUCCESS, 94 and after ...: SATISFIED.

Additional context

Suggested fix (apply with git apply from the repository root):

diff --git a/test/cbmc/proofs/IPUtils/pxUDPPayloadBuffer_to_NetworkBuffer/pxUDPPayloadBuffer_to_NetworkBuffer_harness.c b/test/cbmc/proofs/IPUtils/pxUDPPayloadBuffer_to_NetworkBuffer/pxUDPPayloadBuffer_to_NetworkBuffer_harness.c
index 6d53978b..df6c879a 100644
--- a/test/cbmc/proofs/IPUtils/pxUDPPayloadBuffer_to_NetworkBuffer/pxUDPPayloadBuffer_to_NetworkBuffer_harness.c
+++ b/test/cbmc/proofs/IPUtils/pxUDPPayloadBuffer_to_NetworkBuffer/pxUDPPayloadBuffer_to_NetworkBuffer_harness.c
@@ -85,7 +85,7 @@ void harness()
         pvBuffer = &pxNetworkBuffer->pucEthernetBuffer[ ipconfigBUFFER_PADDING + sizeof( UDPPacket_t ) ];
     }

-    __CPROVER_assume( ( ( *pucIPType ) & 0xf0 == ipTYPE_IPv4 ) || ( ( *pucIPType ) & 0xf0 == ipTYPE_IPv6 ) );
+    __CPROVER_assume( ( ( ( *pucIPType ) & 0xf0 ) == ipTYPE_IPv4 ) || ( ( ( *pucIPType ) & 0xf0 ) == ipTYPE_IPv6 ) );

     ( void ) pxUDPPayloadBuffer_to_NetworkBuffer( pvBuffer );
 }
Dominant language
C
Stars
215
Forks
222
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from FreeRTOS/FreeRTOS-Plus-TCP

All issues in FreeRTOS/FreeRTOS-Plus-TCP

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.