1

I want to change malloc, memcpy and free function and use this libhooker with LD_PRELOAD.
I change them, and test them for some test cases. but in one of them, my code doesn't work correct and don't know why.
Test Case:

int main()
{
    string s = "Hello";
    return 0;
}

my code output:

malloc[0x8229170-0x8229182]
[memcpy] source address 0x8048850 is not allocated.
free(0x8229170)

I don't know how c allocate memory for constant string and why my code is wrong.
Thanks in advance.

Alberto Bonsanto
  • 17,556
  • 10
  • 64
  • 93
user3541386
  • 15
  • 1
  • 4
  • 5
    string? do you mean c++? – Salgar May 23 '14 at 15:52
  • 2
    You are going to provide some background info. How does running that program produce that output? String literals are not allocated by `malloc`. It's part of the data segment (implementation dependent) and not managed by `malloc/free`. – P.P May 23 '14 at 15:54
  • 1
    Possible duplicate: http://stackoverflow.com/questions/2589949/c-string-literals-where-do-they-go – Bill Lynch May 23 '14 at 15:58

1 Answers1

4

String constants typically get allocated in DATA section of a program. This is usually marked as read-only and the location offset is defined at compile time.

This memory is not usually allocated by malloc.

I'm not sure what you're trying to do with memcpy, but you can not assume that everything you want to memcpy will have been allocated using malloc. For instance, everything allocated on the stack. Everything in the data section. Everything allocated using a custom allocator (which may live on the stack). Also you need to be aware of calloc, alloc and realloc too.

Salgar
  • 7,687
  • 1
  • 25
  • 39