So this post has been triggered by an issue I have just had using strdup in the 
OS components of a protected mode build.

For clarity I will be using the term OS for the kernel part of the build and 
application/app for the user part of the build.

The TLDR; is this design question:

Do we expect malloc and free to work with the relevant heaps?  So for the OS 
should they be working with the kernel heap and for apps they should be using 
the user heap?

I am trying to work out if the problem is with strdup or with malloc & free.


Investigation / background

My understanding of strdup is that any string generated should have its memory 
released using free, at least that is the way I have been using it for years.

The system under development uses a protected/kernel build with a memory 
protection unit.  We also have two heaps, one for the OS and one for the app.

At some point in the OS lifecycle we have the need to generate copies of 
strings.  These were generated using strdup.  At some point in the future these 
strings were released using free.  At a point further in the future the system 
crashed.

After some tracking it turns out that strdup was allocating memory using the 
kernel heap (the strings were duplicated in the OS) and then freed in the OS 
but the memory was being released to the user (Application) heap.  When this 
was then later allocated and used in user space the system would crash with a 
memory fault.

Investigation points to the fact that strdup uses lib_malloc which will call 
kmm_malloc in OS builds and malloc in app builds.

It also appears that malloc and free always work with the user heap.

I know that our build is a little old but looking at the sources this seems to 
be the case with the current release.  I am struggling to get the protected 
build working on a F767 board to verify if the problem is still present.

Regards,
Mark
_____________________________
Blog: blog.thepcsite.co.uk
Twitter: @nevynuk





Reply via email to