在x86上创建内存地址对齐
我想在x86机器上创建内存地址对齐错误。我为什么要这样做?因为我想明确地测试我的SIGBUS处理程序 这是我的测试示例在x86上创建内存地址对齐,x86,x86-64,X86,X86 64,我想在x86机器上创建内存地址对齐错误。我为什么要这样做?因为我想明确地测试我的SIGBUS处理程序 这是我的测试示例 #include <stdio.h> int main(int argc, char** argv) { unsigned char array[32]; short *short_ptr; short_ptr = (short *)&array[1]; *short_ptr = 0xffff; // Store
#include <stdio.h>
int main(int argc, char** argv) {
unsigned char array[32];
short *short_ptr;
short_ptr = (short *)&array[1];
*short_ptr = 0xffff; // Store
printf("value of c = %x", *short_ptr);
return 0;
}
#包括
int main(int argc,字符**argv){
无符号字符数组[32];
短*短ptr;
short_ptr=(short*)和数组[1];
*short_ptr=0xffff;//存储
printf(“c=%x”的值,*short_ptr);
返回0;
}
我知道这将在架构上创建一个未对齐异常。但是,我一辈子都不知道如何在x86上实现它
如何操作?要创建对齐故障,必须在EFLAGS中设置AC标志。这是第18位 在组装中执行此操作的简单方法是:
pushf ; Push the flags onto the stack
pop eax ; Pop the pushed flags into EAX
bts eax, 18 ; Set bit 18 in EAX (note, this might not work. You may need to do "mov ebx, 18; bts eax ebx")
push eax ; Push that back onto the stack
popf ; Pop eflags from the stack
我不确定你能不能用短的来做这件事,这取决于短的程度。8位访问总是对齐的,因为您不能访问少于8位的访问。尝试使用int来确定。要创建对齐错误,必须在EFLAGS中设置AC标志。这是第18位 在组装中执行此操作的简单方法是:
pushf ; Push the flags onto the stack
pop eax ; Pop the pushed flags into EAX
bts eax, 18 ; Set bit 18 in EAX (note, this might not work. You may need to do "mov ebx, 18; bts eax ebx")
push eax ; Push that back onto the stack
popf ; Pop eflags from the stack
我不确定你能不能用短的来做这件事,这取决于短的程度。8位访问总是对齐的,因为您不能访问少于8位的访问。尝试使用int来确定。似乎在问类似的问题。我不确定它是否重复,但它可能会引导您走向正确的方向。@Mark-谢谢您的参考。搜索正在成为一门艺术。区别在于,执行本文中的操作只会导致SIGSEGV,而不会导致SIGBUS。SIGBUS是我真正感兴趣的东西。似乎在问一个类似的问题。我不确定它是否重复,但它可能会引导您走向正确的方向。@Mark-谢谢您的参考。搜索正在成为一门艺术。区别在于,执行本文中的操作只会导致SIGSEGV,而不会导致SIGBUS。SIGBUS是我真正感兴趣的。VC++
short
是16位宽,char
是8位,所以你会认为OP的样本应该触发对齐错误。VC++short
是16位宽,char
是8位,所以你会认为OP的样本应该触发对齐错误。