RE: Drivers Align Problems.

For alignment in general, take a look at:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vcconwindowsdataalignmentonipfx86x86-64.asp

Examples of x64 64-Bit Structure Alignment Requirements in DDK Help

DDK environment uses 8-byte alignment by default.

There are many cases you can send misaligned pointer to the function.
(Why do you take an undocumented API as an example, though?)
For example, ZwQueryInformationToken requires all structures must be aligned on a 32-bit boundary (4-byte boundary).

NTSTATUS
ZwQueryInformationToken(
IN HANDLE TokenHandle,
IN TOKEN_INFORMATION_CLASS TokenInformationClass,
OUT PVOID TokenInformation,
IN ULONG TokenInformationLength,
OUT PULONG ReturnLength
);

void Myfunc()
{
UCHAR buffer[32];
USHORT* shortedLength = &buffer[0];
PVOID information = buffer + sizeof(USHORT);
// deliberately use 2-byte aligned pointer
ZwQueryInformationToken(…, information, …);
–> this generates EXCEPTION_DATATYPE_MISALIGNMENT.
}

Pointer variables can be easily detected if it is aligned or not by testing low bits.
However, in IA64 systems, misalignment exceptions are CPU exception.

I assume that you are not creating a “driver” for the compiler (command line program of the compiler), but a real “device driver” (for the operating system kernels). And then, I am wondering why you need a “device driver” for the compiler, anyhow.

Regards,

Chesong Lee

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of tumtum@o2.pl
Sent: Tuesday, August 15, 2006 4:54 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] Drivers Align Problems.

Hi list,

I have some newbie question. Should data and code in the device driver always be aligned (align 4?). For example
calling ntdll.ZwGetContextThread will fail if the context struct is not aligned by 4 (error: EXCEPTION_DATATYPE_MISALIGNMENT)

Are there any rules for align? Is there a defult align? How do MSVC makes it, or how it knows where to align data and where not? I’m currently working for some basic student compiler project and i’m trying to implement driver building.

P.S I’m working on x86 processors.

Thanks for help!


Questions? First check the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

Default align is datatype-size-based. Structures are aligned on the
strictest requirement of its fields, with pads in the middle, and with pads in
the end - to make the next structure in the array also aligned.

Use #pragma pack(1) to switch this off.

Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com

----- Original Message -----
From:
To: “Windows System Software Devs Interest List”
Sent: Tuesday, August 15, 2006 12:53 PM
Subject: [ntdev] Drivers Align Problems.

Hi list,

I have some newbie question. Should data and code in the device driver always
be aligned (align 4?). For example
calling ntdll.ZwGetContextThread will fail if the context struct is not aligned
by 4 (error: EXCEPTION_DATATYPE_MISALIGNMENT)

Are there any rules for align? Is there a defult align? How do MSVC makes it,
or how it knows where to align data and where not? I’m currently working for
some basic student compiler project and i’m trying to implement driver
building.

P.S I’m working on x86 processors.

Thanks for help!


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer