Having succeeded in making a toolchain and porting Newlib for my OS, I am currently facing quite a great problem : there seems to be a bug with malloc_r() function. Precisely, I get a Page Fault caused by bad virtual address 0x00000004. I disassembled the binary and I found that the bug occured at this line :
40001900: 89 41 04 mov %eax,0x4(%ecx)
So I guessed that ECX had been previously set to 0. I then looked for the place where it may have happened :
Code: Select all
400018bb: 89 55 d0 mov %edx,-0x30(%ebp)
400018be: 89 4d cc mov %ecx,-0x34(%ebp)
400018c1: e8 3a 06 00 00 call 40001f00 <_sbrk_r>
if (new_brk == (char*)(MORECORE_FAILURE))
400018c6: 83 c4 10 add $0x10,%esp
400018c9: 8b 55 d0 mov -0x30(%ebp),%edx
400018cc: 83 f8 ff cmp $0xffffffff,%eax
400018cf: 8b 4d cc mov -0x34(%ebp),%ecx[color=#FF0000] // Here ?[/color]
400018d2: 0f 84 ba 03 00 00 je 40001c92 <_malloc_r+0x792>
My sbrk() function works, and I checked that the program really made the syscall to sbrk() and that the return value was correct (by printing debug strings in my kernel).
Where does the problem come from, according to you ?
Thanks for your help.
EDIT : Maybe the memory should be "formatted" in a particular way by sbrk() ? I say that because I use my own format (1 "used" bit and 31 size bits), and not that of standard lib for memory chunk headers.