Skip to content

Commit

Permalink
test: autogenerate tests from fuzzer inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
Gottox committed Jun 2, 2024
1 parent 0370751 commit 1a0acff
Show file tree
Hide file tree
Showing 52 changed files with 269 additions and 1,515 deletions.
Binary file added fuzzer/inputs/extract.fuzz_crash_1
Binary file not shown.
Binary file added fuzzer/inputs/extract.fuzz_crash_10
Binary file not shown.
Binary file added fuzzer/inputs/extract.fuzz_crash_11
Binary file not shown.
Binary file added fuzzer/inputs/extract.fuzz_crash_12
Binary file not shown.
Binary file added fuzzer/inputs/extract.fuzz_crash_13
Binary file not shown.
Binary file added fuzzer/inputs/extract.fuzz_crash_14
Binary file not shown.
Binary file added fuzzer/inputs/extract.fuzz_crash_15
Binary file not shown.
Binary file added fuzzer/inputs/extract.fuzz_crash_16
Binary file not shown.
Binary file added fuzzer/inputs/extract.fuzz_crash_17
Binary file not shown.
Binary file added fuzzer/inputs/extract.fuzz_crash_18
Binary file not shown.
Binary file added fuzzer/inputs/extract.fuzz_crash_19
Binary file not shown.
Binary file added fuzzer/inputs/extract.fuzz_crash_2
Binary file not shown.
Binary file added fuzzer/inputs/extract.fuzz_crash_20
Binary file not shown.
Binary file added fuzzer/inputs/extract.fuzz_crash_21
Binary file not shown.
Binary file added fuzzer/inputs/extract.fuzz_crash_3
Binary file not shown.
Binary file added fuzzer/inputs/extract.fuzz_crash_4
Binary file not shown.
Binary file added fuzzer/inputs/extract.fuzz_crash_5
Binary file not shown.
Binary file added fuzzer/inputs/extract.fuzz_crash_6
Binary file not shown.
Binary file added fuzzer/inputs/extract.fuzz_crash_7
Binary file not shown.
Binary file added fuzzer/inputs/extract.fuzz_crash_8
Binary file not shown.
Binary file added fuzzer/inputs/extract.fuzz_crash_9
Binary file not shown.
Binary file added fuzzer/inputs/extract.fuzz_leak_1
Binary file not shown.
Binary file added fuzzer/inputs/extract.fuzz_oom_1
Binary file not shown.
Binary file added test/fuzzer/fuzz_crash_1
Binary file not shown.
Binary file added test/fuzzer/fuzz_crash_10
Binary file not shown.
Binary file added test/fuzzer/fuzz_crash_11
Binary file not shown.
Binary file added test/fuzzer/fuzz_crash_12
Binary file not shown.
Binary file added test/fuzzer/fuzz_crash_13
Binary file not shown.
Binary file added test/fuzzer/fuzz_crash_14
Binary file not shown.
Binary file added test/fuzzer/fuzz_crash_15
Binary file not shown.
Binary file added test/fuzzer/fuzz_crash_16
Binary file not shown.
Binary file added test/fuzzer/fuzz_crash_17
Binary file not shown.
Binary file added test/fuzzer/fuzz_crash_18
Binary file not shown.
Binary file added test/fuzzer/fuzz_crash_19
Binary file not shown.
Binary file added test/fuzzer/fuzz_crash_2
Binary file not shown.
Binary file added test/fuzzer/fuzz_crash_20
Binary file not shown.
Binary file added test/fuzzer/fuzz_crash_21
Binary file not shown.
Binary file added test/fuzzer/fuzz_crash_22
Binary file not shown.
Binary file added test/fuzzer/fuzz_crash_3
Binary file not shown.
Binary file added test/fuzzer/fuzz_crash_4
Binary file not shown.
Binary file added test/fuzzer/fuzz_crash_5
Binary file not shown.
Binary file added test/fuzzer/fuzz_crash_6
Binary file not shown.
Binary file added test/fuzzer/fuzz_crash_7
Binary file not shown.
Binary file added test/fuzzer/fuzz_crash_8
Binary file not shown.
Binary file added test/fuzzer/fuzz_crash_9
Binary file not shown.
Binary file added test/fuzzer/fuzz_leak_1
Binary file not shown.
59 changes: 59 additions & 0 deletions test/fuzzer/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
fuzzer_repro = files(
'fuzz_crash_1',
'fuzz_crash_10',
'fuzz_crash_11',
'fuzz_crash_12',
'fuzz_crash_13',
'fuzz_crash_14',
'fuzz_crash_15',
'fuzz_crash_16',
'fuzz_crash_17',
'fuzz_crash_18',
'fuzz_crash_19',
'fuzz_crash_2',
'fuzz_crash_20',
'fuzz_crash_21',
'fuzz_crash_22',
'fuzz_crash_3',
'fuzz_crash_4',
'fuzz_crash_5',
'fuzz_crash_6',
'fuzz_crash_7',
'fuzz_crash_8',
'fuzz_crash_9',
'fuzz_leak_1',
)

