Datatype misalignment exception win32 example











up vote
-2
down vote

favorite












Can someone show a simple example on c++, which will cause datatype misalignment exception? I want to see an example without using RaiseException function.










share|improve this question




















  • 2




    Context, please. Standard C++ does not have a "datatype misalignment exception".
    – Pete Becker
    Nov 11 at 20:46






  • 1




    Somewhat remarkably, there is no dedicated SEH exception code for it. Reported as a regular AV (0xc0000005) with the read or write address at 0xff...ffff
    – Hans Passant
    Nov 12 at 8:52










  • I don't think you can anymore. The x86 CPUs have always had a fixup, so its a performance issue but not a SEH. When Windows NT still supported various RISC architecture CPUs unaligned access would trigger SEH exceptions. (Itanium, MIPS and so on).
    – Chris Becke
    Nov 12 at 10:40












  • Windows Data Alignment on IPF, x86, and x64 - With that said, there are situations on the x86 and x64 platform where unaligned access will generate a general-protection exception. (Note that these are general-protection exceptions, not alignment-check exceptions.)
    – RbMm
    Nov 17 at 0:28















up vote
-2
down vote

favorite












Can someone show a simple example on c++, which will cause datatype misalignment exception? I want to see an example without using RaiseException function.










share|improve this question




















  • 2




    Context, please. Standard C++ does not have a "datatype misalignment exception".
    – Pete Becker
    Nov 11 at 20:46






  • 1




    Somewhat remarkably, there is no dedicated SEH exception code for it. Reported as a regular AV (0xc0000005) with the read or write address at 0xff...ffff
    – Hans Passant
    Nov 12 at 8:52










  • I don't think you can anymore. The x86 CPUs have always had a fixup, so its a performance issue but not a SEH. When Windows NT still supported various RISC architecture CPUs unaligned access would trigger SEH exceptions. (Itanium, MIPS and so on).
    – Chris Becke
    Nov 12 at 10:40












  • Windows Data Alignment on IPF, x86, and x64 - With that said, there are situations on the x86 and x64 platform where unaligned access will generate a general-protection exception. (Note that these are general-protection exceptions, not alignment-check exceptions.)
    – RbMm
    Nov 17 at 0:28













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











Can someone show a simple example on c++, which will cause datatype misalignment exception? I want to see an example without using RaiseException function.










share|improve this question















Can someone show a simple example on c++, which will cause datatype misalignment exception? I want to see an example without using RaiseException function.







c++ winapi memory-alignment seh






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 22:47









HostileFork

24.8k775131




24.8k775131










asked Nov 11 at 20:42









Lorismelik

477




477








  • 2




    Context, please. Standard C++ does not have a "datatype misalignment exception".
    – Pete Becker
    Nov 11 at 20:46






  • 1




    Somewhat remarkably, there is no dedicated SEH exception code for it. Reported as a regular AV (0xc0000005) with the read or write address at 0xff...ffff
    – Hans Passant
    Nov 12 at 8:52










  • I don't think you can anymore. The x86 CPUs have always had a fixup, so its a performance issue but not a SEH. When Windows NT still supported various RISC architecture CPUs unaligned access would trigger SEH exceptions. (Itanium, MIPS and so on).
    – Chris Becke
    Nov 12 at 10:40












  • Windows Data Alignment on IPF, x86, and x64 - With that said, there are situations on the x86 and x64 platform where unaligned access will generate a general-protection exception. (Note that these are general-protection exceptions, not alignment-check exceptions.)
    – RbMm
    Nov 17 at 0:28














  • 2




    Context, please. Standard C++ does not have a "datatype misalignment exception".
    – Pete Becker
    Nov 11 at 20:46






  • 1




    Somewhat remarkably, there is no dedicated SEH exception code for it. Reported as a regular AV (0xc0000005) with the read or write address at 0xff...ffff
    – Hans Passant
    Nov 12 at 8:52










  • I don't think you can anymore. The x86 CPUs have always had a fixup, so its a performance issue but not a SEH. When Windows NT still supported various RISC architecture CPUs unaligned access would trigger SEH exceptions. (Itanium, MIPS and so on).
    – Chris Becke
    Nov 12 at 10:40












  • Windows Data Alignment on IPF, x86, and x64 - With that said, there are situations on the x86 and x64 platform where unaligned access will generate a general-protection exception. (Note that these are general-protection exceptions, not alignment-check exceptions.)
    – RbMm
    Nov 17 at 0:28








2




2




Context, please. Standard C++ does not have a "datatype misalignment exception".
– Pete Becker
Nov 11 at 20:46




Context, please. Standard C++ does not have a "datatype misalignment exception".
– Pete Becker
Nov 11 at 20:46




1




1




Somewhat remarkably, there is no dedicated SEH exception code for it. Reported as a regular AV (0xc0000005) with the read or write address at 0xff...ffff
– Hans Passant
Nov 12 at 8:52




Somewhat remarkably, there is no dedicated SEH exception code for it. Reported as a regular AV (0xc0000005) with the read or write address at 0xff...ffff
– Hans Passant
Nov 12 at 8:52












I don't think you can anymore. The x86 CPUs have always had a fixup, so its a performance issue but not a SEH. When Windows NT still supported various RISC architecture CPUs unaligned access would trigger SEH exceptions. (Itanium, MIPS and so on).
– Chris Becke
Nov 12 at 10:40






I don't think you can anymore. The x86 CPUs have always had a fixup, so its a performance issue but not a SEH. When Windows NT still supported various RISC architecture CPUs unaligned access would trigger SEH exceptions. (Itanium, MIPS and so on).
– Chris Becke
Nov 12 at 10:40














Windows Data Alignment on IPF, x86, and x64 - With that said, there are situations on the x86 and x64 platform where unaligned access will generate a general-protection exception. (Note that these are general-protection exceptions, not alignment-check exceptions.)
– RbMm
Nov 17 at 0:28




Windows Data Alignment on IPF, x86, and x64 - With that said, there are situations on the x86 and x64 platform where unaligned access will generate a general-protection exception. (Note that these are general-protection exceptions, not alignment-check exceptions.)
– RbMm
Nov 17 at 0:28












3 Answers
3






active

oldest

votes

















up vote
1
down vote













This has become harder with recent versions of MSVC, because they no longer honour the aligned intrinsics such as _mm_load_ps or _mm_store_ps, they get compiled as if you asked for the unaligned versions. But streaming loads only have an aligned version, so we can use that here to force it to fail on an unaligned address. For example like this:



__m128i test()
{
char buffer[17];
__m128i a = _mm_stream_load_si128((__m128i*)buffer);
__m128i b = _mm_stream_load_si128((__m128i*)(buffer + 1));
return _mm_or_si128(a, b);
}


There are two loads that are 1 byte apart, the address cannot be aligned both times, so it fails. The actual exception would present as "Access violation reading location 0xFFFFFFFF".






share|improve this answer





















  • But its will be Access violation exception with code 0xc0000005, but i want to get Datatype Misalignment exception with code 0x80000002
    – Lorismelik
    Nov 14 at 0:36










  • @Lorismelik OK I don't know how to get that one
    – harold
    Nov 14 at 1:10


















up vote
0
down vote













This will not cause an exception but both g++ and clang++ gives an error message about the misalignment. Perhaps VS will too.



#include <iostream>
#include <stdexcept>

struct example {
uint64_t a;
uint64_t b;
};

int main() {
example A;
A.a = 1;
A.b = 2;
example* B = (example*)((char*)&A+1);
try {
std::cout << B->a << "n";
std::cout << "no exception thrownn";
} catch(const std::exception& ex) {
std::cout << "exception: " << ex.what() << "n";
}
}


Example output:



test.cpp:15:25: runtime error: member access within misaligned address 0x7ffd3f7cbe21 for type 'struct example', which requires 8 byte alignment
0x7ffd3f7cbe21: note: pointer points here
00 00 00 01 00 00 00 00 00 00 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0f 40 00 00
^
144115188075855872
no exception thrown





share|improve this answer





















  • (example*)((char*)&A+1) is undefined behavior. You cannot alias pointers like that. The runtime check is nice, but a conforming implementation is not bound to produce anything that resembles your code. A compiler is allowed to emit code as if you had written int main() {}, for example. With that said, on x86 architectures, it's very challenging to produce code that triggers hardware faults on unaligned access. To my knowledge, it only ever happens when using the vector units (SSE, etc.) to read or write data.
    – IInspectable
    Nov 12 at 9:38


















up vote
0
down vote














I want to see an example without using RaiseException function.




Without who using the RaiseException function? Someone has to...because the only way you're going to get an exception catchable by a C++ construct (be that __try of SEH or try of C++ exceptions) is if something somewhere raises it.



