Table of Contents
Fetching ...

Semantic Analysis of Macro Usage for Portability

Brent Pappas, Paul Gazzillo

TL;DR

This work tackles the challenge of porting C macro usage to safer languages without losing macro abstractions by introducing the Macro Inspector Framework and its implementation, Maki. Maki analyzes macro usage through 26 properties to decide when a macro is interface-equivalent to a function or requires more substantial refactoring, achieving an average of 94% accuracy on real-world benchmarks. Across 21 programs with 86,199 macros, the framework finds that 37% are easily portable, with Maki uncovering 2× more easy-to-port macros on average than prior work and up to 7× in some cases. Case studies port macros in Linux and other large codebases, yielding patches that were often accepted by maintainers, demonstrating practical impact for developers and tool builders aiming to preserve macro semantics during porting. The work lays groundwork for automated porting assistance and future generalization to other macro systems and target languages.

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.

Semantic Analysis of Macro Usage for Portability

TL;DR

This work tackles the challenge of porting C macro usage to safer languages without losing macro abstractions by introducing the Macro Inspector Framework and its implementation, Maki. Maki analyzes macro usage through 26 properties to decide when a macro is interface-equivalent to a function or requires more substantial refactoring, achieving an average of 94% accuracy on real-world benchmarks. Across 21 programs with 86,199 macros, the framework finds that 37% are easily portable, with Maki uncovering 2× more easy-to-port macros on average than prior work and up to 7× in some cases. Case studies port macros in Linux and other large codebases, yielding patches that were often accepted by maintainers, demonstrating practical impact for developers and tool builders aiming to preserve macro semantics during porting. The work lays groundwork for automated porting assistance and future generalization to other macro systems and target languages.

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.
Paper Structure (20 sections, 4 figures, 2 tables)

This paper contains 20 sections, 4 figures, 2 tables.

Figures (4)

  • Figure 1: Maki architecture diagram.
  • Figure 2: Percent of macro definition adaptation categories.
  • Figure 3: How many more easy-to-port, interface-equivalent macro definitions Maki finds over prior work.
  • Figure 4: Percentage of aligned and interface-equivalent definitions in each program.