Goal: I would like to assign the content of a known function to a function pointer in LINUX environment.
Problem: Get segmentation fault when running the function pointer after assignment.

Procedure:
1). declare a funtion pointer.
2). Allocate memory space to the function pointer using the function' malloc'
3). Copy the content of a known function to the function pointer using
'memcpy'. (the interface of the known function is the same as the function pointer).
4). check the memory content of the function pointer using 'memcmp'.
Result: two memory contents match.
5). Run the function pointer
Result: segmentation fault.

Thanks for your attention.

Dani AI

Generated

described copying raw function bytes into malloc'ed memory, seeing identical bytes with memcmp, then getting a SIGSEGV when calling through the copied pointer. was right that simply pointing a function pointer at the existing function is the simplest fix, and correctly noted the deeper reasons why copying rarely works.

Two common causes for the crash: (1) modern Linux often marks heap/data pages non‑executable (the NX bit); writable memory is not allowed to execute unless you explicitly map or change protections, and (2) compiled code contains address-dependent values (relocations, GOT/PLT entries or PC-relative offsets). Copying raw bytes does not update those embedded addresses, so calls or data loads inside the function will jump/read to wrong places.

If copying code is necessary, follow these minimal rules:

  • Allocate page-aligned memory and give it execute permission (use mmap or mprotect). See mmap(2) and mprotect(2) for the correct flags and alignment rules (mmap(2), mprotect(2)).
  • Copy the bytes only into writable memory, then change the page to executable (avoid leaving RWX at the same time if your platform enforces W^X policies).
  • On some CPUs you must flush the instruction cache after writing code; use the compiler builtin where available (for example __builtin___clear_cache). See the GCC builtin docs (GCC Other Builtins).
  • Debug with gdb: disassemble both the original and copied code, inspect /proc/self/maps to confirm permissions, and use readelf -r on the object to find relocations that would need fixing.

Better alternatives for most needs: assign the function pointer directly, generate position‑independent code via a proper JIT framework (libffi/LLVM/etc.), or build a shared object and load it. Copying code into the heap is fragile, nonportable, and a frequent source of subtle crashes.

Recommended Answers

All 2 Replies

Goal: I would like to assign the content of a known function to a function pointer in LINUX environment.
Problem: Get segmentation fault when running the function pointer after assignment.

Procedure:
1). declare a funtion pointer.
2). Allocate memory space to the function pointer using the function' malloc'
3). Copy the content of a known function to the function pointer using
'memcpy'. (the interface of the known function is the same as the function pointer).
4). check the memory content of the function pointer using 'memcmp'.
Result: two memory contents match.
5). Run the function pointer
Result: segmentation fault.

Why not just this:

  • Declare function pointer.
  • Point to the function (assign the pointer).
  • Run the function pointer.

Right, the problem with allocating ram and MOVING code into the ram is that jumps and other address references 'fixed up' by the linker are no longer correct.

Here's what Dave was saying:

typedef int (*MyFunctionPtr)(int foo);

int SomeFunction( int foo )
{
return foo;
}

MyFunctionPtr f;

f = SomeFunction; // points to the function SomeFunction()

printf("%d\n",f(4)); // prints '4'

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.