All of lore.kernel.org
 help / color / mirror / Atom feed
* Re: Understanding Linux addr space, malloc, and heap
@ 2005-10-21 13:45 Vincent W. Freeh
  2005-10-21 14:03 ` Arjan van de Ven
  0 siblings, 1 reply; 25+ messages in thread
From: Vincent W. Freeh @ 2005-10-21 13:45 UTC (permalink / raw)
  To: linux-kernel

Thanks for your quick response.  It basically confirmed that I observed 
what I thought I did.  However, I am no closer to solving my problem.  I 
cannot mprotect data that I malloc beyond the first 65 pages.  Why is 
that?  Can that be fixed?  Second, why does mprotect silently fail?  I 
could live with it failing--but I cannot deal with a call the "works" 
but doesn't work.

Thanks,
vince.

-----------
Subject	Re: Understanding Linux addr space, malloc, and heap
From	Arjan van de Ven <>
Date	Fri, 21 Oct 2005 15:00:02 +0200

On Fri, 2005-10-21 at 08:46 -0400, Vincent W. Freeh wrote:
 > I am trying to understand the Linux addr space.  I figured someone might
 > be able to shed some light on it.  Or at least point me to some sources
 > that will help.
 >
 > I don't understand what is happening with malloc and the heap in my
 > process. According to /proc/<pid>/maps the memory from heap to stack
 > initially looks like that.  I only show the four "maps" from the heap
 > and above.  (This is a slightly altered form consisting of start_addr,
 > end_addr, size_in_pgs, permissions, and path_if_one):
 >
 > 0x08d42000 - 0x08d63000 (33 pgs) rw-p   path `[heap]'
 > 0xb7ef8000 - 0xb7ef9000 (1 pgs) rw-p
 > 0xb7f09000 - 0xb7f0b000 (2 pgs) rw-p
 > 0xbfaf5000 - 0xbfb0b000 (22 pgs) rw-p   path `[stack]'
 >
 > First, please fix any erroneous statements/assumptions above.  Next I
 > have many questions.  A few follow.
 >
 > * How does the heap work?  I learned/teach that heap is a contiguous
 > chunk of memory that holds dynamically-allocated memory.  Doesn't appear
 > to be the case.

that's the old school 1970's stuff

the "heap" is still brk in linux, however there is no 1:1 relation
between heap and malloc. malloc in glibc is implemented both using brk
and mmap, depending on the size of your allocation.


 >
 > * Man pg says can only mprotect mmap-able pages.  But what are these?
 > How can I tell?

you need to mmap these yourself to be sure.. eg you cannot mprotect the
output of malloc, at least not reliably. Only of mmap.

 >
 > * Why does mprotect silently fail?

no it has sideeffects; eg it most likely affects more memory than just
your malloc()'d part


 > * I thought brk indicated the top of the heap and that all dynamic
 > memory would be between bss end and brk.  That's not true.  What is brk
 > for then?

see definition of heap vs malloc above



^ permalink raw reply	[flat|nested] 25+ messages in thread
[parent not found: <505ru-8qi-1@gated-at.bofh.it>]
* Understanding Linux addr space, malloc, and heap
@ 2005-10-21 12:46 Vincent W. Freeh
  2005-10-21 13:00 ` Arjan van de Ven
  0 siblings, 1 reply; 25+ messages in thread
From: Vincent W. Freeh @ 2005-10-21 12:46 UTC (permalink / raw)
  To: linux-kernel

I am trying to understand the Linux addr space.  I figured someone might 
be able to shed some light on it.  Or at least point me to some sources 
that will help.

I don't understand what is happening with malloc and the heap in my 
process. According to /proc/<pid>/maps the memory from heap to stack 
initially looks like that.  I only show the four "maps" from the heap 
and above.  (This is a slightly altered form consisting of start_addr, 
end_addr, size_in_pgs, permissions, and path_if_one):

0x08d42000 - 0x08d63000 (33 pgs) rw-p   path `[heap]'
0xb7ef8000 - 0xb7ef9000 (1 pgs) rw-p
0xb7f09000 - 0xb7f0b000 (2 pgs) rw-p
0xbfaf5000 - 0xbfb0b000 (22 pgs) rw-p   path `[stack]'

I cannot touch (rd, wr, or even mprotect) the map immediate above the 
heap--must be a sandboxing page.  Before any malloc, brk = 0x8d42000 
first page of heap.

