Skip to content

Commit

Permalink
Merge pull request #117 from mrc0mmand/more-decoupling
Browse files Browse the repository at this point in the history
Extract all public stuff from `dfuzzer.h` and drop the file completely
  • Loading branch information
evverx authored Jul 4, 2022
2 parents 4ecce6c + ba1bba0 commit a47afe4
Show file tree
Hide file tree
Showing 12 changed files with 643 additions and 797 deletions.
1 change: 0 additions & 1 deletion src/bus.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <stdlib.h>

#include "bus.h"
#include "dfuzzer.h"
#include "log.h"
#include "util.h"

Expand Down
852 changes: 318 additions & 534 deletions src/dfuzzer.c

Large diffs are not rendered by default.

174 changes: 0 additions & 174 deletions src/dfuzzer.h

This file was deleted.

163 changes: 85 additions & 78 deletions src/fuzz.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,29 @@

#include "fuzz.h"
#include "bus.h"
#include "dfuzzer.h"
#include "log.h"
#include "rand.h"
#include "util.h"

extern guint64 df_buf_size;
static guint64 fuzz_buffer_length = MAX_BUFFER_LENGTH;
/** Pointer on D-Bus interface proxy for calling methods. */
static GDBusProxy *df_dproxy;
/** Exceptions counter; if MAX_EXCEPTIONS is reached testing continues
* with a next method */
static char df_except_counter = 0;


/* Module static functions */
static void df_fuzz_write_log(const struct df_dbus_method *method, GVariant *value);
static int df_exec_cmd_check(const char *cmd);
static int df_fuzz_call_method(const struct df_dbus_method *method, GVariant *value);
void df_fuzz_set_buffer_length(const guint64 length)
{
g_assert(length <= MAX_BUFFER_LENGTH);

fuzz_buffer_length = length;
}

guint64 df_fuzz_get_buffer_length(void)
{
return fuzz_buffer_length;
}

