git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Johannes Schindelin <Johannes.Schindelin@gmx.de>
Cc: "Ævar Arnfjörð Bjarmason" <avarab@gmail.com>, git@vger.kernel.org
Subject: Re: Re* jc/http-clear-finished-pointer
Date: Fri, 27 May 2022 14:35:53 -0700	[thread overview]
Message-ID: <xmqqpmjy1ww6.fsf@gitster.g> (raw)
In-Reply-To: <nycvar.QRO.7.76.6.2205272235220.349@tvgsbejvaqbjf.bet> (Johannes Schindelin's message of "Fri, 27 May 2022 22:41:09 +0200 (CEST)")

Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:

This analysis and explanation was actually still too conservative.

>> +	/*
>> +	 * The value of slot->finished we set before the loop was used
>> +	 * to set our "finished" variable when our request completed.
>> +	 *
>> +	 * 1. The slot may not have been reused for another requst
>> +	 *    yet, in which case it still has &finished.
>> +	 *
>> +	 * 2. The slot may already be in-use to serve another request,
>> +	 *    which can further be divided into two cases:
>> +	 *
>> +	 * (a) If call run_active_slot() hasn't been called for that
>> +	 *     other request, slot->finished may still have the
>> +	 *     address of our &finished.
>> +	 *
>> +	 * (b) If the request did call run_active_slot(), then the
>> +	 *     call would have updated slot->finished at the beginning
>> +	 *     of this function, and with the clearing of the member
>> +	 *     below, we would find that slot->finished is now NULL.

Given that there are only two places that assign to slot->finished
(one is at the beginning of run_active_slot(), the other is when
get_active_slot() either allocates a new one or decides to reuse the
one that finish_active_slot() has marked to be no longer in use),
all in "reused" cases, not just 2-(b), but even in case 2-(a),
slot->finished should be NULL.

>> +	 * In all cases, slot->finished has no useful information to
>> +	 * anybody at this point.  Some compilers warn us for
>> +	 * attempting to smuggle a pointer that is about to become
>> +	 * invalid, i.e. &finished.  We clear it here to assure them.
>> +	 */
>> +	slot->finished = NULL;
>>  }

The conclusion is still valid.  The slot->finished member will not
have any useful information when we get here.  It either keeps the
value &finished we stored if the slot wasn't reused, or it has NULL
if it was reused.

> Having said that, it is quite puzzling that GCC thinks it is safe to
> assign a local variable's pointer to a struct that is then accessed
> outside the current file. This would make it easy to copy and use the
> pointer well after the function scope was left. This is _not_ the case in
> Git's source code, but GCC seems that this isn't possible by
> (mis-)interpreting the final `slot->finished = NULL` to mean that the
> `slot->finished = &finished` was safe (because it clearly isn't).

I do not think it is compiler's job to prevent us from assigning to
slot->finished, in fear of somebody downstream copying the pointer
away to a global variable that can be accessed long after we return.

We can store a pointer to an object we malloc() in a member of a
struct, pass the struct to somebody else, who might copy the member
away in a global variable for their own use later, but when they
give control back to us, we may free() the object via the member in
the struct.  The compiler may see that we free() the pointer we stored
in the struct, but it would not warn us against doing so.

> With all that, here is my ACK.

Thanks.

  reply	other threads:[~2022-05-27 21:36 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-26  8:41 What's cooking in git.git (May 2022, #07; Wed, 25) Junio C Hamano
2022-05-26 16:02 ` Victoria Dye
2022-05-26 17:23   ` Junio C Hamano
2022-05-26 18:02     ` Ævar Arnfjörð Bjarmason
2022-05-26 18:30     ` Victoria Dye
2022-05-26 18:40       ` Ævar Arnfjörð Bjarmason
2022-05-26 23:10       ` Junio C Hamano
2022-05-26 18:08 ` tb/cruft-packs (was Re: What's cooking in git.git (May 2022, #07; Wed, 25)) Taylor Blau
2022-05-26 18:10 ` What's cooking in git.git (May 2022, #07; Wed, 25) Taylor Blau
2022-05-26 20:17   ` Junio C Hamano
2022-05-26 18:11 ` tb/midx-race-in-pack-objects (was Re: What's cooking in git.git (May 2022, #07; Wed, 25)) Taylor Blau
2022-05-26 18:49 ` sg/build-gitweb (was: " Ævar Arnfjörð Bjarmason
2022-05-26 19:06   ` sg/build-gitweb Junio C Hamano
2022-05-26 18:51 ` jc/http-clear-finished-pointer (was: What's cooking in git.git (May 2022, #07; Wed, 25)) Ævar Arnfjörð Bjarmason
2022-05-26 19:37   ` Re* jc/http-clear-finished-pointer Junio C Hamano
2022-05-27 20:41     ` Johannes Schindelin
2022-05-27 21:35       ` Junio C Hamano [this message]
2022-06-01  7:26     ` Ævar Arnfjörð Bjarmason
2022-06-01 15:48       ` Junio C Hamano
2022-05-26 18:54 ` js/bisect-in-c (was: What's cooking in git.git (May 2022, #07; Wed, 25)) Ævar Arnfjörð Bjarmason
2022-05-26 19:38   ` js/bisect-in-c Junio C Hamano
2022-05-26 20:00     ` js/bisect-in-c Junio C Hamano
2022-05-27 12:52       ` js/bisect-in-c Ævar Arnfjörð Bjarmason
2022-05-26 18:57 ` jx/l10n-workflow-change (was: What's cooking in git.git (May 2022, #07; Wed, 25)) Ævar Arnfjörð Bjarmason

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqpmjy1ww6.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=Johannes.Schindelin@gmx.de \
    --cc=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).