utest_dep = dependency('utest')

fuzzer_repro_codegen = executable('repro_codegen', 'repro_codegen.c')

fuzzer_repro_tgt = custom_target(
'repro_test',
output: 'repro_test.c',
capture: true,
input: fuzzer_repro,
command: [
fuzzer_repro_codegen,
'@INPUT@',
],
)

fuzzer_src = files(
'../../fuzzer/simple.c',
)
fuzzer_repro_test = executable(
'repro_test',
[fuzzer_repro_tgt, fuzzer_src],
include_directories: [
libsqsh_include,
libsqsh_common_include,
libsqsh_private_include,
libmksqsh_include,
],
link_with: [libsqsh.get_static_lib(), libmksqsh.get_static_lib()],
dependencies: [threads_dep, utest_dep, cextras_dep],

)

test('simple_fuzzer_repro', fuzzer_repro_test)
41 changes: 41 additions & 0 deletions test/fuzzer/repro_codegen.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <assert.h>
#include <libgen.h>
#include <stdio.h>

#define HEADER \
"#include <sqsh.h>\n" \
"#include <utest.h>\n" \
"int LLVMFuzzerTestOneInput(char *data, size_t size);"

#define TEST_PRE_FORMAT \
"UTEST(fuzzer_repro, %s) {\n" \
" (void)utest_result;\n" \
" char input[] = {\n"

#define TEST_POST \
"\n" \
" };\n" \
" LLVMFuzzerTestOneInput(input, sizeof(input));\n" \
"}\n"

#define FOOTER "UTEST_MAIN()"

int
main(int argc, char *argv[]) {
puts("#include <utest.h>\n"
"");
puts(HEADER);
for (int i = 1; i < argc; i++) {
printf(TEST_PRE_FORMAT, basename(argv[i]));
FILE *f = fopen(argv[i], "rb");
assert(f);
int c;
while ((c = fgetc(f)) != EOF) {
printf("0x%02x, ", c);
}
fclose(f);
puts(TEST_POST);
}
puts(FOOTER);
return 0;
}
166 changes: 166 additions & 0 deletions test/libsqsh/archive/archive.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
* BSD 2-Clause License
*
* Copyright (c) 2023, Enno Boland
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

/**
* @author Enno Boland ([email protected])
* @file file.c
*/

#include "../common.h"
#include <utest.h>

#include <sqsh_archive_private.h>
#include <sqsh_common_private.h>
#include <sqsh_data_private.h>
#include <sqsh_file_private.h>

UTEST(file, load_file) {
int rv;
struct SqshArchive archive = {0};
struct SqshFile file = {0};
uint8_t payload[8192] = {
SQSH_HEADER,
/* inode */
[INODE_TABLE_OFFSET + 15] = METABLOCK_HEADER(0, 128),
0,
0,
0,
INODE_HEADER(2, 0666, 0, 0, 4242, 1),
INODE_BASIC_FILE(1024, 0xFFFFFFFF, 0, 1),
UINT32_BYTES(42),

};
mk_stub(&archive, payload, sizeof(payload));

uint64_t inode_ref = sqsh_address_ref_create(15, 3);
rv = sqsh__file_init(&file, &archive, inode_ref);
ASSERT_EQ(0, rv);

ASSERT_EQ(SQSH_FILE_TYPE_FILE, sqsh_file_type(&file));
ASSERT_EQ(0666, sqsh_file_permission(&file));
ASSERT_EQ((uint32_t)4242, sqsh_file_modified_time(&file));
ASSERT_EQ((uint32_t)1024, sqsh_file_blocks_start(&file));
ASSERT_EQ(false, sqsh_file_has_fragment(&file));

ASSERT_EQ((uint32_t)1, sqsh_file_block_count(&file));
ASSERT_EQ((uint32_t)42, sqsh_file_block_size(&file, 0));

sqsh__file_cleanup(&file);
sqsh__archive_cleanup(&archive);
}

