Skip to content

Commit

Permalink
Remove fsm.opt and split print hooks from options.
Browse files Browse the repository at this point in the history
The important part of the diff here is just:
```diff
--- a/src/libfsm/internal.h
+++ b/src/libfsm/internal.h
@@ -11,7 +11,6 @@
 #include <stdlib.h>

 #include <fsm/fsm.h>
-#include <fsm/options.h>

 #include <adt/common.h>

@@ -76,7 +75,6 @@ struct fsm {

        struct fsm_capture_info *capture_info;
        struct endid_info *endid_info;
-       const struct fsm_options *opt;
 };
```

Everything else is fallout from not needing to pass around the options struct. This leaves the options struct only used for the print routines.
  • Loading branch information
katef committed Jul 22, 2024
1 parent a7d6847 commit 4b2ab25
Show file tree
Hide file tree
Showing 122 changed files with 1,424 additions and 1,306 deletions.
78 changes: 42 additions & 36 deletions fuzz/target.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ scanner_next(void *opaque)
}
#endif

static const struct fsm_options opt;

static struct fsm *
build(const char *pattern)
{
Expand All @@ -95,7 +93,7 @@ build(const char *pattern)
};

time_get(&pre);
fsm = re_comp(RE_PCRE, scanner_next, &s, NULL, &opt, RE_MULTI, &err);
fsm = re_comp(RE_PCRE, scanner_next, &s, NULL, RE_MULTI, &err);
time_get(&post);
delta_usec = time_diff_usec(&pre, &post);
total_usec += delta_usec;
Expand Down Expand Up @@ -133,24 +131,28 @@ build(const char *pattern)
}

static int
codegen(const struct fsm *fsm)
codegen(const struct fsm *fsm, enum fsm_io io_mode)
{
const struct fsm_options opt = {
.io = io_mode,
};

FILE *dev_null = fopen("/dev/null", "w");
assert(dev_null != NULL);
fsm_print(dev_null, fsm, FSM_PRINT_C);
fsm_print(dev_null, fsm, &opt, NULL, FSM_PRINT_C);
fclose(dev_null);
return 1;
}

