Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 95081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:36:08+00:00 2026-05-10T23:36:08+00:00

Is it possible to write a C function that does the following? Allocate a

  • 0

Is it possible to write a C function that does the following?

  1. Allocate a bunch of memory in the heap
  2. Writes machine code in it
  3. Executes those machines instructions

Of course, I would have to restore the state of the stack to what it was prior to the execution of those machine instructions manually, but I want to know if this is feasible in first place.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T23:36:09+00:00Added an answer on May 10, 2026 at 11:36 pm

    It’s certainly possible. For various reasons, we’ve spent a lot of effort of the last 30-40 years trying to make it as difficult as possible, but it is possible. In most systems now, there are hardware and software mechanisms that attempt to protect data space from being executed.

    The basics, though, are fairly straightforward: you construct a piece of code, and assemble it, either by hand or4 via a compiler. You then need a fragment of code space, so you insert the code into your program

    unsigned int prgm[] = { 0x0F, 0xAB, 0x9A ... };  // Random numbers, just as an example 

    since you wanted to use the heap you need to malloc the space

    void * myspace ; if((myspace= malloc(sizeof(prgm))) != NULL) {      memcpy(myspace, pgrm, sizeof(pgrm)); } else { // allocation error } 

    Now, what you need is a way to get the program counter to point to that chunk of data that is also your chunk of code. Here’s where you need a little craftiness. Setting the program counter is no big deal; that’s just a JUMP instruction for your underlying machine. But how to do that?

    One of the easiest ways is by purposefully messing with the stack. The stack, again conceptually, looks something like this (the details depend on both your OS and compiler pairs, and on your hardware):

         | subroutine return addr |     | parameters ...         |     | automatic variables    | 

    The basic trick here is to sneakily get the address of your code into the return address; when a routine returns, it basically jumps to that return addrfess. If you can fake it out, the PC will be set to where you like.

    So, what you need is a routine, let’s call it ‘goThere()’

    void goThere(void * addr){     int a ;     // observe above; this is the first space                  // on the stack following the parameters     int * pa;   // so we use it's address      pa = (&a - (sizeof(int)+(2*sizeof(void*))) ;  // so use the address                 // but back up by the size of an int, the pointer on the                 // stack, and the return address     // Now 'pa' points to the routine's return add on the stack.     *pa = addr; // sneak the address of the new code into return addr     return ;    // and return, tricking it into 'returning'                 // to the address of your special code block } 

    Will it work? Well, maybe, depending on the hardware and OS. Most modern OS’s will protect the heap (via memory mapping or similar) from the PC moving into it. This is a useful thing for security purposes, because we’d just as well not let you take that kind of complete control.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 70k
  • Answers 70k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer No, there's still a distinct possibility of collision - because… May 11, 2026 at 12:56 pm
  • added an answer To start off, you should read Dates and Times Programming… May 11, 2026 at 12:56 pm
  • added an answer The touch events are not currently supported by Events/live. From… May 11, 2026 at 12:56 pm

Related Questions

Is it possible to write a doctest unit test that will check that an
Is it possible to write a regular expression that matches a nested pattern that
Is it possible to write a user interface in Java for an application written
Is it possible to write a template that changes behavior depending on if a
std::next_permutation (and std::prev_permutation) permute all values in the range [first, last) given for a
I have a Status class in C#, used like this: Status MyFunction() { if(...)
When I write Java webapps, I usually use JSTL tags. I think that these
I'm having some trouble in my first foray into threads in C. I'm trying

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.