]> begriffs open source - cmsis-freertos/blob - Test/VeriFast/queue/prvCopyDataToQueue.c
Updated pack to FreeRTOS 10.4.1
[cmsis-freertos] / Test / VeriFast / queue / prvCopyDataToQueue.c
1 /*
2  * FreeRTOS VeriFast Proofs
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
23 #include "proof/queue.h"
24
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));@*/
32 /*@ensures
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
36             ? (R == 0
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)))
40     ) &*&
41     chars(pvItemToQueue, M, x);@*/
42 {
43     BaseType_t xReturn = pdFALSE;
44     UBaseType_t uxMessagesWaiting;
45
46     /* This function is called from a critical section. */
47
48     uxMessagesWaiting = pxQueue->uxMessagesWaiting;
49
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 )
53     {
54         /* This case is unreachable for queues */
55         /*@assert false;@*/
56         #if ( configUSE_MUTEXES == 1 )
57             {
58                 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
59                 {
60                     /* The mutex is no longer being held. */
61                     xReturn = xTaskPriorityDisinherit( pxQueue->u.xSemaphore.xMutexHolder );
62                     pxQueue->u.xSemaphore.xMutexHolder = NULL;
63                 }
64                 else
65                 {
66                     mtCOVERAGE_TEST_MARKER();
67                 }
68             }
69         #endif /* configUSE_MUTEXES */
70     }
71     else if( xPosition == queueSEND_TO_BACK )
72     {
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);@*/
79         /*@assert
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);@*/
87 #else
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. */
89 #endif
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. */
92
93         if( pxQueue->pcWriteTo >= pxQueue->u.xQueue.pcTail )                                             /*lint !e946 MISRA exception justified as comparison of pointers is the cleanest solution. */
94         {
95             /*@div_leq(N, W+1, M);@*/ /* now we know W == N-1 so (W+1)%N == 0 */
96             pxQueue->pcWriteTo = pxQueue->pcHead;
97         }
98         else
99         {
100             /*@{
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));
105             }@*/
106             mtCOVERAGE_TEST_MARKER();
107         }
108     }
109     else
110     {
111 #ifdef VERIFAST /*< void cast of unused return value */
112         /*@split_element(Storage, N, M, R);@*/
113         /*@assert
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);@*/
120 #else
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. */
122 #endif
123         pxQueue->u.xQueue.pcReadFrom -= pxQueue->uxItemSize;
124
125         if( pxQueue->u.xQueue.pcReadFrom < pxQueue->pcHead ) /*lint !e946 MISRA exception justified as comparison of pointers is the cleanest solution. */
126         {
127             pxQueue->u.xQueue.pcReadFrom = ( pxQueue->u.xQueue.pcTail - pxQueue->uxItemSize );
128             /*@{ div_leq(R-1, 0, M); leq_bound(R, 0); }@*/
129             /*@assert R == 0;@*/
130             /*@assert pxQueue->u.xQueue.pcReadFrom == Storage + (N-1) * M;@*/
131         }
132         else
133         {
134             /*@assert 0 < R;@*/
135             /*@assert pxQueue->u.xQueue.pcReadFrom == Storage + (R-1) * M;@*/
136             mtCOVERAGE_TEST_MARKER();
137         }
138
139         /*@
140         if (R == 0)
141         {
142            mod_plus(N, (K+1), N); mod_same(N); mod_mod(K+1, N);
143            assert W == ((N-1) + 1 + (K+1)) % N;
144         }
145         @*/
146         if( xPosition == queueOVERWRITE )
147         {
148             if( uxMessagesWaiting > ( UBaseType_t ) 0 )
149             {
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
153                  * correct. */
154                 --uxMessagesWaiting;
155             }
156             else
157             {
158                 mtCOVERAGE_TEST_MARKER();
159             }
160         }
161         else
162         {
163             mtCOVERAGE_TEST_MARKER();
164         }
165     }
166
167     pxQueue->uxMessagesWaiting = uxMessagesWaiting + ( UBaseType_t ) 1;
168
169     /*@
170     if (xPosition == queueSEND_TO_BACK)
171     {
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);
175     }
176     else if (xPosition == queueSEND_TO_FRONT)
177     {
178         front_enq_lemma(K, R, contents, abs, x);
179         if (0 < R)
180         {
181             mod_lt(R, N);
182         }
183     }
184     @*/
185     return xReturn;
186 }