static int
build_and_codegen(const char *pattern)
build_and_codegen(const char *pattern, enum fsm_io io_mode)
{
struct fsm *fsm = build(pattern);
if (fsm == NULL) {
return EXIT_SUCCESS;
}

if (!codegen(fsm)) {
if (!codegen(fsm, io_mode)) {
return EXIT_SUCCESS;
}

Expand All @@ -176,7 +178,7 @@ shuffle_minimise(const char *pattern)
.offset = 0
};

fsm = re_comp(RE_PCRE, scanner_next, &s, NULL, &opt, RE_MULTI, &err);
fsm = re_comp(RE_PCRE, scanner_next, &s, NULL, RE_MULTI, &err);

if (fsm == NULL) {
/* ignore invalid regexp syntax, etc. */
Expand Down Expand Up @@ -245,14 +247,14 @@ shuffle_minimise(const char *pattern)
__func__, s_i, pattern, expected_state_count, cp_state_count);

fprintf(stderr, "== original input:\n");
fsm_print(stderr, fsm, FSM_PRINT_FSM);
fsm_print(stderr, fsm, NULL, NULL, FSM_PRINT_FSM);


fprintf(stderr, "== expected:\n");
fsm_print(stderr, oracle_min, FSM_PRINT_FSM);
fsm_print(stderr, oracle_min, NULL, NULL, FSM_PRINT_FSM);

fprintf(stderr, "== got:\n");
fsm_print(stderr, cp, FSM_PRINT_FSM);
fsm_print(stderr, cp, NULL, NULL, FSM_PRINT_FSM);

fsm_free(cp);
fsm_free(oracle_min);
Expand Down Expand Up @@ -287,7 +289,7 @@ fuzz_all_print_functions(FILE *f, const char *pattern, bool det, bool min, const
.io = io_mode,
};

fsm = re_comp(RE_PCRE, scanner_next, &s, NULL, &opt, RE_MULTI, &err);
fsm = re_comp(RE_PCRE, scanner_next, &s, NULL, RE_MULTI, &err);
if (fsm == NULL) {
/* ignore invalid regexp syntax, etc. */
return EXIT_SUCCESS;
Expand All @@ -313,28 +315,28 @@ fuzz_all_print_functions(FILE *f, const char *pattern, bool det, bool min, const
/* see if this triggers any asserts */
int r = 0;

r |= fsm_print(f, fsm, FSM_PRINT_AMD64_ATT);
r |= fsm_print(f, fsm, FSM_PRINT_AMD64_GO);
r |= fsm_print(f, fsm, FSM_PRINT_AMD64_NASM);

r |= fsm_print(f, fsm, FSM_PRINT_API);
r |= fsm_print(f, fsm, FSM_PRINT_AWK);
r |= fsm_print(f, fsm, FSM_PRINT_C);
r |= fsm_print(f, fsm, FSM_PRINT_DOT);
r |= fsm_print(f, fsm, FSM_PRINT_FSM);
r |= fsm_print(f, fsm, FSM_PRINT_GO);
r |= fsm_print(f, fsm, FSM_PRINT_IR);
r |= fsm_print(f, fsm, FSM_PRINT_IRJSON);
r |= fsm_print(f, fsm, FSM_PRINT_JSON);
r |= fsm_print(f, fsm, FSM_PRINT_LLVM);
r |= fsm_print(f, fsm, FSM_PRINT_RUST);
r |= fsm_print(f, fsm, FSM_PRINT_SH);
r |= fsm_print(f, fsm, FSM_PRINT_VMC);
r |= fsm_print(f, fsm, FSM_PRINT_VMDOT);

r |= fsm_print(f, fsm, FSM_PRINT_VMOPS_C);
r |= fsm_print(f, fsm, FSM_PRINT_VMOPS_H);
r |= fsm_print(f, fsm, FSM_PRINT_VMOPS_MAIN);
r |= fsm_print(f, fsm, &opt, NULL, FSM_PRINT_AMD64_ATT);
r |= fsm_print(f, fsm, &opt, NULL, FSM_PRINT_AMD64_GO);
r |= fsm_print(f, fsm, &opt, NULL, FSM_PRINT_AMD64_NASM);

r |= fsm_print(f, fsm, &opt, NULL, FSM_PRINT_API);
r |= fsm_print(f, fsm, &opt, NULL, FSM_PRINT_AWK);
r |= fsm_print(f, fsm, &opt, NULL, FSM_PRINT_C);
r |= fsm_print(f, fsm, &opt, NULL, FSM_PRINT_DOT);
r |= fsm_print(f, fsm, &opt, NULL, FSM_PRINT_FSM);
r |= fsm_print(f, fsm, &opt, NULL, FSM_PRINT_GO);
r |= fsm_print(f, fsm, &opt, NULL, FSM_PRINT_IR);
r |= fsm_print(f, fsm, &opt, NULL, FSM_PRINT_IRJSON);
r |= fsm_print(f, fsm, &opt, NULL, FSM_PRINT_JSON);
r |= fsm_print(f, fsm, &opt, NULL, FSM_PRINT_LLVM);
r |= fsm_print(f, fsm, &opt, NULL, FSM_PRINT_RUST);
r |= fsm_print(f, fsm, &opt, NULL, FSM_PRINT_SH);
r |= fsm_print(f, fsm, &opt, NULL, FSM_PRINT_VMC);
r |= fsm_print(f, fsm, &opt, NULL, FSM_PRINT_VMDOT);

r |= fsm_print(f, fsm, &opt, NULL, FSM_PRINT_VMOPS_C);
r |= fsm_print(f, fsm, &opt, NULL, FSM_PRINT_VMOPS_H);
r |= fsm_print(f, fsm, &opt, NULL, FSM_PRINT_VMOPS_MAIN);

assert(r == 0 || errno != 0);

Expand Down Expand Up @@ -380,8 +382,12 @@ harness_fuzzer_target(const uint8_t *data, size_t size)
const char *pattern = (const char *)data_buf;

switch (get_run_mode()) {
case MODE_DEFAULT:
return build_and_codegen(pattern);
case MODE_DEFAULT: {
const uint8_t b0 = data_buf[0];
const enum fsm_io io_mode = (b0 >> 2) % 3;

return build_and_codegen(pattern, io_mode);
}

case MODE_SHUFFLE_MINIMISE:
return shuffle_minimise(pattern);
Expand Down
15 changes: 2 additions & 13 deletions include/fsm/fsm.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

struct fsm;
struct fsm_alloc;
struct fsm_options;
struct path; /* XXX */
struct fsm_capture;
struct fsm_combine_info;
Expand All @@ -35,14 +34,12 @@ typedef unsigned int fsm_end_id_t;
* from fsm_new() is expected to be passed as the "fsm" argument to the
* functions in this API.
*
* An options pointer may be passed for control over various details of
* FSM construction and output. This may be NULL, in which case default
* options are used.
* An fsm_alloc pointer may be passed for callbacks on memory allocation.
* This may be NULL, in which case the default libc functions are used.
* When non-NULL, the storage pointed to must remain extant until fsm_free().
*
* Returns NULL on error; see errno.
* TODO: perhaps automatically create a start state, and never have an empty FSM
* TODO: also fsm_parse should create an FSM, not add into an existing one
*/
struct fsm *
fsm_new(const struct fsm_alloc *alloc);
Expand All @@ -69,14 +66,6 @@ fsm_free(struct fsm *fsm);
struct fsm *
fsm_clone(const struct fsm *fsm);

/* Returns the options of an FSM */
const struct fsm_options *
fsm_getoptions(const struct fsm *fsm);

/* Sets the options of an FSM */
void
fsm_setoptions(struct fsm *fsm, const struct fsm_options *opts);

/*
* Copy the contents of src over dst, and free src.
*
Expand Down
41 changes: 4 additions & 37 deletions include/fsm/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@
#ifndef FSM_OPTIONS_H
#define FSM_OPTIONS_H

#include <stdio.h>

struct fsm;
struct fsm_state;

enum fsm_io {
FSM_IO_GETC,
FSM_IO_STR,
Expand All @@ -28,6 +23,10 @@ enum fsm_ambig {
AMBIG_SINGLE = AMBIG_ERROR | AMBIG_EARLIEST
};

/*
* Print options.
* This is separate to <fsm/print.h> because we also borrow it for libre.
*/
struct fsm_options {
/* boolean: true indicates to omit names for states in output */
unsigned int anonymous_states:1;
Expand Down Expand Up @@ -73,38 +72,6 @@ struct fsm_options {

/* the name of the enclosing package; NULL to use `prefix` (default). */
const char *package_prefix;

/*
* Hooks to override generated code. These give an oportunity to
* emit application-specific constructs, especially based on ids
* attached to end states.
*
* These hooks are optional, and default to whatever makes sense
* for the language.
*
* Placement in the output stream depends on the format.
* This replaces an entire "return xyz;" statement for C-like formats,
* but appends extra information for others.
*/
struct fsm_hooks {
/* character pointer, for C code fragment output. NULL for the default. */
const char *cp;

int (*args)(FILE *, const struct fsm_options *opt,
void *leaf_opaque);

/*
* ids[] is sorted and does not have duplicates.
*/
int (*accept)(FILE *, const struct fsm_options *opt,
const fsm_end_id_t *ids, size_t count,
void *leaf_opaque);

int (*reject)(FILE *, const struct fsm_options *opt,
void *leaf_opaque);

void *hook_opaque;
} hooks;
};

#endif
Expand Down
3 changes: 1 addition & 2 deletions include/fsm/parser.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 37 additions & 3 deletions include/fsm/print.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
#ifndef FSM_PRINT_H
#define FSM_PRINT_H

#include <stdio.h>

struct fsm;
struct fsm_options;

enum fsm_print_lang {
FSM_PRINT_NONE, /* No output */
Expand Down Expand Up @@ -38,6 +37,38 @@ enum fsm_print_lang {
FSM_PRINT_VMOPS_MAIN
};

/*
* Hooks to override generated code. These give an oportunity to
* emit application-specific constructs, especially based on ids
* attached to end states.
*
* These hooks are optional, and default to whatever makes sense
* for the language.
*
* Placement in the output stream depends on the format.
* This replaces an entire "return xyz;" statement for C-like formats,
* but appends extra information for others.
*/
struct fsm_hooks {
/* character pointer, for C code fragment output. NULL for the default. */
const char *cp;

int (*args)(FILE *, const struct fsm_options *opt,
void *leaf_opaque, void *hook_opaque);

/*
* ids[] is sorted and does not have duplicates.
*/
int (*accept)(FILE *, const struct fsm_options *opt,
const fsm_end_id_t *ids, size_t count,
void *leaf_opaque, void *hook_opaque);

int (*reject)(FILE *, const struct fsm_options *opt,
void *leaf_opaque, void *hook_opaque);

void *hook_opaque;
};

/*
* Print an FSM to the given file stream. The output is written in the format
* specified.
Expand All @@ -47,7 +78,10 @@ enum fsm_print_lang {
* Returns 0, or -1 on error and errno will be set. An errno of ENOTSUP means
* the requested IO API is not implemented for this output language.
*/
int fsm_print(FILE *f, const struct fsm *fsm, enum fsm_print_lang lang);
int fsm_print(FILE *f, const struct fsm *fsm,
const struct fsm_options *opt,
const struct fsm_hooks *hooks,
enum fsm_print_lang lang);

#endif

4 changes: 3 additions & 1 deletion include/fsm/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <stddef.h>

struct fsm;
struct fsm_options;
struct fsm_dfavm;

enum fsm_vm_compile_flags {
Expand Down Expand Up @@ -34,7 +35,8 @@ struct fsm_dfavm *
fsm_vm_compile(const struct fsm *fsm);

struct fsm_dfavm *
fsm_vm_compile_with_options(const struct fsm *fsm, struct fsm_vm_compile_opts opts);
fsm_vm_compile_with_options(const struct fsm *fsm,
const struct fsm_options *opt, struct fsm_vm_compile_opts opts);

struct fsm_dfavm *
fsm_vm_read(FILE *f);
Expand Down
2 changes: 0 additions & 2 deletions include/re/literal.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#ifndef RE_LITERAL_H
#define RE_LITERAL_H

struct fsm_options;
struct re_err;

enum re_literal_category {
Expand All @@ -20,7 +19,6 @@ enum re_literal_category {

int
re_is_literal(enum re_dialect dialect, int (*getc)(void *opaque), void *opaque,
const struct fsm_options *opt,
enum re_flags flags, struct re_err *err,
enum re_literal_category *category, char **s, size_t *n);

Expand Down
2 changes: 0 additions & 2 deletions include/re/re.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

struct fsm;
struct fsm_alloc;
struct fsm_options;

enum re_dialect {
RE_LIKE,
Expand Down Expand Up @@ -135,7 +134,6 @@ struct fsm *
re_comp(enum re_dialect dialect,
re_getchar_fun *f, void *opaque,
const struct fsm_alloc *alloc,
const struct fsm_options *opt,
enum re_flags flags, struct re_err *err);

/*
Expand Down
Loading

0 comments on commit 4b2ab25

Please sign in to comment.