Add ASYNC error codes
authorMatt Caswell <matt@openssl.org>
Tue, 6 Oct 2015 13:47:00 +0000 (14:47 +0100)
committerMatt Caswell <matt@openssl.org>
Fri, 20 Nov 2015 23:34:35 +0000 (23:34 +0000)
Add ASYNCerr support to give some meaningful error message in the event of
a failure.

Reviewed-by: Rich Salz <rsalz@openssl.org>
crypto/async/Makefile
crypto/async/async.c
crypto/async/async_err.c [new file with mode: 0644]
crypto/err/err.c
crypto/err/err_all.c
crypto/err/openssl.ec
include/openssl/async.h
include/openssl/err.h

index 886873257b08eb4c9ac07670ba8a83d38ef4bd08..1c9482f12e144c14675a99453f8a98aff4b41f28 100644 (file)
@@ -17,8 +17,8 @@ TEST=
 APPS=
 
 LIB=$(TOP)/libcrypto.a
-LIBSRC=async.c arch/async_posix.c arch/async_win.c arch/async_null.c
-LIBOBJ=async.o arch/async_posix.o arch/async_win.o arch/async_null.o
+LIBSRC=async.c async_err.c arch/async_posix.c arch/async_win.c arch/async_null.c
+LIBOBJ=async.o async_err.o arch/async_posix.o arch/async_win.o arch/async_null.o
 
 SRC= $(LIBSRC)
 
index c3fe3e15c796fc93eeb51d247001d803600bd8ba..e0ff110e3377bc76d0a274a71173a80cd2bfe453 100644 (file)
@@ -51,6 +51,7 @@
  * ====================================================================
  */
 
+#include <openssl/err.h>
 #include <openssl/async.h>
 #include <string.h>
 #include "async_locl.h"
@@ -65,7 +66,7 @@ static async_ctx *async_ctx_new(void)
     async_ctx *nctx = NULL;
 
     if(!(nctx = OPENSSL_malloc(sizeof (async_ctx)))) {
-        /* Error here */
+        ASYNCerr(ASYNC_F_ASYNC_CTX_NEW, ERR_R_MALLOC_FAILURE);
         goto err;
     }
 
@@ -101,11 +102,13 @@ static ASYNC_JOB *async_job_new(void)
     int pipefds[2];
 
     if(!(job = OPENSSL_malloc(sizeof (ASYNC_JOB)))) {
+        ASYNCerr(ASYNC_F_ASYNC_JOB_NEW, ERR_R_MALLOC_FAILURE);
         return NULL;
     }
 
     if(!async_pipe(pipefds)) {
         OPENSSL_free(job);
+        ASYNCerr(ASYNC_F_ASYNC_JOB_NEW, ASYNC_R_CANNOT_CREATE_WAIT_PIPE);
         return NULL;
     }
 
@@ -181,9 +184,10 @@ void async_start_func(void)
         if(!async_fibre_swapcontext(&job->fibrectx,
                                     &async_get_ctx()->dispatcher, 1)) {
             /*
-             * Should not happen. Getting here will close the thread...can't do much
-             * about it
+             * Should not happen. Getting here will close the thread...can't do
+             * much about it
              */
+            ASYNCerr(ASYNC_F_ASYNC_START_FUNC, ASYNC_R_FAILED_TO_SWAP_CONTEXT);
         }
     }
 }
@@ -220,12 +224,16 @@ int ASYNC_start_job(ASYNC_JOB **job, int *ret, int (*func)(void *),
                 async_get_ctx()->currjob = *job;
                 /* Resume previous job */
                 if(!async_fibre_swapcontext(&async_get_ctx()->dispatcher,
-                    &async_get_ctx()->currjob->fibrectx, 1))
+                    &async_get_ctx()->currjob->fibrectx, 1)) {
+                    ASYNCerr(ASYNC_F_ASYNC_START_JOB,
+                             ASYNC_R_FAILED_TO_SWAP_CONTEXT);
                     goto err;
+                }
                 continue;
             }
 
             /* Should not happen */
