将执行传递给内存地址 [英] Passing execution to a memory address

查看:55
本文介绍了将执行传递给内存地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好 - 我有一个问题。我正在玩C(我想知道有更好的方法可以做我想做的事情,但我想

这样做是为了增加我对C)并希望将
读入一个可执行文件到内存的一部分,然后将执行文件传递给它并执行该文件。但是,我无法得到它

工作,我的努力导致了Seg Fault。


以下是我得到的代码


#include" stdio.h"

void(* p)();

int main()

{

FILE *读者;

reader = fopen(" sample"," r"); / *以TEXT模式打开* /


void * x = malloc(8000);

int j = fread(x,1,8000,reader) ;


printf(" Read%d \ nn",j);

fclose(读者);

funcp =(fctype)x;

printf("%d \ n",x);

p = x;

printf(" ;%d \ n",p);


p();

}


其中样本是一个经过测试的小型可执行文件(从c编译,只需

即可显示一条消息。)好像正在读取该文件正好(因为它是b / b
报告读取正确的数字字节。)


我会很感激任何帮助 - 在我看来,我需要一些

等同的跳转指令,如汇编,而不是

函数指针)但我找不到一个。


谢谢,

尼克

解决方案

polas写道:


大家好 - 我有一个问题ñ。我正在玩C(我想知道有更好的方法可以做我想做的事情,但我想

这样做是为了增加我对C)并希望将
读入一个可执行文件到内存的一部分,然后将执行文件传递给它并执行该文件。但是,我无法得到它

工作,我的努力导致了Seg Fault。


以下是我得到的代码


#include" stdio.h"

void(* p)();

int main()

{

FILE *读者;

reader = fopen(" sample"," r"); / *以TEXT模式打开* /


void * x = malloc(8000);

int j = fread(x,1,8000,reader) ;


printf(" Read%d \ nn",j);

fclose(读者);

funcp =(fctype)x;

printf("%d \ n",x);

p = x;

printf(" ;%d \ n",p);


p();

}



这当然是非常不可取的:

标准中没有任何关于它的内容。通常,实现不会让你从程序的数据区域执行代码,因为

通常是(a)错误或(b)攻击(缓冲区溢出!

逃跑!跑开!)。因此,您需要以特定于实现的方式执行代码执行

权限。


在适当的系统上,您可能希望使用dlopen。


-

Chris" electric hedgehog" Dollin

这些快捷方式都是人们使用它们。


polas写道:


大家好 - 我有一个问题。我正在玩C(我想知道有更好的方法可以做我想做的事情,但我想

这样做是为了增加我对C)并希望将
读入一个可执行文件到内存的一部分,然后将执行文件传递给它并执行该文件。但是,我无法得到它

工作,我的努力导致了Seg Fault。


以下是我得到的代码


#include" stdio.h"



它是:

#include< stdio.h>


也你稍后使用malloc所以你需要stdlib.h

#include< stdlib.h>


void(* p )();

int main()



优先使用int main(void)


