COMPILE-TIME FUNCTION CALL INTERCEPTION FOR TESTING IN C/C++

Authors

  • Gábor MÁRTON Eötvös Loránd University. Email: martong@mailbox.elte.hu
  • Zoltán PORKOLÁB Eötvös Loránd University. Email: gsd@elte.hu

DOI:

https://doi.org/10.24193/subbi.2018.1.02

Keywords:

C programming language, unit testing, function call interception, compiler instrumentation.

Abstract

In C/C++, during the test development process we often have to modify the public interface of a class to replace existing dependencies; supplementary setter or constructor functions or extra template parameters are added for dependency injection. These solutions may have serious detrimental effects on the code structure and sometimes on the run-time performance as well. We introduce a new technique that makes dependency replacement possible without the modification of the production code, thus it provides an alternative way to add unit tests. Our new compile-time instrumentation technique enables us to intercept function calls and replace them in runtime. Contrary to existing function call interception (FCI) methods, we instrument the call expression instead of the callee, thus we can avoid the modification and recompilation of the function in order to intercept the call. This has a clear advantage in case of system libraries and third party shared libraries, thus it provides an alternative way to automatize tests for legacy software. We created a prototype implementation based on the LLVM compiler infrastructure which is publicly available for testing.

Author Biographies

Gábor MÁRTON, Eötvös Loránd University. Email: martong@mailbox.elte.hu

Department of Programming Languages and Compilers, Eötvös Loránd University. Email: martong@mailbox.elte.hu

Zoltán PORKOLÁB, Eötvös Loránd University. Email: gsd@elte.hu

Department of Programming Languages and Compilers, Eötvös Loránd University. Email: gsd@elte.hu

References

Kumar Avijit et al. “Binary Rewriting and Call Interception for Efficient Runtime Protection Against Buffer Overflows: Research Articles”. In: Softw. Pract. Exper. 36.9 (July 2006), pp. 971–998. issn: 0038-0644. url: http://dx.doi.org/10.1002/spe.v36:9.

Michael Feathers. Working Effectively with Legacy Code. Upper Saddle River, NJ, USA: Prentice Hall PTR, 2004. isbn: 0131177052.

Martin Fowler. GivenWhenThen. url: https://martinfowler.com/bliki/GivenWhenThen.html.

gcc.gnu.org. Declaring Attributes of Functions. 2017. url: https://gcc.gnu.org/onlinedocs/gcc-4.3.0/gcc/Function-Attributes.html (visited on 06/24/2017).

gcc.gnu.org. Extracting the function pointer from a bound pointer to member function. 2017. url: https://gcc.gnu.org/onlinedocs/gcc-4.9.0/gcc/Bound-member-functions.html (visited on 06/24/2017).

gcc.gnu.org. Program Instrumentation Options. 2017. url: https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html (visited on 06/24/2017).

gnu.org. GDB: The GNU Project Debugger. 2017. url: https: //www.gnu.org/software/gdb/ (visited on 06/24/2017).

gnu.org. Using GNU ld. 2017. url: ftp://ftp.gnu.org/old-gnu/Manuals/ld2.9.1/html_node/ld_3.html (visited on 06/24/2017).

Intel. Pintool API Reference - RTN: Routine Object. 2017. url: https://software.intel.com/sites/landingpage/pintool/docs/53271/Pin/html/group_RTN _BASIC API.html (visited on 06/24/2017).

Intel et al. Itanium C++ ABI. 2017. url: http://refspecs.linuxbase.org/cxxabi-1.83.html (visited on 06/24/2017).

Pilsung Kang. “Function call interception techniques”. In: Software: Practice and Experience (). spe.2501, n/a–n/a. issn: 1097-024X. url: http://dx.doi.org/10.1002/spe.2501.

Chris Lattner and Vikram Adve. “LLVM: A Compilation Framework for Lifelong Program Analysis & Transformation”. In: Proceedings of the 2004 International Symposium on Code Generation and Optimization (CGO’04). Palo Alto, California, Mar. 2004.

llvm.org. Clang will not accept a conversion from a bound pmf to a regular method pointer. 2017. url: https://bugs.llvm.org/show_bug.cgi?id=22121 (visited on 06/24/2017).

llvm.org. LLVM Language Reference Manual. 2017. url: http://llvm. org/docs/LangRef.html (visited on 06/25/2017).

Chi-Keung Luk et al. “Pin: Building Customized Program Analysis Tools with Dynamic Instrumentation”. In: Proceedings of the 2005 ACM SIGPLAN Conference on Programming Language Design and Implementation. PLDI ’05. Chicago, IL, USA: ACM, 2005, pp. 190–200. isbn: 1-59593-056-6. url: http://doi.acm.org/10.1145/1065010.1065034.

Linux Programmer’s Manual. dlsym, dlvsym - obtain address of a symbol in a shared object or executable. 2017. url: http://man7.org/linux/man-pages/man3/dlsym.3. html (visited on 06/24/2017).

Linux Programmer’s Manual. ld.so, ld-linux.so-dynamic linker/loader. 2017. url: http://man7.org/linux/man-pages/man8/ld.so.8.html (visited on 06/24/2017).

Linux Programmer’s Manual. mmap, munmap - map or unmap files or devices into memory. 2017. url: http://man7.org/linux/man-pages/man2/mmap.2.html (visited on 06/24/2017).

Linux Programmer’s Manual. ptrace - process trace. 2017. url: http://man7.org/linux/man-pages/man2/ptrace.2.html (visited on 06/24/2017).

Robert C Martin. Clean code: a handbook of agile software craftsmanship. Pearson Education, 2009.

G´abor M´arton. Access Private. 2017. url: https://googl/ynaZv5 (visited on 06/25/2017).

G´abor M´arton. finstrument-mock-Instrumentation for Testing. 2017. url: https://github.com/martong/finstrument_mock (visited on 06/25/2017).

G´abor M´arton. Performance Measurements of finstrument-mock. 2017. url: https://github.com/martong/finstrument_mock/blob/master/measure/performance_evaluation.pdf (visited on 03/28/2018).

Gerard Meszaros. xUnit test patterns: Refactoring test code. Pearson Education, 2007.

Nicholas Nethercote and Julian Seward. “Valgrind: A Framework for Heavyweight Dynamic Binary Instrumentation”. In: SIGPLAN Not. 42.6 (June 2007), pp. 89–100. issn: 0362-1340. url: http://doi.acm.org/10.1145/1273442.1250746.

D North. Introducing BDD, Better Software Magazine. 2006.

Pradeep Padala. “Playing with ptrace, Part I”. In: 103 (Nov. 2002). issn: 1075-3583 (print), 1938-3827 (electronic).

Michael Ru¨egg and Peter Sommerlad. “Refactoring Towards Seams in C++”. In: Proceedings of the 7th International Workshop on Automation of Software Test. AST ’12. Zurich, Switzerland: IEEE Press, 2012, pp. 117–123. isbn: 978-1-4673-1822-8. url: http://dl.acm.org/citation.cfm?id=2663608.2663632.

Konstantin Serebryany and Timur Iskhodzhanov. “Thread Sanitizer: Data Race Detection in Practice”. In: Proceedings of the Workshop on Binary Instrumentation and Applications. WBIA ’09. New York, New York, USA: ACM, 2009, pp. 62–71. isbn: 978-1-60558-793-6. url: http://doi.acm.org/10.1145/1791194.1791203.

Konstantin Serebryany et al. “Address Sanitizer: A Fast Address Sanity Checker”. In: Proceedings of the 2012 USENIX Conference on Annual Technical Conference. USENIX ATC’12. Boston, MA: USENIX Association, 2012, pp. 28–28. url: http://dl.acm.org/citation.cfm?id=2342821.2342849.

Sai Venkatakrishnan. Build Operate Check Clear - Test Pattern. url: http://developer-in-test.blogspot.hu/2009/05/build-operate-check-clear-test-pattern.html.

Downloads

Published

2018-06-19

How to Cite

MÁRTON, G., & PORKOLÁB, Z. (2018). COMPILE-TIME FUNCTION CALL INTERCEPTION FOR TESTING IN C/C++. Studia Universitatis Babeș-Bolyai Informatica, 63(1), 17–32. https://doi.org/10.24193/subbi.2018.1.02

Issue

Section

Articles