0

I am trying to check validity of pointers in C, specifically if a pointer is trying to access memory outside the known program space. Is there a way to get the addresses of the allotted program stack's bounds (hopefully a cross platorm method), so that before dereferencing a pointer we can at least make sure the pointer is in program space? I have no clue where to start and Google hasn't helped me so far.

StealthyPanda
  • 85
  • 1
  • 4
  • 2
    The program's valid space is not guaranteed to be contiguous, and different platforms will have subtle differences in the way the sub-spaces are named, defined, and organized. These checks are also too coarse to actually protect your program meaningfully - you could have a pointer that points to totally unrelated garbage, to a critical data structure, garbage that vaguely resembles what you were looking for (before it got moved/reallocated), etc. – nanofarad Jul 21 '23 at 19:09
  • 1
    It isn't legal in C to use a pointer to access anything but a specific object, or a range of memory that has been dynamically allocated. Even the pointer itself is invalid unless it points within an object, or just past it. It isn't enough to make sure the pointer you dereference is in "the program space". – Weather Vane Jul 21 '23 at 19:12
  • @StealthyPanda, "> I am trying to check validity of pointers in C," --> Post your code where this is needed so we may provide solutions. – chux - Reinstate Monica Jul 21 '23 at 19:15
  • 1
    Libraries can be dynamically loaded and unloaded, and each library may come with various constant data, zero-initialized data, and other segments. Memory can be dynamically mapped at program-requested addresses and can be unmapped. To check a pointer, you would need to step through a list of all intervals currently mapped in the process. I expect some systems may provide means of doing that, but a simpler technique would be to set a handler for access violations and then test the access by trying it and seeing if the handler is called. And whatever solution you use will not be cross-platform. – Eric Postpischil Jul 21 '23 at 19:29
  • *"I am trying to check validity of pointers in C"* You have chosen the wrong language. Forget it. If you want safe code do not use pointers. As it is not possible in C you need to choose another language – 0___________ Jul 21 '23 at 20:44
  • The 8086 16-bit mode was infamous for pointers with different values being the same address. It used segment registers (actually overlapping 64K pages) such that a full sized (`far`) pointer was a segment and an offset, where segment (binary) 10 offset 10000 was the same address as segment 11 offset 10. – John Bayko Jul 21 '23 at 20:49
  • Memory can be from the program heap, but can also be a mapped file, or shared segment, or in Windows a DLL, all of which will be some segment isolated in the address space to avoid any possible overlaps. No actual bounds, unless you keep track of every memory resource a program ever acquires. This can be automated in other languages that restrict pointers (Java, C#, go), but not C. – John Bayko Jul 21 '23 at 20:52
  • 1
    @JohnBayko [different values being the same address.](https://stackoverflow.com/questions/76740605/is-there-a-way-to-get-the-pointers-bounding-the-program-space-in-c/76740668?noredirect=1#comment135293401_76740605) is true, yet those 2 pointers equate equal to each other. – chux - Reinstate Monica Jul 21 '23 at 21:00

3 Answers3

2

Is there a way to get the addresses of the allotted program stack's bounds (hopefully a cross platorm method)

No.

... before dereferencing a pointer we can at least make sure the pointer is in program space?

Even if in program space, without knowing the type, dereferencing a pointer risks undefined behavior (UB).

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
2

First of all, comparing pointers (except equality) using pointers not referencing the same array is undefined.

You would have (knowing the boundary of the available address space - which is a separate problem) to iterate through all the addresses and check for non-equality. It would be rather expensive and not practical...

Is it possible in C (not invoking UB) to check if two objects overlap?

0___________
  • 60,014
  • 4
  • 34
  • 74
1

I am trying to check validity of pointers in C, specifically if a pointer is trying to access memory outside the known program space.

There are existing tools for this. AddressSanitizer, for instance, which is presently supported by a lot of the more popular compilers. Or the standalone tool Valgrind.

Is there a way to get the addresses of the allotted program stack's bounds (hopefully a cross platorm method), so that before dereferencing a pointer we can at least make sure the pointer is in program space?

There is no such mechanism defined by the C language itself. For some targets, there are platform-specific ways to determine the bounds of a program's address space, but this is not necessarily a single, contiguous chunk, nor constant over time, and the data are not necessarily directly comparable to pointers.

Even having this information in the form of pointers, C does not define pointer comparisons for pointers not derived from the same object.

I have no clue where to start and Google hasn't helped me so far.

Not to put too fine a point on it, but I would suggest you don't start. If you had something new to bring to the table, then maybe, but that does not seem to be the case. If your idea is to learn about C or about programming in general, then choose a simpler project, or join an existing one.

I can appreciate that you might not have recognized the complexity of what you describe. It's hard to know what you don't know. But "I have no clue where to start and Google hasn't helped me so far" is a pretty good sign that either you haven't defined your problem clearly enough or that you are trying to reach too far beyond your current knowledge and ability. In this case, I suspect it's a bit of both.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157