{

FILE * reader;

reader = fopen(" sample"," r"); / *在TEXT模式下打开* /



你应该确保fopen成功。


void * X = malloc的(8000);



你应该确保malloc成功。


int j = fread(x,1,88000 ,读者);



fread返回size_t。在你的情况下这不应该是一个问题,但是如果你想要高于8000的值,那么记住这是一件好事。


>

printf(" read%d \ nn",j);

fclose(读者);

funcp = (fctype)×;



什么是fctype?


printf("%d \ n",x);



x是指向void的指针。你不能用%d打印它。

如果你想打印它的地址使用:printf("%p\\\
",x);
< blockquote class =post_quotes>
p = x;



这不好。你不能指定一个函数

指针的void指针。他们不是一回事。


printf("%d \ nn,p);



你不能那样打印。甚至不确定你是否可以打印出来。


>

p();



p是不是指向一个函数所以应该做什么?


}


其中sample是一个经过测试的小型可执行文件(从c编译,只需

来显示一条消息。)似乎该文件正在被读取(因为它

报告读取正确的字节数。)



假设我们没有看到其他错误。如果样本是可执行的

并且您将其作为文本文件打开,那么您将无法正确阅读

文件。


>

我会很感激任何帮助 - 在我看来,我需要一些

类似的跳转指令,如汇编, (而不是

函数指针)但我找不到一个。



尝试使用系统函数。其他任何东西都是高度

系统依赖和OT在这里。

-

Ioan - Ciprian Tandau

tandau _at_ freeshell _dot_ org(希望它还为时不晚)

(......它仍然有效......)


" polas" < ni ** @ helpforce.comwrote:


大家好 - 我有一个问题。我正在玩C(我想知道有更好的方法可以做我想做的事情,但我想

这样做是为了增加我对C)并希望将
读入一个可执行文件到内存的一部分,然后将执行文件传递给它并执行该文件。



这是不可能的,你在ISO C中做出的任何尝试都是非常毛茸茸的。而且还不便携。常见问题解答:

< http://c-faq.com/osdep/dynlink.html>。


以下是我有的代码


#include" stdio.h"

void(* p)();

int main ()

{

FILE * reader;

reader = fopen(" sample"," r"); / *在TEXT模式下打开* /



嗯,从根本上说这是错误的,并从

评论来判断你知道。那么为什么这样呢?打开二进制文件。


void * x = malloc(8000);

int j = fread(x,1,8000,reader) ;



Wooo!的Wooo!幻数警报!


至少,#define OBJECT_FILE_SIZE 8000,并使用它。更好,

在读取文件时动态分配内存。


printf(" Read%d \ nn,j);

fclose(读者);


funcp =(fctype)x;



此行不属于。你还没有声明funcp或fctype。

你_sure_这是你编译的程序吗?


printf(" %d\\\
",X);



这是printf()指针的错误方法。请改用%p。

其他类型的指针需要转换为void *,但x已经是一个。


p = x;



这在ISO C中是不可能的。函数指针和对象指针

不能彼此分配。可以为对象和函数指针分配

的唯一事情是空指针_constant_。即使

一个碰巧有空值的对象指针也无法移植

转换为函数指针类型,也不是vv


printf("%d \ n",p);



由于同样的原因,这是不可能的,即使你使用%p也不行。

(不可能,因为,因为,在上面,你需要将p转换为void *;但是如上面的

,你不能将函数指针转换为void *。)


p();

}


我将不胜感激任何帮助 - 在我看来,我需要一些

类似于汇编中的等效跳转指令(而不是

函数指针),但我找不到一个。



正如FAQ所写,这不仅是非便携式的,而且非常复杂。请使用您的实施提供的设施。

这些也是非便携式的,但至少它们会比

试图猜测所有系统更容易使用-dependencies你自己,正确的,每个

的时间(因为你在这里错误的单个_will_字节)。


Richard


Hi everyone - I have a question. I am just playing around with C (I
realise there are better ways to do what I want, but I would like to
do it this way to increase my understanding of C) and would like to
read an executable file in to a portion of memory and then pass
execution to this and execute the file. However, I can not get it
working and my efforts have resulted in a Seg Fault.

Below is the code I have got

#include "stdio.h"
void (*p) ();
int main()
{
FILE *reader;
reader=fopen("sample", "r"); /* Open in TEXT mode */

void *x=malloc(8000);
int j=fread(x, 1, 8000,reader);

printf ("Read %d\n",j);
fclose(reader);
funcp=(fctype)x;
printf("%d\n",x);
p=x;
printf("%d\n",p);

p();
}

Where sample is a tested small executable file (compiled from c, just
to display a message.) It seems that the file is being read ok (as it
reports reading the correct number of bytes.)

I would appreciate any help on this - it seems to me that I need some
sort of equivalent jump instruction as in assembly, (instead of
function pointers) but I can not find one.

Thanks,
Nick

解决方案

polas wrote:

Hi everyone - I have a question. I am just playing around with C (I
realise there are better ways to do what I want, but I would like to
do it this way to increase my understanding of C) and would like to
read an executable file in to a portion of memory and then pass
execution to this and execute the file. However, I can not get it
working and my efforts have resulted in a Seg Fault.

Below is the code I have got

#include "stdio.h"
void (*p) ();
int main()
{
FILE *reader;
reader=fopen("sample", "r"); /* Open in TEXT mode */

void *x=malloc(8000);
int j=fread(x, 1, 8000,reader);

printf ("Read %d\n",j);
fclose(reader);
funcp=(fctype)x;
printf("%d\n",x);
p=x;
printf("%d\n",p);

p();
}

This is of course wildly unportable: there''s nothing in the
Standard about it. Typically the implementation will not let
you execute code from the data areas of your program, because
it''s often (a) a mistake or (b) an attack (buffer overflow!
run away! run away!). So you need to give your code execute
permission in an implementation-specific way.

On an apropriate system, you might want to use dlopen instead.

--
Chris "electric hedgehog" Dollin
The shortcuts are all full of people using them.


polas wrote:

Hi everyone - I have a question. I am just playing around with C (I
realise there are better ways to do what I want, but I would like to
do it this way to increase my understanding of C) and would like to
read an executable file in to a portion of memory and then pass
execution to this and execute the file. However, I can not get it
working and my efforts have resulted in a Seg Fault.

Below is the code I have got

#include "stdio.h"

it''s:
#include <stdio.h>

also, you are using malloc a little later so you need stdlib.h
#include <stdlib.h>

void (*p) ();
int main()

preferable to use int main(void)

{
FILE *reader;
reader=fopen("sample", "r"); /* Open in TEXT mode */

you should make sure that fopen succeeded.

void *x=malloc(8000);

you should make sure that malloc succeeded.

int j=fread(x, 1, 8000,reader);

fread returns size_t. It shouldn''t be a problem in your case but
it''s a good thing to remember if you want values higher than 8000.

>
printf ("Read %d\n",j);
fclose(reader);
funcp=(fctype)x;

what is fctype?

printf("%d\n",x);

x is a pointer to void. You are not allowed to print it with %d.
If you want to print it''s address use: printf("%p\n",x);

p=x;

That''s not good. You can''t assign a void pointer to a function
pointer. They''re not the same thing.

printf("%d\n",p);

You can''t print it like that. Not even sure you can print that.

>
p();

p is not pointing to a function so what is it supposed to do?

}

Where sample is a tested small executable file (compiled from c, just
to display a message.) It seems that the file is being read ok (as it
reports reading the correct number of bytes.)

Suppose we don''t see the other mistakes. If sample is executable
and you open it as a text file then you''re not going to read the
file properly.

>
I would appreciate any help on this - it seems to me that I need some
sort of equivalent jump instruction as in assembly, (instead of
function pointers) but I can not find one.

Try to use the system function. Anything else is both highly
system dependent and OT here.
--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it''s not too late)
(... and that it still works...)


"polas" <ni**@helpforce.comwrote:

Hi everyone - I have a question. I am just playing around with C (I
realise there are better ways to do what I want, but I would like to
do it this way to increase my understanding of C) and would like to
read an executable file in to a portion of memory and then pass
execution to this and execute the file.

It''s not portably possible, and any attempt you might make in ISO C is
incredibly hairy. And still not portable. Quoth the FAQ:
<http://c-faq.com/osdep/dynlink.html>.

Below is the code I have got

#include "stdio.h"
void (*p) ();
int main()
{
FILE *reader;
reader=fopen("sample", "r"); /* Open in TEXT mode */

Well, that''s fundamentally wrong to begin with, and judging from the
comment you know it. So why do so? Open it binary.

void *x=malloc(8000);
int j=fread(x, 1, 8000,reader);

Wooo! Wooo! Magic number alert!

At the very least, #define OBJECT_FILE_SIZE 8000, and use that. Better,
allocate memory dynamically while reading the file.

printf ("Read %d\n",j);
fclose(reader);

funcp=(fctype)x;

This line doth not belong. You have not declared either funcp or fctype.
Are you _sure_ that this is the very same program you compiled?

printf("%d\n",x);

This is the wrong way to printf() a pointer. Use %p instead. Pointers of
other types need to be cast to void *, but x already is one.

p=x;

This isn''t possible in ISO C. Function pointers and object pointers
cannot be assigned to one another. The only thing which can be assigned
to both object and function pointers is a null pointer _constant_. Even
an object pointer which happens to have null value cannot be portably
converted to a function pointer type, nor v.v.

printf("%d\n",p);

And for the same reason, this is not possible, not even if you use %p.
(Not possible, because, as above, you need to cast p to void *; but as
also above, you cannot convert a function pointer to void *.)

p();
}

I would appreciate any help on this - it seems to me that I need some
sort of equivalent jump instruction as in assembly, (instead of
function pointers) but I can not find one.

As the FAQ writes, this is not only non-portable but also highly
complicated. Use the facilities provided by your implementation instead.
Those are also non-portable, but at least they''ll be easier to use than
trying to guess all the system-dependencies yourself, correctly, every
time (because a mistake of a single bit _will_ byte you here).

Richard


这篇关于将执行传递给内存地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