Re: 答复: 奇怪的copy_to_user问题
Xiao Long YI
xiaolong.yi在st.com
星期一 六月 16 16:51:17 CST 2008
估计MIPS64是8字节对齐的,struct iwreq不是8字节对齐的
----- Original Message -----
From: "Wei Zhou" <weilyyoung在gmail.com>
To: "tangjc" <tangjc在neusoft.com>
Cc: <linux-kernel在zh-kernel.org>
Sent: Monday, June 16, 2008 4:34 PM
Subject: Re: 答复: 奇怪的copy_to_user问题
> 再补充一点新发现:
>
> 把头两行变量定义对换一下位置就正常了(就是把buffer放在最前面)。
>
>
> 2008/6/16 Wei Zhou <weilyyoung在gmail.com>:
>
>> 代码如下(wireless_tools.29 -- iwpriv.c):
>>
>> {
>> struct iwreq wrq;
>> u_char buffer[4096]; /* Only that big in v25 and later */
>>
>> wrq.u.data.pointer = (caddr_t) buffer;
>> wrq.u.data.flags = subcmd;
>>
>> /* Perform the private ioctl */
>> if(ioctl(skfd, priv[k].cmd, &wrq) < 0)
>> {
>> fprintf(stderr, "Interface doesn't accept private ioctl...\n");
>> fprintf(stderr, "%s (%X): %s\n", cmdname, priv[k].cmd,
>> strerror(errno));
>> return(-1);
>> }
>>
>>
>> n = wrq.u.data.length;
>>
>> buffer[n] = '\0';
>> printf("%s\n", buffer);
>> }
>>
>> 简单说明一下:
>> iwreq 是linux wireless extension
>> 定义的结构,用来作wireless的ioctl,其中wrq.u.data.pointer即我所说的buffer的地址,而length是driver回填的。
>> 在ioctl完成后,读取这个length到n中,我把n打印出来看过,这个值是正确的,就是实际拷贝的字节数。但是buffer里面的内容是空的(全是0)。
>>
>>
>>
>> 2008/6/16 tangjc <tangjc在neusoft.com>:
>>
>> 能将应用程序中调用ioctl部分和内核驱动的ioctl部分代码贴出来吗
>>>
>>> -----邮件原件-----
>>> 发件人: linux-kernel-bounces在zh-kernel.org
>>> [mailto:linux-kernel-bounces在zh-kernel.org] 代表 Wei Zhou
>>> 发送时间: 2008年6月16日 14:38
>>> 收件人: Li Yu
>>> 抄送: linux-kernel在zh-kernel.org
>>> 主题: Re: 奇怪的copy_to_user问题
>>>
>>> 可能我没表达清楚,举个例子把:
>>>
>>> 在不正常的程序中,尝试做以下改动:
>>> 1. 把buffer初始化为全部是0x5a。
>>> 2. 调用ioctl
>>> 3.
>>> 把buffer的每个字节打印出来,这时发现前8个字节仍旧是0x5a,而后面的内容是正确
>>> 的(也就是driver中希望返回的第9个字节以及之后的内容)。
>>>
>>> 所以结果仍旧是不对的,而且奇怪的地方就在于仅仅前8个字节不对。
>>>
>>>
>>> 2008/6/16 Li Yu <raise.sail在gmail.com>:
>>>
>>> >
>>> > 你是说这种情况下,ioctl就能始终返回正确的内容?
>>> >
>>> > Wei Zhou 写道:
>>> > > "把从开始地址的内存中写些特殊模式,看看调用之后的这些模式是不是还保 持原
>>> > > 样。"
>>> > > 这个我试过,仍就保持原先的特殊模式内容。特别是返回几十字节的情况下,前面
>>> > > 8个字节的内容是调用前的特殊模式,之后的内容是正确的第9个字节开始的东西。
>>> > >
>>> > > 2008/6/16 Li Yu <raise.sail在gmail.com
>>> > > <mailto:raise.sail在gmail.com>>:
>>> > >
>>> > >
>>> > > 在栈上定义一个大些的数组,然后使用这个数组的某个中间地址作为参数传递
>>> > > 给内 核,看看正确不。
>>> > > 还可以把从开始地址的内存中写些特殊模式,看看调用之后的这些模式是不是
>>> > > 还保 持原样。
>>> > >
>>> > > 如果是用户空间程序的内存被意外地写上别的数据了,栈上连续空间的起始点
>>> > > 是最 有可能中招的。
>>> > >
>>> > > Wei Zhou 写道:
>>> > >
>>> > >
>>> > > 这个涉及调用函数深度啦,有的函数调用层次浅,堆栈够用,ok,
>>> > > 有的函数调用的深,堆栈不够用,相邻地址数据被破坏,如果这些数
>>> > > 据没啥用OK,如果这些数据有用,程序崩溃或者死机。哎,栈上分配
>>> > > 内存不是个好习惯啊,前几天还被一哥们的程序搞的崩溃,就是栈上
>>> > > 内存太大,花掉了半天时间
>>> > > 定位。可以改用动态分配内存试试,说不定就是这个问题引起的。
>>> > >
>>> > >
>>> > > 谢谢。 用动态分配确实可以解决,只是想找找原因。
>>> > > 我的两个程序调用深度都不深,都是2层(即main() -> foo()).
>>> > > 而且wireless_tools version 28到version 29的iwpriv在调用深度和栈
>>> > > 使用上都一样,所以就想找到原因。
>>> > > 另外,我也尝试把这块buffer大小改为10字节,栈使用上就肯定够小了,
>>> > > 但仍旧不对。:-(
>>> > > _______________________________________________
>>> > > Linux 内核开发中文邮件列表
>>> > > Linux-kernel在zh-kernel.org
>>> > > <mailto:Linux-kernel在zh-kernel.org
>>> >
>>> > > http://zh-kernel.org/mailman/listinfo/linux-kernel
>>> > > Linux 内核开发中文社区: http://zh-kernel.org
>>> > >
>>> > >
>>> > >
>>> > >
>>> > >
>>> > > --
>>> > > Best Regards,
>>> > > Weily
>>> >
>>> > _______________________________________________
>>> > Linux 内核开发中文邮件列表
>>> > Linux-kernel在zh-kernel.org
>>> > http://zh-kernel.org/mailman/listinfo/linux-kernel
>>> > Linux 内核开发中文社区: http://zh-kernel.org
>>> >
>>>
>>>
>>>
>>> --
>>> Best Regards,
>>> Weily
>>> _______________________________________________
>>> Linux 内核开发中文邮件列表
>>> Linux-kernel在zh-kernel.org
>>> http://zh-kernel.org/mailman/listinfo/linux-kernel
>>> Linux 内核开发中文社区: http://zh-kernel.org
>>>
>>>
>>>
>>> ----------------------------------------------------------------------------------------------
>>> Confidentiality Notice: The information contained in this e-mail and any
>>> accompanying attachment(s) is intended only for the use of the intended
>>> recipient and may be confidential and/or privileged of Neusoft Group
>>> Ltd.,
>>> its subsidiaries and/or its affiliates. If any reader of this
>>> communication
>>> is not the intended recipient, unauthorized use, forwarding, printing,
>>> storing, disclosure or copying is strictly prohibited, and may be
>>> unlawful.
>>> If you have received this communication in error, please immediately
>>> notify
>>> the sender by return e-mail, and delete the original message and all
>>> copies
>>> from your system. Thank you.
>>>
>>> -----------------------------------------------------------------------------------------------
>>>
>>>
>>
>>
>> --
>> Best Regards,
>> Weily
>
>
>
>
> --
> Best Regards,
> Weily
> _______________________________________________
> Linux 内核开发中文邮件列表
> Linux-kernel在zh-kernel.org
> http://zh-kernel.org/mailman/listinfo/linux-kernel
> Linux 内核开发中文社区: http://zh-kernel.org
关于邮件列表 Linux-kernel 的更多信息