0

Yesterday I was asking myself a question.

Does it is possible to programmatically "brute force" all the calls to a specific function into a program, and test if the error cases of this call is always properly handled ?

Example:

int main(void)
{
    char *mallocforfun = NULL;
    char **matrix = NULL;
    if ((matrix = (char **)malloc(sizeof(char*) * 42)))
    {
        for (int i = 0; i < 42; i++)
        {
            matrix[i] = (char *)malloc(sizeof(char) * 42);
            bzero(matrix[i], 42);
        }
        matrix[i] = NULL;
    }
    mallocforfun = (char*)malloc(sizeof(char) * 42);
    ...
    // do some stuff and free everything
    return (0);
}

So in this example, if we would test malloc function, the tester will put three breakpoint:

int main(void)
{
    char *mallocforfun = NULL;
    char **matrix = NULL;
    1st: if ((matrix = (char **)malloc(sizeof(char*) * 42)))
    {
        for (int i = 0; i < 42; i++)
        {
            2nd: matrix[i] = (char *)malloc(sizeof(char) * 42);
            bzero(matrix[i], 42);
        }
        matrix[i] = NULL;
    }
    3rd: mallocforfun = (char*)malloc(sizeof(char) * 42);
    ...
    // do some stuff and free everything
    return (0);
}

Run the program, change malloc function return into an error value, see if it crash, delete last tested breakpoint, rerun, and so on.

I want to verify that I have handled all error returns by running the program repeatedly in an environment where malloc fails once at each call site in turn on subsequent runs of the program. (Thank's to @Mic for the explicit formulation)

I think it's possible to do something like this with non-stripped binary with gdb script. I searched by myself but can't find anything looking like that.

So I feel like I have a "keyword" missing. Do you know if this kind of test have a specific name, or a tool doing something like that ?

10
  • I'm not sure what you mean, as the 2nd paragraph seems to contradict the 2nd codebox. Do you want to generate brute-force input for all possible combinations of arguments? Or do you just want to test the arguments that are passed during normal operation and ensure they pass specific conditions? If the latter, you just need assert(). Commented Jun 30, 2017 at 8:34
  • It's kind of second option, but I want to do it dynamically for any function at execution time. In my case, the tool will change variables values with: matrix = NULL -> Test program execution flow. matrix[i] = NULL -> Test program execution flow. mallocforfun = NULL -> Test program execution flow.
    – avallete
    Commented Jun 30, 2017 at 8:40
  • What do you mean, "dynamically for any function at execution time"? So you don't want to have to add debugging code to your source, but instead to use a debugger to attach to it and add breakpoints and/or change variables arbitrarily? Commented Jun 30, 2017 at 8:43
  • 1
    You could replace all calls to malloc with a call to a malloc wrapper that does what you want. Mock malloc functional testing gives two good anwers, one where you use the malloc wrapper in your source and one where you use it in the link phase, see stackoverflow.com/questions/12510195/…, stackoverflow.com/questions/1711170/…
    – Mic
    Commented Jun 30, 2017 at 9:02
  • 1
    as different functions will have different semantics it's hard to see how you could automatically handle any function. would you consider C++, where exceptions solve this problem?
    – Mic
    Commented Jun 30, 2017 at 10:03

0

Browse other questions tagged or ask your own question.