Probing a virtual memory address.
Hokay, long long time ago, I worked on a Vax machine.
It had a nifty OS call that allowed you to probe a memory address and find out if you could read or write to it. (They were based on machine code instructions PROBER and PROBEW, power pc architecture has 'em too.) Sure you can do... int isWritable( int * p) int tmp = *p; int result = 0; *p = tmp+1; result = *p == (tmp+1); *p = tmp; return result; } but if there is nothing there you'll segfault, and if there is DMA hardware there... who knows what will happen. Is there a linux routine I can invoke to work out if an address is writable? |
I would look at pmap source code. "pmap -x pid" is displaying the infomation needed.
|
Quote:
(To trivially see that, run "strace pmap -x PID") Anything better out there? |
I'm missing why you discard pmap and its Linux implementation. All the information you need is there, isn't it ?
|
IsValidDataPointer,IsValidFunctionPointer
Quote:
I want to implement the following two asserts... assert( IsValidDataPointer(pointer)); assert( IsValidFunctionPointer(pointer)); So what on ye olde 1980's Vax was a single machine code instruction, using the /proc/PID/maps solution will be open a file, scanf about 100 lines, close file. And scanf is an extraordinarily heavyweight function involving 10's of thousands of machine instructions. Ok, can be done. Just seems way too heavyweight. |
I agree it is heavyweight compared to a single machine code instruction but given the fact the x86 architecture is missing these instructions, it has to be done another way. The pmap solution is also probably inaccurate as the pseudo file content might be partially obsoleted by its own parsing. Beware too that a read-only memory address doesn't imply it is executable nor it necessarily points to a valid instruction.
Anyway, here is a quick and dirty portable implementation I just wrote to see if it was doable in C. Make sure compiler optimizations are disabled for the write probe to work. Code:
#include <stdio.h> |
Quote:
Code:
|
Your probew implementation is doing unnecessary tests if your goal is to implement the VAX original instructions and might be destructive too if you are unlucky.
|
I guess I'm trying to be too portable...
Quote:
As to the unlucky, I doubt if your test is atomic either. Your test probably also compiles to load to register, do something in register, store to memory. The sad thing is the routine I want is available in the kernel... It is called "access_ok". It just doesn't seem to be exported into userland. |
Every program that writes something to a random address might screw up itself of worst, especially if it writes something different than what was there before.
On your embedded device, access_ok might be implemented by using a similar approach than the initial solution I suggested, according to its documentation. Note that, depending on architecture, this function probably just checks that the pointer is in the user space range - after calling this function, memory access functions may still return -EFAULT. I'm afraid what you are after has no perfect solution. |
Quote:
Now I just need to work out to package it as a snippet of inline assembler, I'm done! |
All times are GMT -5. The time now is 08:25 AM. |