gpt4 book ai didi

c - thread_create_running 导致我的整个计算机在 OSX 10.6 上重新启动

转载 作者:太空宇宙 更新时间:2023-11-04 04:49:22 24 4
gpt4 key购买 nike

我正在尝试在 OSX 中编写一个 C 程序,它将改变另一个程序的执行流程,就像调试器一样。但在我将所有“部分”组合在一起之前,我需要先测试每个部分是否单独工作。

  • 我已成功使用 mach_vm_read_overwrite()mach_vm_write() 读取和写入堆栈。
  • 我已成功使用 thread_get_state()thread_set_state() 读取和写入寄存器。

剩下的就是使用thread_create_running() 在任务中创建一个线程来执行我的任意函数。但是,每当我创建一个线程时,OSX 就会完全崩溃并自动重新启动我的计算机,大声笑。有人可以更详细地解释发生了什么吗?

这是我的远程程序,test.c:

#include <unistd.h>
#include <stdio.h>

void function1() {
printf("lol 1\n");
}

void function2() {
printf("lol 2\n");
}

void function3() {
printf("lol 3\n");
}

int main(int argc, char **argv) {
while(1) {
function1();
sleep(1);
function2();
sleep(1);
function3();
sleep(1);
}
return 0;
}

这是我正在进行的微型调试器:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ptrace.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <stdint.h>
#include <mach/mach_traps.h>
#include <mach/mach_init.h>
#include <mach/mach_error.h>
#include <mach/mach.h>
#include <mach/mach_types.h>
#include <mach/i386/thread_status.h>

void error(char *msg) {
printf("error: %s\n", msg);
exit(1);
}

int main(int argc, char **argv) {
pid_t pid;
mach_port_t eq_task;
kern_return_t err;
thread_act_port_array_t thread_list;
mach_msg_type_number_t thread_count;
x86_thread_state_t x86_state;
mach_msg_type_number_t sc = x86_THREAD_STATE_COUNT;
thread_act_t remoteThread;

// Make sure we have an argument
if (argc != 2)
error("requires a PID");
else
pid = (pid_t)atoi(argv[1]);

// Make sure we're root
if (getuid() && geteuid())
error("requires root");

// Get the task port
err = task_for_pid(mach_task_self(), pid, &eq_task);
if ((err != KERN_SUCCESS) || !MACH_PORT_VALID(eq_task))
error("getting eq task");

// Suspend the process
if(task_suspend(eq_task))
error("suspending the task");

// Get a list of threads from the port
if (task_threads(eq_task, &thread_list, &thread_count))
error("cannot get list of tasks");

// Get the registers
if (thread_get_state(thread_list[0], x86_THREAD_STATE, (thread_state_t)&x86_state, &sc))
error("getting state from thread");

// Create a new thread
err = thread_create_running(eq_task, x86_THREAD_STATE, (thread_state_t)&x86_state, x86_THREAD_STATE_COUNT, &remoteThread);

// BLACK SCREEN AND CRASH

// Resume the process again
if(task_resume(eq_task))
error("resuming the task");

}

最佳答案

我假设您尝试在 XD 位中执行此操作,除非您运行的是 AMD 而不是英特尔。那么就是NX对XD了。这会导致应用程序甚至整个计算机崩溃。

关于c - thread_create_running 导致我的整个计算机在 OSX 10.6 上重新启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17384547/

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