gpt4 book ai didi

macos - lldb 在 macOS 11.1 上尝试 "attach failed"一个 arm64 二进制文件时给出 "run"

转载 作者:行者123 更新时间:2023-12-05 06:55:29 24 4
gpt4 key购买 nike

这是在 M1 mini 上运行的,运行 11.1 和更新的 Xcode。 (已应用所有待定更新。)SIP 尚未禁用(如果可以的话,在 M1 上)。

我们有一个程序用于测试正在移植的应用程序中的 mmap() 故障。我们像这样构建测试程序:

cc -v -arch arm64 -m64 -Wl,-no_adhoc_codesign -o mapfail mapfail.c

然后,我们签名。它似乎已正确签名:

@macarm[git:master]$ codesign -vvv mapfail
mapfail: valid on disk
mapfail: satisfies its Designated Requirement
@macarm[git:master]$

我们在系统上启用了开发者模式。我们还将我的用户添加到 _developer 组。我的用户不是管理员,但我作为一个用户尝试过并得到了同样的结果。

当我们运行程序时,它遇到了一个段错误 (SIGSEGV),所以我们想用 lldb 调试它,但发生了这种情况:

@macarm[git:master]$ lldb mapfail
(lldb) target create "mapfail"
Current executable set to '/Users/layer/mapfail' (arm64).
(lldb) run
error: process exited with status -1 (attach failed ((os/kern) invalid argument))
(lldb)

即使在 Apple 开发者论坛上搜索也没有找到任何信息。

mapfail.c 的代码:

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <mach/mach.h>
#include <mach/machine/vm_param.h>

#define nat long

#define UseMAP_ANON
/* define UseDevZero */

#define FIROUNDUP(a, n) (((a) + ((n) - 1)) & ~((n) - 1))
#define FIROUNDDOWN(a, n) ((a) & ~((n) - 1))

int bucket_o_zeros = -1;
int ChunkSize;

# ifdef UseMAP_ANON
# define FIMAP_ANON MAP_ANON
# else
# define FIMAP_ANON 0
# endif

typedef struct {
char *base; /* lowest address -- 64k aligned */
char *pos; /* 1+ highest address allocated */
char *commit; /* 1+ highest address committed */
} heap_descriptor;

heap_descriptor test1, test2;
unsigned nat test1_base, test1_size;

int
ok_to_map(unsigned nat base, unsigned nat top)
{
/* check if the specified memory is free */
unsigned nat address = base;
vm_size_t size;
mach_port_t object_name;
task_t task = mach_task_self();
struct vm_region_basic_info_64 info;
mach_msg_type_number_t info_count = VM_REGION_BASIC_INFO_COUNT_64;
kern_return_t retval;
int res = 0;

retval = vm_region_64(task, &address, &size, VM_REGION_BASIC_INFO_64,
(vm_region_info_64_t)&info, &info_count, &object_name);
if (retval == KERN_NO_SPACE) {
res = 1;
} else if ((retval == KERN_SUCCESS) && (address > top)) {
res = 1;
}
if (object_name != MACH_PORT_NULL)
mach_port_deallocate(mach_task_self(), object_name);

printf("maping from 0x%lx to 0x%lx is %s\n", base, top, res ? "OK" : "BAD");
return res;
}

char *
setup_heap( heap_descriptor *hd, unsigned nat base, unsigned nat size)
{
/* allocate memory at the given address, and return the address
* which was allocated
*/

char *res = (char *)MAP_FAILED;

/* initial setup of heap when there is nothing to map in or commit */
base = FIROUNDDOWN(base, ChunkSize);
size = FIROUNDUP(size, ChunkSize);

/* we demand that OS puts memory at our base, so we can grow later */
if (base == 0 || ok_to_map(base, base+size)) {
res = (char *) mmap((void *) base, size, PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_PRIVATE | FIMAP_ANON | (base ? MAP_FIXED : 0),
bucket_o_zeros, 0);
}
if (res != (char *) MAP_FAILED) {
/* success - check alignment */
if (base == 0) {
nat diff;
base = (unsigned nat) res;
res = (char *) FIROUNDUP((nat) res, ChunkSize);
diff = ((unsigned nat)res - base);
if (diff > 0) {
/* this had been over-requested already */
size -= ChunkSize;
/* must munmap the two ends */
munmap((caddr_t) base, diff);
munmap((caddr_t) (res + size), diff);
}
}
/* initialize the heap descriptor */
hd->pos = hd->base = res;
hd->commit = (char *) (hd->base + size);
return hd->base;
} else {
return 0; /* failure */
}
}

int
try_setup_heap(char *kind, heap_descriptor *hd, unsigned nat base, unsigned nat size)
{
/* try to allocate where first requested, and then let the system decide.
* return true or false depending on whether it worked.
*/

char *result_base;

base = FIROUNDUP(base,ChunkSize);
size = FIROUNDUP(size,ChunkSize);

/* ask for a specific area */
if (setup_heap(hd, base, size)) return 1; /* success */

/* now ask for any location */
if ((result_base = setup_heap(hd, 0, size + ChunkSize)) == 0) {
fprintf(stderr,
"Unable to reserve at %ld (0x%lx) bytes of memory for the %s heap\n",
size, size, kind);
return 0;
} else {
fprintf(stderr,
"Unable to reserve 0x%lx for the %s heap,\n using 0x%lx instead\n",
base, kind, (unsigned nat) result_base);
return 1;
}
}

int main(int argc, char **argv, char**envp)
{
ChunkSize = getpagesize();
#if defined(UseDevZero)
if(bucket_o_zeros == -1){
bucket_o_zeros = open("/dev/zero", O_RDWR);
}
#endif
if (argc > 2) {
sscanf(argv[1], "%lx", &test1_base);
sscanf(argv[2], "%lx", &test1_size);
if (try_setup_heap("test1", &test1, test1_base, test1_size)) {
printf("test1 heap mapped from 0x%lx to 0x%lx\n",
(unsigned nat) test1.base, (unsigned nat)test1.commit);
} else {
printf("test1 not mapped\n");
}
} else {
unsigned nat addr = 0x100000000;
unsigned nat size = 0x100000;
int i, n, didit=0;;
sscanf(argv[1], "%d", &n);
for (i=0; i < n; i++) {
if (try_setup_heap("test2", &test2, addr, size)) {
printf("test1 heap mapped from 0x%lx to 0x%lx\n",
(unsigned nat) test2.base, (unsigned nat)test2.commit);
didit++;
}
addr += 0x100000000;
}
printf("Total mappings: %d\n", didit);
}
}

/*
* To compile on M1 with debugging:
* cc -v -arch arm64 -m64 -Wl,-no_adhoc_codesign -o mapfail mapfail.c
*/

最佳答案

二进制文件是否在调试器之外运行?我尝试了一个 int main(){return 0;} 什么都不做的程序,用你的命令行编译它,临时签名 codesign -s - a.out我可以运行它并调试它。这应该可以帮助您找出问题所在——什么都不做的程序是否可以临时协同设计?用您的签名签名的无所事事的程序是否有效?您的应用程序临时代码是否有效?您在使用权利吗?

关于macos - lldb 在 macOS 11.1 上尝试 "attach failed"一个 arm64 二进制文件时给出 "run",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65331087/

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