Submitted:
31 October 2023
Posted:
02 November 2023
You are already at the latest version
Abstract
Keywords:
Introduction
- We summarize existing work on safe C++ standards and safe subsets of C++.
- We present a static analysis tool and methodology for identifying potentially problematic code constructs in existing C/C++ code.
- We analyze data from 5.8 million code samples in the Exebench benchmark suite and 5 modern C++ applications to determine how close existing C/C++ code is to conforming to a safe subset of C++.
1. Background
1.1. Memory Safety Errors in C/C++
1.2. Recent Arguments Against C/C++
1.2.1. Executive Order (EO) 14028
1.2.2. NSA
1.2.3. NIST
1.3. ISO C++ Response
2. Safe C/C++ Standards
2.1. CPP Core Guidelines
2.2. MISRA C++
2.3. AUTOSAR C++
2.4. CERT
2.5. High Integrity C++
2.6. Joint Strike Fighter
2.7. Summary and Comparison
- C++ Core F.8: Prefer pure functions.
- CERT EXP54-CPP: Do not access an object outside its lifetime.
- JSF AV 70.1: An object shall not be improperly used before its lifetime begins or after its lifetime ends.
- AUTOSAR M5-2-5: An array or container shall not be accessed beyond its range.
- JSF AV 15 Provision shall be made for run-time checking (defensive programming).
- CERT STR53-CPP: Range check element access.
3. Safe Subsets of C/C++
3.1. SafeC
3.2. CCured
3.3. Cyclone
3.4. Ironclad C++
3.5. What work has been required to translate to previous safe subsets of C++?
4. Methodology
- (Q1) How often are pointers found in modern C++ code?
- (Q2) How often are "problematic" code constructs found in modern C++ code?
- (Q3) Is "modern" C++ code closer to a safe subset than C++ code at large?
4.1. Static Analysis Patterns
4.1.1. Pointers
- C++ Core ES.42: Keep use of pointers simple and straightforward.
- C++ Core ES.65: Don’t dereference an invalid pointer.
- C++ Core R.3: A raw pointer is non-owning.
- C++ Core I.11: Never transfer ownership by a raw pointer (T*) or reference (T&).
4.1.2. Unsafe Functions
- C++ Core R.10: Avoid malloc() and free().
- AUTOSAR A18-5-1: Functions malloc, calloc, realloc, and free shall not be used.
- C++ Core SL.4: Use the standard library in a type-safe manner.
- HIC 17.2.1: Wrap use of the C standard library.
- AUTOSAR A17-1-1: Use of the C standard library shall be encapsulated and isolated.
4.1.3. Casts
- C++ Core ES.48: Avoid casts.
- C++ Core ES.49: If you must use a cast, use a named cast.
- C++ Core C.146: Use dynamic_cast where class hierarchy navigation is unavoidable.
- HIC 5.4.1: Only use casting forms: static_cast (excl. void*), dynamic_cast, or explicit constructor call.
- AUTOSAR A5-2-2: Traditional C-style casts shall not be used.
- AUTOSAR A5-2-4: reinterpret_cast shall not be used.
- MISRA 5-2-4: C-style casts (other than void casts) and functional notation casts (other than explicit constructor calls) shall not be used.
- C++ Core C.46: By default, declare single-argument constructors explicit.
- C++ Core C.181: Avoid "naked" unions.
4.1.4. References
- C++ Core F.43: Never (directly or indirectly) return a pointer or reference to a local object.
- JSF AV 111: A function shall not return a pointer or reference to a non-static local object.
- MISRA 7-5-1: A function shall not return a reference or a pointer to an automatic variable (including parameters) defined within the function.
- MISRA 7-5-3: A function shall not return a reference or pointer to a parameter that is passed by reference or const reference.
4.1.5. Arrays
- C++ Core I.13: Do not pass an array as a single pointer.
- C++ Core ES.27: Use std::array or stack_array for arrays on the stack.
- HIC 8.1.1: Do not use multiple levels of pointer indirection.
4.2. Running Analysis on Exebench
4.3. Running Analysis on Modern C++ Programs
5. Results
5.1. Pointers, Smart Pointers, and Void Pointers
5.2. Unsafe Functions
5.3. Casts and Unions
5.4. References
5.5. Arrays
5.6. Summary
6. Limitations and Future Work
7. Conclusions
Funding
Data Availability Statement
Acknowledgments
Conflicts of Interest
Abbreviations
| AST | Abstract Syntax Tree |
| AUTOSAR | Automotive Open System Architecture |
| CERT | Computer Emergency Response Team |
| HIC | High Integrity C++ |
| JSON | JavaScript Object Notation |
| MISRA | Motor Industry Software Reliability Association |
| JSF | Joint Strike Fighter |
| NSA | National Security Agency (United States) |
| NIST | National Institute for Standards and Technology (United States) |
References
- Hinnant, R; Orr, R.; Stroustrup, B.; Vandevoorde, D.; Wong, M. DG Opinion on Safety for ISO C++. In The C++ Standards Committee.; JTC1, SC22, WG21. ISO: 2023; Document Number P2759R0.
- National Vulnerability Database CWE Over Time. Available online: https://nvd.nist.gov/general/visualizations/vulnerability-visualizations/cwe-over-time (accessed on 27 July 2023).
- National Security Agency Cybersecurity Information Sheet. Available online: https://media.defense.gov/2022/Nov/10/ 2003112742/-1/-1/0/CSI_SOFTWARE_MEMORY_SAFETY.PDF (accessed on 27 July 2023).
- Armengol-Estape, J.; Woodruff, J.; Brauckmann, A.; Magalhaes, J.; De Souza, W.; O’Boyle, M. Exebench: An ML-Scale Dataset of Executable C Functions. In Proceedings of the 6th ACM SIGPLAN International Symposium on Machine Learning.. New York, NY, USA, 2022. MAPS 2022, Association for Computing Machinery, 50–59.
- Zhou, J.; Criswell, J.; Hicks, M. Fat Pointers for Temporal Memory Safety in C. In Proceedings of the ACM on Programming Languages.. OOPSLA 2023, Association for Computing Machinery, 316–347.
- Miller, M. Trends, Challnges, and strategic shifts in the software vulnerability mitigation landscape. BlueHat IL, February 7th, 2019. Available online: https://github.com/Microsoft/MSRC-Security-Research/blob/master/presentations/2019_02_BlueHatIL/ 2019_01%20-%20BlueHatIL%20-%20Trends%2C%20challenge%2C%20and%20shifts%20in%20software%20vulnerability %20mitigation.pdf (accessed on 4 Oct 2023).
- Taylor, A.; Whalley, A.; Jansens, D.; Nasko, O. An update on Memory Safety in Chrome. Google Security Blog, September 21, 2021. Available online: https://security.googleblog.com/2021/09/an-update-on-memory-safety-in-chrome.html (accessed on 4 Oct 2023).
- Biden, J. Executive Order on Improving the Nation’s Cybersecurity. The White House, May 12, 2021. Available online: https://www.whitehouse.gov/briefing-room/presidential-actions/2021/05/12/executive-order-on-improving-the-nations-cybersecurity/ (accessed on 6 Oct 2023).
- Periklis Akritidis, Manuel Costa, Miguel Castro, and Steven Hand. 2009. Baggy bounds checking: an efficient and backwards-compatible defense against out-of-bounds errors. In Proceedings of the 18th conference on USENIX security symposium (SSYM’09). USENIX Association, USA, 51–66.
- Santosh Nagarakatte, Jianzhou Zhao, Milo M.K. Martin, and Steve Zdancewic. 2009. SoftBound: highly compatible and complete spatial memory safety for c. SIGPLAN Not. 44, 6 (09), 245–258. [CrossRef]
- George, C. Necula, Jeremy Condit, Matthew Harren, Scott McPeak, and Westley Weimer. 2005. CCured: type-safe retrofitting of legacy software. ACM Trans. Program. Lang. Syst. 27, 3 (May 2005), 477–526. [CrossRef]
- Jeremy Condit, Matthew Harren, Scott McPeak, George C. Necula, and Westley Weimer. 2003. CCured in the real world. In Proceedings of the ACM SIGPLAN 2003 conference on Programming language design and implementation (PLDI ’03). Association for Computing Machinery, New York, NY, USA, 232–244. [CrossRef]
- Trevor Jim, J. Greg Morrisett, Dan Grossman, Michael W. Hicks, James Cheney, and Yanling Wang. 2002. Cyclone: A Safe Dialect of C. In Proceedings of the General Track of the annual conference on USENIX Annual Technical Conference (ATEC ’02). USENIX Association, USA, 275–288.
- Todd M. Austin, Scott E. Breach, and Gurindar S. Sohi. 1994. Efficient detection of all pointer and array access errors. In Proceedings of the ACM SIGPLAN 1994 conference on Programming language design and implementation (PLDI ’94). Association for Computing Machinery, New York, NY, USA, 290–301. [CrossRef]
- Christian DeLozier, Richard Eisenberg, Santosh Nagarakatte, Peter-Michael Osera, Milo M.K. Martin, and Steve Zdancewic. 2013. Ironclad C++: a library-augmented type-safe subset of c++. In Proceedings of the 2013 ACM SIGPLAN international conference on Object oriented programming systems languages and applications (OOPSLA ’13). Association for Computing Machinery, New York, NY, USA, 287–304. [CrossRef]
- SaferCPlusPlus, Hardened C++ for the internet age. Available online: http://duneroadrunner.github.io/SaferCPlusPlus/ (accessed on 25 October 2023).
- International Standard ISO/IEC 14882:2020. Programming Languages – C++. International Organization for Standards, 2020.
- High Integrity C++ Standard. Available online: https://www.perforce.com/resources/qac/high-integrity-cpp-coding-standard (accessed on 27 Oct 2023).
- SEI CERT C++ Coding Standard. Available online: https://wiki.sei.cmu.edu/confluence/pages/viewpage.action?pageId=88046682 (accessed on 29 Oct 2023).
- Joint Strike Fighter Air Vehicle C++ Coding Standards. Available online: https://www.stroustrup.com/JSF-AV-rules.pdf (accessed on 29 Oct 2023).
- C++ Core Guidelines. Available online: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines.html (accessed on 29 Oct 2023).
- AUTOSAR Guidelines for the use of the C++14 language in critical and safetyrelated systems. Available online: https://www.autosar.org/fileadmin/standards/R22-11/AP/AUTOSAR_RS_CPP14Guidelines.pdf (accessed on 29 Oct 2023).
- MISRA Publications. Available online: https://misra.org.uk/publications/ (accessed on 01 Oct 2023).
- Anderson Faustino da Silva, Bruno Conde Kind, José Wesley de Souza Magalhães, Jerônimo Nunes Rocha, Breno Campos Ferreira Guimarães, and Fernando Magno Quinão Pereira. Anghabench: A suite with one million compilable c benchmarks for code-size reduction. In 2021 IEEE/ACM International Symposium on Code Generation and Optimization (CGO), pages 378–390, 2021. [CrossRef]
- Georgios Gousios, Bogdan Vasilescu, Alexander Serebrenik, and Andy Zaidman. Lean GHTorrent: Github data on demand. MSR, 2014. [CrossRef]
- SSH CRC32 attack detection code contains remote integer overflow. Available online: https://www.kb.cert.org/vuls/id/945216 (accessed on 30 Oct 2023).
- AST Matcher Reference. Available online: https://clang.llvm.org/docs/LibASTMatchersReference.html (accessed on 30 Oct 2023).








