libtpms/tests/fuzz.cc
Marc-André Lureau b214dabfc8 fuzz: better oss-fuzz integration
There were a few issues with the oss-fuzz integration from commit
8373f09854 ("build-sys: add oss-fuzz
support").

When building on OSS-Fuzz, the projects should use the provided CFLAGS
and CXXFLAGS and don't append any extra sanitization / fuzzing flags.

$LIB_FUZZING_ENGINE is defined to set the library to link to, and it
is a c++ library, so we should build fuzzer with c++...

Now --enable-fuzzer is only used for -fsanitize=fuzzer.

Add a tests/fuzz-main.c as fallback, to run the corpus on other builds.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2019-04-01 11:22:03 -04:00

42 lines
1.1 KiB
C++

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <libtpms/tpm_types.h>
#include <libtpms/tpm_library.h>
#include <libtpms/tpm_error.h>
#include <libtpms/tpm_memory.h>
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
unsigned char *rbuffer = NULL;
uint32_t rlength;
uint32_t rtotal = 0;
TPM_RESULT res;
unsigned char startup[] = {
0x80, 0x01, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x01, 0x44, 0x00, 0x00
};
res = TPMLIB_ChooseTPMVersion(TPMLIB_TPM_VERSION_2);
assert(res == TPM_SUCCESS);
res = TPMLIB_MainInit();
if (res != TPM_SUCCESS)
fprintf(stderr, "Error: TPMLIB_MainInit() failed\n");
res = TPMLIB_Process(&rbuffer, &rlength, &rtotal, startup, sizeof(startup));
if (res != TPM_SUCCESS)
fprintf(stderr, "Error: TPMLIB_Process(Startup) failed\n");
res = TPMLIB_Process(&rbuffer, &rlength, &rtotal, (unsigned char*)data, size);
if (res != TPM_SUCCESS)
fprintf(stderr, "Error: TPMLIB_Process(fuzz-command) failed\n");
TPMLIB_Terminate();
TPM_Free(rbuffer);
return 0;
}