UTEST(file, resolve_file) {
int rv = 0;
struct SqshArchive archive = {0};
uint8_t payload[] = {
/* clang-format off */
SQSH_HEADER,
[1024] = '1', '2', '3', '4', '5', '6', '7', '8',
/* inode */
[INODE_TABLE_OFFSET] = METABLOCK_HEADER(0, 1024),
INODE_HEADER(1, 0, 0, 0, 0, 1),
INODE_BASIC_DIR(0, 1024, 0, 0),
[INODE_TABLE_OFFSET+2+128] =
INODE_HEADER(3, 0, 0, 0, 0, 2),
INODE_BASIC_SYMLINK(3),
't', 'g', 't',
[INODE_TABLE_OFFSET+2+256] =
INODE_HEADER(2, 0, 0, 0, 0, 3),
INODE_BASIC_FILE(1024, 0xFFFFFFFF, 0, 8),
DATA_BLOCK_REF(8, 0),
[DIRECTORY_TABLE_OFFSET] = METABLOCK_HEADER(0, 128),
DIRECTORY_HEADER(2, 0, 0),
DIRECTORY_ENTRY(128, 2, 3, 3),
's', 'r', 'c',
DIRECTORY_ENTRY(256, 3, 2, 3),
't', 'g', 't',
[FRAGMENT_TABLE_OFFSET] = 0,
/* clang-format on */
};
mk_stub(&archive, payload, sizeof(payload));

struct SqshFile *symlink = sqsh_lopen(&archive, "/src", &rv);
ASSERT_EQ(0, rv);
ASSERT_EQ(SQSH_FILE_TYPE_SYMLINK, sqsh_file_type(symlink));

rv = sqsh_file_symlink_resolve(symlink);
ASSERT_EQ(0, rv);
ASSERT_EQ(SQSH_FILE_TYPE_FILE, sqsh_file_type(symlink));
ASSERT_EQ((uint32_t)3, sqsh_file_inode(symlink));

sqsh_close(symlink);

sqsh__archive_cleanup(&archive);
}

UTEST(file, resolve_unkown_dir_inode) {
int rv = 0;
struct SqshArchive archive = {0};
uint8_t payload[] = {
/* clang-format off */
SQSH_HEADER,
[1024] = '1', '2', '3', '4', '5', '6', '7', '8',
/* inode */
[INODE_TABLE_OFFSET] = METABLOCK_HEADER(0, 1024),
INODE_HEADER(1, 0, 0, 0, 0, 1),
INODE_BASIC_DIR(0, 1024, 0, 0),
[INODE_TABLE_OFFSET+2+128] =
INODE_HEADER(3, 0, 0, 0, 0, 2),
INODE_BASIC_SYMLINK(3),
't', 'g', 't',
[INODE_TABLE_OFFSET+2+256] =
INODE_HEADER(2, 0, 0, 0, 0, 3),
INODE_BASIC_FILE(1024, 0xFFFFFFFF, 0, 8),
DATA_BLOCK_REF(8, 0),
[DIRECTORY_TABLE_OFFSET] = METABLOCK_HEADER(0, 128),
DIRECTORY_HEADER(2, 0, 0),
DIRECTORY_ENTRY(128, 2, 3, 3),
's', 'r', 'c',
DIRECTORY_ENTRY(256, 3, 2, 3),
't', 'g', 't',
[FRAGMENT_TABLE_OFFSET] = 0,
/* clang-format on */
};
mk_stub(&archive, payload, sizeof(payload));

struct SqshFile *symlink = sqsh_lopen(&archive, "/src", &rv);
ASSERT_EQ(0, rv);
ASSERT_EQ(SQSH_FILE_TYPE_SYMLINK, sqsh_file_type(symlink));
symlink->dir_inode = 0;
symlink->has_dir_inode = false;

rv = sqsh_file_symlink_resolve(symlink);
ASSERT_EQ(-SQSH_ERROR_INODE_PARENT_UNSET, rv);

sqsh_close(symlink);

sqsh__archive_cleanup(&archive);
}

UTEST_MAIN()
Loading

0 comments on commit 1a0acff

Please sign in to comment.