]> begriffs open source - cmsis-freertos/blob - Test/VeriFast/queue/prvCopyDataToQueue.c
Update README.md - branch main is now the base branch
[cmsis-freertos] / Test / VeriFast / queue / prvCopyDataToQueue.c
1 /*
2  * FreeRTOS V202111.00
3  * Copyright (C) Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
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.
21  *
22  * https://www.FreeRTOS.org
23  * https://github.com/FreeRTOS
24  *
25  */
26
27 #include "proof/queue.h"
28
29 static BaseType_t prvCopyDataToQueue( Queue_t * const pxQueue,
30                                       const void * pvItemToQueue,
31                                       const BaseType_t xPosition )
32
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));@*/
37
38 /*@ensures
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
42  *          ? (R == 0
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)))
46  *  ) &*&
47  *  chars(pvItemToQueue, M, x);@*/
48 {
49     BaseType_t xReturn = pdFALSE;
50     UBaseType_t uxMessagesWaiting;
51
52     /* This function is called from a critical section. */
53
54     uxMessagesWaiting = pxQueue->uxMessagesWaiting;
55
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 )
59     {
60         /* This case is unreachable for queues */
61         /*@assert false;@*/
62         #if ( configUSE_MUTEXES == 1 )
63             {
64                 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
65                 {
66                     /* The mutex is no longer being held. */
67                     xReturn = xTaskPriorityDisinherit( pxQueue->u.xSemaphore.xMutexHolder );
68                     pxQueue->u.xSemaphore.xMutexHolder = NULL;
69                 }
70                 else
71                 {
72                     mtCOVERAGE_TEST_MARKER();
73                 }
74             }
75         #endif /* configUSE_MUTEXES */
76     }
77     else if( xPosition == queueSEND_TO_BACK )
78     {
79         #ifdef VERIFAST /*< void cast of unused return value */
80
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);@*/
86
87             /*@assert
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);@*/
95         #else
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. */
100
101         if( pxQueue->pcWriteTo >= pxQueue->u.xQueue.pcTail ) /*lint !e946 MISRA exception justified as comparison of pointers is the cleanest solution. */
102         {
103             /*@div_leq(N, W+1, M);@*/ /* now we know W == N-1 so (W+1)%N == 0 */
104             pxQueue->pcWriteTo = pxQueue->pcHead;
105         }
106         else
107         {
108             /*@{
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));
113              * }@*/
114             mtCOVERAGE_TEST_MARKER();
115         }
116     }
117     else
118     {
119         #ifdef VERIFAST /*< void cast of unused return value */
120             /*@split_element(Storage, N, M, R);@*/
121
122             /*@assert
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);@*/
129         #else
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. */
131         #endif
132         pxQueue->u.xQueue.pcReadFrom -= pxQueue->uxItemSize;
133
134         if( pxQueue->u.xQueue.pcReadFrom < pxQueue->pcHead ) /*lint !e946 MISRA exception justified as comparison of pointers is the cleanest solution. */
135         {
136             pxQueue->u.xQueue.pcReadFrom = ( pxQueue->u.xQueue.pcTail - pxQueue->uxItemSize );
137             /*@{ div_leq(R-1, 0, M); leq_bound(R, 0); }@*/
138             /*@assert R == 0;@*/
139             /*@assert pxQueue->u.xQueue.pcReadFrom == Storage + (N-1) * M;@*/
140         }
141         else
142         {
143             /*@assert 0 < R;@*/
144             /*@assert pxQueue->u.xQueue.pcReadFrom == Storage + (R-1) * M;@*/
145             mtCOVERAGE_TEST_MARKER();
146         }
147
148         /*@
149          * if (R == 0)
150          * {
151          * mod_plus(N, (K+1), N); mod_same(N); mod_mod(K+1, N);
152          * assert W == ((N-1) + 1 + (K+1)) % N;
153          * }
154          * @*/
155         if( xPosition == queueOVERWRITE )
156         {
157             if( uxMessagesWaiting > ( UBaseType_t ) 0 )
158             {
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
162                  * correct. */
163                 --uxMessagesWaiting;
164             }
165             else
166             {
167                 mtCOVERAGE_TEST_MARKER();
168             }
169         }
170         else
171         {
172             mtCOVERAGE_TEST_MARKER();
173         }
174     }
175
176     pxQueue->uxMessagesWaiting = uxMessagesWaiting + ( UBaseType_t ) 1;
177
178     /*@
179      * if (xPosition == queueSEND_TO_BACK)
180      * {
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);
184      * }
185      * else if (xPosition == queueSEND_TO_FRONT)
186      * {
187      *  front_enq_lemma(K, R, contents, abs, x);
188      *  if (0 < R)
189      *  {
190      *      mod_lt(R, N);
191      *  }
192      * }
193      * @*/
194     return xReturn;
195 }