CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2014
    Posts
    61

    ArrayList: implementation from usermode to kernel mode

    I have this arraylist implementation working fine in usermode, when i tried pass to kernel mode version i receive a bsod on this part:

    Code:
    FAULTING_SOURCE_LINE_NUMBER:  128
     
    FAULTING_SOURCE_CODE:  
       124: {
       125:     int index = 0;
       126:     while (index <= list->current)
       127:     {
    >  128:      if (e->data->Length == list->elements[index].data->Length &&
       129:             0 == wcsncmp(e->data->Buffer,
       130:                 list->elements[index].data->Buffer,
       131:                 list->elements[index].data->Length))
       132:             return index;
       133:         index++;
    kernel mode version:

    Code:
    NTSTATUS NTAPI RtlDuplicateUnicodeString(IN ULONG Flags, IN PCUNICODE_STRING SourceString, OUT PUNICODE_STRING DestinationString);
    
    typedef unsigned char uint8_t;
     
    typedef struct
    {
        UNICODE_STRING *data;
    }Element;
     
    typedef struct
    {
        int current;
        int size;
        int increment_rate;
        Element *elements;
    }ArrayList;
     
    void FreeUString(UNICODE_STRING *src)
    {
        RtlFreeUnicodeString(src);
        src->Length = src->MaximumLength = 0;
    }
     
    void initWithSizeAndIncRate(ArrayList *const list, int size, int rate)
    {
        list->size = size;
        list->increment_rate = rate;
        list->elements = (Element*)ExAllocatePoolWithTag(NonPagedPool, sizeof(Element), 'Fo');
        list->current = -1;
    }
     
    void initWithSize(ArrayList *const list, int size)
    {
        initWithSizeAndIncRate(list, size, 50);
    }
     
    void init(ArrayList *const list)
    {
        initWithSize(list, 100);
    }
     
    void arraryCopy(void *dest, int dIndex, const void* src, int sIndex, int len, int destLen, size_t size)
    {
        uint8_t *udest = (uint8_t*)dest;
        uint8_t *usrc = (uint8_t*)src;
        dIndex *= size;
        sIndex *= size;
        len *= size;
        destLen *= size;
     
        if (src != dest)
        {
            memcpy(&udest[dIndex], &usrc[sIndex], len);
        }
        else
        {
            if (dIndex > sIndex)
            {
                uint8_t *tmp = (uint8_t*)ExAllocatePoolWithTag(NonPagedPool, size, 'F');
                memcpy(tmp, &udest[dIndex], (destLen - dIndex));
                memcpy(&udest[dIndex], &usrc[sIndex], len);
                memcpy(&udest[dIndex + len], tmp, (destLen - dIndex));
                ExFreePoolWithTag(tmp, 'F');
            }
            else if (sIndex > dIndex)
            {
                memcpy(&udest[dIndex], &usrc[sIndex], (destLen - sIndex) + 1);
            }
            else
                return;
        }
    }
     
    void clear(ArrayList *const list)
    {
        while (list->current >= 0)
        {
            FreeUString(list->elements[list->current].data);
            list->current--;
        }
    }
     
    void wide(ArrayList* const list)
    {
        list->size += list->increment_rate;
        Element *newArr = (Element*)ExAllocatePoolWithTag(NonPagedPool, sizeof(Element), 'T');
        arraryCopy(newArr, 0, list->elements, 0, list->current, list->size, sizeof(Element));
        //ExFreePoolWithTag(list->elements, 'Foo');
        list->elements = newArr;
    }
     
    int add(ArrayList *const list, Element *e)
    {
        UNICODE_STRING *dest = { NULL };
        NTSTATUS status = STATUS_SUCCESS;
     
        if (++list->current < list->size)
        {
            status = RtlDuplicateUnicodeString(1, e->data, dest);
            DbgPrint("RtlDuplicateUnicodeString() status: 0x%x", status);
     
            list->elements[list->current].data = dest;
            return 1;
        }
        else
        {
            wide(list);
            status = RtlDuplicateUnicodeString(1, e->data, dest);
            DbgPrint("RtlDuplicateUnicodeString() status: 0x%x", status);
            list->elements[list->current].data = dest;
            return 1;
        }
        return 0;
    }
     
    int indexOf(const ArrayList *const list, Element *e)
    {
        int index = 0;
        while (index <= list->current)
        {
            if (e->data->Length == list->elements[index].data->Length &&
                0 == wcsncmp(e->data->Buffer,
                    list->elements[index].data->Buffer,
                    list->elements[index].data->Length))
                return index;
            index++;
        }
        return 0;
    }
     
    void clean(ArrayList *list)
    {
        ExFreePoolWithTag(list->elements, 'Fo');
    }
     
    ArrayList list;
    Element e;
    usermode version:

    Code:
    typedef struct
    {
        UNICODE_STRING *data;
    }Element;
     
    typedef struct
    {
        int current;
        int size;
        int increment_rate;
        Element *elements;
    }ArrayList;
     
    UNICODE_STRING * CopyUString(UNICODE_STRING *src)
    {
        UNICODE_STRING *dest = (UNICODE_STRING *)malloc(sizeof(UNICODE_STRING));
        dest->Length = src->Length;
        dest->MaximumLength = src->MaximumLength;
        dest->Buffer = (PWSTR)malloc(sizeof(WCHAR) * dest->MaximumLength);
        memcpy(dest->Buffer, src->Buffer, sizeof(WCHAR) * dest->MaximumLength);
        return dest;
    }
     
    void FreeUString(UNICODE_STRING *src)
    {
        free(src->Buffer);
        src->Length = src->MaximumLength = 0;
    }
     
    void initWithSizeAndIncRate(ArrayList *const list, int size, int rate)
    {
        list->size = size;
        list->increment_rate = rate;
        list->elements = (Element*)calloc(sizeof(Element), list->size);
        list->current = -1;
    }
     
    void initWithSize(ArrayList *const list, int size)
    {
        initWithSizeAndIncRate(list, size, 50);
    }
     
    void init(ArrayList *const list)
    {
        initWithSize(list, 100);
    }
     
    void arraryCopy(void *dest, int dIndex, const void* src, int sIndex, int len, int destLen, size_t size)
    {
        uint8_t *udest = (uint8_t*)dest;
        uint8_t *usrc = (uint8_t*)src;
        dIndex *= size;
        sIndex *= size;
        len *= size;
        destLen *= size;
     
        if (src != dest)
        {
            memcpy(&udest[dIndex], &usrc[sIndex], len);
        }
        else
        {
            if (dIndex > sIndex)
            {
                uint8_t *tmp = (uint8_t*)calloc(destLen, size);
                memcpy(tmp, &udest[dIndex], (destLen - dIndex));
                memcpy(&udest[dIndex], &usrc[sIndex], len);
                memcpy(&udest[dIndex + len], tmp, (destLen - dIndex));
                free(tmp);
            }
            else if (sIndex > dIndex)
            {
                memcpy(&udest[dIndex], &usrc[sIndex], (destLen - sIndex) + 1);
            }
            else
                return;
        }
    }
     
    void clear(ArrayList *const list)
    {
        while (list->current >= 0)
        {
            FreeUString(list->elements[list->current].data);
            list->current--;
        }
    }
     
    void wide(ArrayList* const list)
    {
        list->size += list->increment_rate;
        Element *newArr = (Element*)calloc(sizeof(Element), list->size);
        arraryCopy(newArr, 0, list->elements, 0, list->current, list->size, sizeof(Element));
        free(list->elements);
        list->elements = newArr;
    }
     
    int add(ArrayList *const list, Element *e)
    {
        if (++list->current < list->size)
        {
            list->elements[list->current].data = CopyUString(e->data);
            return 1;
        }
        else
        {
            wide(list);
            list->elements[list->current].data = CopyUString(e->data);
            return 1;
        }
        return 0;
    }
     
    int indexOf(const ArrayList *const list, Element *e)
    {
        int index = 0;
        while (index <= list->current)
        {
            if (e->data->Length == list->elements[index].data->Length &&
                0 == wcsncmp(e->data->Buffer,
                list->elements[index].data->Buffer,
                list->elements[index].data->Length))
                return index;
            index++;
        }
        return 0;
    }
     
    void clean(ArrayList *list)
    {
        free(list->elements);
    }
    Some idea about what's causing this bsod? thanks in advance.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: ArrayList: implementation from usermode to kernel mode

    Sorry for not answering your "main" question... but what is the reason to use the old "C" malloc/free instead of new/delete in a C++ program?
    Victor Nijegorodov

  3. #3
    Join Date
    Apr 2014
    Posts
    61

    Re: ArrayList: implementation from usermode to kernel mode

    Quote Originally Posted by VictorN View Post
    Sorry for not answering your "main" question... but what is the reason to use the old "C" malloc/free instead of new/delete in a C++ program?
    Because my driver project is in C. Have a big quantity of code already wrote in C sintaxe and i'm not disposed to change all this sintaxe.
    You have some experience with driver development for Windows? but i also think that this question is relative to C/C++ sintaxes.
    Even so, in kernel driver development new/delete operators not are avaiable, then i mandatorily also must use ExAllocatePoolWithTag/ExFreePoolWithTag of any way. See here.
    Last edited by FL4SHC0D3R; April 14th, 2018 at 03:09 PM.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: ArrayList: implementation from usermode to kernel mode

    Quote Originally Posted by FL4SHC0D3R View Post
    Because my driver project is in C. Have a big quantity of code already wrote in C sintaxe and i'm not disposed to change all this sintaxe.
    You have some experience with driver development for Windows? but i also think that this question is relative to C/C++ sintaxes.
    Sorry, I don't have any experience with driver development for Windows.
    Would you like to move your thread to the related Driver Forum?
    Victor Nijegorodov

  5. #5
    Join Date
    Apr 2014
    Posts
    61

    Re: ArrayList: implementation from usermode to kernel mode

    Yes, please.

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: ArrayList: implementation from usermode to kernel mode

    [moved from Visual C++ Forum]
    Victor Nijegorodov

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured