forked from openssl/openssl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
QUIC err handling: Save and restore error state
We save the error state from the thread that encountered a permanent error condition caused by system or internal error to the QUIC_CHANNEL. Then we restore it whenever we are returning to a user call when protocol is shutdown.
- Loading branch information
Showing
12 changed files
with
223 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
LIBS=../../libcrypto | ||
SOURCE[../../libcrypto]=\ | ||
err_blocks.c err_mark.c err.c err_all.c err_all_legacy.c err_prn.c | ||
err_blocks.c err_mark.c err.c err_all.c err_all_legacy.c err_prn.c err_save.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Copyright 2023 The OpenSSL Project Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License 2.0 (the "License"). You may not use | ||
* this file except in compliance with the License. You can obtain a copy | ||
* in the file LICENSE in the source distribution or at | ||
* https://www.openssl.org/source/license.html | ||
*/ | ||
|
||
#define OSSL_FORCE_ERR_STATE | ||
|
||
#include <openssl/err.h> | ||
#include "err_local.h" | ||
|
||
/* | ||
* Save and restore error state. | ||
* We are using CRYPTO_zalloc(.., NULL, 0) instead of OPENSSL_malloc() in | ||
* these functions to prevent mem alloc error loop. | ||
*/ | ||
|
||
ERR_STATE *OSSL_ERR_STATE_new(void) | ||
{ | ||
return CRYPTO_zalloc(sizeof(ERR_STATE), NULL, 0); | ||
} | ||
|
||
void OSSL_ERR_STATE_save(ERR_STATE *es) | ||
{ | ||
size_t i; | ||
ERR_STATE *thread_es; | ||
|
||
if (es == NULL) | ||
return; | ||
|
||
for (i = 0; i < ERR_NUM_ERRORS; i++) { | ||
err_clear(es, i, 1); | ||
} | ||
|
||
thread_es = ossl_err_get_state_int(); | ||
if (thread_es == NULL) | ||
return; | ||
|
||
memcpy(es, thread_es, sizeof(*es)); | ||
/* Taking over the pointers, just clear the thread state. */ | ||
memset(thread_es, 0, sizeof(*thread_es)); | ||
} | ||
|
||
void OSSL_ERR_STATE_restore(const ERR_STATE *es) | ||
{ | ||
size_t i; | ||
ERR_STATE *thread_es; | ||
|
||
if (es == NULL || es->bottom == es->top) | ||
return; | ||
|
||
thread_es = ossl_err_get_state_int(); | ||
if (thread_es == NULL) | ||
return; | ||
|
||
for (i = ((size_t)es->bottom + 1) % ERR_NUM_ERRORS; i != (size_t)es->top; | ||
i = (i + 1) % ERR_NUM_ERRORS) { | ||
size_t top; | ||
|
||
if ((es->err_flags[i] & ERR_FLAG_CLEAR) != 0) | ||
continue; | ||
|
||
err_get_slot(thread_es); | ||
top = thread_es->top; | ||
err_clear(thread_es, top, 0); | ||
|
||
thread_es->err_flags[top] = es->err_flags[i] & ~ERR_FLAG_MARK; | ||
thread_es->err_buffer[top] = es->err_buffer[i]; | ||
|
||
err_set_debug(thread_es, top, es->err_file[i], es->err_line[i], | ||
es->err_func[i]); | ||
|
||
if (es->err_data[i] != NULL && es->err_data_size[i] != 0) { | ||
void *data; | ||
size_t data_sz = es->err_data_size[i]; | ||
|
||
data = CRYPTO_malloc(data_sz, NULL, 0); | ||
if (data != NULL) { | ||
memcpy(data, es->err_data[i], data_sz); | ||
err_set_data(thread_es, top, data, data_sz, | ||
es->err_data_flags[i] | ERR_TXT_MALLOCED); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
=pod | ||
|
||
=head1 NAME | ||
|
||
OSSL_ERR_STATE_new, OSSL_ERR_STATE_save, OSSL_ERR_STATE_restore, | ||
OSSL_ERR_STATE_free - saving and restoring error state | ||
|
||
=head1 SYNOPSIS | ||
|
||
#include <openssl/err.h> | ||
|
||
ERR_STATE *OSSL_ERR_STATE_new(void); | ||
void OSSL_ERR_STATE_save(ERR_STATE *es); | ||
void OSSL_ERR_STATE_restore(const ERR_STATE *es); | ||
void OSSL_ERR_STATE_free(ERR_STATE *es); | ||
|
||
=head1 DESCRIPTION | ||
|
||
These functions save and restore the error state from the thread | ||
local error state to a preallocated error state structure. | ||
|
||
OSSL_ERR_STATE_new() allocates an empty error state structure to | ||
be used when saving and restoring thread error state. | ||
|
||
OSSL_ERR_STATE_save() saves the thread error state to I<es>. It | ||
subsequently clears the thread error state. Any previously saved | ||
state in I<es> is cleared prior to saving the new state. | ||
|
||
OSSL_ERR_STATE_restore() adds all the error entries from the | ||
saved state I<es> to the thread error state. Existing entries in | ||
the thread error state are not affected if there is enough space | ||
for all the added entries. | ||
|
||
OSSL_ERR_STATE_free() frees the saved error state I<es>. | ||
|
||
=head1 RETURN VALUES | ||
|
||
OSSL_ERR_STATE_new() returns a pointer to the allocated ERR_STATE | ||
structure or NULL on error. | ||
|
||
OSSL_ERR_STATE_save(), OSSL_ERR_STATE_restore(), OSSL_ERR_STATE_free() | ||
do not return any values. | ||
|
||
=head1 NOTES | ||
|
||
OSSL_ERR_STATE_save() cannot fail as it takes over any allocated | ||
data from the thread error state. | ||
|
||
OSSL_ERR_STATE_restore() is a best effort function. The only failure | ||
that can happen during its operation is when memory allocation fails. | ||
Because it manipulates error stack it avoids raising memory errors | ||
on such failure. At worst the restored error entries will be missing | ||
the auxiliary error data. | ||
|
||
=head1 SEE ALSO | ||
|
||
L<ERR_raise(3)>, L<ERR_get_error(3)>, L<ERR_clear_error(3)> | ||
|
||
=head1 COPYRIGHT | ||
|
||
Copyright 2023 The OpenSSL Project Authors. All Rights Reserved. | ||
|
||
Licensed under the Apache License 2.0 (the "License"). You may not use | ||
this file except in compliance with the License. You can obtain a copy | ||
in the file LICENSE in the source distribution or at | ||
L<https://www.openssl.org/source/license.html>. | ||
|
||
=cut |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters