2 * FreeRTOS VeriFast Proofs
3 * Copyright (C) Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 #include "proof/queue.h"
25 static BaseType_t prvCopyDataToQueue( Queue_t * const pxQueue,
26 const void * pvItemToQueue,
27 const BaseType_t xPosition )
28 /*@requires queue(pxQueue, ?Storage, ?N, ?M, ?W, ?R, ?K, ?is_locked, ?abs) &*&
29 (K < N || xPosition == queueOVERWRITE) &*&
30 chars(pvItemToQueue, M, ?x) &*&
31 (xPosition == queueSEND_TO_BACK || xPosition == queueSEND_TO_FRONT || (xPosition == queueOVERWRITE && N == 1));@*/
33 (xPosition == queueSEND_TO_BACK
34 ? queue(pxQueue, Storage, N, M, (W+1)%N, R, (K+1), is_locked, append(abs, singleton(x)))
35 : (xPosition == queueSEND_TO_FRONT
37 ? queue(pxQueue, Storage, N, M, W, (N-1), (K+1), is_locked, cons(x, abs))
38 : queue(pxQueue, Storage, N, M, W, (R-1), (K+1), is_locked, cons(x, abs)))
39 : xPosition == queueOVERWRITE &*& queue(pxQueue, Storage, N, M, W, R, 1, is_locked, singleton(x)))
41 chars(pvItemToQueue, M, x);@*/
43 BaseType_t xReturn = pdFALSE;
44 UBaseType_t uxMessagesWaiting;
46 /* This function is called from a critical section. */
48 uxMessagesWaiting = pxQueue->uxMessagesWaiting;
50 /* The abstract list of list of chars of `Storage` is `contents` */
51 /*@assert buffer(Storage, N, M, ?contents);@*/
52 if( pxQueue->uxItemSize == ( UBaseType_t ) 0 )
54 /* This case is unreachable for queues */
56 #if ( configUSE_MUTEXES == 1 )
58 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
60 /* The mutex is no longer being held. */
61 xReturn = xTaskPriorityDisinherit( pxQueue->u.xSemaphore.xMutexHolder );
62 pxQueue->u.xSemaphore.xMutexHolder = NULL;
66 mtCOVERAGE_TEST_MARKER();
69 #endif /* configUSE_MUTEXES */
71 else if( xPosition == queueSEND_TO_BACK )
73 #ifdef VERIFAST /*< void cast of unused return value */
74 /* Now we focus the proof on the logical element of the buffer that
75 * will be updated using the following lemma to split the buffer into 3
76 * parts: a prefix, the element we want to update, and the suffix. This
77 * enables the subsequent memcpy to verify. */
78 /*@split_element(Storage, N, M, W);@*/
80 buffer(Storage, W, M, ?prefix) &*&
81 chars(Storage + W * M, M, _) &*&
82 buffer(Storage + (W + 1) * M, (N-1-W), M, ?suffix);@*/
83 memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( size_t ) pxQueue->uxItemSize );
84 /* After the update we stitch the buffer back together */
85 /*@join_element(Storage, N, M, W);@*/
86 /*@combine_list_update(prefix, x, suffix, W, contents);@*/
88 ( void ) memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( size_t ) pxQueue->uxItemSize ); /*lint !e961 !e418 !e9087 MISRA exception as the casts are only redundant for some ports, plus previous logic ensures a null pointer can only be passed to memcpy() if the copy size is 0. Cast to void required by function signature and safe as no alignment requirement and copy length specified in bytes. */
90 /*@mul_mono_l(W, N-1, M);@*/
91 pxQueue->pcWriteTo += pxQueue->uxItemSize; /*lint !e9016 Pointer arithmetic on char types ok, especially in this use case where it is the clearest way of conveying intent. */
93 if( pxQueue->pcWriteTo >= pxQueue->u.xQueue.pcTail ) /*lint !e946 MISRA exception justified as comparison of pointers is the cleanest solution. */
95 /*@div_leq(N, W+1, M);@*/ /* now we know W == N-1 so (W+1)%N == 0 */
96 pxQueue->pcWriteTo = pxQueue->pcHead;
101 div_lt(W+1, N, M); // now we know W+1 < N
102 mod_lt(W+1, N); // so, W+1 == (W+1)%N
103 note(pxQueue->pcWriteTo == Storage + ((W + 1) * M));
104 note( Storage + ((W + 1) * M) == Storage + (((W + 1) % N) * M));
106 mtCOVERAGE_TEST_MARKER();
111 #ifdef VERIFAST /*< void cast of unused return value */
112 /*@split_element(Storage, N, M, R);@*/
114 buffer(Storage, R, M, ?prefix) &*&
115 chars(Storage + R * M, M, _) &*&
116 buffer(Storage + (R + 1) * M, (N-1-R), M, ?suffix);@*/
117 memcpy( ( void * ) pxQueue->u.xQueue.pcReadFrom, pvItemToQueue, ( size_t ) pxQueue->uxItemSize );
118 /*@join_element(Storage, N, M, R);@*/
119 /*@combine_list_update(prefix, x, suffix, R, contents);@*/
121 ( void ) memcpy( ( void * ) pxQueue->u.xQueue.pcReadFrom, pvItemToQueue, ( size_t ) pxQueue->uxItemSize ); /*lint !e961 !e9087 !e418 MISRA exception as the casts are only redundant for some ports. Cast to void required by function signature and safe as no alignment requirement and copy length specified in bytes. Assert checks null pointer only used when length is 0. */
123 pxQueue->u.xQueue.pcReadFrom -= pxQueue->uxItemSize;
125 if( pxQueue->u.xQueue.pcReadFrom < pxQueue->pcHead ) /*lint !e946 MISRA exception justified as comparison of pointers is the cleanest solution. */
127 pxQueue->u.xQueue.pcReadFrom = ( pxQueue->u.xQueue.pcTail - pxQueue->uxItemSize );
128 /*@{ div_leq(R-1, 0, M); leq_bound(R, 0); }@*/
130 /*@assert pxQueue->u.xQueue.pcReadFrom == Storage + (N-1) * M;@*/
135 /*@assert pxQueue->u.xQueue.pcReadFrom == Storage + (R-1) * M;@*/
136 mtCOVERAGE_TEST_MARKER();
142 mod_plus(N, (K+1), N); mod_same(N); mod_mod(K+1, N);
143 assert W == ((N-1) + 1 + (K+1)) % N;
146 if( xPosition == queueOVERWRITE )
148 if( uxMessagesWaiting > ( UBaseType_t ) 0 )
150 /* An item is not being added but overwritten, so subtract
151 * one from the recorded number of items in the queue so when
152 * one is added again below the number of recorded items remains
158 mtCOVERAGE_TEST_MARKER();
163 mtCOVERAGE_TEST_MARKER();
167 pxQueue->uxMessagesWaiting = uxMessagesWaiting + ( UBaseType_t ) 1;
170 if (xPosition == queueSEND_TO_BACK)
172 enq_lemma(K, (R+1)%N, contents, abs, x);
173 mod_plus_one(W, R + 1 + K, N);
174 mod_plus_distr(R+1, K, N);
176 else if (xPosition == queueSEND_TO_FRONT)
178 front_enq_lemma(K, R, contents, abs, x);