Path: blob/main/crypto/krb5/src/tests/fuzzing/fuzz_profile.c
34879 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* tests/fuzzing/fuzz_profile.c */2/*3* Copyright (C) 2024 by Arjun. All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* * Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* * Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in14* the documentation and/or other materials provided with the15* distribution.16*17* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS18* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT19* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS20* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE21* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,22* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES23* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR24* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)25* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,26* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)27* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED28* OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031/*32* Fuzzing harness implementation for profile_parse_file.33*/3435#include "autoconf.h"36#include <prof_int.h>3738void dump_profile(struct profile_node *root, int level);3940#define kMinInputLength 241#define kMaxInputLength 10244243extern int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);4445int46LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)47{48errcode_t ret;49FILE *fp_w, *fp_r;50char file_name[256], *output;51struct profile_node *root;5253if (size < kMinInputLength || size > kMaxInputLength)54return 0;5556snprintf(file_name, sizeof(file_name), "/tmp/libfuzzer.%d", getpid());5758/* Write data into the file. */59fp_w = fopen(file_name, "w");60if (!fp_w)61return 1;62fwrite(data, 1, size, fp_w);63fclose(fp_w);6465/* Provide the file pointer to the parser. */66fp_r = fopen(file_name, "r");67if (!fp_r)68return 1;6970initialize_prof_error_table();7172ret = profile_parse_file(fp_r, &root, NULL);73if (!ret) {74ret = profile_write_tree_to_buffer(root, &output);75if (!ret)76free(output);7778profile_verify_node(root);79profile_free_node(root);80}8182fclose(fp_r);83unlink(file_name);8485return 0;86}878889