关于mprotect()

doudoulyy 2007-09-05 10:14:31
关于mprotect函数,看到有如下写法:
buf = ( char * )malloc( 1024 + 8191 );
if ( !buf ){
printf( "malloc error\n" );
return;
}

/*
* Align to a multiple of PAGESIZE, assumed to be a power of two
*/
buf = ( char * )( ( ( unsigned int )buf + 8191 ) & ~8191 );
c = buf[77];
buf[77] = c;
printf( "ok\n" );

/*
* Mark the buffer read-only.
*/
if ( mprotect( buf, 1024, PROT_NONE) ){
printf( "mprotect error\n" );
return;
}

/*
* Write error, program dies on SIGSEGV
*/
c = *buf; /* 运行时,这里会core */

这里的:
buf = ( char * )( ( ( unsigned int )buf + 8191 ) & ~8191 );
这条语句,什么意思啊?
...全文
268 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
doudoulyy 2007-09-05
  • 打赏
  • 举报
回复
使buf为一个页的开头

2,100

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