Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hilmi-yilmaz/malloc_failer
This is a script which makes it possible to fail a specific malloc after X times.
https://github.com/hilmi-yilmaz/malloc_failer
Last synced: 2 months ago
JSON representation
This is a script which makes it possible to fail a specific malloc after X times.
- Host: GitHub
- URL: https://github.com/hilmi-yilmaz/malloc_failer
- Owner: hilmi-yilmaz
- License: mit
- Created: 2021-03-25T15:47:26.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2021-10-08T13:40:25.000Z (over 3 years ago)
- Last Synced: 2024-08-03T22:04:22.453Z (6 months ago)
- Language: Shell
- Size: 17.6 KB
- Stars: 17
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-42 - malloc_failer - A script to test malloc protection. (Cursus / Testers)
README
# :smiling_imp: malloc_failer :smiling_imp:
### :warning: **This tool modifies your original files. It saves copies of your files in a hidden directory. When your done using this tool, you can use the --reverse option to get your original file back. Because this tool is still being tested, make sure you make a backup of your files.**
## :boom: Description
The malloc_failer let's you fail a specific malloc after X times. This can be used to check whether you have memory leaks when you exit your program because a malloc failed.
It adds the following wrapper code to your code (the X is given when running this script, explained in the **_Usage_** section):
```C
#includestatic void *xmalloc(size_t size)
{
static int fail = X;
static int i = 1;if (i == fail)
return (NULL);
i++;
return (malloc(size));
}
```It then sandwiches your malloc between this **_#define_** directive:
```C
#define malloc(x) xmalloc(x)
int **matrix = (int **)malloc(size); /* This would be the malloc you want to fail */
#undef malloc
```## :gear: Installation and Setup
Clone this repository. The only file you need is **_malloc_failer.sh_**, which is an executable.
You can add this tool to a location in your PATH so that you can use it for every project.
## :video_game: Usage
There are 3 things you need to specify:
1. The file from which to fail a malloc.
2. The line number of the malloc.
3. X, which is a number at which to fail the malloc.The format is:
```sh
./malloc_failer.sh [file_to_be_tested.c] [line_number_of_malloc] [at_which_malloc_to_fail]
```## :soccer: Example
Below you can see an example program. Let's say this file is called **_matrix.c_**.
```C
#includeint main(void)
{
int i = 0;
int j = 0;
int rows = 4;
int columns = 4;
int **matrix;matrix = (int **)malloc(sizeof(int *) * rows);
if (matrix == NULL)
return (-1);
while (i < rows)
{
matrix[i] = (int *)malloc(sizeof(int) * columns); /* ----- line 16 ----- */
if (matrix[i] == NULL)
return (-1);
while (j < columns)
{
matrix[i][j] = 0;
j++;
}
j = 0;
i++;
}
/* Free all data */
i = 0;
while (i < rows)
{
free(matrix[i]);
i++;
}
free(matrix);
return (0);
}
```
This program contains leaks when an allocation of `matrix[i]` fails (line 16). To fail the malloc on line 16 at the third time, you can run:```sh
./malloc_failer.sh matrix.c 16 3
```Your original file is modified. A copy of your file is saved in the **_.malloc_failer/_** directory with the *.orig* prefix. Now you can run your program as always with a memory error detection tool, like:
```sh
gcc -Wall -Wextra -Werror -fsanitize=address -g matrix.c
```To get your original file back, you can run:
```sh
./malloc_failer.sh --reverse
```The reverse option makes sure your project directory is the same as before you ran the *malloc_failer*.
:warning: **Make sure you are using a memory error detection tool, so that leaks can be found. Examples of such detectors are ASAN (AdressSanitizer) and Valgrind.**
## :compass: Roadmap
- Add calloc and realloc.
- Make more user friendly.## :mailbox: Contribute
Found a bug? Ran into a specific problem? Missing a feature? Feel free to **file a new issue** with a respective title and description on the [issue page](https://github.com/hilmi-yilmaz/malloc_failer/issues). You can also ask questions in [GitHub Discussion](https://github.com/hilmi-yilmaz/malloc_failer/discussions).
## :credit_card: Credits
Thanks **_Tishj_** for finding some bugs and coming up with ideas for the _malloc_failer_!## :blue_book: License
[MIT](https://opensource.org/licenses/MIT)