man
man

Reputation: 155

Going through the stack

I made a simple function to iterate through the whole stack and print out contents of each entry in there.

void ListStack()
{
    NT_TIB* tib = (NT_TIB*)NtCurrentTeb();

    for (void* address = tib->StackBase; address > tib->StackLimit; address = (char*)address - sizeof(void*))
    {
        void* pointsTo = *((void**)address);

        printf("address=%p, points to=%p\n", address, pointsTo);
    }
}

On x86, this works fine. However, on x64, it gives me read access violation.

What's wrong with this?

Upvotes: 1

Views: 202

Answers (1)

user1354557
user1354557

Reputation: 2503

The problem is that you are dereferencing tib->StackBase.

The stack region is greater than or equal to tib->StackLimit and less than tib->StackBase. It is very likely that the page that page containing tib->StackBase will not be mapped into memory.

So just change your for loop to look like this:

for (void* address = (char*)tib->StackBase - sizeof(void*);
           address >= tib->StackLimit;
           address = (char*)address - sizeof(void*))

Upvotes: 1

Related Questions