| Safe C/C++ Standard | Initialization | Spatial | Lifetime | Type |
|---|---|---|---|---|
| Core Guidelines [21] | I.12, F.60, F.22, F.23, ES.20, ES.22 | I.13, C.90, C.152, R.14, ES.27, ES.42, ES.55, ES.71, ES.103 | I.11, F.7, F.26, F.27, F.42, F.43, F.44, F.45, F.53, C.21, C.31, C.33, C.49, C.82, C.127, C.149, R.1, R.3, R.4, R.5, R.10, R.11, R.36, ES.61, ES.65 | I.4, F.55, C.46, C.146, C.164, C.181, C.182, C.183, ES.34, ES.48, ES.49, SL.4 |
| MISRA [23] | 0-3-1, 8-5-1 | 0-3-1, 3-1-3, 5-0-15, 5-2-12, 18-0-5, 27-0-1 | 0-3-1, 7-5-1, 7-5-2, 7-5-3, 15-0-2, 18-4-1 | 5-2-2, 5-2-4, 5-2-6, 5-2-7, 5-2-8, 9-5-1 |
| Autosar [22] | A3-3-2, A5-3-2, A8-5-0, A12-6-1 | M5-0-15, M5-2-5, M5-2-12, A17-1-1, M18-0-5, A18-1-1, A27-0-4, A27-0-2 | A3-8-1, A5-1-4, A5-3-3, M7-5-1, M7-5-2, A7-5-1, A8-4-11, A8-4-12, A8-4-13, A18-1-4, A18-5-1, A18-5-3, A18-5-8, A20-8-1 | M5-2-2, A5-2-1, A5-2-2, A5-2-4, M5-2-6, M5-2-8, M5-2-9, A9-5-1, A13-5-2, A13-5-3 |
| CERT [19] | EXP53 | CNTR50, CTR53, CTR55, STR50, STR53 | EXP51, EXP54, EXP61 | DCL50, EXP58, INT50, MEM50, MEM51, MEM56 |
| HIC [18] | 8.4.1 | 4.1.1, 6.2.1, 17.2.1 | 3.4.1, 3.4.2, 8.1.1 | 3.5.1, 5.4.1, 5.4.3, 12.1.1 |
| JSF [20] | 71, 71.1, 117, 118, 142, 143, 174 | 15, 20-25, 96, 97, 215 | 70.1, 111, 173, 206 | 153, 178, 179, 182, 183, 185 |
| Application | Description | Lines of Code |
|---|---|---|
| cereal | Serialization Library | 31,355 |
| fmt | Formatting library alternative to stdio and iostream | 43,993 |
| folly | Core library components used at Facebook | 377,963 |
| json | JSON support for modern C++ | 103,137 |
| redex | Bytecode optimizer for Android apps | 281,642 |
Disclaimer/Publisher’s Note: The statements, opinions and data contained in all publications are solely those of the individual author(s) and contributor(s) and not of MDPI and/or the editor(s). MDPI and/or the editor(s) disclaim responsibility for any injury to people or property resulting from any ideas, methods, instructions or products referred to in the content. |
© 2023 by the authors. Licensee MDPI, Basel, Switzerland. This article is an open access article distributed under the terms and conditions of the Creative Commons Attribution (CC BY) license (http://creativecommons.org/licenses/by/4.0/).