gpt4 book ai didi

c++ - 使用ptrace时mmap的返回值

转载 作者:太空宇宙 更新时间:2023-11-04 04:19:53 27 4
gpt4 key购买 nike

我在学习如何使用 ptrace 时遇到了一个奇怪的问题:

我写了一个程序:

#include <cstdio>
#include <sys/mman.h>
#include <string.h>
#include <errno.h>

int main()
{
long x=(long)mmap(0,-235,2,34,-1,0);
printf("Child: x=%ld (",x);
for(int i=31;i>=0;i--) printf((x&(1<<i))?"1":"0");
printf(")\n");
printf("Child errno: %s\n",strerror(errno));
return 0;
}

它只是使用错误的参数进行 mmap 系统调用。然后它打印返回值(也是二进制)和 errno。

这里是执行该程序后的输出:

Child: x=-1 (11111111111111111111111111111111)
Child errno: Cannot allocate memory

我用 strace 运行它:

execve("./nic.e", ["./nic.e"], [/* 35 vars */]) = 0
uname({sys="Linux", node="dom", ...}) = 0
brk(0) = 0x9237000
brk(0x9237cd0) = 0x9237cd0
set_thread_area({entry_number:-1 -> 6, base_addr:0x9237830, limit:1048575, seg_32bit:1, contents:0, read_exec_only:0, limit_in_pages:1, seg_not_present:0, useable:1}) = 0
brk(0x9258cd0) = 0x9258cd0
brk(0x9259000) = 0x9259000
mmap2(NULL, 4294967061, PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = -1 ENOMEM (Cannot allocate memory)
fstat64(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7798000
write(1, "Child: x=-1 (1111111111111111111"..., 47Child: x=-1 (11111111111111111111111111111111)
) = 47
write(1, "Child errno: Cannot allocate mem"..., 36Child errno: Cannot allocate memory
) = 36
exit_group(0) = ?

strace 告诉我们这个错误的 mmap 返回 -1,并出现错误 ENOMEM。

到目前为止一切正常。

这是我的 ptrace 代码(我删除了所有不需要的内容):

#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/ptrace.h>
#include <sys/user.h>
#include <sys/wait.h>
#include <sys/syscall.h>
#include <sys/reg.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>

int main(int argc,char**argv)
{
int pid=fork();
if(!pid)
{
ptrace(PTRACE_TRACEME,0,NULL,NULL);
execve("nic.e",NULL,NULL);
exit(1);
}

while(true)
{
int status;
waitpid(pid,&status,0);

if(WIFEXITED(status)) return 0;

int signal;

if(WIFSTOPPED(status))
{
signal=WSTOPSIG(status);
}

if(WIFSIGNALED(status)) return 0;

if(signal==SIGTRAP)
{
user_regs_struct regs;
ptrace(PTRACE_GETREGS,pid,NULL,&regs);

if(regs.orig_eax==__NR_mmap2)
{
static bool mmap_back=false;
if(!mmap_back) mmap_back=true;
else
{
mmap_back=false;
long x=regs.eax;
printf("mmap return: %ld (",x);
for(int j=31;j>=0;j--) printf((x&(1<<j))?"1":"0");
printf(")\n");
}
}
}
ptrace(PTRACE_SYSCALL,pid,NULL,NULL);
}
return 0;
}

它应该打印与子进程打印相同的内容 - mmap2 系统调用的重新运行值。

但是这是输出:

mmap return: -12 (11111111111111111111111111110100)
mmap return: -1216753664 (10110111011110011101000000000000)
Child: x=-1 (11111111111111111111111111111111)
Child errno: Cannot allocate memory

为什么 mmap 返回 -12?我是否错误地捕获了返回值?

最佳答案

在 32 位 x86 linux 上,%eax 包含返回值或出错时的否定错误值。

参见例如4.43.3 and 3.4

系统调用的返回值 -12 意味着函数失败,并且 errno 应设置为 12,这至少在我的系统上与 ENOMEM 匹配。

看来 strace 正在帮助您翻译此内容。如果您希望您的应用程序表现得像 strace,您应该执行类似于 4.4 中的测试和转换。 :

if ((unsigned long)(x) >= (unsigned long)(-2048)) {
printf("syscall failed. errno = %ld\n", -(res));
}

关于c++ - 使用ptrace时mmap的返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14143604/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com