Skip to content

Commit

Permalink
i#1734 Dr. Fuzz: add drfuzz repeat test
Browse files Browse the repository at this point in the history
- add drfuzz test to repeatedly execute a function with different args
- replace dr_init with dr_client_main for all clients in tests/framework

Review-URL: https://codereview.appspot.com/251590043
  • Loading branch information
zhaoqin committed Aug 6, 2015
1 parent e925dfa commit 700cd29
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 12 deletions.
3 changes: 3 additions & 0 deletions tests/framework/CMakeLists.txt
Expand Up @@ -93,3 +93,6 @@ add_drmf_test(strace_test drsyscall_app.c strace_client.c
# drfuzz tests
add_drmf_test(drfuzz_test_empty drfuzz_app_empty.c drfuzz_client_empty.c
drfuzz "done\nTEST PASSED")

add_drmf_test(drfuzz_test_repeat drfuzz_app_repeat.c drfuzz_client_repeat.c
drfuzz "hello 1\nhello 2\nhello 3\nhello 4\nhello 5\ndone\nTEST PASSED")
56 changes: 56 additions & 0 deletions tests/framework/drfuzz_app_repeat.c
@@ -0,0 +1,56 @@
/* **************************************************************
* Copyright (c) 2015 Google, Inc. 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.
*
* * Neither the name of Google, Inc. nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* 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 GOOGLE, INC. 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.
*/

/* Test of the Dr. Fuzz Extension */

#include <stdio.h>

#ifdef WINDOWS
# define EXPORT __declspec(dllexport)
#else
# define EXPORT
#endif

/* repeatme should be re-executed 5 times with arg 1-5 */
EXPORT void
repeatme(int i)
{
printf("hello %d\n", i);
}

int
main(int argc, char **argv)
{
repeatme(0);
printf("done\n");
return 0;
}
8 changes: 4 additions & 4 deletions tests/framework/drfuzz_client_empty.c
Expand Up @@ -36,17 +36,17 @@
#include "drmgr.h"
#include "drfuzz.h"

static
void exit_event(void)
static void
exit_event(void)
{
if (drfuzz_exit() != DRMF_SUCCESS)
DR_ASSERT_MSG(false, "drfuzz failed to exit");
dr_fprintf(STDERR, "TEST PASSED\n");
drmgr_exit();
}

DR_EXPORT
void dr_init(client_id_t id)
DR_EXPORT void
dr_client_main(client_id_t id, int argc, const char *argv[])
{
drmgr_init();
if (drfuzz_init(id) != DRMF_SUCCESS)
Expand Down
91 changes: 91 additions & 0 deletions tests/framework/drfuzz_client_repeat.c
@@ -0,0 +1,91 @@
/* **************************************************************
* Copyright (c) 2015 Google, Inc. 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.
*
* * Neither the name of Google, Inc. nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* 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 GOOGLE, INC. 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.
*/

/* Test of the Dr. Fuzz Extension */

#include "dr_api.h"
#include "drmgr.h"
#include "drfuzz.h"

static void
pre_fuzz_cb(generic_func_t target_pc, void *fuzzcxt, void **user_data)
{
ptr_uint_t arg_value;
if (drfuzz_get_arg(target_pc, 0, false/*cur*/, (void **)&arg_value) != DRMF_SUCCESS)
DR_ASSERT_MSG(false, "drfuzz failed to get arg");
arg_value = (arg_value + 1);
if (drfuzz_set_arg(fuzzcxt, 0, (void *)arg_value) != DRMF_SUCCESS)
DR_ASSERT_MSG(false, "drfuzz failed to set arg");
}

static bool
post_fuzz_cb(generic_func_t target_pc, void *fuzzcxt, void *user_data)
{
ptr_uint_t arg_value;
if (drfuzz_get_arg(target_pc, 0, false/*cur*/, (void **)&arg_value) != DRMF_SUCCESS)
DR_ASSERT_MSG(false, "drfuzz failed to get arg");
if (arg_value == 5)
return false; /* stop */
return true; /* repeat */
}

static void
exit_event(void)
{
if (drfuzz_exit() != DRMF_SUCCESS)
DR_ASSERT_MSG(false, "drfuzz failed to exit");
dr_fprintf(STDERR, "TEST PASSED\n");
drmgr_exit();
}

DR_EXPORT void
dr_client_main(client_id_t id, int argc, const char *argv[])
{
module_data_t *app;
generic_func_t repeatme_addr;
drmgr_init();
if (drfuzz_init(id) != DRMF_SUCCESS)
DR_ASSERT_MSG(false, "drfuzz failed to init");
dr_register_exit_event(exit_event);

/* fuzz repeatme */
app = dr_get_main_module();
if (app == NULL)
DR_ASSERT_MSG(false, "failed to get application module");
repeatme_addr = dr_get_proc_address(app->handle, "repeatme");
if (repeatme_addr == NULL)
DR_ASSERT_MSG(false, "failed to find function repeatme");
if (drfuzz_fuzz_target(repeatme_addr, 1, DRFUZZ_CALLCONV_CDECL,
pre_fuzz_cb, post_fuzz_cb) != DRMF_SUCCESS)
DR_ASSERT_MSG(false, "drfuzz failed to fuzz function repeatme");
dr_free_module_data(app);
}
8 changes: 4 additions & 4 deletions tests/framework/drsyscall_client.c
Expand Up @@ -328,8 +328,8 @@ test_static_iterator(void)
ASSERT(false, "drsys_iterate_syscalls failed");
}

static
void exit_event(void)
static void
exit_event(void)
{
drsys_gateway_t gateway;
if (drsys_syscall_gateway(&gateway) != DRMF_SUCCESS ||
Expand All @@ -341,8 +341,8 @@ void exit_event(void)
drmgr_exit();
}

DR_EXPORT
void dr_init(client_id_t id)
DR_EXPORT void
dr_client_main(client_id_t id, int argc, const char *argv[])
{
drsys_options_t ops = { sizeof(ops), 0, };
drmgr_init();
Expand Down
8 changes: 4 additions & 4 deletions tests/framework/strace_client.c
Expand Up @@ -202,17 +202,17 @@ event_filter_syscall(void *drcontext, int sysnum)
return true; /* intercept everything */
}

static
void exit_event(void)
static void
exit_event(void)
{
if (drsys_exit() != DRMF_SUCCESS)
ASSERT(false, "drsys failed to exit");
dr_fprintf(STDERR, "TEST PASSED\n");
drmgr_exit();
}

DR_EXPORT
void dr_init(client_id_t id)
DR_EXPORT void
dr_client_main(client_id_t id, int argc, const char *argv[])
{
drsys_options_t ops = { sizeof(ops), 0, };
drmgr_init();
Expand Down

0 comments on commit 700cd29

Please sign in to comment.