failmalloc: Use nghttp2_mem instead of using dlsym

This commit is contained in:
Tatsuhiro Tsujikawa 2014-12-20 23:10:46 +09:00
parent e60183313b
commit 959d05e6f8
4 changed files with 72 additions and 46 deletions

View File

@ -22,17 +22,22 @@
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* HAVE_CONFIG_H */
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <CUnit/Basic.h> #include <CUnit/Basic.h>
/* include test cases' include files here */ /* include test cases' include files here */
#include "failmalloc_test.h" #include "failmalloc_test.h"
static int init_suite1(void) { return 0; } static int init_suite1(void) { return 0; }
static int clean_suite1(void) { return 0; } static int clean_suite1(void) { return 0; }
int main(int argc, char *argv[]) { int main(int argc _U_, char *argv[] _U_) {
CU_pSuite pSuite = NULL; CU_pSuite pSuite = NULL;
unsigned int num_tests_failed; unsigned int num_tests_failed;

View File

@ -59,13 +59,15 @@ static void data_feed_init(data_feed *df, nghttp2_bufs *bufs) {
df->datalimit = df->data + data_length; df->datalimit = df->data + data_length;
} }
static ssize_t null_send_callback(nghttp2_session *session, const uint8_t *data, static ssize_t null_send_callback(nghttp2_session *session _U_,
size_t len, int flags, void *user_data) { const uint8_t *data _U_, size_t len,
int flags _U_, void *user_data _U_) {
return len; return len;
} }
static ssize_t data_feed_recv_callback(nghttp2_session *session, uint8_t *data, static ssize_t data_feed_recv_callback(nghttp2_session *session _U_,
size_t len, int flags, void *user_data) { uint8_t *data, size_t len, int flags _U_,
void *user_data) {
data_feed *df = ((my_user_data *)user_data)->df; data_feed *df = ((my_user_data *)user_data)->df;
size_t avail = df->datalimit - df->datamark; size_t avail = df->datalimit - df->datamark;
size_t wlen = nghttp2_min(avail, len); size_t wlen = nghttp2_min(avail, len);
@ -75,8 +77,9 @@ static ssize_t data_feed_recv_callback(nghttp2_session *session, uint8_t *data,
} }
static ssize_t fixed_length_data_source_read_callback( static ssize_t fixed_length_data_source_read_callback(
nghttp2_session *session, int32_t stream_id, uint8_t *buf, size_t len, nghttp2_session *session _U_, int32_t stream_id _U_, uint8_t *buf _U_,
uint32_t *data_flags, nghttp2_data_source *source, void *user_data) { size_t len, uint32_t *data_flags, nghttp2_data_source *source _U_,
void *user_data) {
my_user_data *ud = (my_user_data *)user_data; my_user_data *ud = (my_user_data *)user_data;
size_t wlen; size_t wlen;
if (len < ud->data_source_length) { if (len < ud->data_source_length) {
@ -93,7 +96,7 @@ static ssize_t fixed_length_data_source_read_callback(
#define TEST_FAILMALLOC_RUN(FUN) \ #define TEST_FAILMALLOC_RUN(FUN) \
do { \ do { \
size_t nmalloc, i; \ int nmalloc, i; \
\ \
nghttp2_failmalloc = 0; \ nghttp2_failmalloc = 0; \
nghttp2_nmalloc = 0; \ nghttp2_nmalloc = 0; \
@ -131,7 +134,8 @@ static void run_nghttp2_session_send(void) {
iv[1].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS; iv[1].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS;
iv[1].value = 100; iv[1].value = 100;
rv = nghttp2_session_client_new(&session, &callbacks, &ud); rv = nghttp2_session_client_new3(&session, &callbacks, &ud, NULL,
nghttp2_mem_fm());
if (rv != 0) { if (rv != 0) {
goto client_new_fail; goto client_new_fail;
} }
@ -240,9 +244,10 @@ static void run_nghttp2_session_recv(void) {
nghttp2_failmalloc_pause(); nghttp2_failmalloc_pause();
nvlen = ARRLEN(nv); nvlen = ARRLEN(nv);
nghttp2_nv_array_copy(&nva, nv, nvlen); nghttp2_nv_array_copy(&nva, nv, nvlen, nghttp2_mem_fm());
nghttp2_hd_deflate_init(&deflater); nghttp2_hd_deflate_init(&deflater, nghttp2_mem_fm());
nghttp2_session_server_new(&session, &callbacks, &ud); nghttp2_session_server_new3(&session, &callbacks, &ud, NULL,
nghttp2_mem_fm());
nghttp2_failmalloc_unpause(); nghttp2_failmalloc_unpause();
/* HEADERS */ /* HEADERS */
@ -250,7 +255,7 @@ static void run_nghttp2_session_recv(void) {
nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_STREAM, 1, nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_STREAM, 1,
NGHTTP2_HCAT_REQUEST, NULL, nva, nvlen); NGHTTP2_HCAT_REQUEST, NULL, nva, nvlen);
nghttp2_frame_pack_headers(&bufs, &frame.headers, &deflater); nghttp2_frame_pack_headers(&bufs, &frame.headers, &deflater);
nghttp2_frame_headers_free(&frame.headers); nghttp2_frame_headers_free(&frame.headers, nghttp2_mem_fm());
data_feed_init(&df, &bufs); data_feed_init(&df, &bufs);
nghttp2_bufs_reset(&bufs); nghttp2_bufs_reset(&bufs);
@ -297,9 +302,10 @@ static void run_nghttp2_session_recv(void) {
iv[1].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS; iv[1].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS;
iv[1].value = 100; iv[1].value = 100;
nghttp2_frame_settings_init(&frame.settings, NGHTTP2_FLAG_NONE, nghttp2_frame_settings_init(&frame.settings, NGHTTP2_FLAG_NONE,
nghttp2_frame_iv_copy(iv, 2), 2); nghttp2_frame_iv_copy(iv, 2, nghttp2_mem_fm()),
2);
nghttp2_frame_pack_settings(&bufs, &frame.settings); nghttp2_frame_pack_settings(&bufs, &frame.settings);
nghttp2_frame_settings_free(&frame.settings); nghttp2_frame_settings_free(&frame.settings, nghttp2_mem_fm());
nghttp2_bufs_reset(&bufs); nghttp2_bufs_reset(&bufs);
nghttp2_failmalloc_unpause(); nghttp2_failmalloc_unpause();
@ -336,16 +342,16 @@ static void run_nghttp2_frame_pack_headers(void) {
return; return;
} }
rv = nghttp2_hd_deflate_init(&deflater); rv = nghttp2_hd_deflate_init(&deflater, nghttp2_mem_fm());
if (rv != 0) { if (rv != 0) {
goto deflate_init_fail; goto deflate_init_fail;
} }
rv = nghttp2_hd_inflate_init(&inflater); rv = nghttp2_hd_inflate_init(&inflater, nghttp2_mem_fm());
if (rv != 0) { if (rv != 0) {
goto inflate_init_fail; goto inflate_init_fail;
} }
nvlen = ARRLEN(nv); nvlen = ARRLEN(nv);
rv = nghttp2_nv_array_copy(&nva, nv, nvlen); rv = nghttp2_nv_array_copy(&nva, nv, nvlen, nghttp2_mem_fm());
if (rv < 0) { if (rv < 0) {
goto nv_copy_fail; goto nv_copy_fail;
} }
@ -359,10 +365,10 @@ static void run_nghttp2_frame_pack_headers(void) {
if (rv != 0) { if (rv != 0) {
goto fail; goto fail;
} }
nghttp2_frame_headers_free(&oframe.headers); nghttp2_frame_headers_free(&oframe.headers, nghttp2_mem_fm());
fail: fail:
nghttp2_frame_headers_free(&frame.headers); nghttp2_frame_headers_free(&frame.headers, nghttp2_mem_fm());
nv_copy_fail: nv_copy_fail:
nghttp2_hd_inflate_free(&inflater); nghttp2_hd_inflate_free(&inflater);
inflate_init_fail: inflate_init_fail:
@ -389,7 +395,7 @@ static void run_nghttp2_frame_pack_settings(void) {
iv[1].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS; iv[1].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS;
iv[1].value = 100; iv[1].value = 100;
iv_copy = nghttp2_frame_iv_copy(iv, 2); iv_copy = nghttp2_frame_iv_copy(iv, 2, nghttp2_mem_fm());
if (iv_copy == NULL) { if (iv_copy == NULL) {
goto iv_copy_fail; goto iv_copy_fail;
@ -407,15 +413,15 @@ static void run_nghttp2_frame_pack_settings(void) {
rv = nghttp2_frame_unpack_settings_payload2( rv = nghttp2_frame_unpack_settings_payload2(
&oframe.settings.iv, &oframe.settings.niv, buf->pos + NGHTTP2_FRAME_HDLEN, &oframe.settings.iv, &oframe.settings.niv, buf->pos + NGHTTP2_FRAME_HDLEN,
nghttp2_buf_len(buf) - NGHTTP2_FRAME_HDLEN); nghttp2_buf_len(buf) - NGHTTP2_FRAME_HDLEN, nghttp2_mem_fm());
if (rv != 0) { if (rv != 0) {
goto fail; goto fail;
} }
nghttp2_frame_settings_free(&oframe.settings); nghttp2_frame_settings_free(&oframe.settings, nghttp2_mem_fm());
fail: fail:
nghttp2_frame_settings_free(&frame.settings); nghttp2_frame_settings_free(&frame.settings, nghttp2_mem_fm());
iv_copy_fail: iv_copy_fail:
nghttp2_bufs_free(&bufs); nghttp2_bufs_free(&bufs);
} }
@ -436,7 +442,7 @@ static int deflate_inflate(nghttp2_hd_deflater *deflater,
return rv; return rv;
} }
rv = inflate_hd(inflater, NULL, bufs, 0); rv = (int)inflate_hd(inflater, NULL, bufs, 0);
if (rv < 0) { if (rv < 0) {
return rv; return rv;
@ -466,13 +472,13 @@ static void run_nghttp2_hd(void) {
return; return;
} }
rv = nghttp2_hd_deflate_init(&deflater); rv = nghttp2_hd_deflate_init(&deflater, nghttp2_mem_fm());
if (rv != 0) { if (rv != 0) {
goto deflate_init_fail; goto deflate_init_fail;
} }
rv = nghttp2_hd_inflate_init(&inflater); rv = nghttp2_hd_inflate_init(&inflater, nghttp2_mem_fm());
if (rv != 0) { if (rv != 0) {
goto inflate_init_fail; goto inflate_init_fail;

View File

@ -24,32 +24,42 @@
*/ */
#include "malloc_wrapper.h" #include "malloc_wrapper.h"
#define __USE_GNU
#include <dlfcn.h>
int nghttp2_failmalloc = 0; int nghttp2_failmalloc = 0;
int nghttp2_failstart = 0; int nghttp2_failstart = 0;
int nghttp2_countmalloc = 1; int nghttp2_countmalloc = 1;
int nghttp2_nmalloc = 0; int nghttp2_nmalloc = 0;
static void *(*real_malloc)(size_t) = NULL; #define CHECK_PREREQ \
do { \
if (nghttp2_failmalloc && nghttp2_nmalloc >= nghttp2_failstart) { \
return NULL; \
} \
if (nghttp2_countmalloc) { \
++nghttp2_nmalloc; \
} \
} while (0)
static void init(void) { real_malloc = dlsym(RTLD_NEXT, "malloc"); } static void *my_malloc(size_t size, void *mud _U_) {
CHECK_PREREQ;
return malloc(size);
}
void *malloc(size_t size) { static void my_free(void *ptr, void *mud _U_) { free(ptr); }
if (real_malloc == NULL) {
init(); static void *my_calloc(size_t nmemb, size_t size, void *mud _U_) {
} CHECK_PREREQ;
if (nghttp2_failmalloc && nghttp2_nmalloc >= nghttp2_failstart) { return calloc(nmemb, size);
return NULL;
} else {
if (nghttp2_countmalloc) {
++nghttp2_nmalloc;
}
return real_malloc(size);
} }
static void *my_realloc(void *ptr, size_t size, void *mud _U_) {
CHECK_PREREQ;
return realloc(ptr, size);
} }
static nghttp2_mem mem = {NULL, my_malloc, my_free, my_calloc, my_realloc};
nghttp2_mem *nghttp2_mem_fm(void) { return &mem; }
static int failmalloc_bk, countmalloc_bk; static int failmalloc_bk, countmalloc_bk;
void nghttp2_failmalloc_pause(void) { void nghttp2_failmalloc_pause(void) {

View File

@ -27,6 +27,8 @@
#include <stdlib.h> #include <stdlib.h>
#include "nghttp2_mem.h"
/* Global variables to control the behavior of malloc() */ /* Global variables to control the behavior of malloc() */
/* If nonzero, malloc failure mode is on */ /* If nonzero, malloc failure mode is on */
@ -40,7 +42,10 @@ extern int nghttp2_countmalloc;
incremented if nghttp2_nmalloc is nonzero. */ incremented if nghttp2_nmalloc is nonzero. */
extern int nghttp2_nmalloc; extern int nghttp2_nmalloc;
void *malloc(size_t size); /* Returns pointer to nghttp2_mem, which, when dereferenced, contains
specifically instrumented memory allocators for failmalloc
tests. */
nghttp2_mem *nghttp2_mem_fm(void);
/* Copies nghttp2_failmalloc and nghttp2_countmalloc to statically /* Copies nghttp2_failmalloc and nghttp2_countmalloc to statically
allocated space and sets 0 to them. This will effectively make allocated space and sets 0 to them. This will effectively make