我有一个想法,这很复杂,您必须熟悉汇编和多线程,以了解我为什么要这样做。
您是否曾经注意到,当您正在运行的程序处于热循环中时,取消按钮永远不会做任何事情?但是,如果您在热循环中放置IF语句,您会大大减慢其速度。
在Linux内核中,计时器安排在CPU上,当时间效果过期时。 在Java和C程序中的用户空间中,请参阅内核/sched/core.c中的时间表方法
,除非您在热循环中放置IF语句以检查中止的标志,否则在热循环中有一个线程从用户空间循环。
只有内核才能抢占线程。
但是我有一个想法,即我们如何从用户空间中抢占C编程中的线程。
我们可以将功能地址拆卸到RET以获取其组装。因此,我们可以识别循环的条件跳跃。在热循环之后,我们可以介绍__sched_yield()的首选语句,我们可以在记忆中识别拆卸中调整收益率的相对地址。
由此,我们几乎可以从用户空间中断用户线程。应需要疯狂/memotect可执行代码以更新条件跳跃语句以跳到goto语句。
你怎么认为?这有多容易?
I have an idea and it's kind of complicated and you must be familiar with assembly and multithreading to understand why I want to do this.
Did you ever notice that the cancel button never does anything when the program you are running is in a hot loop? But if you put an if statement in the hot loop you slow it down drastically.
In the Linux kernel, processes are scheduled by a timer onto the CPU by an interrupt when the timeslice has expired. See the schedule method in kernel/sched/core.c
Now in user space in Java and C programs, you have a thread in a hot loop unless you put an if statement inside the hot loop to check an aborted flag you cannot interrupt the hot loop from user space.
Only the kernel can preempt a thread.
But I have an idea on how we can preempt a thread in C programming from a userspace.
We can disassemble a function address to a RET to get its assembly. We can therefore identify the conditional jumps of the loop. We can introduce a go-to statement of __sched_yield() after the hot loop we can identify where in memory the relative address of the Sched yield from the disassembly.
From this, we can virtually interrupt a user thread from user space. Shall need to madvise/memprotect the executable code to update the conditional jump statement to jump to the goto statement.
What do you think? How easy is this?
发布评论
评论(1)
在Java,C和Rust,您可以通过直接更改循环变量以超越循环不变性来抢占热环中的线程。您可以从任何线程执行此操作。
如果您需要使用循环变量(可能会使用),请确保将其复制为这样的变量,或者您将进行数据竞赛:
或者
您可以使用这种方法创建可取消的API,您的取消令牌可以代表所有循环索引了代码所在。因此,您可以创建深刻的响应代码。即使您深入加密,压缩或上传数据。只要您不在syscall中。
我还粘贴了Java版本下方此源代码的C版本。 Rust版本在C版本下方,
这是同一事物的C版本:
Rust版本使用不安全。
In Java, C and Rust you can preempt a thread in a hot loop by directly changing the looped variable to go beyond the loop invariant. You can do this from any thread.
If you need to use the loop variable (you probably do), make sure you copy it into a variable like this, or you'll have a data race:
Or
You can create cancellable APIs with this approach, your cancel token can represent all the loops indexes that the code is in. So you can create profoundly responsive code. Even if you are deep in encryption or compression or uploading data. As long as you're not in a syscall.
I also pasted a C version of this sourcecode below the Java version. The Rust version is below the C version
This is the C version of the same thing:
The Rust version uses unsafe.