atom: add a mock for struct atom

And define a macro specifically for fuzzer so we know when to build the
mock.

Signed-off-by: Yuxuan Shui <yshuiv7@gmail.com>
This commit is contained in:
Yuxuan Shui 2024-02-18 18:25:21 +00:00
parent ae88e83aa1
commit 8f8d42f0c6
No known key found for this signature in database
GPG Key ID: D3A4405BE6CC17F4
4 changed files with 40 additions and 2 deletions

View File

@ -134,3 +134,29 @@ void destroy_atoms(struct atom *a) {
assert(atoms->atom_to_name == NULL);
free(a);
}
#if defined(UNIT_TEST) || defined(CONFIG_FUZZER)
static inline int mock_atom_getter(struct cache *cache, const char *atom_name attr_unused,
size_t atom_len attr_unused, struct cache_handle **value,
void *user_data attr_unused) {
auto atoms = container_of(cache, struct atom_impl, c);
xcb_atom_t atom = (xcb_atom_t)HASH_COUNT(atoms->atom_to_name) + 1;
struct atom_entry *entry = ccalloc(1, struct atom_entry);
entry->atom = atom;
HASH_ADD_INT(atoms->atom_to_name, atom, entry);
*value = &entry->entry;
return 0;
}
struct atom *init_mock_atoms(void) {
return init_atoms_impl(NULL, mock_atom_getter);
}
#else
struct atom *init_mock_atoms(void) {
abort();
}
#endif

View File

@ -74,3 +74,9 @@ const char *get_atom_name(struct atom *a, xcb_atom_t, xcb_connection_t *c);
const char *get_atom_name_cached(struct atom *a, xcb_atom_t atom);
void destroy_atoms(struct atom *a);
/// A mock atom object for unit testing. Successive calls to get_atom will return
/// secutive integers as atoms, starting from 1. Calling get_atom_name with atoms
/// previously seen will result in the string that was used to create the atom; if
/// the atom was never returned by get_atom, it will abort.
struct atom *init_mock_atoms(void);

View File

@ -98,7 +98,7 @@ endif
if cc.has_argument('-fsanitize=fuzzer')
c2_fuzz = executable('c2_fuzz', srcs + ['fuzzer/c2.c'],
c_args: cflags + ['-fsanitize=fuzzer', '-Dmain=__main__'],
c_args: cflags + ['-fsanitize=fuzzer', '-DCONFIG_FUZZER'],
link_args: ['-fsanitize=fuzzer'],
dependencies: [ base_deps, deps, test_h_dep ],
build_by_default: false,

View File

@ -2791,10 +2791,16 @@ static void session_run(session_t *ps) {
ev_run(ps->loop, 0);
}
#ifdef CONFIG_FUZZER
#define PICOM_MAIN(...) no_main(__VA_ARGS__)
#else
#define PICOM_MAIN(...) main(__VA_ARGS__)
#endif
/**
* The function that everybody knows.
*/
int main(int argc, char **argv) {
int PICOM_MAIN(int argc, char **argv) {
// Set locale so window names with special characters are interpreted
// correctly
setlocale(LC_ALL, "");