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.
22 * https://www.FreeRTOS.org
23 * https://github.com/FreeRTOS
27 #include "proof/queue.h"
29 static BaseType_t prvCopyDataToQueue( Queue_t * const pxQueue,
30 const void * pvItemToQueue,
31 const BaseType_t xPosition )
33 /*@requires queue(pxQueue, ?Storage, ?N, ?M, ?W, ?R, ?K, ?is_locked, ?abs) &*&
34 * (K < N || xPosition == queueOVERWRITE) &*&
35 * chars(pvItemToQueue, M, ?x) &*&
36 * (xPosition == queueSEND_TO_BACK || xPosition == queueSEND_TO_FRONT || (xPosition == queueOVERWRITE && N == 1));@*/
39 * (xPosition == queueSEND_TO_BACK
40 * ? queue(pxQueue, Storage, N, M, (W+1)%N, R, (K+1), is_locked, append(abs, singleton(x)))
41 * : (xPosition == queueSEND_TO_FRONT
43 * ? queue(pxQueue, Storage, N, M, W, (N-1), (K+1), is_locked, cons(x, abs))
44 * : queue(pxQueue, Storage, N, M, W, (R-1), (K+1), is_locked, cons(x, abs)))
45 * : xPosition == queueOVERWRITE &*& queue(pxQueue, Storage, N, M, W, R, 1, is_locked, singleton(x)))
47 * chars(pvItemToQueue, M, x);@*/
49 BaseType_t xReturn = pdFALSE;
50 UBaseType_t uxMessagesWaiting;
52 /* This function is called from a critical section. */
54 uxMessagesWaiting = pxQueue->uxMessagesWaiting;
56 /* The abstract list of list of chars of `Storage` is `contents` */
57 /*@assert buffer(Storage, N, M, ?contents);@*/
58 if( pxQueue->uxItemSize == ( UBaseType_t ) 0 )
60 /* This case is unreachable for queues */
62 #if ( configUSE_MUTEXES == 1 )
64 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
66 /* The mutex is no longer being held. */
67 xReturn = xTaskPriorityDisinherit( pxQueue->u.xSemaphore.xMutexHolder );
68 pxQueue->u.xSemaphore.xMutexHolder = NULL;
72 mtCOVERAGE_TEST_MARKER();
75 #endif /* configUSE_MUTEXES */
77 else if( xPosition == queueSEND_TO_BACK )
79 #ifdef VERIFAST /*< void cast of unused return value */
81 /* Now we focus the proof on the logical element of the buffer that
82 * will be updated using the following lemma to split the buffer into 3
83 * parts: a prefix, the element we want to update, and the suffix. This
84 * enables the subsequent memcpy to verify. */
85 /*@split_element(Storage, N, M, W);@*/
88 * buffer(Storage, W, M, ?prefix) &*&
89 * chars(Storage + W * M, M, _) &*&
90 * buffer(Storage + (W + 1) * M, (N-1-W), M, ?suffix);@*/
91 memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( size_t ) pxQueue->uxItemSize );
92 /* After the update we stitch the buffer back together */
93 /*@join_element(Storage, N, M, W);@*/
94 /*@combine_list_update(prefix, x, suffix, W, contents);@*/
96 ( 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. */
97 #endif /* ifdef VERIFAST */
98 /*@mul_mono_l(W, N-1, M);@*/
99 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. */
101 if( pxQueue->pcWriteTo >= pxQueue->u.xQueue.pcTail ) /*lint !e946 MISRA exception justified as comparison of pointers is the cleanest solution. */
103 /*@div_leq(N, W+1, M);@*/ /* now we know W == N-1 so (W+1)%N == 0 */
104 pxQueue->pcWriteTo = pxQueue->pcHead;
109 * div_lt(W+1, N, M); // now we know W+1 < N
110 * mod_lt(W+1, N); // so, W+1 == (W+1)%N
111 * note(pxQueue->pcWriteTo == Storage + ((W + 1) * M));
112 * note( Storage + ((W + 1) * M) == Storage + (((W + 1) % N) * M));
114 mtCOVERAGE_TEST_MARKER();
119 #ifdef VERIFAST /*< void cast of unused return value */
120 /*@split_element(Storage, N, M, R);@*/
123 * buffer(Storage, R, M, ?prefix) &*&
124 * chars(Storage + R * M, M, _) &*&
125 * buffer(Storage + (R + 1) * M, (N-1-R), M, ?suffix);@*/
126 memcpy( ( void * ) pxQueue->u.xQueue.pcReadFrom, pvItemToQueue, ( size_t ) pxQueue->uxItemSize );
127 /*@join_element(Storage, N, M, R);@*/
128 /*@combine_list_update(prefix, x, suffix, R, contents);@*/
130 ( 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. */
132 pxQueue->u.xQueue.pcReadFrom -= pxQueue->uxItemSize;
134 if( pxQueue->u.xQueue.pcReadFrom < pxQueue->pcHead ) /*lint !e946 MISRA exception justified as comparison of pointers is the cleanest solution. */
136 pxQueue->u.xQueue.pcReadFrom = ( pxQueue->u.xQueue.pcTail - pxQueue->uxItemSize );
137 /*@{ div_leq(R-1, 0, M); leq_bound(R, 0); }@*/
139 /*@assert pxQueue->u.xQueue.pcReadFrom == Storage + (N-1) * M;@*/
144 /*@assert pxQueue->u.xQueue.pcReadFrom == Storage + (R-1) * M;@*/
145 mtCOVERAGE_TEST_MARKER();
151 * mod_plus(N, (K+1), N); mod_same(N); mod_mod(K+1, N);
152 * assert W == ((N-1) + 1 + (K+1)) % N;
155 if( xPosition == queueOVERWRITE )
157 if( uxMessagesWaiting > ( UBaseType_t ) 0 )
159 /* An item is not being added but overwritten, so subtract
160 * one from the recorded number of items in the queue so when
161 * one is added again below the number of recorded items remains
167 mtCOVERAGE_TEST_MARKER();
172 mtCOVERAGE_TEST_MARKER();
176 pxQueue->uxMessagesWaiting = uxMessagesWaiting + ( UBaseType_t ) 1;
179 * if (xPosition == queueSEND_TO_BACK)
181 * enq_lemma(K, (R+1)%N, contents, abs, x);
182 * mod_plus_one(W, R + 1 + K, N);
183 * mod_plus_distr(R+1, K, N);
185 * else if (xPosition == queueSEND_TO_FRONT)
187 * front_enq_lemma(K, R, contents, abs, x);