+            ASYNCerr(ASYNC_F_ASYNC_START_JOB, ERR_R_INTERNAL_ERROR);
             async_release_job(async_get_ctx()->currjob);
             async_get_ctx()->currjob = NULL;
             *job = NULL;
@@ -240,6 +248,7 @@ int ASYNC_start_job(ASYNC_JOB **job, int *ret, int (*func)(void *),
         if(args != NULL) {
             async_get_ctx()->currjob->funcargs = OPENSSL_malloc(size);
             if(!async_get_ctx()->currjob->funcargs) {
+                ASYNCerr(ASYNC_F_ASYNC_START_JOB, ERR_R_MALLOC_FAILURE);
                 async_release_job(async_get_ctx()->currjob);
                 async_get_ctx()->currjob = NULL;
                 return ASYNC_ERR;
@@ -251,8 +260,10 @@ int ASYNC_start_job(ASYNC_JOB **job, int *ret, int (*func)(void *),
 
         async_get_ctx()->currjob->func = func;
         if(!async_fibre_swapcontext(&async_get_ctx()->dispatcher,
-            &async_get_ctx()->currjob->fibrectx, 1))
+            &async_get_ctx()->currjob->fibrectx, 1)) {
+            ASYNCerr(ASYNC_F_ASYNC_START_JOB, ASYNC_R_FAILED_TO_SWAP_CONTEXT);
             goto err;
+        }
     }
 
 err:
@@ -267,15 +278,20 @@ int ASYNC_pause_job(void)
 {
     ASYNC_JOB *job;
 
-    if(!async_get_ctx() || !async_get_ctx()->currjob)
+    if(!async_get_ctx() || !async_get_ctx()->currjob) {
+        /*
+         * Could be we've deliberately not been started within a job so we
+         * don't put an error on the error queue here.
+         */
         return 0;
+    }
 
     job = async_get_ctx()->currjob;
     job->status = ASYNC_JOB_PAUSING;
 
     if(!async_fibre_swapcontext(&job->fibrectx,
                                &async_get_ctx()->dispatcher, 1)) {
-        /* Error */
+        ASYNCerr(ASYNC_F_ASYNC_PAUSE_JOB, ASYNC_R_FAILED_TO_SWAP_CONTEXT);
         return 0;
     }
 
@@ -297,11 +313,14 @@ int ASYNC_init_pool(size_t max_size, size_t init_size)
     STACK_OF(ASYNC_JOB) *pool;
     size_t curr_size = 0;
 
-    if (init_size > max_size)
+    if (init_size > max_size) {
+        ASYNCerr(ASYNC_F_ASYNC_INIT_POOL, ASYNC_R_INVALID_POOL_SIZE);
         return 0;
+    }
 
     pool = sk_ASYNC_JOB_new_null();
     if (pool == NULL) {
+        ASYNCerr(ASYNC_F_ASYNC_INIT_POOL, ERR_R_MALLOC_FAILURE);
         return 0;
     }
     /* Pre-create jobs as required */
@@ -324,6 +343,7 @@ int ASYNC_init_pool(size_t max_size, size_t init_size)
     }
 
     if (!async_set_pool(pool, curr_size, max_size)) {
+        ASYNCerr(ASYNC_F_ASYNC_INIT_POOL, ASYNC_R_FAILED_TO_SET_POOL);
         async_empty_pool(pool);
         sk_ASYNC_JOB_free(pool);
         return 0;
diff --git a/crypto/async/async_err.c b/crypto/async/async_err.c
new file mode 100644 (file)
index 0000000..1391c0f
--- /dev/null
@@ -0,0 +1,101 @@
+/* crypto/async/async_err.c */
+/* ====================================================================
+ * Copyright (c) 1999-2015 The OpenSSL Project.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * 3. All advertising materials mentioning features or use of this
+ *    software must display the following acknowledgment:
+ *    "This product includes software developed by the OpenSSL Project
+ *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
+ *
+ * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
+ *    endorse or promote products derived from this software without
+ *    prior written permission. For written permission, please contact
+ *    openssl-core@OpenSSL.org.
+ *
+ * 5. Products derived from this software may not be called "OpenSSL"
+ *    nor may "OpenSSL" appear in their names without prior written
+ *    permission of the OpenSSL Project.
+ *
+ * 6. Redistributions of any form whatsoever must retain the following
+ *    acknowledgment:
+ *    "This product includes software developed by the OpenSSL Project
+ *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
+ * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This product includes cryptographic software written by Eric Young
+ * (eay@cryptsoft.com).  This product includes software written by Tim
+ * Hudson (tjh@cryptsoft.com).
+ *
+ */
+
+/*
+ * NOTE: this file was auto generated by the mkerr.pl script: any changes
+ * made to it will be overwritten when the script next updates this file,
+ * only reason strings will be preserved.
+ */
+
+#include <stdio.h>
+#include <openssl/err.h>
+#include <openssl/async.h>
+
+/* BEGIN ERROR CODES */
+#ifndef OPENSSL_NO_ERR
+
+# define ERR_FUNC(func) ERR_PACK(ERR_LIB_ASYNC,func,0)
+# define ERR_REASON(reason) ERR_PACK(ERR_LIB_ASYNC,0,reason)
+
+static ERR_STRING_DATA ASYNC_str_functs[] = {
+    {ERR_FUNC(ASYNC_F_ASYNC_CTX_NEW), "ASYNC_CTX_NEW"},
+    {ERR_FUNC(ASYNC_F_ASYNC_INIT_POOL), "ASYNC_init_pool"},
+    {ERR_FUNC(ASYNC_F_ASYNC_JOB_NEW), "ASYNC_JOB_NEW"},
+    {ERR_FUNC(ASYNC_F_ASYNC_PAUSE_JOB), "ASYNC_pause_job"},
+    {ERR_FUNC(ASYNC_F_ASYNC_START_FUNC), "ASYNC_START_FUNC"},
+    {ERR_FUNC(ASYNC_F_ASYNC_START_JOB), "ASYNC_start_job"},
+    {0, NULL}
+};
+
+static ERR_STRING_DATA ASYNC_str_reasons[] = {
+    {ERR_REASON(ASYNC_R_CANNOT_CREATE_WAIT_PIPE), "cannot create wait pipe"},
+    {ERR_REASON(ASYNC_R_FAILED_TO_SET_POOL), "failed to set pool"},
+    {ERR_REASON(ASYNC_R_FAILED_TO_SWAP_CONTEXT), "failed to swap context"},
+    {ERR_REASON(ASYNC_R_INVALID_POOL_SIZE), "invalid pool size"},
+    {0, NULL}
+};
+
+#endif
+
+void ERR_load_ASYNC_strings(void)
+{
+#ifndef OPENSSL_NO_ERR
+
+    if (ERR_func_error_string(ASYNC_str_functs[0].error) == NULL) {
+        ERR_load_strings(0, ASYNC_str_functs);
+        ERR_load_strings(0, ASYNC_str_reasons);
+    }
+#endif
+}
index 077929c2951dbd4a944b11a28b59103ce710319e..b7ca3ded055dc1f57e076ec204fa1cc5caa778ce 100644 (file)
@@ -157,6 +157,7 @@ static ERR_STRING_DATA ERR_str_libraries[] = {
     {ERR_PACK(ERR_LIB_FIPS, 0, 0), "FIPS routines"},
     {ERR_PACK(ERR_LIB_CMS, 0, 0), "CMS routines"},
     {ERR_PACK(ERR_LIB_HMAC, 0, 0), "HMAC routines"},
+    {ERR_PACK(ERR_LIB_ASYNC, 0, 0), "ASYNC routines"},
     {0, NULL},
 };
 
index d9a2a576824c453e4d11eb047d5dd368e4e5e753..baf76e7e26e0cb15d7015a5cb7985c2b71af1392 100644 (file)
 # include <openssl/jpake.h>
 #endif
 #include <internal/ct_int.h>
+#include <openssl/async.h>
 
 void ERR_load_crypto_strings(void)
 {
@@ -165,5 +166,6 @@ void ERR_load_crypto_strings(void)
 # ifndef OPENSSL_NO_CT
     ERR_load_CT_strings();
 # endif
+    ERR_load_ASYNC_strings();
 #endif
 }
index 8a10b80b0499ffd9e2647c0f7c91a45449f2f98d..0d308a9ad9821a92811f875e06f126caf4638e7f 100644 (file)
@@ -36,6 +36,7 @@ L HMAC                include/openssl/hmac.h          crypto/hmac/hmac_err.c
 L CMS          include/openssl/cms.h           crypto/cms/cms_err.c
 L JPAKE                include/openssl/jpake.h         crypto/jpake/jpake_err.c
 L FIPS         include/openssl/fips.h          crypto/fips_err.h
+L ASYNC                include/openssl/async.h         crypto/async/async_err.c
 
 # additional header files to be scanned for function names
 L NONE         crypto/x509/x509_vfy.h          NONE
index e123261e6bd91441c6706a061ed7d3e1a9f72161..9592cd6757a2dd80606a79091ce63f61cef7d22d 100644 (file)
@@ -79,8 +79,30 @@ ASYNC_JOB *ASYNC_get_current_job(void);
 void ASYNC_wake(ASYNC_JOB *job);
 void ASYNC_clear_wake(ASYNC_JOB *job);
 
-# ifdef  __cplusplus
-}
-# endif
+/* BEGIN ERROR CODES */
+/*
+ * The following lines are auto generated by the script mkerr.pl. Any changes
+ * made after this point may be overwritten when the script is next run.
+ */
+void ERR_load_ASYNC_strings(void);
 
+/* Error codes for the ASYNC functions. */
+
+/* Function codes. */
+# define ASYNC_F_ASYNC_CTX_NEW                            100
+# define ASYNC_F_ASYNC_INIT_POOL                          101
+# define ASYNC_F_ASYNC_JOB_NEW                            102
+# define ASYNC_F_ASYNC_PAUSE_JOB                          103
+# define ASYNC_F_ASYNC_START_FUNC                         104
+# define ASYNC_F_ASYNC_START_JOB                          105
+
+/* Reason codes. */
+# define ASYNC_R_CANNOT_CREATE_WAIT_PIPE                  100
+# define ASYNC_R_FAILED_TO_SET_POOL                       101
+# define ASYNC_R_FAILED_TO_SWAP_CONTEXT                   102
+# define ASYNC_R_INVALID_POOL_SIZE                        103
+
+#ifdef  __cplusplus
+}
+#endif
 #endif
index 4c6d8d42ca1d85cc2e218b4471016ea40bc887cb..79bf6a3214f95948aa19d73a82e10db9e0c95bd8 100644 (file)
@@ -194,6 +194,7 @@ typedef struct err_state_st {
 # define ERR_LIB_HMAC            48
 # define ERR_LIB_JPAKE           49
 # define ERR_LIB_CT              50
+# define ERR_LIB_ASYNC           51
 
 # define ERR_LIB_USER            128
 
@@ -231,6 +232,7 @@ typedef struct err_state_st {
 # define HMACerr(f,r) ERR_PUT_error(ERR_LIB_HMAC,(f),(r),__FILE__,__LINE__)
 # define JPAKEerr(f,r) ERR_PUT_error(ERR_LIB_JPAKE,(f),(r),__FILE__,__LINE__)
 # define CTerr(f,r) ERR_PUT_error(ERR_LIB_CT,(f),(r),__FILE__,__LINE__)
+# define ASYNCerr(f,r) ERR_PUT_error(ERR_LIB_ASYNC,(f),(r),__FILE__,__LINE__)
 
 /*
  * Borland C seems too stupid to be able to shift and do longs in the