473,399 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Read/copy/call a functions machine code?

Is it possible to create a pointer to a function, and then get its size (the
actual size the function takes in machine code), such that you can copy the
function to another memory location. You could then modify it (I know it
would be modifing the machine code) and then call the modified function via
a function pointer?

Feb 18 '08 #1
8 2364
There's also the fact that on some modern processors, you'd need (?) to copy
the code in a data segment, and this is non-executable. And the code segment
is non-writable, or should be, so you can't copy it back.
--
Guillaume Dargaud
http://www.gdargaud.net/
Feb 18 '08 #2
On Feb 18, 12:12 pm, "MisterE" <Mist...@nimga.comwrote:
Is it possible to create a pointer to a function
Sure, T (*ptr)(T); declares ptr as a function pointer that takes T and
returns T.
and then get its size (the actual size the function takes in machine code),
Nope, that cannot be done. There is not even machine code in a
function pointer, and a function pointer does not have to point to
actual memory in the implementation.
such that you can copy the function to another memory location. You could then modify it (I know it
You can do that

int (*ptr)(int) = putchar;
T (*tmp)(T) = (T (*)(T))ptr; /* any type T is, this is guaranteed to
work, cast is not needed */
ptr = getchar;
ptr();
ptr = (int (*)(int))tmp; /* this is guaranteed to work too, cast not
needed */
ptr('\n');

What my code demonstrates here is that there is no 'void *' for
function pointers because you can store any function pointer to any
other function pointer and back.
would be modifing the machine code) and then call the modified function via
a function pointer?
ISO C does not define 'machine code'.
Why do you ask here? try it!
It doesn't seem to me you care about ISO C or portability, rather than
getting that 'hack' work.
Feb 18 '08 #3
"MisterE" <Mi*****@nimga.comwrote in message news:
Is it possible to create a pointer to a function, and then get its size
(the actual size the function takes in machine code), such that you can
copy the function to another memory location. You could then modify it (I
know it would be modifing the machine code) and then call the modified
function via a function pointer?
Yes and no.
If you cast the function pointer to an unsigned char *, then most compilers
will allow you to read the instructions until you hit upon a return
instruction, which will be the end of the function.
However it is not guaranteed, and most modern Oses frown on allowing code to
be modified on the fly. There are ways around this, of course, or the OS
itself wouldn't be able to load programs into memory.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Feb 18 '08 #4
Malcolm McLean wrote:
"MisterE" <Mi*****@nimga.comwrote in message news:
>Is it possible to create a pointer to a function, and then get its size
(the actual size the function takes in machine code), such that you can
copy the function to another memory location. You could then modify it (I
know it would be modifing the machine code) and then call the modified
function via a function pointer?
Yes and no.
If you cast the function pointer to an unsigned char *, then most compilers
will allow you to read the instructions until you hit upon a return
instruction, which will be the end of the function.
Not necessarily.

(a) A function may have multiple return points -- there's no need for there
to be a single exit point in the code.

(b) The compiler is at liberty to put returns in front of the function
body, if that leads to more efficient code.

(c) A tail-optimised function may have no returns at all, just jumps to
other functions.

A Poul Anderson quote occurs to me, but I can't remember where it comes
from.

--
"Well begun is half done." - Proverb

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Feb 18 '08 #5
In article <L-******************************@bt.com>,
Malcolm McLean <re*******@btinternet.comwrote:
>If you cast the function pointer to an unsigned char *, then most compilers
will allow you to read the instructions until you hit upon a return
instruction, which will be the end of the function.
Qui?? Many a function would have multiple return instructions.

On all of the machines that I have had experience with that allowed
the code to be examined under program control, there was no limit
such as "until a return instruction: reading was possible until
you ran off the end of the readable memory in that address block
(the exact end of which was not necessarily predicatable and might
not have anything to do with the location of return instructions.)

But then I've used processors that didn't -have- return
instructions, just branch instructions that took the destination
location from memory or a register.

Some systems might put a "guard page" (or guard segment) after the
end of a routine to catch overruns, but that's more common for
data segments than for instruction segments.
--
"The slogans of an inadequate criticism peddle ideas to fashion"
-- Walter Benjamin
Feb 18 '08 #6

"Malcolm McLean" <re*******@btinternet.comwrote in message
news:L-******************************@bt.com...
"MisterE" <Mi*****@nimga.comwrote in message news:
>Is it possible to create a pointer to a function, and then get its size
(the actual size the function takes in machine code), such that you can
copy the function to another memory location. You could then modify it (I
know it would be modifing the machine code) and then call the modified
function via a function pointer?
Yes and no.
If you cast the function pointer to an unsigned char *, then most
compilers will allow you to read the instructions until you hit upon a
return instruction, which will be the end of the function.
I take it you've never actually tried this? :-)

--
Bart
Feb 18 '08 #7
In article <77******************************@bt.com>,
Malcolm McLean <re*******@btinternet.comwrote:
>Embedded or multiple returns are a bit more complex. Often machine code
obeys the one in one out rule of a single point of entry and a single point
of exit, and you can pick up the real return by looking for the stack
manipulation that immediately precedes it.
Just as a data point, if I give gcc the -fomit-frame-pointer option on
my x86 Mac, there's no stack manipulation before a return, and
(presumably as a consequence) it's happy to emit multiple return
instructions.

-- Richard
--
:wq
Feb 18 '08 #8
Jack Klein wrote, On 19/02/08 03:35:

<snip>
But it is way beyond the scope of standard C, where there are only two
things you can do with a function, call it or take its address. And
the only thing you can do with its address, after taking it, is use it
to call the function.
You can also store it for later use ;-)
--
Flash Gordon
Feb 19 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tess | last post by:
Hi, Long time reader, first time poster... Any help is appreciated. I have a few questions regarding Winform controls embedded within an html page. For more info please see the appendix. Now,...
11
by: Sam Wilson [Bentley] | last post by:
If you pass a C++ object by value as an argument to a function which has a variable-length argument list (...), the MSVC7 C++ compiler does not call the object's copy constructor and will not...
39
by: JKop | last post by:
Back when I read my first C++ book, I was given the following scenario: class Cheese { public: int number_of_holes; int colour;
3
by: Douwe | last post by:
I try to build my own version of printf which just passes all arguments to the original printf. As long as I keep it with the single argument version everything is fine. But their is also a version...
26
by: Adam Warner | last post by:
Hello all, I'm very new to C but I have a number of years of Common Lisp programming experience. I'm trying to figure out ways of translating higher order concepts such as closures into C. The...
6
by: Kobu | last post by:
Do the "larger" input functions like scanf, gets, fgets use fgetc to take input or an operating system call function like read() (I know it could be any "way", but I'm trying to find out how it's...
3
by: Dave Coate | last post by:
Hello again, I am going to re-post a question. I got some excellent suggestions from Rob and Mattias on this but their ideas did not solve the problem. Here is the original post: ...
5
by: Martijn van Buul | last post by:
Hi. I'm having a peculiar problem at work. I've been googling for it, but haven't found an authorative answer. In a nutshell (Long story follows), what I'd like to know is: If I have a C++...
18
by: WaterWalk | last post by:
Hello. Suppose there is an implementation of C++, in which when a class object is allocated, its member functions are also allocated in addition to its data members. So that every class object has...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.