All submissions to this site are governed by the Second Life Viewer Contribution Agreement. By submitting patches and other information using this site, you acknowledge that you have read, understood, and agreed to those terms.

Review Board 1.6.11

Welcome to the Second Life Viewer Code Review tool.
See the documentation on our wiki for how to use this site.

STORM-864: As as developer, I would like an object oriented wrapper to make safe use of memory pools easier

Review Request #99 - Created Jan. 17, 2011 and submitted

Aleric Inglewood Reviewers
viewer
STORM-864
None viewer-development
Please see http://jira.secondlife.com/browse/STORM-864

I made a mercurial repository available for testing here:

hg clone https://bitbucket.org/aleric/viewer-development-storm-864

From the commit message:

Introduces a LLThreadLocalData class that can be
accessed through the static LLThread::tldata().
Currently this object contains two (public) thread-local
objects: a LLAPRRootPool and a LLVolatileAPRPool.

The first is the general memory pool used by this thread
(and this thread alone), while the second is intended
for short lived memory allocations (needed for APR).
The advantages of not mixing those two is that the latter
is used most frequently, and as a result of it's nature
can be destroyed and reconstructed on a "regular" basis.

This patch adds LLAPRPool (completely replacing the old one),
which is a wrapper around apr_pool_t* and has complete
thread-safity checking.

Whenever an apr call requires memory for some resource,
a memory pool in the form of an LLAPRPool object can
be created with the same life-time as this resource;
assuring clean up of the memory no sooner, but also
not much later than the life-time of the resource
that needs the memory.

Many, many function calls and constructors had the
pool parameter simply removed (it is no longer the
concern of the developer, if you don't write code
that actually does an libapr call then you are no
longer bothered with memory pools at all).