At a hardware level, the x86 processor has an Alignment Check (AC) flag. If the flag is set then interrupt 17 is executed when an unaligned access occurs. But with Windows, you can only hook such an interrupt if you are in kernel mode.



If you wanted a usermode C++ program to receive exceptions related to unaligned accesses--originating from the processor's innate ability to observe unaligned access, you'd have to write that in a device driver. And even then, getting an exception would involve some callback to usermode which led to raising it.



Just for the heck of it to see if I could cause crashes of some kind on bad alignments, I tried adapting some inline assembly from this answer into MSVC. (MSVC's syntax is different, but I gather this should do it... at least doing a subsequent pushfd and examining it with extracted local variables suggested the AC bit does get changed by the process):



#include <iostream>

int main() {
__asm {
pushfd
mov eax, [esp]
or eax, 0x40000
mov [esp], eax
popfd
}

char *data = new char[sizeof(int) + 1];
int *p = reinterpret_cast<int*>(data + 1);
*p = 304;
std::cout << *p;
}


But it doesn't appear to trigger any new errors. I can't read the cr0 register to see if alignment mask is set, it won't let me (mov eax, cr0 causes a "privileged instruction" error). And I don't know what Windows puts to respond to interrupt 17 by default, maybe a no-op? Or it's likely that x86 programs on a 64-bit build of Windows don't heed any of this, but I can't do the 64-bit instruction because MSVC doesn't support inline assembly in 64-bit. :-/



Regardless, point remains: for your Win32 C++ program to be notified via exception of an alignment error, something has to tell it...and Windows isn't configured to reacting to alignment problems noticed by the processor itself. So all your notifications are going to be coming from things like compiler warnings or usermode added runtime checks on pointer values themselves.






share|improve this answer























  • MSVC doesn't support inline assembly in 64-bit mode. But you can still compile an ASM file into object code, and use that.
    – IInspectable
    Nov 12 at 9:46











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253036%2fdatatype-misalignment-exception-win32-example%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













This has become harder with recent versions of MSVC, because they no longer honour the aligned intrinsics such as _mm_load_ps or _mm_store_ps, they get compiled as if you asked for the unaligned versions. But streaming loads only have an aligned version, so we can use that here to force it to fail on an unaligned address. For example like this:



__m128i test()
{
char buffer[17];
__m128i a = _mm_stream_load_si128((__m128i*)buffer);
__m128i b = _mm_stream_load_si128((__m128i*)(buffer + 1));
return _mm_or_si128(a, b);
}


There are two loads that are 1 byte apart, the address cannot be aligned both times, so it fails. The actual exception would present as "Access violation reading location 0xFFFFFFFF".






share|improve this answer





















  • But its will be Access violation exception with code 0xc0000005, but i want to get Datatype Misalignment exception with code 0x80000002
    – Lorismelik
    Nov 14 at 0:36










  • @Lorismelik OK I don't know how to get that one
    – harold
    Nov 14 at 1:10















up vote
1
down vote













This has become harder with recent versions of MSVC, because they no longer honour the aligned intrinsics such as _mm_load_ps or _mm_store_ps, they get compiled as if you asked for the unaligned versions. But streaming loads only have an aligned version, so we can use that here to force it to fail on an unaligned address. For example like this:



__m128i test()
{
char buffer[17];
__m128i a = _mm_stream_load_si128((__m128i*)buffer);
__m128i b = _mm_stream_load_si128((__m128i*)(buffer + 1));
return _mm_or_si128(a, b);
}


There are two loads that are 1 byte apart, the address cannot be aligned both times, so it fails. The actual exception would present as "Access violation reading location 0xFFFFFFFF".






share|improve this answer





















  • But its will be Access violation exception with code 0xc0000005, but i want to get Datatype Misalignment exception with code 0x80000002
    – Lorismelik
    Nov 14 at 0:36










  • @Lorismelik OK I don't know how to get that one
    – harold
    Nov 14 at 1:10













up vote
1
down vote










up vote
1
down vote









This has become harder with recent versions of MSVC, because they no longer honour the aligned intrinsics such as _mm_load_ps or _mm_store_ps, they get compiled as if you asked for the unaligned versions. But streaming loads only have an aligned version, so we can use that here to force it to fail on an unaligned address. For example like this:



__m128i test()
{
char buffer[17];
__m128i a = _mm_stream_load_si128((__m128i*)buffer);
__m128i b = _mm_stream_load_si128((__m128i*)(buffer + 1));
return _mm_or_si128(a, b);
}


There are two loads that are 1 byte apart, the address cannot be aligned both times, so it fails. The actual exception would present as "Access violation reading location 0xFFFFFFFF".






share|improve this answer












This has become harder with recent versions of MSVC, because they no longer honour the aligned intrinsics such as _mm_load_ps or _mm_store_ps, they get compiled as if you asked for the unaligned versions. But streaming loads only have an aligned version, so we can use that here to force it to fail on an unaligned address. For example like this:



__m128i test()
{
char buffer[17];
__m128i a = _mm_stream_load_si128((__m128i*)buffer);
__m128i b = _mm_stream_load_si128((__m128i*)(buffer + 1));
return _mm_or_si128(a, b);
}


There are two loads that are 1 byte apart, the address cannot be aligned both times, so it fails. The actual exception would present as "Access violation reading location 0xFFFFFFFF".







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 21:11









harold

41.1k356108




41.1k356108












  • But its will be Access violation exception with code 0xc0000005, but i want to get Datatype Misalignment exception with code 0x80000002
    – Lorismelik
    Nov 14 at 0:36










  • @Lorismelik OK I don't know how to get that one
    – harold
    Nov 14 at 1:10


















  • But its will be Access violation exception with code 0xc0000005, but i want to get Datatype Misalignment exception with code 0x80000002
    – Lorismelik
    Nov 14 at 0:36










  • @Lorismelik OK I don't know how to get that one
    – harold
    Nov 14 at 1:10
















But its will be Access violation exception with code 0xc0000005, but i want to get Datatype Misalignment exception with code 0x80000002
– Lorismelik
Nov 14 at 0:36




But its will be Access violation exception with code 0xc0000005, but i want to get Datatype Misalignment exception with code 0x80000002
– Lorismelik
Nov 14 at 0:36












@Lorismelik OK I don't know how to get that one
– harold
Nov 14 at 1:10




@Lorismelik OK I don't know how to get that one
– harold
Nov 14 at 1:10












up vote
0
down vote













This will not cause an exception but both g++ and clang++ gives an error message about the misalignment. Perhaps VS will too.



#include <iostream>
#include <stdexcept>

struct example {
uint64_t a;
uint64_t b;
};

int main() {
example A;
A.a = 1;
A.b = 2;
example* B = (example*)((char*)&A+1);
try {
std::cout << B->a << "n";
std::cout << "no exception thrownn";
} catch(const std::exception& ex) {
std::cout << "exception: " << ex.what() << "n";
}
}


Example output:



test.cpp:15:25: runtime error: member access within misaligned address 0x7ffd3f7cbe21 for type 'struct example', which requires 8 byte alignment
0x7ffd3f7cbe21: note: pointer points here
00 00 00 01 00 00 00 00 00 00 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0f 40 00 00
^
144115188075855872
no exception thrown





share|improve this answer





















  • (example*)((char*)&A+1) is undefined behavior. You cannot alias pointers like that. The runtime check is nice, but a conforming implementation is not bound to produce anything that resembles your code. A compiler is allowed to emit code as if you had written int main() {}, for example. With that said, on x86 architectures, it's very challenging to produce code that triggers hardware faults on unaligned access. To my knowledge, it only ever happens when using the vector units (SSE, etc.) to read or write data.
    – IInspectable
    Nov 12 at 9:38















up vote
0
down vote













This will not cause an exception but both g++ and clang++ gives an error message about the misalignment. Perhaps VS will too.



#include <iostream>
#include <stdexcept>

struct example {
uint64_t a;
uint64_t b;
};

int main() {
example A;
A.a = 1;
A.b = 2;
example* B = (example*)((char*)&A+1);
try {
std::cout << B->a << "n";
std::cout << "no exception thrownn";
} catch(const std::exception& ex) {
std::cout << "exception: " << ex.what() << "n";
}
}


Example output:



test.cpp:15:25: runtime error: member access within misaligned address 0x7ffd3f7cbe21 for type 'struct example', which requires 8 byte alignment
0x7ffd3f7cbe21: note: pointer points here
00 00 00 01 00 00 00 00 00 00 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0f 40 00 00
^
144115188075855872
no exception thrown





share|improve this answer





















  • (example*)((char*)&A+1) is undefined behavior. You cannot alias pointers like that. The runtime check is nice, but a conforming implementation is not bound to produce anything that resembles your code. A compiler is allowed to emit code as if you had written int main() {}, for example. With that said, on x86 architectures, it's very challenging to produce code that triggers hardware faults on unaligned access. To my knowledge, it only ever happens when using the vector units (SSE, etc.) to read or write data.
    – IInspectable
    Nov 12 at 9:38













up vote
0
down vote










up vote
0
down vote









This will not cause an exception but both g++ and clang++ gives an error message about the misalignment. Perhaps VS will too.



#include <iostream>
#include <stdexcept>

struct example {
uint64_t a;
uint64_t b;
};

int main() {
example A;
A.a = 1;
A.b = 2;
example* B = (example*)((char*)&A+1);
try {
std::cout << B->a << "n";
std::cout << "no exception thrownn";
} catch(const std::exception& ex) {
std::cout << "exception: " << ex.what() << "n";
}
}


Example output:



test.cpp:15:25: runtime error: member access within misaligned address 0x7ffd3f7cbe21 for type 'struct example', which requires 8 byte alignment
0x7ffd3f7cbe21: note: pointer points here
00 00 00 01 00 00 00 00 00 00 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0f 40 00 00
^
144115188075855872
no exception thrown





share|improve this answer












This will not cause an exception but both g++ and clang++ gives an error message about the misalignment. Perhaps VS will too.



#include <iostream>
#include <stdexcept>

struct example {
uint64_t a;
uint64_t b;
};

int main() {
example A;
A.a = 1;
A.b = 2;
example* B = (example*)((char*)&A+1);
try {
std::cout << B->a << "n";
std::cout << "no exception thrownn";
} catch(const std::exception& ex) {
std::cout << "exception: " << ex.what() << "n";
}
}


Example output:



test.cpp:15:25: runtime error: member access within misaligned address 0x7ffd3f7cbe21 for type 'struct example', which requires 8 byte alignment
0x7ffd3f7cbe21: note: pointer points here
00 00 00 01 00 00 00 00 00 00 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0f 40 00 00
^
144115188075855872
no exception thrown






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 21:03









Ted Lyngmo

1,815315




1,815315












  • (example*)((char*)&A+1) is undefined behavior. You cannot alias pointers like that. The runtime check is nice, but a conforming implementation is not bound to produce anything that resembles your code. A compiler is allowed to emit code as if you had written int main() {}, for example. With that said, on x86 architectures, it's very challenging to produce code that triggers hardware faults on unaligned access. To my knowledge, it only ever happens when using the vector units (SSE, etc.) to read or write data.
    – IInspectable
    Nov 12 at 9:38


















  • (example*)((char*)&A+1) is undefined behavior. You cannot alias pointers like that. The runtime check is nice, but a conforming implementation is not bound to produce anything that resembles your code. A compiler is allowed to emit code as if you had written int main() {}, for example. With that said, on x86 architectures, it's very challenging to produce code that triggers hardware faults on unaligned access. To my knowledge, it only ever happens when using the vector units (SSE, etc.) to read or write data.
    – IInspectable
    Nov 12 at 9:38
















(example*)((char*)&A+1) is undefined behavior. You cannot alias pointers like that. The runtime check is nice, but a conforming implementation is not bound to produce anything that resembles your code. A compiler is allowed to emit code as if you had written int main() {}, for example. With that said, on x86 architectures, it's very challenging to produce code that triggers hardware faults on unaligned access. To my knowledge, it only ever happens when using the vector units (SSE, etc.) to read or write data.
– IInspectable
Nov 12 at 9:38




(example*)((char*)&A+1) is undefined behavior. You cannot alias pointers like that. The runtime check is nice, but a conforming implementation is not bound to produce anything that resembles your code. A compiler is allowed to emit code as if you had written int main() {}, for example. With that said, on x86 architectures, it's very challenging to produce code that triggers hardware faults on unaligned access. To my knowledge, it only ever happens when using the vector units (SSE, etc.) to read or write data.
– IInspectable
Nov 12 at 9:38










up vote
0
down vote














I want to see an example without using RaiseException function.




Without who using the RaiseException function? Someone has to...because the only way you're going to get an exception catchable by a C++ construct (be that __try of SEH or try of C++ exceptions) is if something somewhere raises it.



At a hardware level, the x86 processor has an Alignment Check (AC) flag. If the flag is set then interrupt 17 is executed when an unaligned access occurs. But with Windows, you can only hook such an interrupt if you are in kernel mode.



If you wanted a usermode C++ program to receive exceptions related to unaligned accesses--originating from the processor's innate ability to observe unaligned access, you'd have to write that in a device driver. And even then, getting an exception would involve some callback to usermode which led to raising it.



Just for the heck of it to see if I could cause crashes of some kind on bad alignments, I tried adapting some inline assembly from this answer into MSVC. (MSVC's syntax is different, but I gather this should do it... at least doing a subsequent pushfd and examining it with extracted local variables suggested the AC bit does get changed by the process):



#include <iostream>

int main() {
__asm {
pushfd
mov eax, [esp]
or eax, 0x40000
mov [esp], eax
popfd
}

char *data = new char[sizeof(int) + 1];
int *p = reinterpret_cast<int*>(data + 1);
*p = 304;
std::cout << *p;
}


But it doesn't appear to trigger any new errors. I can't read the cr0 register to see if alignment mask is set, it won't let me (mov eax, cr0 causes a "privileged instruction" error). And I don't know what Windows puts to respond to interrupt 17 by default, maybe a no-op? Or it's likely that x86 programs on a 64-bit build of Windows don't heed any of this, but I can't do the 64-bit instruction because MSVC doesn't support inline assembly in 64-bit. :-/



Regardless, point remains: for your Win32 C++ program to be notified via exception of an alignment error, something has to tell it...and Windows isn't configured to reacting to alignment problems noticed by the processor itself. So all your notifications are going to be coming from things like compiler warnings or usermode added runtime checks on pointer values themselves.






share|improve this answer























  • MSVC doesn't support inline assembly in 64-bit mode. But you can still compile an ASM file into object code, and use that.
    – IInspectable
    Nov 12 at 9:46















up vote
0
down vote














I want to see an example without using RaiseException function.




Without who using the RaiseException function? Someone has to...because the only way you're going to get an exception catchable by a C++ construct (be that __try of SEH or try of C++ exceptions) is if something somewhere raises it.



At a hardware level, the x86 processor has an Alignment Check (AC) flag. If the flag is set then interrupt 17 is executed when an unaligned access occurs. But with Windows, you can only hook such an interrupt if you are in kernel mode.



If you wanted a usermode C++ program to receive exceptions related to unaligned accesses--originating from the processor's innate ability to observe unaligned access, you'd have to write that in a device driver. And even then, getting an exception would involve some callback to usermode which led to raising it.



Just for the heck of it to see if I could cause crashes of some kind on bad alignments, I tried adapting some inline assembly from this answer into MSVC. (MSVC's syntax is different, but I gather this should do it... at least doing a subsequent pushfd and examining it with extracted local variables suggested the AC bit does get changed by the process):



#include <iostream>

int main() {
__asm {
pushfd
mov eax, [esp]
or eax, 0x40000
mov [esp], eax
popfd
}

char *data = new char[sizeof(int) + 1];
int *p = reinterpret_cast<int*>(data + 1);
*p = 304;
std::cout << *p;
}


But it doesn't appear to trigger any new errors. I can't read the cr0 register to see if alignment mask is set, it won't let me (mov eax, cr0 causes a "privileged instruction" error). And I don't know what Windows puts to respond to interrupt 17 by default, maybe a no-op? Or it's likely that x86 programs on a 64-bit build of Windows don't heed any of this, but I can't do the 64-bit instruction because MSVC doesn't support inline assembly in 64-bit. :-/



Regardless, point remains: for your Win32 C++ program to be notified via exception of an alignment error, something has to tell it...and Windows isn't configured to reacting to alignment problems noticed by the processor itself. So all your notifications are going to be coming from things like compiler warnings or usermode added runtime checks on pointer values themselves.






share|improve this answer























  • MSVC doesn't support inline assembly in 64-bit mode. But you can still compile an ASM file into object code, and use that.
    – IInspectable
    Nov 12 at 9:46













up vote
0
down vote










up vote
0
down vote










I want to see an example without using RaiseException function.




Without who using the RaiseException function? Someone has to...because the only way you're going to get an exception catchable by a C++ construct (be that __try of SEH or try of C++ exceptions) is if something somewhere raises it.



At a hardware level, the x86 processor has an Alignment Check (AC) flag. If the flag is set then interrupt 17 is executed when an unaligned access occurs. But with Windows, you can only hook such an interrupt if you are in kernel mode.



If you wanted a usermode C++ program to receive exceptions related to unaligned accesses--originating from the processor's innate ability to observe unaligned access, you'd have to write that in a device driver. And even then, getting an exception would involve some callback to usermode which led to raising it.



Just for the heck of it to see if I could cause crashes of some kind on bad alignments, I tried adapting some inline assembly from this answer into MSVC. (MSVC's syntax is different, but I gather this should do it... at least doing a subsequent pushfd and examining it with extracted local variables suggested the AC bit does get changed by the process):



#include <iostream>

int main() {
__asm {
pushfd
mov eax, [esp]
or eax, 0x40000
mov [esp], eax
popfd
}

char *data = new char[sizeof(int) + 1];
int *p = reinterpret_cast<int*>(data + 1);
*p = 304;
std::cout << *p;
}


But it doesn't appear to trigger any new errors. I can't read the cr0 register to see if alignment mask is set, it won't let me (mov eax, cr0 causes a "privileged instruction" error). And I don't know what Windows puts to respond to interrupt 17 by default, maybe a no-op? Or it's likely that x86 programs on a 64-bit build of Windows don't heed any of this, but I can't do the 64-bit instruction because MSVC doesn't support inline assembly in 64-bit. :-/



Regardless, point remains: for your Win32 C++ program to be notified via exception of an alignment error, something has to tell it...and Windows isn't configured to reacting to alignment problems noticed by the processor itself. So all your notifications are going to be coming from things like compiler warnings or usermode added runtime checks on pointer values themselves.






share|improve this answer















I want to see an example without using RaiseException function.




Without who using the RaiseException function? Someone has to...because the only way you're going to get an exception catchable by a C++ construct (be that __try of SEH or try of C++ exceptions) is if something somewhere raises it.



At a hardware level, the x86 processor has an Alignment Check (AC) flag. If the flag is set then interrupt 17 is executed when an unaligned access occurs. But with Windows, you can only hook such an interrupt if you are in kernel mode.



If you wanted a usermode C++ program to receive exceptions related to unaligned accesses--originating from the processor's innate ability to observe unaligned access, you'd have to write that in a device driver. And even then, getting an exception would involve some callback to usermode which led to raising it.



Just for the heck of it to see if I could cause crashes of some kind on bad alignments, I tried adapting some inline assembly from this answer into MSVC. (MSVC's syntax is different, but I gather this should do it... at least doing a subsequent pushfd and examining it with extracted local variables suggested the AC bit does get changed by the process):



#include <iostream>

int main() {
__asm {
pushfd
mov eax, [esp]
or eax, 0x40000
mov [esp], eax
popfd
}

char *data = new char[sizeof(int) + 1];
int *p = reinterpret_cast<int*>(data + 1);
*p = 304;
std::cout << *p;
}


But it doesn't appear to trigger any new errors. I can't read the cr0 register to see if alignment mask is set, it won't let me (mov eax, cr0 causes a "privileged instruction" error). And I don't know what Windows puts to respond to interrupt 17 by default, maybe a no-op? Or it's likely that x86 programs on a 64-bit build of Windows don't heed any of this, but I can't do the 64-bit instruction because MSVC doesn't support inline assembly in 64-bit. :-/



Regardless, point remains: for your Win32 C++ program to be notified via exception of an alignment error, something has to tell it...and Windows isn't configured to reacting to alignment problems noticed by the processor itself. So all your notifications are going to be coming from things like compiler warnings or usermode added runtime checks on pointer values themselves.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 17 at 0:07

























answered Nov 11 at 22:34









HostileFork

24.8k775131




24.8k775131












  • MSVC doesn't support inline assembly in 64-bit mode. But you can still compile an ASM file into object code, and use that.
    – IInspectable
    Nov 12 at 9:46


















  • MSVC doesn't support inline assembly in 64-bit mode. But you can still compile an ASM file into object code, and use that.
    – IInspectable
    Nov 12 at 9:46
















MSVC doesn't support inline assembly in 64-bit mode. But you can still compile an ASM file into object code, and use that.
– IInspectable
Nov 12 at 9:46




MSVC doesn't support inline assembly in 64-bit mode. But you can still compile an ASM file into object code, and use that.
– IInspectable
Nov 12 at 9:46


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253036%2fdatatype-misalignment-exception-win32-example%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

The Sandy Post

Danny Elfman

Pages that link to "Head v. Amoskeag Manufacturing Co."