6fed1979f93317a530240837b673ac61b7808768
[oweals/openssl.git] / crypto / async / async.c
1 /* crypto/async/async.c */
2 /*
3  * Written by Matt Caswell (matt@openssl.org) for the OpenSSL project.
4  */
5 /* ====================================================================
6  * Copyright (c) 2015 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  */
53
54 #include <openssl/crypto.h>
55 #include <openssl/async.h>
56 #include <string.h>
57 #include "async_locl.h"
58
59 #define ASYNC_JOB_RUNNING   0
60 #define ASYNC_JOB_PAUSING   1
61 #define ASYNC_JOB_PAUSED    2
62 #define ASYNC_JOB_STOPPING  3
63
64 static size_t pool_max_size = 0;
65 static size_t curr_size = 0;
66
67 DECLARE_STACK_OF(ASYNC_JOB)
68 static STACK_OF(ASYNC_JOB) *pool = NULL;
69
70
71 static ASYNC_CTX *ASYNC_CTX_new(void)
72 {
73     ASYNC_CTX *nctx = NULL;
74
75     if(!(nctx = OPENSSL_malloc(sizeof (ASYNC_CTX)))) {
76         /* Error here */
77         goto err;
78     }
79
80     ASYNC_FIBRE_init_dispatcher(&nctx->dispatcher);
81     nctx->currjob = NULL;
82     if(!ASYNC_set_ctx(nctx))
83         goto err;
84
85     return nctx;
86 err:
87     if(nctx) {
88         OPENSSL_free(nctx);
89     }
90
91     return NULL;
92 }
93
94 static int ASYNC_CTX_free(void)
95 {
96     if(ASYNC_get_ctx()) {
97         OPENSSL_free(ASYNC_get_ctx());
98     }
99
100     if(!ASYNC_set_ctx(NULL))
101         return 0;
102
103     return 1;
104 }
105
106 static ASYNC_JOB *ASYNC_JOB_new(void)
107 {
108     ASYNC_JOB *job = NULL;
109
110     if(!(job = OPENSSL_malloc(sizeof (ASYNC_JOB)))) {
111         return NULL;
112     }
113
114     job->status = ASYNC_JOB_RUNNING;
115     job->funcargs = NULL;
116
117     return job;
118 }
119
120 static void ASYNC_JOB_free(ASYNC_JOB *job)
121 {
122     if(job) {
123         if(job->funcargs)
124             OPENSSL_free(job->funcargs);
125         ASYNC_FIBRE_free(&job->fibrectx);
126         OPENSSL_free(job);
127     }
128 }
129
130 static ASYNC_JOB *async_get_pool_job(void) {
131     ASYNC_JOB *job;
132
133     if (pool == NULL) {
134         /*
135          * Pool has not been initialised, so init with the defaults, i.e.
136          * global pool, with no max size and no pre-created jobs
137          */
138         if (ASYNC_init_pool(0, 0, 0) == 0)
139             return NULL;
140     }
141
142     job = sk_ASYNC_JOB_pop(pool);
143     if (job == NULL) {
144         /* Pool is empty */
145         if (pool_max_size && curr_size >= pool_max_size) {
146             /* Pool is at max size. We cannot continue */
147             return NULL;
148         }
149         job = ASYNC_JOB_new();
150         if (job) {
151             ASYNC_FIBRE_makecontext(&job->fibrectx);
152             curr_size++;
153         }
154     }
155     return job;
156 }
157
158 static void async_release_job(ASYNC_JOB *job) {
159     if(job->funcargs)
160         OPENSSL_free(job->funcargs);
161     job->funcargs = NULL;
162     /* Ignore error return */
163     sk_ASYNC_JOB_push(pool, job);
164 }
165
166 void ASYNC_start_func(void)
167 {
168     ASYNC_JOB *job;
169
170     while (1) {
171         /* Run the job */
172         job = ASYNC_get_ctx()->currjob;
173         job->ret = job->func(job->funcargs);
174
175         /* Stop the job */
176         job->status = ASYNC_JOB_STOPPING;
177         if(!ASYNC_FIBRE_swapcontext(&job->fibrectx,
178                                     &ASYNC_get_ctx()->dispatcher, 1)) {
179             /*
180              * Should not happen. Getting here will close the thread...can't do much
181              * about it
182              */
183         }
184     }
185 }
186
187 int ASYNC_start_job(ASYNC_JOB **job, int *ret, int (*func)(void *),
188                          void *args, size_t size)
189 {
190     if(ASYNC_get_ctx() || !ASYNC_CTX_new()) {
191         return ASYNC_ERR;
192     }
193
194     if(*job) {
195         ASYNC_get_ctx()->currjob = *job;
196     }
197
198     for (;;) {
199         if(ASYNC_get_ctx()->currjob) {
200             if(ASYNC_get_ctx()->currjob->status == ASYNC_JOB_STOPPING) {
201                 *ret = ASYNC_get_ctx()->currjob->ret;
202                 async_release_job(ASYNC_get_ctx()->currjob);
203                 ASYNC_get_ctx()->currjob = NULL;
204                 *job = NULL;
205                 ASYNC_CTX_free();
206                 return ASYNC_FINISH;
207             }
208
209             if(ASYNC_get_ctx()->currjob->status == ASYNC_JOB_PAUSING) {
210                 *job = ASYNC_get_ctx()->currjob;
211                 ASYNC_get_ctx()->currjob->status = ASYNC_JOB_PAUSED;
212                 ASYNC_CTX_free();
213                 return ASYNC_PAUSE;
214             }
215
216             if(ASYNC_get_ctx()->currjob->status == ASYNC_JOB_PAUSED) {
217                 ASYNC_get_ctx()->currjob = *job;
218                 /* Resume previous job */
219                 if(!ASYNC_FIBRE_swapcontext(&ASYNC_get_ctx()->dispatcher,
220                     &ASYNC_get_ctx()->currjob->fibrectx, 1))
221                     goto err;
222                 continue;
223             }
224
225             /* Should not happen */
226             async_release_job(ASYNC_get_ctx()->currjob);
227             ASYNC_get_ctx()->currjob = NULL;
228             *job = NULL;
229             ASYNC_CTX_free();
230             return ASYNC_ERR;
231         }
232
233         /* Start a new job */
234         if(!(ASYNC_get_ctx()->currjob = async_get_pool_job())) {
235             ASYNC_CTX_free();
236             return ASYNC_NO_JOBS;
237         }
238
239         if(args != NULL) {
240             ASYNC_get_ctx()->currjob->funcargs = OPENSSL_malloc(size);
241             if(!ASYNC_get_ctx()->currjob->funcargs) {
242                 async_release_job(ASYNC_get_ctx()->currjob);
243                 ASYNC_get_ctx()->currjob = NULL;
244                 ASYNC_CTX_free();
245                 return ASYNC_ERR;
246             }
247             memcpy(ASYNC_get_ctx()->currjob->funcargs, args, size);
248         } else {
249             ASYNC_get_ctx()->currjob->funcargs = NULL;
250         }
251
252         ASYNC_get_ctx()->currjob->func = func;
253         if(!ASYNC_FIBRE_swapcontext(&ASYNC_get_ctx()->dispatcher,
254             &ASYNC_get_ctx()->currjob->fibrectx, 1))
255             goto err;
256     }
257
258 err:
259     async_release_job(ASYNC_get_ctx()->currjob);
260     ASYNC_get_ctx()->currjob = NULL;
261     *job = NULL;
262     ASYNC_CTX_free();
263     return ASYNC_ERR;
264 }
265
266
267 int ASYNC_pause_job(void)
268 {
269     ASYNC_JOB *job;
270
271     if(!ASYNC_get_ctx() || !ASYNC_get_ctx()->currjob)
272         return 0;
273
274     job = ASYNC_get_ctx()->currjob;
275     job->status = ASYNC_JOB_PAUSING;
276
277     if(!ASYNC_FIBRE_swapcontext(&job->fibrectx,
278                                &ASYNC_get_ctx()->dispatcher, 1)) {
279         /* Error */
280         return 0;
281     }
282
283     return 1;
284 }
285
286 int ASYNC_in_job(void)
287 {
288     if(ASYNC_get_ctx())
289         return 1;
290
291     return 0;
292 }
293
294 int ASYNC_init_pool(unsigned int local, size_t max_size, size_t init_size)
295 {
296     if (local != 0) {
297         /* We only support a global pool so far */
298         return 0;
299     }
300
301     pool_max_size = max_size;
302     pool = sk_ASYNC_JOB_new_null();
303     if (pool == NULL) {
304         return 0;
305     }
306     /* Pre-create jobs as required */
307     while (init_size) {
308         ASYNC_JOB *job;
309         job = ASYNC_JOB_new();
310         if (job) {
311             ASYNC_FIBRE_makecontext(&job->fibrectx);
312             job->funcargs = NULL;
313             sk_ASYNC_JOB_push(pool, job);
314             curr_size++;
315             init_size--;
316         } else {
317             /*
318              * Not actually fatal because we already created the pool, just skip
319              * creation of any more jobs
320              */
321             init_size = 0;
322         }
323     }
324
325     return 1;
326 }
327
328 void ASYNC_free_pool(void)
329 {
330     ASYNC_JOB *job;
331
332     do {
333         job = sk_ASYNC_JOB_pop(pool);
334         ASYNC_JOB_free(job);
335     } while (job);
336     sk_ASYNC_JOB_free(pool);
337 }