However, I kept the notion of short-lived and
long-lived allocations alive (see my remark in
the jira here: https://jira.secondlife.com/browse/STORM-864?focusedCommentId=235356&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-235356
which requires that the LLAPRFile API needs
to allow the user to specify how long they
think a file will stay open. By choosing
'short_lived' as default for the constructor
that immediately opens a file, the number of
instances where this needs to be specified is
drastically reduced however (obviously, any
automatic LLAPRFile is short lived).
Compiles and viewer functions normally.

The new classes (LLAPRPool, LLThreadLocalData) and the LLAPRFile changes have been tested a lot more extensive, since they have been used as-is for months in imprudence and before that in my own viewer (since April). However, the patch contains changes to code elsewhere (to adapt it to the new API) that is rather new in this source tree. Note that changes with regard to LLAPRFile already have been in Snowglobe since version 1.1 (with some minor changes) including the method used to make thread-local data available.
Review request changed
Updated (Jan. 29, 2011, 9:10 a.m.)
This new diff incorporates the changes as discussed above, and is updated to be based on revision fe7fe04ccc9a (Fri Jan 28 21:23:35 2011 -0700).
I also added comments in this version on every line that correctly uses apr_pool_t, so it's easy to just do a grep on apr_pool_t and check if nobody accidently used that instead of LLAPRPool (in the future).

I made this patch (again) available as mercurial repository, here:

hg clone https://bitbucket.org/aleric/viewer-development-storm-864

(that is the same name as before, but be warned that I destroyed
the old one and created a new repository).
Posted (Jan. 31, 2011, 7:56 p.m.)
First pass and I couldn't see anything wrong with the new structure. It simplifies the code using APR quite a bit and surely makes it more resilient. I'll do a pull and build on a test repo before giving my stamp of approval. It also surely needs to be reviewed by Bao who dealt with that code quite a bit and might have more pointed comments.
indra/llcommon/llapr.cpp (Diff revision 2)
 
 
Use "its reference count"
indra/llcommon/llaprpool.h (Diff revision 2)
 
 
Ooops! Wrong license. We now use LGPL. Please shange to "license=viewerlgpl"
indra/llcommon/llaprpool.h (Diff revision 2)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
The rest of the license should be changed too. Please take example on any currently shipped file for an appropriate header.
indra/llcommon/llaprpool.h (Diff revision 2)
 
 
Use "its subpools"
indra/llcommon/llaprpool.h (Diff revision 2)
 
 
Use "its parents"
indra/llcommon/llaprpool.cpp (Diff revision 2)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Same comment wrt license as before: use LGPL.
indra/llcommon/llaprpool.cpp (Diff revision 2)
 
 
 
K,it's just a paranoid assert. Still, I'm curious how you came up with the (*4) value. Also, not much point using bitshift here (I know: old habits... :) 
  1. I have no idea :p. This assert wasn't created by me, it comes from the old code.
    
indra/llcommon/llscopedvolatileaprpool.h (Diff revision 2)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Same comment on license: use LGPL
Posted (Feb. 2, 2011, 6:30 p.m.)

   

  
indra/llcommon/llaprpool.h (Diff revision 2)
 
 
 
 
 
 
 
 
 
 
 
I think it's be more clear to use an explicit name like "getAPRPool()" rather than the function operator here. It produces hard to read code in a couple of places and it's just plain unclear what "myPool()" really does.
  1. That is actually on purpose. Developers should never actually "get" the pool.
    Like the comment says, it's painful that we have to provide access at all:
    we want to hide apt_pool_t completely from the user. The user should use
    LLAPRPool instead. And then, when that has to be passed to the actual
    libapr function, it's passed like this:  apr_whatever_create(foo, mPool());
    because that is just a little bit more safe than have it auto convert
    and allow writing apr_whatever_create(foo, mPool); or so I thought.
    It's deliberately a bit weird, to make people think twice before they
    "get" the underlaying apr_pool_t.  Also... see comment below...
    
indra/llmessage/llpumpio.cpp (Diff revision 2)
 
 
An example where the use of operator() is particularly unsightly...
  1. And rightfully so! It should be "extremely unpleasant" for the user to get to the underlaying apr_pool_t*.
    That this code hacks access is exactly that: a hack. One has to be very careful when doing this.
    The reason I did it here is because 1) I know what I'm doing, so it's ok in this case, 2) for this
    first introduction of LLAPRPool I tried to make minimal changes to the actual functionality of
    existing code using apr pools (with the exception of LLAPRFile, the rewrite of its API actually
    coming from SNOW-103 which already proved itself in snowglobe, imprudence and other TPV's).
    The fact that this access here is needed actually signifies that this code is not handling
    pools in a very safe way, but I consider(ed) it better to keep the code "as-is" and hack around the
    *safity* of LLAPRPool (and as a result not changing anything!) than to rewrite the interface at this
    point for this particular part of the code; changing things is more risk, in this case, than not
    using the API of LLAPRPool as intended. It would be harder to find if that rewrite would introduce
    some kind of bug we made lots of changes at the same time. I propose to leave this as it is now
    and look at changing it later once everyone feels secure about the stability of the current patch.
    
    Okay, that sounded nice -- but the truth is that I have no idea (at this point) if it is even
    possible to do what that code does without accessing the underlaying apr_pool_t* like that (meaning,
    not passing it directly to an APR function, but storing it in a structure). I just know that it
    should feel dangerous to do so, so it seems OK that code that does it doesn't look very nice.
    
  2. Then it should maybe be made even more obvious with a super-ugly name like getAPRPool_DO_NOT_CALL_THIS_DIRECTLY_use_LLAPRPool_instead()
  3. That is not possible, as operator() is definitely part of the normal API (it's just unfortunate that it has to exist).
    Let me explain more about what is so "dangerous" about allowing access. Also, realize that before this patch
    everything was completely open (accessible) and spread out through the code, so it's at least 1000 times better
    this way -- just not 100% bullet proof).
    
    Firstly, we can't use getAPRPool_DO_NOT_CALL_THIS_DIRECTLY_use_LLAPRPool_instead() instead of
    operator()(void), because that would change perfectly legal code like (where mPool is a LLAPRPool):
    
         apr_thread_cond_create(&mAPRCondp, mPool());
    
    into
    
         apr_thread_cond_create(&mAPRCondp, mPool.getAPRPool_DO_NOT_CALL_THIS_DIRECTLY_use_LLAPRPool_instead());
    
    where mPool() is the same as mPool.operator()(), returning the underlaying apr_pool_t* to pass it to
    the apr library call using it.
    
    The reason that I call it "dangerous" that the user (that is, other developers that use this
    LLAPRPool interface) can access the underlaying apr_pool_t* is because they might be tempted
    to do something like:
    
         apr_pool_t* pool = mPool();
         // Use 'pool'.
         apr_pool_destroy(pool);
    
    Fortunately, this would be easy detectable by grepping the source code for 'apr_pool_t'.
    Less obvious would be:
    
         some_apr_defined_struct.pool = mPool();
         // leave some_apr_defined_struct around for OTHER threads to use.
    
    That would, obviously, defy the thread-safity checks of LLAPRPool, and we also wouldn't
    find it by grepping for apr_pool_t because this struct is defined in a header of libapr.
    
    I'd almost say that one could call it "far fetched" to assume that anyone would
    do anything like this-- so the chance that anything wrong ever happening is indeed
    very small. Nevertheless, to avoid all that, I made the 'rule of thumb' that the
    operator() only be used WHEN passing the pool directly to an apr_* library call.
    
    It would have been better if we could even drop that restriction (by getting rid of
    the current possibility to corrupt things by getting access to the underlaying
    apr_pool_t), but that is only possible if the operator() is removed completely
    and we overload all apr_* functions that we use (making those friend of LLAPRPool,
    so they can call the real libapr function with apr_pool_t* parameter). I didn't feel
    that the remaining security risk warranted that though.
    
    Nevertheless, if merov wants then I can do this. The only draw back would
    be that 1) every source file would need to include the apr_pools.h (through
    linden_common.h I presume), just to stop every source code from possibly
    including that themselves and using apr pools directly. 2) Every time we want
    to use a new APR library function that needs a pool to be passed, we'd have
    to add it to the list of wrappers.
    
    Actually, both are pretty minimal changes; I'd have no objection or problem
    with this change at all (I just thought it would be harder to accept).
    
    For the rest of the code this would result in 1) code like,
    
         apr_thread_cond_create(&mAPRCondp, mPool);  // No more '()'
    
    while 2) code like,
    
         some_apr_defined_struct.pool = mPool; // or mPool()
    
    wouldn't compile anymore.
    
    
  4. I'm going to weigh in on the side of providing a getPool() method rather than overloading the function call operator.
    
    I get your point about the danger, and perhaps we should look at the cases in the existing code where it is needed to see if perhaps additional methods (static wrappers?) would help ameliorate the risk.
    
    Operator overloading should be used only when it is the most natural solution (as in using + for string concatenation); in any other case it just obscures what is going on, and I don't think that it's a good way of dealing with the risk you are concerned about.