git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Re* jc/http-clear-finished-pointer

From
Junio C Hamano <gitster@pobox.com>
Date
May 27, 2022, 21:35 UTC
Message-ID
<xmqqpmjy1ww6.fsf@gitster.g>
In-Reply-To
<nycvar.QRO.7.76.6.2205272235220.349@tvgsbejvaqbjf.bet>
Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
This analysis and explanation was actually still too conservative.
Show 18 quoted lines
>> +	/*
>> +	 * 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.

Show 7 quoted lines
>> +	 * 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.

Show 7 quoted lines
> 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.
Previous: Johannes SchindelinNext: Ævar Arnfjörð Bjarmason
Message 17 of 24 in “What's cooking in git.git (May 2022, #07; Wed, 25)”
  1. Junio C HamanoMay 26, 2022
  2. Victoria DyeMay 26, 2022
  3. Junio C HamanoMay 26, 2022
  4. Ævar Arnfjörð BjarmasonMay 26, 2022
  5. Victoria DyeMay 26, 2022
  6. Ævar Arnfjörð BjarmasonMay 26, 2022
  7. Junio C HamanoMay 26, 2022
  8. tb/cruft-packs (was Re: What's cooking in git.git (May 2022, #07; Wed, 25))Taylor Blau, May 26, 2022
  9. Taylor BlauMay 26, 2022
  10. Junio C HamanoMay 26, 2022
  11. tb/midx-race-in-pack-objects (was Re: What's cooking in git.git (May 2022, #07; Wed, 25))Taylor Blau, May 26, 2022
  12. sg/build-gitweb (was: What's cooking in git.git (May 2022, #07; Wed, 25))Ævar Arnfjörð Bjarmason, May 26, 2022
  13. Junio C HamanoMay 26, 2022
  14. jc/http-clear-finished-pointer (was: What's cooking in git.git (May 2022, #07; Wed, 25))Ævar Arnfjörð Bjarmason, May 26, 2022
  15. Re* jc/http-clear-finished-pointerJunio C Hamano, May 26, 2022
  16. Johannes SchindelinMay 27, 2022
  17. Junio C HamanoMay 27, 2022
  18. Ævar Arnfjörð BjarmasonJun 1, 2022
  19. Junio C HamanoJun 1, 2022
  20. js/bisect-in-c (was: What's cooking in git.git (May 2022, #07; Wed, 25))Ævar Arnfjörð Bjarmason, May 26, 2022
  21. Junio C HamanoMay 26, 2022
  22. Junio C HamanoMay 26, 2022
  23. Ævar Arnfjörð BjarmasonMay 27, 2022
  24. jx/l10n-workflow-change (was: What's cooking in git.git (May 2022, #07; Wed, 25))Ævar Arnfjörð Bjarmason, May 26, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.