Last active
March 26, 2022 17:05
-
-
Save ex0dus-0x/37c0992e01931e57e568c64fd5be2fa9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* callgraph.c | |
* | |
* Subroutine-rich sample to help testing | |
* any type of analysis tooling involving callgraphs, | |
* whether static or dynamic. | |
* | |
* Each function implements some type of edge case | |
* that one may want to consider covering in their tool, | |
* such as function pointers. | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
void start(int value); | |
void end(void); | |
void foo(int value); | |
void bar(int value); | |
void flop(void); | |
void flap(void); | |
void borp(int value); | |
/* Tests: deterministic control split */ | |
void start(int value) { | |
if (value < 42) | |
foo(value); | |
else | |
bar(value); | |
} | |
/* Tests: nondeterministic control split */ | |
void foo(int value) | |
{ | |
//srand(time(NULL)); | |
//int seed = rand() + value; | |
if (value >= 12) | |
flop(); | |
else | |
flap(); | |
} | |
/* Tests: function pointers and indirect calls */ | |
void bar(int value) | |
{ | |
void (*p)(); | |
if (value != 1) | |
p = flap; | |
else | |
p = flop; | |
p(); | |
} | |
/* Tests: back edge to parent (WARNING: infinite loop) */ | |
void flop(void) | |
{ | |
borp(0); | |
} | |
void flap(void) | |
{ | |
borp(1); | |
} | |
void borp(int value) | |
{ | |
if (value == 0) | |
flop(); | |
else if (value == 1) | |
flap(); | |
} | |
/* Tests: imports */ | |
void end(void) | |
{ | |
exit(0); | |
} | |
int main(int argc, char *argv[]) | |
{ | |
if (argc < 2) { | |
return -1; | |
} | |
int value = atoi(argv[1]); | |
start(value); | |
printf("Done! Ending...\n"); | |
end(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment