OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Apr 25, 2024 11:01 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Telling dlmalloc() where the heap is
PostPosted: Thu Jul 01, 2004 4:04 am 
Hi

I'm currently writing my memory manager and I have a curiuose problem with dlmalloc().

My kernel is loaded at 0xC0000000 and I want my Kernel heap to be at 0xE0000000.

I have written a page allocator, mapper and an sbrk and on their own they work really well.

If I try this:
*foo = malloc(0x100000); /* I want 1MB from the heap */

It calls my sbrk nicely and I can see using the bochs debugger that 1MB worth of pages (and their page table) have been mapped at 0xE0000000. Great!

But the trouble I'm having is that after this, I get a page fault that something (possibly dlmalloc) is writing to 0xC above that 1MB? huh?

So I get my mapper to map one more page than needed and the 0xC can then be written to.

But, now I get a page fault saying that something has try to write to the 0x802xxxxx region, way off my heap.

How can I tell dlmalloc where my heap is, or is there aome other problem

thanks
srg


Top
  
 
 Post subject: Re:Telling dlmalloc() where the heap is
PostPosted: Thu Jul 01, 2004 5:29 am 
Offline
Member
Member
User avatar

Joined: Sat Jan 15, 2005 12:00 am
Posts: 8561
Location: At his keyboard!
Hi,

srg wrote:
But the trouble I'm having is that after this, I get a page fault that something (possibly dlmalloc) is writing to 0xC above that 1MB? huh?


Either some code uses memory that it didn't allocate, or dlmalloc is buggy.

What does your OS do to handle a page fault? You need some way of getting the EIP that is pushed on the stack when a page fault occurs. Use this EIP to find the code that caused the page fault and figure out why it caused the page fault.

srg wrote:
So I get my mapper to map one more page than needed and the 0xC can then be written to.



You can't fix a bug by hiding it...

srg wrote:
But, now I get a page fault saying that something has try to write to the 0x802xxxxx region, way off my heap.


Hmm - maybe there 2 bugs! :)


Cheers,

Brendan

_________________
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.


Top
 Profile  
 
 Post subject: Re:Telling dlmalloc() where the heap is
PostPosted: Thu Jul 01, 2004 6:01 am 
Brendan wrote:
Hi,

srg wrote:
But the trouble I'm having is that after this, I get a page fault that something (possibly dlmalloc) is writing to 0xC above that 1MB? huh?


Either some code uses memory that it didn't allocate, or dlmalloc is buggy.

What does your OS do to handle a page fault? You need some way of getting the EIP that is pushed on the stack when a page fault occurs. Use this EIP to find the code that caused the page fault and figure out why it caused the page fault.

srg wrote:
So I get my mapper to map one more page than needed and the 0xC can then be written to.



You can't fix a bug by hiding it...

srg wrote:
But, now I get a page fault saying that something has try to write to the 0x802xxxxx region, way off my heap.


Hmm - maybe there 2 bugs! :)


Cheers,

Brendan



Don't worry, I'm not tryng to hide it. Anyway, according to the 386 Programmers Manual, all that is on the stack is a small error code? I'll investigate.

I must admit I doubt dlmalloc is buggy, Tim and others use it. Also some linux's use it AFAIK.

srg


Top
  
 
 Post subject: Re:Telling dlmalloc() where the heap is
PostPosted: Thu Jul 01, 2004 6:06 am 
Offline
Member
Member
User avatar

Joined: Thu Nov 16, 2006 12:01 pm
Posts: 7614
Location: Germany
dlmalloc() has indeed been the base for the Linux memory management. The chance that you've found a bug in a release version of dlmalloc() is about as big as that it's a bug introduced by the GCC code generator.

_________________
Every good solution is obvious once you've found it.


Top
 Profile  
 
 Post subject: Re:Telling dlmalloc() where the heap is
PostPosted: Thu Jul 01, 2004 8:46 am 
Right

Sorry about that Bendan, I've found out about where the EIP is saved.

Anyway, I do have a Page Fault handler that ATM pops into eax, ebx and ecx, writes 'P' to the first byte fo screen memory and then halts, so I can use the bochs debugger on it.

As for this problem, I've fixed a couple of bugs of my own and it never tries to write anything in the 0x802xxxxx range any more.

BUT There is still an overshoot above tha amount of memory requested to be allocated, and it's by 12 bytes. I've tracked down the address of the offending instruction and it's in dlmalloc. What's more, the more times I call malloc, the overshoot (for want of a better word ) gets bigger and bigger.

Could I have my dlmalloc mal-adjusted possibly?

I have it attached.

srg


Top
  
 
 Post subject: Re:Telling dlmalloc() where the heap is
PostPosted: Thu Jul 01, 2004 10:10 am 
D'oooooooooohhhhhhhhhhhh

It was an off by one error in my page mapping loop, I forgot that pages start a 0 and not one :-[

thanks for the help
srg


Top
  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Majestic-12 [Bot] and 229 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group