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

Re: [PATCH v2 5/5] oidtree: a crit-bit tree for odb_loose_cache

From
René Scharfe <l.s.r@web.de>
Date
Aug 6, 2021, 17:53 UTC
Message-ID
<bab9f889-ee2e-d3c3-0319-e297b59261a0@web.de>
In-Reply-To
<3cbec773-cd99-cf9f-a713-45ef8e6746c3@ahunt.org>
Am 06.08.21 um 17:31 schrieb Andrzej Hunt:
Show 57 quoted lines
>
>
> On 29/06/2021 22:53, Eric Wong wrote:
>> [...snip...]
>> diff --git a/oidtree.c b/oidtree.c
>> new file mode 100644
>> index 0000000000..c1188d8f48
>> --- /dev/null
>> +++ b/oidtree.c
>> @@ -0,0 +1,94 @@
>> +/*
>> + * A wrapper around cbtree which stores oids
>> + * May be used to replace oid-array for prefix (abbreviation) matches
>> + */
>> +#include "oidtree.h"
>> +#include "alloc.h"
>> +#include "hash.h"
>> +
>> +struct oidtree_node {
>> +    /* n.k[] is used to store "struct object_id" */
>> +    struct cb_node n;
>> +};
>> +
>> [... snip ...]
>> +
>> +void oidtree_insert(struct oidtree *ot, const struct object_id *oid)
>> +{
>> +    struct oidtree_node *on;
>> +
>> +    if (!ot->mempool)
>> +        ot->mempool = allocate_alloc_state();
>> +    if (!oid->algo)
>> +        BUG("oidtree_insert requires oid->algo");
>> +
>> +    on = alloc_from_state(ot->mempool, sizeof(*on) + sizeof(*oid));
>> +    oidcpy_with_padding((struct object_id *)on->n.k, oid);
>
> I think this object_id cast introduced undefined behaviour - here's
> my layperson's interepretation of what's going on (full UBSAN output
> is pasted below):
>
> cb_node.k is a uint8_t[], and hence can be 1-byte aligned (on my
> machine: offsetof(struct cb_node, k) == 21). We're casting its
> pointer to "struct object_id *", and later try to access
> object_id.hash within oidcpy_with_padding. My compiler assumes that
> an object_id pointer needs to be 4-byte aligned, and reading from a
> misaligned pointer means we hit undefined behaviour. (I think the
> 4-byte alignment requirement comes from the fact that object_id's
> largest member is an int?)
>
> I'm not sure what an elegant and idiomatic fix might be - IIUC it's
> hard to guarantee misaligned access can't happen with a flex array
> that's being used for arbitrary data (you would presumably have to
> declare it as an array of whatever the largest supported type is, so
> that you can guarantee correct alignment even when cbtree is used
> with that type) - which might imply that k needs to be declared as a
> void pointer? That in turn would make cbtree.c harder to read.

C11 has alignas. We could also make the member before the flex array, otherbits, wider, e.g. promote it to uint32_t.

A more parsimonious solution would be to turn the int member of struct object_id, algo, into an unsigned char for now and reconsider the issue once we support our 200th algorithm or so. This breaks notes, though. Its GET_PTR_TYPE seems to require struct leaf_node to have 4-byte alignment for some reason. That can be ensured by adding an int member.

Anyway, with either of these fixes UBSan is still unhappy about a different issue. Here's a patch for that:

--- >8 ---
Subject: [PATCH] object-file: use unsigned arithmetic with bit mask

33f379eee6 (make object_directory.loose_objects_subdir_seen a bitmap, 2021-07-07) replaced a wasteful 256-byte array with a 32-byte array and bit operations. The mask calculation shifts a literal 1 of type int left by anything between 0 and 31. UndefinedBehaviorSanitizer doesn't like that and reports:

object-file.c:2477:18: runtime error: left shift of 1 by 31 places cannot be represented in type 'int'
Make sure to use an unsigned 1 instead to avoid the issue.
Signed-off-by: René Scharfe <l.s.r@web.de>
---
 object-file.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/object-file.c b/object-file.c
