Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace Standard C++ Allocator?

Tags:

c++

allocator

I want to replace the standard allocator with a more robust allocator (the C++ standard only requires an overflow check on vector::resize). The various C++ allocators supplied with many libraries fall flat on their face when fed negative self tests.

I have access to a more robust allocator. ESAPI's allocator not only checks for overflow, it also has debug instrumentation to help find mistakes. http://code.google.com/p/owasp-esapi-cplusplus/source/browse/trunk/esapi/util/zAllocator.h.

Is there a standard way to replace the C++ allocator used in a program without too much effort? I also want to ensure its replaced in library code, which I may not have access to source code.

like image 899
jww Avatar asked Nov 07 '12 05:11

jww


1 Answers

Unlike malloc which is a library function that can be replaced by another function with the same signature, std::allocator is a class template and template code is instantiated as needed and inlined into code that uses it. Some standard library code will have already been compiled into the library's object files and will contain instantiated std::allocator code which can't be replaced. So the only way is if the standard library provides some non-standard way to replace its std::allocator. Luckily, GCC's libstdc++ allows you to do just that, allowing you to select the implementation used for std::allocator when GCC is configured and built, with a few different choices

It wouldn't be too much work to add the ESAPI allocator to the GCC sources as one of the options, then rebuild GCC to use that allocator as the base class of std::allocator providing its implementation. You might need to tweak the ESAPI allocator code a bit, and maybe alter the libstdc++ configure script to allow you to say --enable-libstdcxx-allocator=esapi

like image 57
Jonathan Wakely Avatar answered Sep 22 '22 20:09

Jonathan Wakely