Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Debugging some simple memory problom cause by memset or memcpy.

License

Notifications You must be signed in to change notification settings

JCKeep/memset_track

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

memset_track

For debugging some simple memory problom cause by memset or memcpy. This is just a simple way to tracking memory problom without ASAN.

Usage

  1. Register the memory region you want to tracking in your product.

    uint8_t tracked_mem[16];
    
    memset_track_register("test1", (uintptr_t)&tracked_mem, sizeof(tracked_mem));

    If you did not want to track it anymore, you could unregister it.

    memset_track_unregister((uintptr_t)&tracked_mem);
  2. Build this GCC Plugin memset_track_plugin

    # Just run make, or modify Makefile for your custem gcc build.
    $ make

    If it works, you will got a memset_track_plugin.so in target dir

  3. Compile your product with GCC Plugin memset_track_plugin

    # Just add param to your gcc
    -fplugin=$(YOUR_CUSTEM_PATH)/memset_track_plugin.so
    

Example

shell$ make example
gcc -O0 -o target/example/example1 example/example1.c \
        -fplugin=target/memset_track_plugin.so \
        -fplugin-arg-memset_track_plugin-verbose \
        -I./include \
        -rdynamic
===> [MEMSET_TRACKING] start, rewriting the AST node...
Before: 
# .MEM_14 = VDEF <.MEM_13>
memset_s (&buf, 18, 0, 18);

After: 
# .MEM_14 = VDEF <.MEM_13>
memset_track_s (&buf, 18, 0, 18);
<=== [MEMSET_TRACKING] finish.
redirect memset_s to memset_track_s in main()

shell$ ./target/example/example1
ERROR: memset_s overlaps the memory [tracked_mem] at IP = 0x5a234d6706b1
./target/example/example1(print_stack_trace+0x49)[0x5a234d67047d]
./target/example/example1(memset_track_s+0xd6)[0x5a234d670614]
./target/example/example1(main+0x51)[0x5a234d6706b1]
/lib/x86_64-linux-gnu/libc.so.6(+0x29d90)[0x74d230229d90]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0x80)[0x74d230229e40]
./target/example/example1(_start+0x25)[0x5a234d670185]
buf:         fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe 
tracked_mem: fe fe 00 00 00 00 00 00 00 00 00 00 00 00 00 00

About

Debugging some simple memory problom cause by memset or memcpy.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published