-
April 14th, 2018, 01:27 PM
#1
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.
-
April 14th, 2018, 02:31 PM
#2
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
-
April 14th, 2018, 02:57 PM
#3
Re: ArrayList: implementation from usermode to kernel mode
Originally Posted by VictorN
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.
-
April 14th, 2018, 03:02 PM
#4
Re: ArrayList: implementation from usermode to kernel mode
Originally Posted by FL4SHC0D3R
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
-
April 14th, 2018, 03:13 PM
#5
Re: ArrayList: implementation from usermode to kernel mode
-
April 14th, 2018, 03:17 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|