Papers
Topics
Authors
Recent
Search
2000 character limit reached

Semantic Analysis of Macro Usage for Portability

Published 18 Jan 2024 in cs.SE | (2401.10422v1)

Abstract: C is an unsafe language. Researchers have been developing tools to port C to safer languages such as Rust, Checked C, or Go. Existing tools, however, resort to preprocessing the source file first, then porting the resulting code, leaving barely recognizable code that loses macro abstractions. To preserve macro usage, porting tools need analyses that understand macro behavior to port to equivalent constructs. But macro semantics differ from typical functions, precluding simple syntactic transformations to port them. We introduce the first comprehensive framework for analyzing the portability of macro usage. We decompose macro behavior into 26 fine-grained properties and implement a program analysis tool, called Maki, that identifies them in real-world code with 94% accuracy. We apply Maki to 21 programs containing a total of 86,199 macro definitions. We found that real-world macros are much more portable than previously known. More than a third (37%) are easy-to-port, and Maki provides hints for porting more complicated macros. We find, on average, 2x more easy-to-port macros and up to 7x more in the best case compared to prior work. Guided by Maki's output, we found and hand-ported macros in four real-world programs. We submitted patches to Linux maintainers that transform eleven macros, nine of which have been accepted.

Definition Search Book Streamline Icon: https://streamlinehq.com
References (54)
  1. Appleseed Lab. Sugarc. https://github.com/appleseedlab/superc/tree/master/src/superc/cdesugarer, 2022.
  2. A framework for preprocessor-aware c source code analyses. Software Practice and Experience 30 (07 2000).
  3. Brent Pappas, P. G. Artifact for semantic analysis of macro usage for portability. https://zenodo.org/record/8326488, 2023.
  4. Chance, E. c2go. https://github.com/elliotchance/c2go, 2021.
  5. CMU SEI SERT Team. Pre00-c. prefer inline or static functions to function-like macros. https://wiki.sei.cmu.edu/confluence/display/c/PRE00-C.+Prefer+inline+or+static+functions+to+function-like+macros, Apr 2022.
  6. Correct Computation Inc. 3c. https://github.com/correctcomputation/checkedc-clang/, 2021.
  7. Correct Computation Inc. 3c. https://github.com/correctcomputation/checkedc-clang/issues/400, 2021.
  8. Correct Computation Inc. 3c. https://github.com/correctcomputation/checkedc-clang/issues/40, 2021.
  9. Correct Computation Inc. 3c. https://github.com/correctcomputation/checkedc-clang/issues/439, 2021.
  10. Datawrapper GmbH. Datawrapper: Create charts, maps, and tables. https://www.datawrapper.de/, 2023. Used to create charts.
  11. Dietrich, C. CppSig: Extracting Type Information for C-Preprocessor Macro Expansions. Association for Computing Machinery, New York, NY, USA, 2021, p. 62–68.
  12. An empirical analysis of c preprocessor use. IEEE Trans. Softw. Eng. 28, 12 (dec 2002), 1146–1170.
  13. Favre, J.-M. Cpp denotational semantics. In Proceedings Third IEEE International Workshop on Source Code Analysis and Manipulation (2003), pp. 22–31.
  14. Flatt, M. Composable and compilable macros: You want it when? In Proceedings of the Seventh ACM SIGPLAN International Conference on Functional Programming (New York, NY, USA, 2002), ICFP ’02, Association for Computing Machinery, p. 72–83.
  15. Free Software Foundation. Gawk v5.1.1. https://www.gnu.org/software/gawk/, 2021.
  16. Free Software Foundation. Bash v5.2 rc1. https://www.gnu.org/software/bash/, 2022.
  17. Free Software Foundation. Emacs v28.1. https://www.gnu.org/software/emacs/, 2022.
  18. FVWM Team. Fvwm v2.6.9. https://www.fvwm.org/, 2019.
  19. Garbe, F. Performance measurement of c software product lines. Master’s thesis, University of Passau, 2017.
  20. Analyzing multiple configurations of a c program. 21st IEEE International Conference on Software Maintenance (ICSM’05) (2005), 379–388.
  21. Superc: Parsing all of c by taming the preprocessor. SIGPLAN Not. 47, 6 (jun 2012), 323–334.
  22. Gosling, J. Ace: a syntax-driven c preprocessor. Australian Unix Users Group (1989).
  23. Graham, P. On Lisp. Prentice Hall, 1993.
  24. Immunant. c2rust. https://github.com/immunant/c2rust/issues/16, 2018.
  25. Immunant. c2rust. https://github.com/immunant/c2rust, 2022.
  26. ISO Technical Committee ISO/IEC JTC 1/SC 22. ISO-IEC-9899-2011. Standard, International Organization for Standardization, Dec. 2011.
  27. Variability-aware parsing in the presence of lexical macros and conditional compilation. In Proceedings of the 2011 ACM International Conference on Object Oriented Programming Systems Languages and Applications (New York, NY, USA, 2011), OOPSLA ’11, Association for Computing Machinery, p. 805–824.
  28. Macros - the rust programming language. https://doc.rust-lang.org/book/ch19-06-macros.html, Nov 2022.
  29. C reconfigurator. https://github.com/itu-square/c-reconfigurator, 2017.
  30. Linux Kernel Maintainers. Linux kernel mailing list. https://lore.kernel.org/.
  31. Lua Team. lua. https://github.com/lua/lua, 2022.
  32. Martin Fowler, K. B. Refactoring: Improving the Design of Existing Code. Addison-Wesley Longman Publishing Co., Inc., USA, 1999.
  33. Astec: A new approach to refactoring c. SIGSOFT Softw. Eng. Notes 30, 5 (sep 2005), 21–30.
  34. Giving meaning to macros. In Proceedings. 12th IEEE International Workshop on Program Comprehension, 2004. (2004), pp. 79–85.
  35. Microsoft Research. 3c. https://github.com/Microsoft/checkedc/, 2021.
  36. Resugaring: Lifting evaluation sequences through syntactic sugar. In Proceedings of the 35th ACM SIGPLAN Conference on Programming Language Design and Implementation (New York, NY, USA, 2014), PLDI ’14, Association for Computing Machinery, p. 361–371.
  37. Popa, A. Convert macros to constexpr. https://devblogs.microsoft.com/cppblog/convert-macros-to-constexpr/, Jun 2018.
  38. Reddy, S. Auto refactor of a macro followed by a comment to a constexpr put the semicolon after the comment. https://developercommunity.visualstudio.com/t/auto-refactor-of-a-macro-followed-by-a-comment-to/354205, Oct 2018.
  39. Rust Team. rust. https://github.com/rust-lang/rust, 2022.
  40. Spinellis, D. Global analysis and transformations in preprocessed languages. IEEE Trans. Softw. Eng. 29, 11 (nov 2003), 1019–1030.
  41. Sterba, D. Re: [patch] fs: Ntfs read-write driver gpl implementation by paragon software. https://lore.kernel.org/linux-fsdevel/[email protected]/.
  42. Xfig v3.2.8b. https://mcj.sourceforge.net/, 2021.
  43. The Clang Team. clang::ppcallbacks class reference — clang 16.0.0git documentation. https://clang.llvm.org/doxygen/classclang_1_1PPCallbacks.html, Nov 2022.
  44. The Clang Team. Introduction to the clang ast — clang 16.0.0git documentation. https://clang.llvm.org/docs/IntroductionToTheClangAST.html, 2022.
  45. The GCC Team. The c preprocessor. https://gcc.gnu.org/onlinedocs/cpp/, 2022.
  46. The GCC Team. Operator precedence problems. https://gcc.gnu.org/onlinedocs/cpp/Operator-Precedence-Problems.html#Operator-Precedence-Problems, Nov 2022.
  47. The GCC Team. Designated inits. https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html, 2023.
  48. The GCC Team. Statement exprs. https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html, 2023.
  49. The Linux Kernel. Linux kernel coding style. https://github.com/torvalds/linux/blob/master/Documentation/process/coding-style.rst, 2023. Section 12, ”Macros, Enums and RTL”.
  50. The Linux Kernel. Linux Kernel drivers/staging, 2023.
  51. The Linux Kernel. Memory Allocation Guide. https://www.kernel.org/doc/html/latest//core-api/memory-allocation.html, 2023.
  52. The Linux Kernel. Submitting patches: the essential guide to getting your code into the kernel. https://github.com/torvalds/linux/blob/master/Documentation/process/submitting-patches.rst, 2023.
  53. The Racket Team. Racket. https://racket-lang.org/, 2022.
  54. Programmable syntax macros. In Proceedings of the ACM SIGPLAN 1993 Conference on Programming Language Design and Implementation (New York, NY, USA, 1993), PLDI ’93, Association for Computing Machinery, p. 156–165.
Citations (1)

Summary

No one has generated a summary of this paper yet.

Paper to Video (Beta)

No one has generated a video about this paper yet.

Whiteboard

No one has generated a whiteboard explanation for this paper yet.

Open Problems

We haven't generated a list of open problems mentioned in this paper yet.

Continue Learning

We haven't generated follow-up questions for this paper yet.

Authors (2)

Collections

Sign up for free to add this paper to one or more collections.

Tweets

Sign up for free to view the 1 tweet with 0 likes about this paper.