【问题标题】:Finding the roots of a function on the stack在堆栈上查找函数的根
【发布时间】:2013-12-01 20:28:19
【问题描述】:

我是一名 CS 专业的学生,​​我的班级有一个实验室来创建一个保存内存信息(位置、大小等)的链表,以模拟 C 语言中的简单垃圾收集器。我们需要做的一件事就是找到我们系统的基本指针。问题是我们几乎没有人能够做到,教授暗示这些概念将在决赛中。

实验室已经结束,所以不要担心这是否值得一个分数或任何东西,我正在努力掌握这个想法,以便为我的期末考试做好准备。

我不确定“根”是否是一个常用术语,但我们的想法是我们保存主函数基的位置,然后当我们调用根函数时,我们立即保存它的位置函数,并遍历两者之间的所有内存,寻找指向我们链表的指针。做的指针被认为是“根”。


这是我实验室的代码。也许只是看它比尝试解释它更容易。我意识到它可能不是很好的代码,它并没有真正任何事情,但我只是按照他们告诉我的去做。

*我的问题是我的链接列表中的“开始”和“结束”范围从未被指向 当我遍历堆栈时,所以我相信我的指针一定做错了。我的 &start 和 &fin 总是非常接近迭代器,但从不重叠,我不明白为什么。

两个文件都保存为 .c 文件,编译它应该像 gcc -g *.c 一样简单

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
//#include "gc_lib.h"

typedef struct hnode{

    bool used;
    long size;
    void* loc;
    struct hnode* next;
    struct hnode* prev;
}hnode;

//Globals
long HEAP_SIZE = 0;
bool AUTO_FREE = false;
void* MAIN_BASE = NULL;
void* MY_HEAP = NULL;
hnode* head = 0;


bool gc_init( long heapsize, void* main_base, bool autofree ){

    if( heapsize <= 0 )
        return false;

    HEAP_SIZE = heapsize;
    AUTO_FREE = autofree;
    MAIN_BASE = main_base;

    if( ( MY_HEAP = malloc( HEAP_SIZE ) ) == NULL )
        return false;

    return true;    
}


void* gc_malloc( unsigned long size ){

    if( size <= 0 )
        return NULL;

    //first malloc
    if( !head ){

        head = malloc( sizeof( hnode ) );
        head -> size = size;
        head -> loc = MY_HEAP;
        head -> used = true;        
        head -> prev = 0;

        hnode* hMem = malloc( sizeof( hnode ) );
        hMem -> size = HEAP_SIZE - size;
        hMem -> loc = (void*)((char*)(MY_HEAP) + size);
        hMem -> used = false;
        hMem -> next = 0;
        hMem -> prev = head;

        head -> next = hMem;

        return head -> loc;
    }

    hnode* findSpot = head;
    void* tempLoc = MY_HEAP;    
    int tempS = 0;

    while( findSpot ){

        //Used node
        if( findSpot -> used == true ){

            tempS += findSpot -> size;
            tempLoc = (void*)((char*)(MY_HEAP) + tempS);
            findSpot = findSpot -> next;
        }
        //Empty node; fits perfectly
        else if( ( findSpot -> used == false ) && ( findSpot -> size == size ) ){

            findSpot -> used = true;
            return findSpot -> loc;
        }
        //Empty node; fits imperfectly
        else if( ( findSpot -> used == false ) && ( findSpot -> size > size ) ){

            int splitSize = ( findSpot -> size ) - size;

            findSpot -> used = true;
            findSpot -> size = size; 

            hnode* newNode = malloc ( sizeof( hnode ) );
            newNode -> prev = findSpot;
            newNode -> next = findSpot -> next;
            newNode -> size = splitSize;
            newNode -> used = false;

            if( findSpot -> next )
                findSpot -> next -> prev = newNode;

            findSpot -> next = newNode;
            tempS += findSpot -> size;
            tempLoc = (void*)((char*)(MY_HEAP) + tempS);

            newNode -> loc = tempLoc;
            return findSpot -> loc;
        }
        //Empty node; too small
        else if( ( findSpot -> used == false ) && ( findSpot -> size < size ) ){

            tempS += findSpot -> size;
            tempLoc = (void*)((char*)(MY_HEAP) + tempS);
            findSpot = findSpot -> next;
        }
    }

    return NULL;
}


