I have this code below that prints a pointers’ memory address. I’ve never noticed it before, but the returned value is in a specific format that, I guess, does not represent the real virtual memory address.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char* ptr = (char *) malloc(250 * sizeof(char));
char* ptr2;
int val;
strcpy(ptr, "Hello, world!");
printf("Mem addr: %pn", ptr);
...
}
Execution results in:
Mem addr: 0x143e2a0
In kernel context, using printk
, you can use the format specifier %p
to print this kind of unique identifier that “masks” the real virtual memory address, accordingly to kernel’s documentation. And there is a variation %px
that gives you the “real” print of the address.
Is there a way to translate this identifier printed by the user program for a real virtual memory address in a kernel module, for example? I guess I could implement a kernel module to help me getting this real value, because I imagine this kind of information isn’t available in user context for security reasons. Is there a specific kernel routine to do this?
PS1: Notice that, when I say “real virtual memory address” i’m not refering to “physical address”.
PS2: I’m using a linux kernel 6.1 x86_64 to do this.