项目作者: abhishek-ghogare

项目描述 :
Memory management wrapper library
高级语言: C
项目地址: git://github.com/abhishek-ghogare/memory-management-wrapper-library.git
创建时间: 2019-03-19T18:13:35Z
项目社区:https://github.com/abhishek-ghogare/memory-management-wrapper-library

开源协议:MIT License

下载


Linux Memory Management Wrapper Library

Easy to use Valgrind alternative in Linux kernel for memory leaks detection and debugging. The library wraps Linux Kernel & user side [k]malloc, [k]realloc, [k]calloc, [k]free functions and tracks memory allocations.
Function void mmwl_status (void); prints list of allocated memory which are not freed till the point; with useful information like where the memory was allocated (filename, function name and line number) and stack trace at the time of allocation.
In addition to memory leaks, library can also detect possible out of bound writes to the allocated memory or invalid address passed to [k]free call by comparing the block signatures.

How to use

MMWL is very easy to use in both user & kernel side. Since the function calls [k]malloc, [k]realloc, [k]calloc, [k]free are defined as macros in mmwl.h, there is no need to replace each of these calls within the code base with the wrapper functions.

  • Include mmwl.h header file in your program
  • Call void mmwl_status (void); just before the end of the execution
  • Build including mmwl_core.c source file

User program example

  1. ...
  2. #include <stdlib.h>
  3. #include "mmwl.h" /* include library header */
  4. int main () {
  5. char* str1 = (char *) malloc(60);
  6. free(str1);
  7. mmwl_status(); /* see status just before exiting */
  8. return 0;
  9. }

Kernel program example

  1. ....
  2. #include <linux/kernel.h>
  3. ....
  4. #include "mmwl.h" /* include library header */
  5. ....
  6. int __init init_module(void) {
  7. mem = (int *)kmalloc(300, GFP_KERNEL);
  8. return 0;
  9. }
  10. void __exit cleanup_module(void) {
  11. kfree(mem);
  12. mmwl_status(); /* see status just before exiting */
  13. }

Sample log

  1. mmwl:inf: *** mmwl statistics START ***
  2. mmwl:inf: total alloc count : 3
  3. mmwl:inf: total free count : 2
  4. mmwl:inf: total size allocated : 3260
  5. mmwl:inf: total size freed : 1260
  6. mmwl:inf: current allocated size : 2000
  7. mmwl:inf: current alloc count : 1
  8. mmwl:inf: list of allocations :
  9. mmwl:inf: addr:0x55a30b2d61d0 size:2000 @main:17 in test.c
  10. mmwl:err: signature mismatch, addr:0x55a30b2d5450
  11. mmwl:err: addr:0x55a30b2d5450 size:60 @main:7 in test.c
  12. mmwl:inf: *** mmwl statistics END ***

Signature mismatch error occurs for following reasons:

  1. Signature was overwritten by the user program, suggesting out of bound write.
  2. “free” was called with wrong address.
  3. The block was already freed, hence multiple “free” called on same address.

Author

Abhishek Ghogare

License

This project is licensed under the MIT License - see the LICENSE.md file for details