void print_roots( void ){

    register void* base asm("ebp");

    void* iter = base;
    printf( "Roots:\n" );

    if( head ){

        void* mBase = MAIN_BASE;
        hnode* nTemp = head;
        void* start = 0;
        void* fin = 0;

        while( iter != mBase ){

            if( nTemp )
                start = nTemp -> loc;

            while( nTemp && nTemp -> used)                
                nTemp = nTemp -> next;

            fin = nTemp -> loc + nTemp -> size;


            if( iter >= start && iter <= fin )
                fprintf( stdout, ">>>>%p\n", iter );

            printf("iter: %p\n", (iter)++ );
        }

        printf("MAIN_BASE: %p\n", MAIN_BASE );
        printf("base: %p\n", base );
        printf("\tstart: %p\n", &start );
        printf("\tfin: %p\n", &fin );
    }
}

这是我们提供的测试文件:

#include <stdio.h>
#include <stdbool.h>
#include <assert.h>

#include "gc_lib.h"



int main(int argc, char** argv){
    register void* base asm("ebp");
    gc_init(100, base, false);

    void* x1 = gc_malloc(8);
    assert(x1 != NULL);
    void* x2 = gc_malloc(8);
    assert(x2 != NULL);
    assert((x2 == x1 + 8) || (x2 == x1 - 8));

    printf("%p\n", x1);
    printf("%p\n", x2);

    print_roots();

    return 0;
}

【问题讨论】:

    标签: c pointers cpu-registers dereference


    【解决方案1】:

    如果我是正确的,你想知道为什么永远不会满足以下条件,并且相应的 printf 永远不会执行:

     if( iter >= start && iter <= fin )
                fprintf( stdout, ">>>>%p\n", iter );
    

    据我所知,代码register void* base asm("ebp");base 变量放在EBP 寄存器中。虽然,似乎只是建议编译器将其放置在那里,因此可以被忽略 - 来自gcc docs

    此选项不保证 GCC 生成的代码始终在您指定的寄存器中包含此变量。

    因此,无法保证获得 EBP 值。

    但这里似乎并非如此。 iterbase 值开始,这是一个指向调用 void print_roots( void ) 的位置的指针(我在这里可能错了,但这并不重要 - 它指向堆栈中的某个位置)。它通过增加它的值进行迭代,直到它等于MAIN_BASE,它指向一个堆栈,int main(int argc, char** argv) 函数存储关于它自己的一些东西。在这两个值之间,预计会找到 main 函数的局部变量(x1x2),它们指向堆中的某个位置(其中一些 hnode-&gt;loc 指向)。

    以下代码定义了startfin 变量的值:

    nTemp = head;
    if( nTemp )
        start = nTemp -> loc;
    
    while( nTemp && nTemp -> used)                
        nTemp = nTemp -> next;
    
    fin = nTemp -> loc + nTemp -> size;
    

    所以,startfin 指向堆(因为列表中的任何hnode 都是指向堆的指针),而iter 指向堆栈。这就是为什么永远不会满足条件iter &gt;= start &amp;&amp; iter &lt;= fin 的原因。

    【讨论】:

    • 感谢您的回复!在发布此消息后一天左右,我最终弄清楚了同样的事情。不过,你是完全正确的。我需要做一些去引用来访问我想要的东西
    猜你喜欢
    • 1970-01-01
    • 2017-09-12
    • 2014-12-21
    • 2012-04-30
    • 1970-01-01
    • 2015-11-09
    • 2014-08-30
    • 2014-02-25
    • 1970-01-01
    相关资源
    最近更新 更多