If I malloc <= 33 pages the memory comes from the first map above and 
the brk changes as appropriate.  However, some new maps appear between 
the two above. And the 2d one above gets bigger.  However, all data 
comes from the heap.  brk remain below the top of the heap.  As shown below.

0x08d42000 - 0x08d63000 (33 pgs) ---p   path `[heap]'
0xb7d00000 - 0xb7d01000 (1 pgs) rw-p
0xb7d01000 - 0xb7d21000 (32 pgs) ---p
0xb7d21000 - 0xb7e00000 (223 pgs) ---p
0xb7ef7000 - 0xb7ef9000 (2 pgs) rw-p
0xb7f09000 - 0xb7f0b000 (2 pgs) rw-p
0xbfaf5000 - 0xbfb0b000 (22 pgs) rw-p   path `[stack]'

Now if I malloc > 33 pages, the data comes from the heap and the next 
map(s).  That is the 34th pages is 0xb7d01000, in above example.  What 
is going on?

Another thing I don't understand is that I can touch maps 3 & 4 above 
(0xb7d01000 & 0xb7d21000) both rd and wr.  However, I cannot mprotect 
the 4th map---but mprotect does not fail, just doesn't change 
permissions.  I can mprotect the 32 pages in the map 3.  This is my 
initial problem: I can only mprotect 65 pages.  The 66th page (from map 
4) silently doesn't mprotect.

Looking around at other processes, they seem very different.  Both tcsh 
and emacs (appear to) have the 1 pg sandbox just below the stack (good 
place) and much larger heaps.

First, please fix any erroneous statements/assumptions above.  Next I 
have many questions.  A few follow.

* How does the heap work?  I learned/teach that heap is a contiguous 
chunk of memory that holds dynamically-allocated memory.  Doesn't appear 
to be the case.

* Man pg says can only mprotect mmap-able pages.  But what are these? 
How can I tell?

* Why does mprotect silently fail?

* I thought brk indicated the top of the heap and that all dynamic 
memory would be between bss end and brk.  That's not true.  What is brk 
for then?

Thanks,
v.
--
Vincent (Vince) W. Freeh
Dept of Computer Science
North Carolina State University
http://www.csc.ncsu.edu/faculty/freeh
919-513-7196

^ permalink raw reply	[flat|nested] 25+ messages in thread

end of thread, other threads:[~2005-10-23 21:30 UTC | newest]

Thread overview: 25+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-10-21 13:45 Understanding Linux addr space, malloc, and heap Vincent W. Freeh
2005-10-21 14:03 ` Arjan van de Ven
2005-10-21 15:11   ` Vincent W. Freeh
2005-10-21 15:20     ` Anton Altaparmakov
2005-10-21 15:21     ` Paulo Marques
2005-10-21 15:22     ` Arjan van de Ven
2005-10-21 15:37       ` Vincent W. Freeh
2005-10-21 15:48         ` Arjan van de Ven
2005-10-21 16:04           ` Vincent W. Freeh
2005-10-21 16:23             ` Arjan van de Ven
2005-10-21 15:52         ` Kyle Moffett
2005-10-21 16:10           ` Vincent W. Freeh
2005-10-21 16:19             ` Theodore Ts'o
2005-10-21 16:26             ` Paulo Marques
2005-10-21 16:14         ` Andreas Schwab
2005-10-21 16:24           ` Vincent W. Freeh
2005-10-22 19:27             ` Kyle Moffett
2005-10-21 15:37       ` Alex Bligh - linux-kernel
2005-10-21 15:47         ` Arjan van de Ven
2005-10-21 15:58           ` Paulo Marques
     [not found] <505ru-8qi-1@gated-at.bofh.it>
     [not found] ` <505Lp-B4-81@gated-at.bofh.it>
     [not found]   ` <506QZ-2cH-3@gated-at.bofh.it>
     [not found]     ` <5070Y-2qP-23@gated-at.bofh.it>
     [not found]       ` <507ac-2Cm-25@gated-at.bofh.it>
     [not found]         ` <507NL-3Em-29@gated-at.bofh.it>
     [not found]           ` <507Xd-3QT-19@gated-at.bofh.it>
     [not found]             ` <50xnU-7s2-37@gated-at.bofh.it>
2005-10-23 10:41               ` Bodo Eggert
2005-10-23 10:44                 ` Arjan van de Ven
2005-10-23 21:29                   ` Kyle Moffett
  -- strict thread matches above, loose matches on Subject: below --
2005-10-21 12:46 Vincent W. Freeh
2005-10-21 13:00 ` Arjan van de Ven

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.