在 C 中:发送 func 指针,用它调用 func,使用 EIP、jmp_buf 和 longjmp
我需要确保我首先了解一些基本的东西:
- 如何将函数 A 作为参数传递给函数 B?
- 如何从 B 内部调用函数 A?
现在,最大的打击是:
我正在尝试按照以下方式做一些事情:
jmp_buf buf;
buf.__jmpbuf[JB_PC] = functionA;
longjmp(buf,10);
这意味着我想使用 longjmp
来转到一个函数。我该怎么做呢?
I need to make sure i understand some basic stuff first:
- how do i pass function A as a parameter to function B?
- how do i call function A from inside B ?
Now for the big whammy:
I'm trying to do something along the lines of this:
jmp_buf buf;
buf.__jmpbuf[JB_PC] = functionA;
longjmp(buf,10);
Meaning that I want to use longjmp
in order to go to a function. How should I do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用指向函数的指针。声明函数指针的语法是:
因此,例如,我们可能有以下代码:
另外,附带说明一下,虽然 &functionA 获取 functionA 的地址,但实际上没有必要在那里使用 & 符号。我个人这样做,因为我认为它更清楚地表明它是一个函数指针。您可以使用与调用函数时相同的语法来调用函数指针。
至于使用跳转缓冲区,我相信你所做的事情是不可靠的。如果您想创建一个跳转缓冲区并在调用某个函数之前调用 setjmp,然后调用 longjmp 以便返回到调用之前,那么这是明确定义的。然而,jmp_buf 的实际定义和结构是特定于实现的。它必须满足某些要求(例如,它必须是数组类型,因为 setjmp 必须能够按值获取它并修改它),但除此之外, setjmp.h 的规范没有定义jmp_buf的结构。因此,任何尝试直接操作 jmp_buf 的操作都将特定于特定平台。
You need to use a pointer to a function. The syntax for declaring a function pointer is:
So, for example, we might have the following code:
Also, a side note, that while &functionA takes the address of functionA, it is actually not necessary to use the ampersand there... I personally do it, since I think it makes it more clear that it is a function pointer. You invoke a function pointer using the same syntax that you would when invoking a function.
As for using jump buffers, I believe what you are doing is not something that can be relied upon. If you want to create a jump buffer and invoke setjmp before invoking some function, then later invoke longjmp so that you return to immediately prior to the call, then that is well-defined. The actual definition and structure of jmp_buf, though, is implementation-specific. There are certain requirements that it has to meet (e.g. it has to be an array type, because setjmp has to be able to take it by value and yet modify it), but other than that, the specification for setjmp.h does not define the structure of jmp_buf. So, anything that attempts to manipulate jmp_buf directly is going to be specific to a particular platform.
将函数A作为参数传递给函数B:
typedef void function_type(void);
无效函数A(无效)
{
printf("这是函数A\n");
}
int main(int argc, char **argv)
{
函数B(&函数A);
返回(0);
}
从函数 B 调用函数 A:
void functionB(function_type *func)
{
函数();
}
使用
longjmp()
转到函数。最好的答案是“不要这样做”——几乎总是有更好的方法来实现相同的目标。您能解释一下您需要这个的情况吗?Passing functionA as a parameter to functionB:
typedef void function_type(void);
void functionA(void)
{
printf("This is function A\n");
}
int main(int argc, char **argv)
{
functionB(&functionA);
return (0);
}
Calling function A from function B:
void functionB(function_type *func)
{
func();
}
Using
longjmp()
to go to a function. The best answer is "Don't do this" - there's almost always a better way to achieve the same aim. Can you explain the situation where you need this?