1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /*********************************************************************** 3 * Copyright (c) 2018, Intel Corporation 4 * 5 * All rights reserved. 6 ***********************************************************************/ 7 #ifdef HAVE_CONFIG_H 8 #include <config.h> 9 #endif 10 11 #include <stdbool.h> 12 #include <stdlib.h> 13 14 #define LOGMODULE test 15 extern "C" { 16 #include "tss2_sys.h" 17 #include "tss2_tcti.h" 18 #include "util/log.h" 19 #include "test.h" 20 #include "test-options.h" 21 #include "context-util.h" 22 #include "tss2-sys/sysapi_util.h" 23 #include "tcti/tcti-fuzzing.h" 24 } 25 26 extern "C" 27 int LLVMFuzzerTestOneInput(const uint8_t * Data,size_t Size)28LLVMFuzzerTestOneInput ( 29 const uint8_t *Data, 30 size_t Size) 31 { 32 int ret; 33 TSS2_SYS_CONTEXT *sapi_context; 34 _TSS2_SYS_CONTEXT_BLOB *ctx = NULL; 35 TSS2_TCTI_FUZZING_CONTEXT *tcti_fuzzing = NULL; 36 37 /* Use the fuzzing tcti */ 38 test_opts_t opts = { 39 .tcti_type = FUZZING_TCTI, 40 .device_file = DEVICE_PATH_DEFAULT, 41 .socket_address = HOSTNAME_DEFAULT, 42 .socket_port = PORT_DEFAULT, 43 }; 44 45 get_test_opts_from_env (&opts); 46 if (sanity_check_test_opts (&opts) != 0) { 47 LOG_ERROR("Checking test options"); 48 exit(1); /* fatal error */ 49 } 50 51 sapi_context = sapi_init_from_opts (&opts); 52 if (sapi_context == NULL) { 53 LOG_ERROR("SAPI context not initialized"); 54 exit(1); /* fatal error */ 55 } 56 57 ctx = syscontext_cast (sapi_context); 58 tcti_fuzzing = tcti_fuzzing_context_cast (ctx->tctiContext); 59 tcti_fuzzing->data = Data; 60 tcti_fuzzing->size = Size; 61 62 ret = test_invoke (sapi_context); 63 64 sapi_teardown_full (sapi_context); 65 66 if (ret) { 67 LOG_ERROR("Test failed"); 68 exit(1); /* fatal error */ 69 } 70 71 return 0; // Non-zero return values are reserved for future use. 72 } 73