index 3d27dc8dea..a8be899481 100644
--- a/object-file.c
+++ b/object-file.c
@@ -2474,7 +2474,7 @@ struct oidtree *odb_loose_cache(struct object_directory *odb,
 	struct strbuf buf = STRBUF_INIT;
 	size_t word_bits = bitsizeof(odb->loose_objects_subdir_seen[0]);
 	size_t word_index = subdir_nr / word_bits;
-	size_t mask = 1 << (subdir_nr % word_bits);
+	size_t mask = 1u << (subdir_nr % word_bits);
 	uint32_t *bitmap;

 	if (subdir_nr < 0 ||
--
2.32.0
Previous: Andrzej HuntNext: Eric Wong
Message 36 of 99 in “speed up alt_odb_usable() with many alternates”
  1. speed up alt_odb_usable() with many alternatesEric Wong, Jun 24, 2021
  2. 0/5 optimizations for many odb alternatesEric Wong, Jun 27, 2021
  3. 2/5 avoid strlen via strbuf_addstr in link_alt_odb_entryEric Wong, Jun 27, 2021
  4. 1/5 speed up alt_odb_usable() with many alternatesEric Wong, Jun 27, 2021
  5. 3/5 make object_directory.loose_objects_subdir_seen a bitmapEric Wong, Jun 27, 2021
  6. René ScharfeJun 27, 2021
  7. Eric WongJun 28, 2021
  8. 4/5 oidcpy_with_padding: constify `src' argEric Wong, Jun 27, 2021
  9. 5/5 oidtree: a crit-bit tree for odb_loose_cacheEric Wong, Jun 27, 2021
  10. Junio C HamanoJun 29, 2021
  11. Eric WongJun 29, 2021
  12. 0/5 optimizations for many alternatesEric Wong, Jun 29, 2021
  13. 0/5 optimizations for many alternatesEric Wong, Jul 7, 2021
  14. 1/5 speed up alt_odb_usable() with many alternatesEric Wong, Jul 7, 2021
  15. Junio C HamanoJul 8, 2021
  16. Eric WongJul 8, 2021
  17. Junio C HamanoJul 8, 2021
  18. 2/5 avoid strlen via strbuf_addstr in link_alt_odb_entryEric Wong, Jul 7, 2021
  19. Junio C HamanoJul 8, 2021
  20. 3/5 make object_directory.loose_objects_subdir_seen a bitmapEric Wong, Jul 7, 2021
  21. 4/5 oidcpy_with_padding: constify `src' argEric Wong, Jul 7, 2021
  22. 5/5 oidtree: a crit-bit tree for odb_loose_cacheEric Wong, Jul 7, 2021
  23. 1/5 speed up alt_odb_usable() with many alternatesEric Wong, Jun 29, 2021
  24. René ScharfeJul 3, 2021
  25. René ScharfeJul 4, 2021
  26. Eric WongJul 6, 2021
  27. 2/5 avoid strlen via strbuf_addstr in link_alt_odb_entryEric Wong, Jun 29, 2021
  28. 3/5 make object_directory.loose_objects_subdir_seen a bitmapEric Wong, Jun 29, 2021
  29. 4/5 oidcpy_with_padding: constify `src' argEric Wong, Jun 29, 2021
  30. 5/5 oidtree: a crit-bit tree for odb_loose_cacheEric Wong, Jun 29, 2021
  31. René ScharfeJul 4, 2021
  32. Eric WongJul 6, 2021
  33. Ævar Arnfjörð BjarmasonJul 4, 2021
  34. Eric WongJul 7, 2021
  35. Andrzej HuntAug 6, 2021
  36. René ScharfeAug 6, 2021
  37. Eric WongAug 7, 2021
  38. Carlo ArenasAug 9, 2021
  39. 0/3 pedantic errors in nextCarlo Marcelo Arenas Belón, Aug 9, 2021
  40. 2/3 object-store: avoid extra ';' from KHASH_INITCarlo Marcelo Arenas Belón, Aug 9, 2021
  41. Junio C HamanoAug 9, 2021
  42. 1/3 oidtree: avoid nested struct oidtree_nodeCarlo Marcelo Arenas Belón, Aug 9, 2021
  43. 3/3 ci: run a pedantic build as part of the GitHub workflowCarlo Marcelo Arenas Belón, Aug 9, 2021
  44. Bagas SanjayaAug 9, 2021
  45. Carlo ArenasAug 9, 2021
  46. Phillip WoodAug 9, 2021
  47. Carlo ArenasAug 9, 2021
  48. Phillip WoodAug 10, 2021
  49. Junio C HamanoAug 10, 2021
  50. Ævar Arnfjörð BjarmasonAug 30, 2021
  51. Carlo ArenasAug 31, 2021
  52. Ævar Arnfjörð BjarmasonAug 31, 2021
  53. Carlo ArenasAug 31, 2021
  54. Jeff KingSep 1, 2021
  55. Junio C HamanoSep 1, 2021
  56. Ævar Arnfjörð BjarmasonAug 30, 2021
  57. 0/4 developer: support pedanticCarlo Marcelo Arenas Belón, Sep 1, 2021
  58. 1/4 developer: retire USE_PARENS_AROUND_GETTEXT_N supportCarlo Marcelo Arenas Belón, Sep 1, 2021
  59. 2/4 developer: enable pedantic by defaultCarlo Marcelo Arenas Belón, Sep 1, 2021
  60. 3/4 developer: add an alternative script for detecting broken N_()Carlo Marcelo Arenas Belón, Sep 1, 2021
  61. 4/4 developer: move detect-compiler out of the main directoryCarlo Marcelo Arenas Belón, Sep 1, 2021
  62. Jeff KingSep 1, 2021
  63. gettext: remove optional non-standard parens in N_() definitionÆvar Arnfjörð Bjarmason, Sep 1, 2021
  64. Eric SunshineSep 1, 2021
  65. Jeff KingSep 2, 2021
  66. Junio C HamanoSep 2, 2021
  67. Ævar Arnfjörð BjarmasonSep 1, 2021
  68. Carlo ArenasSep 1, 2021
  69. 0/3 support pedantic in developer modeCarlo Marcelo Arenas Belón, Sep 3, 2021
  70. 1/3 gettext: remove optional non-standard parens in N_() definitionCarlo Marcelo Arenas Belón, Sep 3, 2021
  71. Ævar Arnfjörð BjarmasonSep 10, 2021
  72. 2/3 win32: allow building with pedantic mode enabledCarlo Marcelo Arenas Belón, Sep 3, 2021
  73. René ScharfeSep 3, 2021
  74. Carlo Marcelo Arenas BelónSep 3, 2021
  75. Junio C HamanoSep 3, 2021
  76. René ScharfeSep 3, 2021
  77. René ScharfeSep 4, 2021
  78. Carlo ArenasSep 4, 2021
  79. Jonathan TanSep 27, 2021
  80. Carlo ArenasSep 28, 2021
  81. Jonathan TanSep 28, 2021
  82. Junio C HamanoSep 28, 2021
  83. Jonathan TanSep 28, 2021
  84. Carlo ArenasSep 29, 2021
  85. Junio C HamanoSep 29, 2021
  86. 3/3 developer: enable pedantic by defaultCarlo Marcelo Arenas Belón, Sep 3, 2021
  87. Ævar Arnfjörð BjarmasonSep 5, 2021
  88. Junio C HamanoAug 9, 2021
  89. Eric WongAug 9, 2021
  90. Carlo Marcelo Arenas BelónAug 10, 2021
  91. René ScharfeAug 10, 2021
  92. Carlo ArenasAug 10, 2021
  93. Carlo ArenasAug 11, 2021
  94. René ScharfeAug 11, 2021
  95. Junio C HamanoAug 11, 2021
  96. René ScharfeAug 10, 2021
  97. René ScharfeAug 10, 2021
  98. oidtree: avoid unaligned access to crit-bit treeRené Scharfe, Aug 14, 2021
  99. Junio C HamanoAug 16, 2021

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.