[Python-Dev] valgrind

Herman Geza hg211 at hszk.bme.hu
Tue Nov 7 18:09:46 CET 2006

> > For example 
> > if free(addr) sees that the memory block at addr is the last block then it 
> > may call brk with a decreased end_data_segment.
> It can't. In brk, you can only manage memory in chunks of "one page"
> (i.e. 4kiB on x86). Since we only access memory on the same page,
> access is guaranteed to succeed.
Yes, I'm aware of it. But logically, it is possible, isn't it?
At malloc(), libc recognizes that brk needed, it calls sbrk(4096).
Suppose that python releases this very same block immediately. At free(), 
libc recognizes that sbrk(-4096) could be executed, so the freed block not 
available anymore (even for reading)

> > Or the last block 
> > in an mmap'd area - it calls unmap. So when Py_ADDRESS_IN_RANGE tries 
> > to read from this freed memory block it gets SIGSEGV. However, I've never 
> > got SIGSEGV from python. 
> Likewise. This is guaranteed to work, by the processor manufacturers.
The same: if the freed block is the last one in the mmap'd area, libc may 
unmap it, doesn't it?

> > I don't really think that reading from an already-freed block is ever 
> > legal. 
> Define "legal". There is no law against it; you don't go to jail for
> doing it. What other penalties would you expect (other than valgrind
> spitting out error messages, and users complaining from time to time
> that it's "illegal")?
Ok, sorry about the strong word "legal".

> > I asked my original question because I saw that I'm not the only 
> > one who sees "Illegal reads" from python. Is valgrind wrong in this case?
> If it is this case, then no, valgrind is right. Notice that valgrind
> doesn't call them "illegal"; it calls them "invalid".
> > I just want to be sure that I'll never get SIGSEGV from python.
> You least won't get SIGSEGVs from that part of the code.
That's what I still don't understand. If valgrind is right then how can 
python be sure that it can still reach a freed block?

> > Note that Misc/valgrind-python.supp contains suppressions "Invalid read"'s 
> Right. This is to tell valgrind that these reads are known to work
> as designed.
Does this mean that python strongly depends on libc? If I want to port 
python to another platform which uses a totally different malloc, is 
Py_ADDRESS_IN_RANGE guaranteed to work or do I have to make some changes?
(actually I'm porting python to another platfrom that's why I'm asking 
these questions, not becaue I'm finical or something)

Geza Herman

More information about the Python-Dev mailing list