0

I am writing an LLVM plugin. In it, I create a global array of pointers that take data along the flow of different functions in the program. When pointers take an address in a function scope, it is logical to assume that, once outside their scope, there is a risk that the content of the memory will be overwritten as the OS sees fit.

I would like to know if there is a way to make the content of the pointer unvariable (unless I find myself in the need to change it) through all scopes, of course, inside the program.

I thought that a flag like volatile would do the trick, but it seems like all this does is preserve its sequential position in comparison to non-volatile instructions.

Palec
  • 12,743
  • 8
  • 69
  • 138
  • When you say "in a function scope" do you mean in the scope of a function in the code that you yourself are writing, or do you mean in the scope of a function being analyzed by your LLVM plugin? – John Zwinck May 04 '16 at 02:14
  • Are you **sure** you **don't** just want to **copy** the **data**, not the **pointer** to the data? – user253751 May 04 '16 at 02:57
  • i could work with the data instead, but, I cant have an array of different types, or can I? I am copying pointer of int16, char*, structs and others. is there a type that can store any other type? the only solution i found is to take their address, because that way i can have an array. sorry about the **bold** – Fernando Pérez May 04 '16 at 15:22

1 Answers1

1

Most likely you need to copy the data.

You can actually mark memory unwritable, which won't allow anyone to write to it. However, if the pointer is on the stack or has been freed, this will probably have undesired consequences (namely, segfaults, and lots of them.) Additionally, it has to be done by page, e.g. in large chunks.

Remember, if I free a pointer, the data it pointed to can get reused later by another function. Even if I "promise" not to modify the data pointed to by the pointer, once it's freed... all bets are off. Someone else might allocate the same memory.

If you need to retain the data after free / it goes out of scope, you must copy it.

Todd Christensen
  • 1,297
  • 8
  • 11
  • I understand, but I can only get copy pointers into my globalArray because I dont know how to create an array wich supports ALL existing types. Is there a way? – Fernando Pérez May 04 '16 at 15:24
  • One strategy is to allocate your own pointer, copy the data in, and then place that new pointer into your global array. – Todd Christensen May 04 '16 at 18:47
  • That would work on some scenarios, but mine has some restriction on that, plus allocating my own pointer (1 or 2) would be simple, but my function works with three or four hounder pointers per module, so... Anyway I have altered my paradigms and now my pointers never go dangling. BTW Thanks a lot Todd. – Fernando Pérez May 11 '16 at 04:14