lldb 在尝试在 macOS 11.1 上"run" arm64 二进制文件时提供"attach failed"



这是在M1 mini上运行的,带有更新的Xcode的11.1。(所有挂起的更新都已应用。(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) 

即使在苹果开发者论坛上搜索,也没有得到任何信息。

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 %sn", 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 heapn",
size, size, kind);
return 0;
} else {
fprintf(stderr,
"Unable to reserve 0x%lx for the %s heap,n using 0x%lx insteadn",
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%lxn",
(unsigned nat) test1.base, (unsigned nat)test1.commit);
} else {
printf("test1 not mappedn");
}
} 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%lxn",
(unsigned nat) test2.base, (unsigned nat)test2.commit);
didit++;
}
addr += 0x100000000;
}
printf("Total mappings: %dn", 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签名,我可以运行它并调试它。这应该有助于你找出问题所在——一个特别的代码签名的什么都不干的程序能工作吗?用你的签名签名的无所事事程序有效吗?你的应用程序特别代码签名有效吗?您是否使用权益?

这个问题是已知的,由于尚未为Apple Silicon构建C/C++扩展二进制文件,因此目前还无法运行。

请参阅以下链接以保持跟踪:

https://github.com/microsoft/vscode-cpptools/issues/6595

最新更新