A forum for reverse engineering, OS internals and malware analysis 

Forum for discussion about user-mode development.
 #12981  by native99
 Wed May 02, 2012 6:36 am
Hi
i'm building a simple code in wdk in "native subsystem" i have a library that overload new and delete operator here is the code:
header file
Code: Select all
#include<ntddk.h>

    #define HEAP_ZERO_MEMORY    0x00000008
    #define HEAP_GROWABLE       0x00000002

    typedef NTSTATUS
    (NTAPI * PRTL_HEAP_COMMIT_ROUTINE)(
        IN PVOID Base,
        IN OUT PVOID *CommitAddress,
        IN OUT PSIZE_T CommitSize
        );

    typedef struct _RTL_HEAP_PARAMETERS {
        ULONG Length;
        SIZE_T SegmentReserve;
        SIZE_T SegmentCommit;
        SIZE_T DeCommitFreeBlockThreshold;
        SIZE_T DeCommitTotalFreeThreshold;
        SIZE_T MaximumAllocationSize;
        SIZE_T VirtualMemoryThreshold;
        SIZE_T InitialCommit;
        SIZE_T InitialReserve;
        PRTL_HEAP_COMMIT_ROUTINE CommitRoutine;
        SIZE_T Reserved[ 2 ];
    } RTL_HEAP_PARAMETERS, *PRTL_HEAP_PARAMETERS;


    void*  operator new(size_t sz);

    void   operator delete(void* p);

    void* mg_malloc(unsigned int size);
    int mg_free(void *buffer);
    void InitAPSupportLibrary();
    void FinishAPSupportLibrary();

    extern "C"
    {
    PVOID RtlAllocateHeap(
            __in      PVOID HeapHandle,
            __in_opt  ULONG Flags,
            __in      SIZE_T Size
            );
        BOOLEAN RtlFreeHeap(
            __in      PVOID HeapHandle,
            __in_opt  ULONG Flags,
            __in      PVOID HeapBase
            );

    PVOID

    RtlCreateHeap(


      IN ULONG                Flags,
      IN PVOID                Base ,
      IN ULONG                Reserve ,
      IN ULONG                Commit,
      IN BOOLEAN              Lock ,
      IN PRTL_HEAP_PARAMETERS RtlHeapParams  );




        PVOID RtlDestroyHeap(
            __in  PVOID HeapHandle
            );
    }
and the cpp file code:
Code: Select all
#include "main.h"

    PVOID mainHeapHandle = NULL;


    void InitAPSupportLibrary()
    {
        mainHeapHandle = RtlCreateHeap(HEAP_GROWABLE,NULL,0,0,NULL,0);
    }

    void FinishAPSupportLibrary()
    {
        if(mainHeapHandle) RtlDestroyHeap(mainHeapHandle);

    }


    void* mg_malloc(unsigned int size)
    {

        if(!mainHeapHandle) 
            InitAPSupportLibrary();

        void *buf  = RtlAllocateHeap(mainHeapHandle,HEAP_ZERO_MEMORY,size);
        return buf;

    }

    int mg_free(void *buffer)
    {
        if (!mainHeapHandle) return 0;
        return RtlFreeHeap(mainHeapHandle,0,buffer)?1:0;
    }

    void*  operator new(size_t sz)
    {
        void* p = (void*)mg_malloc(sz);
        return p;
    }
    void  operator delete(void* p)
    {
        mg_free(p);
    }
and the error are :
error C2373: 'operator new' : redefinition; different type modifiers
error C2373: 'operator delete' : redefinition; different type modifiers

thanks for your help
 #12985  by xdeadcode
 Wed May 02, 2012 9:51 am
Hi native99,

You must know what is your project settings, but assuming that it is "__cdecl" project then try to modify declarations of new and delete operators to something like:
Code: Select all
void* __cdecl operator new(size_t sz);
void   __cdecl operator delete(void* p);
Best regards