Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C++ is it possible to specify which delete operator is used?

Tags:

c++

c++14

clang++

I am instrumenting some code and noticed that with the C++14 features there are two new delete operators (From http://en.cppreference.com/w/cpp/memory/new/operator_delete):

These are 5-6) Called instead of (1-2) if a user-defined replacement is provided except that it's implementation-defined whether (1-2) or (5-6) is called when deleting objects of incomplete type and arrays of non-class and trivially-destructible class types (since C++17). The standard library implementations are identical to (1-2).

I have overloaded these and wanted to call these two exclusively. When I overload these two with gcc I don't have a problem. With clang++ I get an undefined reference to operator delete(void*)

Here is the code

void* operator new(long unsigned int howMuch) {
    return reinterpret_cast<void*>(0xdeadbeef);
}

void* operator new[](long unsigned int howMuch) {
    return reinterpret_cast<void*>(0xdeadbeef);
}

void operator delete(void* what, long unsigned int howmuch) {
        if(what != reinterpret_cast<void*>(0xdeadbeef)) __builtin_trap();
        if(howmuch != 1) __builtin_trap();
}

extern "C"
void _start() {
    delete new char;
    asm("syscall" : : "a"(60) : ); 
}

Compiled with gcc: g++ -ggdb -std=c++14 -nostdlib -fno-builtin -fno-exceptions 1.cc there is no problem and it runs fine.

Is it possible to do this with llvm/clang?

like image 373
Peter Stpme Avatar asked Jan 29 '16 05:01

Peter Stpme


People also ask

When to use delete [] or delete?

delete is used for one single pointer and delete[] is used for deleting an array through a pointer.

Which operator is used to delete?

delete keyword in C++ Delete is an operator that is used to destroy array and non-array(pointer) objects which are created by new expression. New operator is used for dynamic memory allocation which puts variables on heap memory.

Can you use Delete in C?

There's no new / delete expression in C. The closest equivalent are the malloc and free functions, if you ignore the constructors/destructors and type safety.

What is the syntax for delete operator?

Syntax of delete operatordelete pointer_variable; // delete ptr; It deallocates memory for one element.


1 Answers

You can explicitly call sized or non-sized delete operators like this:

char* ptr = new char;
delete ptr; // compiler selects which to call
operator delete(ptr); // explicitly call the non-sized delete 
operator delete(ptr, 1); // explicitly call sized delete

For a full example:

void* operator new(long unsigned int howMuch) {                                                                                                                                               
    return reinterpret_cast<void*>(0xdeadbeef);
}

void* operator new[](long unsigned int howMuch) {
    return reinterpret_cast<void*>(0xdeadbeef);
}

void operator delete(void* what) {
    if(what != reinterpret_cast<void*>(0xdeadbeef)) __builtin_trap();
}

void operator delete(void* what, long unsigned int howmuch) {
    if(what != reinterpret_cast<void*>(0xdeadbeef)) __builtin_trap();
    if(howmuch != 1) __builtin_trap();
}

extern "C"
void _start() {
    char* ptr = new char;
    delete ptr;
    operator delete(ptr);
    operator delete(ptr, 1);
    asm("syscall" : : "a"(60) : ); 
}

Compiling and looking at generated code it is clear which operators are called when:

$ clang++ -std=c++14 -nostdlib  -fno-builtin  -fno-exceptions -fsized-deallocation sized-deallocation.cpp -o sized-deallocation.bin && gdb -batch -ex 'file sized-deallocation.bin' -ex 'disassemble _start' | c++filt
Dump of assembler code for function _start:
   0x0000000000401070 <+0>: push   %rbp
   0x0000000000401071 <+1>: mov    %rsp,%rbp
   0x0000000000401074 <+4>: sub    $0x10,%rsp
   0x0000000000401078 <+8>: mov    $0x1,%eax
   0x000000000040107d <+13>:    mov    %eax,%edi
   0x000000000040107f <+15>:    callq  0x401000 <operator new(unsigned long)>
   0x0000000000401084 <+20>:    mov    %rax,-0x8(%rbp)
   0x0000000000401088 <+24>:    mov    -0x8(%rbp),%rax
   0x000000000040108c <+28>:    cmp    $0x0,%rax
   0x0000000000401090 <+32>:    mov    %rax,-0x10(%rbp)
   0x0000000000401094 <+36>:    je     0x4010aa <_start+58>
   0x000000000040109a <+42>:    mov    $0x1,%eax
   0x000000000040109f <+47>:    mov    %eax,%esi
   0x00000000004010a1 <+49>:    mov    -0x10(%rbp),%rdi
   0x00000000004010a5 <+53>:    callq  0x401040 <operator delete(void*, unsigned long)>
   0x00000000004010aa <+58>:    mov    -0x8(%rbp),%rdi
   0x00000000004010ae <+62>:    callq  0x401020 <operator delete(void*)>
   0x00000000004010b3 <+67>:    mov    $0x1,%eax
   0x00000000004010b8 <+72>:    mov    %eax,%esi
   0x00000000004010ba <+74>:    mov    -0x8(%rbp),%rdi
   0x00000000004010be <+78>:    callq  0x401040 <operator delete(void*, unsigned long)>
   0x00000000004010c3 <+83>:    mov    $0x3c,%eax
   0x00000000004010c8 <+88>:    syscall 
   0x00000000004010ca <+90>:    add    $0x10,%rsp
   0x00000000004010ce <+94>:    pop    %rbp
   0x00000000004010cf <+95>:    retq   
End of assembler dump.

The actual reason though why you are getting undefined reference to `operator delete(void*)' with Clang is that (as @T.C. said) Clang needs the -fsized-deallocation flag to enable C++14 sized deallocation.

Your example compiles without error if the following command is used:

clang++ -ggdb -std=c++14 -nostdlib  -fno-builtin  -fno-exceptions -fsized-deallocation 1.cc

Since Clang 3.7 C++14 sized deallocation is disabled by default:

C++ Support in Clang > C++14 implementation status > C++ Sized Deallocation N3778

(7): In Clang 3.7 and later, sized deallocation is only enabled if the user passes the -fsized-deallocation flag. The user must supply definitions of the sized deallocation functions, either by providing them explicitly or by using a C++ standard library that does. libstdc++ added these functions in version 5.0, and libc++ added them in version 3.7.

Clang 3.7 Release Notes > What’s New in Clang 3.7? > New Compiler Flags

The sized deallocation feature of C++14 is now controlled by the -fsized-deallocation flag. This feature relies on library support that isn’t yet widely deployed, so the user must supply an extra flag to get the extra functionality.

The reasoning for this change was that these functions were missing in widely deployed standard libraries at the time (2015-03-19):

C++14: Disable sized deallocation by default due to ABI breakage

There are no widely deployed standard libraries providing sized deallocation functions, so we have to punt and ask the user if they want us to use sized deallocation. In the future, when such libraries are deployed, we can teach the driver to detect them and enable this feature.

The manual entry for this option can be found here:

Clang command line argument reference > Compilation flags > Target-independent compilation options

-fsized-deallocation, -fno-sized-deallocation

Enable C++14 sized global deallocation functions

like image 143
Iwan Aucamp Avatar answered Nov 01 '22 04:11

Iwan Aucamp