develooper Front page | perl.perl5.porters | Postings from February 2012

Re: [perl #108798] PL_check is not thread-safe

Thread Previous | Thread Next
From:
Aaron Crane
Date:
February 8, 2012 07:42
Subject:
Re: [perl #108798] PL_check is not thread-safe
Message ID:
CACmk_tsE8oMROdYynuLpiizZ=s__Sd3O69mXFmb18w0+D6F0yg@mail.gmail.com
Zefram <zefram@fysh.org> wrote:
>        static void THX_wrap_op_checker(pTHX_ Optype opcode,
>                Perl_check_t new_checker, Perl_check_t *old_checker_p)
>        {
>                if(*old_checker_p) return;
>                OP_REFCNT_LOCK;
>                if(!*old_checker_p) {
>                        *old_checker_p = PL_check[opcode];
>                        PL_check[opcode] = new_checker;
>                }
>                OP_REFCNT_UNLOCK;
>        }

Do we support any platforms where an access to a Perl_check_t might
not be atomic?  (For that matter, do any such platforms exist?  Since
it's a function pointer, that would presumably mean 64-bit platforms
which only do atomic memory accesses of up to 32 bits.)

If so, I think the OP_REFCNT_LOCK needs to precede the initial
test/return, to avoid this race:

1. Thread A loads the first half of *old_checker_p, which is all-bits-zero
2. Thread B takes the lock
3. Thread B writes 64 bits into *old_checker_p; the second half
happens to be all-bits-zero
4. Thread B releases the lock
5. Thread A loads the second half of the new value of *old_checker_p
(all-bits-zero again)
6. Thread A thinks the full value was 0, so continues; but it wasn't,
so it should have returned

Or am I misunderstanding something?

-- 
Aaron Crane ** http://aaroncrane.co.uk/

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About