0

I am working in Ubuntu 16.04 VM

I have a C program that takes in one argument that is expected to be a hexadecimal number between 0-7ffffffffffffff.

How do I check if the memory address specified is allocated in the virtual memory of my program and access the single byte of memory at the address?

EDIT: so I can see the contents of my mapping via:

FILE *fptr = fopen("/proc/self/maps", "r");
c = fgetc(fptr);
while (c != EOF){
   print("%c\n", c);
   c = fgetc(fptr);
}

and I'm using the following function: void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) that returns a pointer to the allocated region

however when I try using the command line argument, I type cast (void *) addr and it changes it to the hex value when I'm to assume that it is already in hex and I don't know how to avoid it unnecessarily converting.

  • The memory space of the program is not deterministic in part due to address space layout randomization. Why do you need to know this? Are you attempting to exploit a vulnerability? – dbush Oct 08 '18 at 20:37
  • The question [Checking if a pointer is allocated memory or not](https://stackoverflow.com/questions/1576300/checking-if-a-pointer-is-allocated-memory-or-not) may interest you. – Missu Oct 08 '18 at 20:40
  • From the link I don't like the "Conclusion: So it is impossible to check if a particular pointer is allocated with memory or not within a function" the poster came up with, I have seen interesting in using something with my proc/pid/maps – phirom peterschmidt Oct 08 '18 at 20:47
  • "it changes it to the hex value" This doesn't parse. Please specify what command you are running, what input you give, and what output you observe. There's no such thing as "hex value". Integer values are integers and pointer values are pointers. They don't have any inherent base. Their printed representation may or may not be hexadecimal. – n. m. could be an AI Oct 09 '18 at 03:30
  • printf("%ld", argv[1]); will print the argument passed, however I want to type cast it as (void *) which printf("%p", (void *) argv[1]); which will print the hexadecimal value of the argument as if that value were decimal. – phirom peterschmidt Oct 09 '18 at 14:35
  • Once again, *values* are not decimal or hexadecimal. *Printed representations* are. `%p` conversion prints the address using hexadecimal notation. It doesn't mean that "the address is a hexadecimal value". The latter phrase just makes no sense. It isn't clear what your problem is. (Of you want to answer to a specific user, use the @ notation like this: @phirompeterschmidt). – n. m. could be an AI Oct 09 '18 at 16:22

0 Answers0