summaryrefslogtreecommitdiff
path: root/FreeRTOS/Test/VeriFast/queue/prvCopyDataToQueue.c
blob: 136d7b20c1dac6b394683f995ddfb82995ce6adc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
 * FreeRTOS V202111.00
 * Copyright (C) Amazon.com, Inc. or its affiliates.  All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * https://www.FreeRTOS.org
 * https://github.com/FreeRTOS
 *
 */

#include "proof/queue.h"

static BaseType_t prvCopyDataToQueue( Queue_t * const pxQueue,
                                      const void * pvItemToQueue,
                                      const BaseType_t xPosition )

/*@requires queue(pxQueue, ?Storage, ?N, ?M, ?W, ?R, ?K, ?is_locked, ?abs) &*&
 *  (K < N || xPosition == queueOVERWRITE) &*&
 *  chars(pvItemToQueue, M, ?x) &*&
 *  (xPosition == queueSEND_TO_BACK || xPosition == queueSEND_TO_FRONT || (xPosition == queueOVERWRITE && N == 1));@*/

/*@ensures
 *  (xPosition == queueSEND_TO_BACK
 *      ? queue(pxQueue, Storage, N, M, (W+1)%N, R, (K+1), is_locked, append(abs, singleton(x)))
 *      : (xPosition == queueSEND_TO_FRONT
 *          ? (R == 0
 *              ? queue(pxQueue, Storage, N, M, W, (N-1), (K+1), is_locked, cons(x, abs))
 *              : queue(pxQueue, Storage, N, M, W, (R-1), (K+1), is_locked, cons(x, abs)))
 *          : xPosition == queueOVERWRITE &*& queue(pxQueue, Storage, N, M, W, R, 1, is_locked, singleton(x)))
 *  ) &*&
 *  chars(pvItemToQueue, M, x);@*/
{
    BaseType_t xReturn = pdFALSE;
    UBaseType_t uxMessagesWaiting;

    /* This function is called from a critical section. */

    uxMessagesWaiting = pxQueue->uxMessagesWaiting;

    /* The abstract list of list of chars of `Storage` is `contents` */
    /*@assert buffer(Storage, N, M, ?contents);@*/
    if( pxQueue->uxItemSize == ( UBaseType_t ) 0 )
    {
        /* This case is unreachable for queues */
        /*@assert false;@*/
        #if ( configUSE_MUTEXES == 1 )
            {
                if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
                {
                    /* The mutex is no longer being held. */
                    xReturn = xTaskPriorityDisinherit( pxQueue->u.xSemaphore.xMutexHolder );
                    pxQueue->u.xSemaphore.xMutexHolder = NULL;
                }
                else
                {
                    mtCOVERAGE_TEST_MARKER();
                }
            }
        #endif /* configUSE_MUTEXES */
    }
    else if( xPosition == queueSEND_TO_BACK )
    {
        #ifdef VERIFAST /*< void cast of unused return value */

            /* Now we focus the proof on the logical element of the buffer that
             * will be updated using the following lemma to split the buffer into 3
             * parts: a prefix, the element we want to update, and the suffix. This
             * enables the subsequent memcpy to verify. */
            /*@split_element(Storage, N, M, W);@*/

            /*@assert
             *  buffer(Storage, W, M, ?prefix) &*&
             *  chars(Storage + W * M, M, _) &*&
             *  buffer(Storage + (W + 1) * M, (N-1-W), M, ?suffix);@*/
            memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( size_t ) pxQueue->uxItemSize );
            /* After the update we stitch the buffer back together */
            /*@join_element(Storage, N, M, W);@*/
            /*@combine_list_update(prefix, x, suffix, W, contents);@*/
        #else
            ( 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. */
        #endif /* ifdef VERIFAST */
        /*@mul_mono_l(W, N-1, M);@*/
        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. */

        if( pxQueue->pcWriteTo >= pxQueue->u.xQueue.pcTail ) /*lint !e946 MISRA exception justified as comparison of pointers is the cleanest solution. */
        {
            /*@div_leq(N, W+1, M);@*/ /* now we know W == N-1 so (W+1)%N == 0 */
            pxQueue->pcWriteTo = pxQueue->pcHead;
        }
        else
        {
            /*@{
             *  div_lt(W+1, N, M); // now we know W+1 < N
             *  mod_lt(W+1, N);    // so, W+1 == (W+1)%N
             *  note(pxQueue->pcWriteTo == Storage + ((W + 1) * M));
             *  note(                      Storage + ((W + 1) * M) == Storage + (((W + 1) % N) * M));
             * }@*/
            mtCOVERAGE_TEST_MARKER();
        }
    }
    else
    {
        #ifdef VERIFAST /*< void cast of unused return value */
            /*@split_element(Storage, N, M, R);@*/

            /*@assert
             *  buffer(Storage, R, M, ?prefix) &*&
             *  chars(Storage + R * M, M, _) &*&
             *  buffer(Storage + (R + 1) * M, (N-1-R), M, ?suffix);@*/
            memcpy( ( void * ) pxQueue->u.xQueue.pcReadFrom, pvItemToQueue, ( size_t ) pxQueue->uxItemSize );
            /*@join_element(Storage, N, M, R);@*/
            /*@combine_list_update(prefix, x, suffix, R, contents);@*/
        #else
            ( 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. */
        #endif
        pxQueue->u.xQueue.pcReadFrom -= pxQueue->uxItemSize;

        if( pxQueue->u.xQueue.pcReadFrom < pxQueue->pcHead ) /*lint !e946 MISRA exception justified as comparison of pointers is the cleanest solution. */
        {
            pxQueue->u.xQueue.pcReadFrom = ( pxQueue->u.xQueue.pcTail - pxQueue->uxItemSize );
            /*@{ div_leq(R-1, 0, M); leq_bound(R, 0); }@*/
            /*@assert R == 0;@*/
            /*@assert pxQueue->u.xQueue.pcReadFrom == Storage + (N-1) * M;@*/
        }
        else
        {
            /*@assert 0 < R;@*/
            /*@assert pxQueue->u.xQueue.pcReadFrom == Storage + (R-1) * M;@*/
            mtCOVERAGE_TEST_MARKER();
        }

        /*@
         * if (R == 0)
         * {
         * mod_plus(N, (K+1), N); mod_same(N); mod_mod(K+1, N);
         * assert W == ((N-1) + 1 + (K+1)) % N;
         * }
         * @*/
        if( xPosition == queueOVERWRITE )
        {
            if( uxMessagesWaiting > ( UBaseType_t ) 0 )
            {
                /* An item is not being added but overwritten, so subtract
                 * one from the recorded number of items in the queue so when
                 * one is added again below the number of recorded items remains
                 * correct. */
                --uxMessagesWaiting;
            }
            else
            {
                mtCOVERAGE_TEST_MARKER();
            }
        }
        else
        {
            mtCOVERAGE_TEST_MARKER();
        }
    }

    pxQueue->uxMessagesWaiting = uxMessagesWaiting + ( UBaseType_t ) 1;

    /*@
     * if (xPosition == queueSEND_TO_BACK)
     * {
     *  enq_lemma(K, (R+1)%N, contents, abs, x);
     *  mod_plus_one(W, R + 1 + K, N);
     *  mod_plus_distr(R+1, K, N);
     * }
     * else if (xPosition == queueSEND_TO_FRONT)
     * {
     *  front_enq_lemma(K, R, contents, abs, x);
     *  if (0 < R)
     *  {
     *      mod_lt(R, N);
     *  }
     * }
     * @*/
    return xReturn;
}