guint64 df_get_number_of_iterations(const char *signature)
{
Expand Down Expand Up @@ -252,6 +259,77 @@ static int df_check_if_exited(const int pid) {
return 1;
}

/**
* @function Calls method from df_list (using its name) with its arguments.
* @param value GVariant tuple containing all method arguments signatures and
* their values
* @param void_method If method has out args 1, 0 otherwise
* @return 0 on success, -1 on error, 1 if void method returned non-void
* value or 2 when tested method raised exception (so it should be skipped)
*/
static int df_fuzz_call_method(const struct df_dbus_method *method, GVariant *value)
{
g_autoptr(GError) error = NULL;
g_autoptr(GVariant) response = NULL;
g_autoptr(gchar) dbus_error = NULL;
const gchar *fmt;

// Synchronously invokes method with arguments stored in value (GVariant *)
// on df_dproxy.
response = g_dbus_proxy_call_sync(
df_dproxy,
method->name,
value,
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&error);
if (!response) {
// D-Bus exceptions are accepted
dbus_error = g_dbus_error_get_remote_error(error);
if (dbus_error) {
if (g_str_equal(dbus_error, "org.freedesktop.DBus.Error.NoReply"))
/* If the method is annotated as "NoReply", don't consider
* not replying as an error */
return method->expect_reply ? -1 : 0;
else if (g_str_equal(dbus_error, "org.freedesktop.DBus.Error.Timeout")) {
sleep(10);
return -1;
} else if (g_str_equal(dbus_error, "org.freedesktop.DBus.Error.AccessDenied") ||
g_str_equal(dbus_error, "org.freedesktop.DBus.Error.AuthFailed")) {
df_verbose("%s %sSKIP%s [M] %s - raised exception '%s'\n",
ansi_cr(), ansi_blue(), ansi_normal(),
method->name, dbus_error);
return 2;
}
}

g_dbus_error_strip_remote_error(error);
if (strstr(error->message, "Timeout")) {
df_verbose("%s %sSKIP%s [M] %s - timeout reached\n",
ansi_cr(), ansi_blue(), ansi_normal(), method->name);
return 2;
}

df_debug("%s EXCE %s - D-Bus exception thrown: %s\n",
ansi_cr(), method->name, error->message);
df_except_counter++;
return 0;
} else {
/* Check if a method without return value returns void */
if (!method->returns_value) {
fmt = g_variant_get_type_string(response);
if (!g_str_equal(fmt, "()")) {
df_fail("%s %sFAIL%s [M] %s - void method returns '%s' instead of '()'\n",
ansi_cr(), ansi_red(), ansi_normal(), method->name, fmt);
return 1;
}
}
}

return 0;
}

/**
* @function Function is testing a method in a cycle, each cycle generates
* data for function arguments, calls method and waits for result.
Expand Down Expand Up @@ -357,7 +435,7 @@ int df_fuzz_test_method(

df_fail(" reproducer: %sdfuzzer -v -n %s -o %s -i %s -t %s",
ansi_yellow(), name, obj, intf, method->name);
df_fail(" -b %"G_GUINT64_FORMAT, df_buf_size);
df_fail(" -b %"G_GUINT64_FORMAT, fuzz_buffer_length);
if (execute_cmd != NULL)
df_fail(" -e '%s'", execute_cmd);
df_fail("%s\n", ansi_normal());
Expand All @@ -375,77 +453,6 @@ int df_fuzz_test_method(
return 1;
}

/**
* @function Calls method from df_list (using its name) with its arguments.
* @param value GVariant tuple containing all method arguments signatures and
* their values
* @param void_method If method has out args 1, 0 otherwise
* @return 0 on success, -1 on error, 1 if void method returned non-void
* value or 2 when tested method raised exception (so it should be skipped)
*/
static int df_fuzz_call_method(const struct df_dbus_method *method, GVariant *value)
{
g_autoptr(GError) error = NULL;
g_autoptr(GVariant) response = NULL;
g_autoptr(gchar) dbus_error = NULL;
const gchar *fmt;

// Synchronously invokes method with arguments stored in value (GVariant *)
// on df_dproxy.
response = g_dbus_proxy_call_sync(
df_dproxy,
method->name,
value,
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&error);
if (!response) {
// D-Bus exceptions are accepted
dbus_error = g_dbus_error_get_remote_error(error);
if (dbus_error) {
if (g_str_equal(dbus_error, "org.freedesktop.DBus.Error.NoReply"))
/* If the method is annotated as "NoReply", don't consider
* not replying as an error */
return method->expect_reply ? -1 : 0;
else if (g_str_equal(dbus_error, "org.freedesktop.DBus.Error.Timeout")) {
sleep(10);
return -1;
} else if (g_str_equal(dbus_error, "org.freedesktop.DBus.Error.AccessDenied") ||
g_str_equal(dbus_error, "org.freedesktop.DBus.Error.AuthFailed")) {
df_verbose("%s %sSKIP%s [M] %s - raised exception '%s'\n",
ansi_cr(), ansi_blue(), ansi_normal(),
method->name, dbus_error);
return 2;
}
}

g_dbus_error_strip_remote_error(error);
if (strstr(error->message, "Timeout")) {
df_verbose("%s %sSKIP%s [M] %s - timeout reached\n",
ansi_cr(), ansi_blue(), ansi_normal(), method->name);
return 2;
}

df_debug("%s EXCE %s - D-Bus exception thrown: %s\n",
ansi_cr(), method->name, error->message);
df_except_counter++;
return 0;
} else {
/* Check if a method without return value returns void */
if (!method->returns_value) {
fmt = g_variant_get_type_string(response);
if (!g_str_equal(fmt, "()")) {
df_fail("%s %sFAIL%s [M] %s - void method returns '%s' instead of '()'\n",
ansi_cr(), ansi_red(), ansi_normal(), method->name, fmt);
return 1;
}
}
}

return 0;
}

static int df_fuzz_get_property(GDBusProxy *pproxy, const char *interface,
const struct df_dbus_property *property)
{
Expand Down
3 changes: 3 additions & 0 deletions src/fuzz.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ static inline void df_dbus_property_clear(df_dbus_property_t *p)
G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(df_dbus_method_t, df_dbus_method_clear)
G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(df_dbus_property_t, df_dbus_property_clear)

void df_fuzz_set_buffer_length(const guint64 length);
guint64 df_fuzz_get_buffer_length(void);

guint64 df_get_number_of_iterations(const char *signature);
/**
* @function Saves pointer on D-Bus interface proxy for this module to be
Expand Down
Loading

0 comments on commit a47afe4

Please sign in to comment.