找出一个指针是否是在栈,堆或程序文本指点? [英] Find out whether a pointer is pointing at the stack, heap or program text?

查看:84
本文介绍了找出一个指针是否是在栈,堆或程序文本指点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法找出一个指针是否在一个位置指向:

Is there a way to find out whether a pointer is pointing at a location in:


  • 堆栈


  • 或程序(如果有的话,这部分如精灵的.text)?

另外,可以这样做的可移植性(Linux的64/32位,OSX和Windows 7 +)?

Also, can this be done portably (Linux 64/32 bit, OSX and Windows 7+)?

跟进:

我并不想找出是否有什么东西被malloc分配。

I'm not trying to find out if something has been malloc'd.

我想从void *指针到堆栈或堆在数据的程序有效区分void *的函数指针。

I want to efficiently distinguish void* pointers to functions in the program from void* pointers to data on the stack or heap.

这是写在C语言运行库,而不是一个正常的C程序。

This is for a language runtime written in C, not a "normal" C program.

这答案一直到目前为止,最有用的:<一href=\"http://stackoverflow.com/questions/276612/checking-if-something-was-malloced/276626#276626\">Checking如果事情是malloced

This answer has been the most useful so far: Checking if something was malloced

推荐答案

您不能做你的便携式方式想要什么,因为C语言标准没有规定的堆栈,程序区和堆为不同的区域。他们的位置可以取决于处理器结构,操作系统,加载器,链接器和编译器。试图猜测,其中一个指针指向是打破用C提供的抽象,所以你可能是你不应该这样做。

You cannot do what you want in a portable way, because the C language standard does not specify the stack, program area, and heap as distinct areas. Their location can depend on the processor architecture, the operating system, the loader, the linker, and the compiler. Trying to guess where a pointer is pointing is breaking the abstraction provided by C, so you probably you shouldn't be doing that.

不过,也有办法来写code,将使一个正确的猜测在特定的环境。你做到这一点通过检查现有对象的地址,寻找模式。考虑下面的程序。

Nevertheless, there are ways to write code that will make a correct guess for a specific environment. You do that by examining the addresses of existing objects, and looking for patterns. Consider the following program.

#include <stdlib.h>
#include <stdio.h>

void
function()
{
    int stack2;

    printf("stack2:  %15p\n", &stack2);
}

int
main(int argc, char *argv[])
{
    int stack;
    void *heap = malloc(1);
    void *heap2 = malloc(1);

    printf("program: %15p\n", main);
    printf("heap:    %15p\n", heap);
    printf("heap2:   %15p\n", heap2);
    printf("stack:   %15p\n", &stack);
    function();
    return 0;
}

通过检查它的输出,您可以看到一个模式,比如在x64的Linux以下。

By examining its output you can see a pattern, such as the following on x64 Linux.

program:        0x400504
heap:          0x1675010
heap2:         0x1675030
stack:    0x7fff282c783c
stack2:   0x7fff6ae37afc

从上面可以决定(可能)堆从0x1675010了增长,在它下面什么是程序code(或静态数据,这些数据没有提及),并且纸叠生长在一个未predictable方式(可能是由于堆栈随机化)围绕一个非常大的地址,像0x7fff282c783c。

From the above you can determine that (probably) the heap grows up from 0x1675010, anything below it is program code (or static data, which you didn't mention), and that the stack grows in an unpredictable manner (probably due to stack randomization) around a very large address, like 0x7fff282c783c.

在32位英特尔的Linux与输出比较:

Compare this with the output under 32-bit Intel Linux:

program:       0x804842f
heap:          0x804b008
heap2:         0x804b018
stack:        0xbf84ad38
stack2:       0xbf84ad14

微软Windows和32位Microsoft C编译器:

Microsoft Windows and the 32-bit Microsoft C compiler:

program:        01271020
heap:           002E3B00
heap2:          002E3B10
stack:          0024F978
stack2:         0024F964

在Windows Cygwin的GCC:

gcc under Windows Cygwin:

program:        0040130B
heap:           00A41728
heap2:          00A417A8
stack:          0028FF44
stack2:         0028FF14

根据英特尔的32位的FreeBSD GCC:

gcc under Intel 32-bit FreeBSD:

program:       0x8048524
heap:          0x804b030
heap2:         0x804b040
stack:        0xbfbffb3c
stack2:       0xbfbffb1c

在Intel 64位的FreeBSD GCC:

gcc under Intel 64-bit FreeBSD:

program:        0x400770
heap:        0x801006058
heap2:       0x801006060
stack:    0x7fffffffdaec
stack2:   0x7fffffffdabc

在SPARC-64的FreeBSD GCC:

gcc under SPARC-64 FreeBSD:

program:        0x100860
heap:         0x40c04098
heap2:        0x40c040a0
stack:     0x7fdffffe9ac
stack2:    0x7fdffffe8dc

运行的PowerPC MacOS X的:

PowerPC running MacOS X:

program:          0x1ed4
heap:           0x100120
heap2:          0x100130
stack:        0xbffffba0
stack2:       0xbffffb38

的PowerPC运行Linux:

PowerPC running Linux:

program:      0x10000514
heap:         0x100c6008
heap2:        0x100c6018
stack:        0xbff45db0
stack2:       0xbff45d88

StrongARM的运行NetBSD的:

StrongARM running NetBSD:

program:          0x1c5c
heap:             0x5030
heap2:            0x5040
stack:        0xefbfdcd0
stack2:       0xefbfdcb4

和ARMv6的运行Linux:

and ARMv6 running Linux:

program:          0x842c
heap:           0xb63008
heap2:          0xb63018
stack:        0xbe83eac4
stack2:       0xbe83eaac

正如你所看到的可能性是无穷无尽的。

As you can see the possibilities are endless.

这篇关于找出一个指针是否是在栈,堆或程序文本指点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