Abstract
Code bloat is a pervasive issue in production software, degrading performance and expanding the attack surface. This work investigates the potential for fine-grained library customization in statically linked libraries. Using midilib as a case study, we demonstrate that dependence analysis can identify and remove "resultless" code—instructions that perform computations never utilized by the host application. Our approach achieves significant reductions in code size, removing over 50% of instructions in target functions.
Introduction
Modern software development relies heavily on third-party libraries to accelerate production. However, these libraries are typically designed for general-purpose use, containing a vast array of features that any single application may only partially utilize. When libraries are statically linked, this "jack-of-all-trades" design leads to significant code bloat.
Code bloat is more than a storage concern; it impacts security by increasing the attack surface and degrades performance through increased memory pressure and CPU cache misses. While traditional "dead-code elimination" (DCE) removes unreachable code, it often misses "resultless" code—instructions that are executed but whose outputs are never consumed. This paper proposes a fine-grained customization scheme to prune these unnecessary computations at the instruction level.
Methodology
The proposed customization framework leverages the LLVM compiler infrastructure to perform two primary types of pruning:
1. Eliminating Resultless Field Assignments
Many library functions return complex data structures (e.g., C structs). Often, an application only requires a small subset of the fields within these structures. Our tool maps the memory offsets of fields and identifies those never read by the application. Using dependence analysis, the tool traces backward from these unused fields to identify the specific instructions responsible for their calculation. If an instruction's only purpose is to populate an unused field, it is removed.
2. Eliminating Unused Packet Types
Libraries often use dispatcher patterns, such as switch statements, to handle various data types or packets. If an application only processes a specific subset of these types, the logic for all other types becomes redundant. By identifying mismatched control dependencies, the tool can prune the logic associated with unused packet types.
Case Study: midilib
To evaluate the effectiveness of this approach, we conducted a case study on midilib, an open-source C library for MIDI file I/O. We analyzed the library's interaction with five utility applications: m2rtttl, mididump, mfc120, mozart, and miditest. The primary analysis focused on the midifile.c component and its interaction with the m2rtttl utility.
Key Insights
- Structural Opportunities: Data structures are a primary source of bloat. In our study, the
MIDI_MSGstruct contained 44 primitive fields, yet the application utilized only 9. This discrepancy indicates that a significant portion of the library's internal logic is dedicated to "under-the-hood" computations that provide no value to the specific application. - Beyond Reachability: By focusing on data and control dependencies rather than simple reachability, we can identify code that is technically "live" but functionally "dead." This allows for a much deeper level of optimization than standard compiler passes.
Results
The techniques were applied to the midiReadGetNextMessage function within midilib. The results demonstrate substantial efficiency gains:
- Field Pruning: Identifying four unused fields allowed for the removal of 51 LLVM instructions (7% of the function).
- Control Path Pruning: Identifying unused packet types led to the removal of 355 out of 722 LLVM instructions.
- Combined Impact: By integrating both techniques, we successfully removed 367 LLVM instructions, representing 50.83% of the target function's instructions and a 39.63% reduction in source code lines.
Limitations and Future Work
While promising, the current proof-of-concept faces several challenges:
- C Unions: Because union fields share memory offsets, the tool currently treats all fields in a union as "used" if any single field is accessed, preventing potential breakage but limiting pruning depth.
- Static Analysis Constraints: Pointers and dynamic memory allocation complicate the proof of non-usage.
- Scalability: Future work will focus on automating this process for larger, more complex libraries and incorporating dynamic analysis to complement static findings.
Summary updated