C++ const and Immutability: An Empirical Study of Writes-Through-const Jon Eyolfson1 and Patrick Lam2 1 University of Waterloo Waterloo, ON, Canada rtifact Comple * A t * te n * A te * W is E s e P n
[email protected] l C l o D O C o * * c u O e m s E u C 2 University of Waterloo e e n R E t v e o d t y * s E a a * l d u e a Waterloo ON, Canada t
[email protected] Abstract The ability to specify immutability in a programming language is a powerful tool for developers, enabling them to better understand and more safely transform their code without fearing unin- tended changes to program state. The C++ programming language allows developers to specify a form of immutability using the const keyword. In this work, we characterize the meaning of the C++ const qualifier and present the ConstSanitizer tool, which dynamically verifies a stricter form of immutability than that defined in C++: it identifies const uses that are either not con- sistent with transitive immutability, that write to mutable fields, or that write to formerly-const objects whose const-ness has been cast away. We evaluate a set of 7 C++ benchmark programs to find writes-through-const, establish root causes for how they fail to respect our stricter definition of immutability, and assign attributes to each write (namely: synchronized, not visible, buffer/cache, delayed initialization, and incorrect). ConstSanitizer finds 17 archetypes for writes in these programs which do not respect our version of immutability.