From 35ad8323928ebdf07fad38bed22e65f099dfae02 Mon Sep 17 00:00:00 2001 From: Michael Rash Date: Sat, 24 May 2014 10:14:28 -0400 Subject: [PATCH] [libfko] started on libfiu fault injection code --- lib/fko_common.h | 4 +++ lib/fko_funcs.c | 9 ++++++ test/fko-wrapper/fko_fault_injection.c | 43 ++++++++++++++------------ 3 files changed, 37 insertions(+), 19 deletions(-) diff --git a/lib/fko_common.h b/lib/fko_common.h index d2753981..c7ad5d5a 100644 --- a/lib/fko_common.h +++ b/lib/fko_common.h @@ -35,6 +35,10 @@ #include "config.h" #endif +#if HAVE_LIBFIU + #include +#endif + #include #include diff --git a/lib/fko_funcs.c b/lib/fko_funcs.c index e009540a..640760eb 100644 --- a/lib/fko_funcs.c +++ b/lib/fko_funcs.c @@ -43,6 +43,10 @@ fko_new(fko_ctx_t *r_ctx) int res; char *ver; +#if HAVE_LIBFIU + fiu_return_on("fko_new_calloc", FKO_ERROR_MEMORY_ALLOCATION); +#endif + ctx = calloc(1, sizeof *ctx); if(ctx == NULL) return(FKO_ERROR_MEMORY_ALLOCATION); @@ -59,6 +63,11 @@ fko_new(fko_ctx_t *r_ctx) /* Set the version string. */ ctx->initval = FKO_CTX_INITIALIZED; + +#if HAVE_LIBFIU + fiu_return_on("fko_new_strdup", FKO_ERROR_MEMORY_ALLOCATION); +#endif + ver = strdup(FKO_PROTOCOL_VERSION); ctx->initval = 0; if(ver == NULL) diff --git a/test/fko-wrapper/fko_fault_injection.c b/test/fko-wrapper/fko_fault_injection.c index ca713fa1..49faf5b5 100644 --- a/test/fko-wrapper/fko_fault_injection.c +++ b/test/fko-wrapper/fko_fault_injection.c @@ -4,31 +4,36 @@ #include #include "fko.h" +const char *fiu_tags[] = { + "fko_new_calloc", + "fko_new_strdup" +}; +const int fiu_rvs[] = { + FKO_ERROR_MEMORY_ALLOCATION, + FKO_ERROR_MEMORY_ALLOCATION +}; +const int num_fiu_tags = 2; + int main(void) { fko_ctx_t ctx = NULL; - int res = 0; + int res = 0, i; fiu_init(0); - if (0) { - fiu_enable("fko_strdup1", FKO_ERROR_MEMORY_ALLOCATION, NULL, 0); - res = fko_new(&ctx); - if (res == FKO_SUCCESS) - printf("[-] fko_new(): %s\n", fko_errstr(res)); - else - printf("[+] fko_new(): %s\n", fko_errstr(res)); - fko_destroy(ctx); - fiu_disable("fko_strdup1"); - } + for (i=0; i < num_fiu_tags; i++) { + printf("[+] libfiu injection tag: %s\n", fiu_tags[i]); - fiu_enable("fko_set_rand_value1", FKO_ERROR_MEMORY_ALLOCATION, NULL, 0); - res = fko_new(&ctx); - if (res == FKO_SUCCESS) - printf("[-] fko_new(): %s\n", fko_errstr(res)); - else - printf("[+] fko_new(): %s\n", fko_errstr(res)); - fko_destroy(ctx); - fiu_disable("fko_set_rand_value1"); + fiu_enable(fiu_tags[i], fiu_rvs[i], NULL, 0); + + res = fko_new(&ctx); + if (res == FKO_SUCCESS) + printf("[-] fko_new(): %s\n", fko_errstr(res)); + else + printf("[+] fko_new(): %s\n", fko_errstr(res)); + fko_destroy(ctx); + + fiu_disable(fiu_tags[i]); + } return 0; }