Code Monkey home page Code Monkey logo

ld-preload-example's Introduction

ld-preload-example

This example demostrates how to use dynamic loading to wrap library functions.

demo-dlsym.c shows a simple usage of the "dlsym" function to obtain references
to functions and then call them.

leak.c: is a small program which calls malloc and free, and leaks memory.

tracking-malloc.c: implements wrapper versions of malloc and free, and tracks
the number of bytes allocated and freed.

The leak program can be build directly using the tracking-malloc.o file:
	gcc -c -std=c89 -g -Wall -Wextra -Werror -o tracking-malloc.o \
		tracking-malloc.c -ldl
	gcc -std=c89 -g -Wall -Wextra -Werror -o tracking-malloc-leak \
		tracking-malloc.o leak.c -ldl
	./tracking-malloc-leak

Alternatively, on linux we can use LD_LIBRARY_PATH and LD_PRELOAD to make the
leak program call the wrapper versions of malloc/free:
	gcc -std=c89 -g -Wall -Wextra -Werror -o leak leak.c
	gcc -std=c89 -g -Wall -Wextra -Werror -rdynamic -fPIC -shared \
		-o libtracking-malloc.so tracking-malloc.c -ldl
	TRACKING_MALLOC_ENABLE=1 \
		LD_LIBRARY_PATH="." \
		LD_PRELOAD=libtracking-malloc.so \
		./leak

=============================================================================

eric@dione:~/src/ld-preload-example$ make leak
gcc -std=c89 -g -Wall -Wextra -Werror -o leak leak.c
eric@dione:~/src/ld-preload-example$
eric@dione:~/src/ld-preload-example$ ./leak
eric@dione:~/src/ld-preload-example$
eric@dione:~/src/ld-preload-example$ valgrind ./leak
==2425== Memcheck, a memory error detector
==2425== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==2425== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==2425== Command: ./leak
==2425==
==2425==
==2425== HEAP SUMMARY:
==2425==     in use at exit: 42 bytes in 1 blocks
==2425==   total heap usage: 6 allocs, 5 frees, 380 bytes allocated
==2425==
==2425== LEAK SUMMARY:
==2425==    definitely lost: 42 bytes in 1 blocks
==2425==    indirectly lost: 0 bytes in 0 blocks
==2425==      possibly lost: 0 bytes in 0 blocks
==2425==    still reachable: 0 bytes in 0 blocks
==2425==         suppressed: 0 bytes in 0 blocks
==2425== Rerun with --leak-check=full to see details of leaked memory
==2425==
==2425== For counts of detected and suppressed errors, rerun with: -v
==2425== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
eric@dione:~/src/ld-preload-example$
eric@dione:~/src/ld-preload-example$ make tracking-malloc-leak
gcc -c -std=c89 -g -Wall -Wextra -Werror -o tracking-malloc.o tracking-malloc.c -ldl
gcc -std=c89 -g -Wall -Wextra -Werror -o tracking-malloc-leak tracking-malloc.o leak.c -ldl
eric@dione:~/src/ld-preload-example$ ./tracking-malloc-leak
eric@dione:~/src/ld-preload-example$ TRACKING_MALLOC_ENABLE=1 ./tracking-malloc-leak
+ 42
- 42
+ 126
- 126
+ 42
+ 42
- 42
+ 43
- 43
eric@dione:~/src/ld-preload-example$
eric@dione:~/src/ld-preload-example$ make libtracking-malloc.so
gcc -std=c89 -g -Wall -Wextra -Werror -rdynamic -fPIC -shared -o libtracking-malloc.so tracking-malloc.c -ldl
eric@dione:~/src/ld-preload-example$ LD_LIBRARY_PATH="." LD_PRELOAD=libtracking-malloc.so ./leak
eric@dione:~/src/ld-preload-example$ TRACKING_MALLOC_ENABLE=1 \
> LD_LIBRARY_PATH="." LD_PRELOAD=libtracking-malloc.so ./leak
+ 42
- 42
+ 126
- 126
+ 42
+ 42
- 42
+ 43
- 43
eric@dione:~/src/ld-preload-example$

ld-preload-example's People

Contributors

ericherman avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar  avatar

Forkers

j-r-jones

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.