Skip to content

Commit 134377c

Browse files
committed
pack-bitmap: add loading corrupt bitmap_index test
This patch add a test function `test_bitmap_load_corrupt` in patch-bitmap.c , a `load corrupt bitmap` test case in t5310-pack-bitmaps.sh and a new command `load-corrupt` for `test-tool` in t/helper/test-bitmap.c. To make sure we are loading a corrupt bitmap, we need enable bitmap table lookup so that `prepare_bitmap()` won't call `load_bitmap_entries_v1()`. So to test corrupt bitmap_index, we first call `prepare_bitmap()` to set everything up but `bitmap_index->bitmaps` for us. Then we do any corruption we want to the bitmap_index. Finally we can test loading corrupt bitmap by calling `load_bitmap_entries_v1()`. Signed-off-by: Lidong Yan <[email protected]>
1 parent 6f84262 commit 134377c

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

pack-bitmap.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3022,6 +3022,71 @@ int test_bitmap_pseudo_merge_objects(struct repository *r, uint32_t n)
30223022
return ret;
30233023
}
30243024

3025+
typedef void(corrupt_fn)(struct bitmap_index *);
3026+
3027+
static int bitmap_corrupt_then_load(struct repository *r, corrupt_fn *do_corrupt)
3028+
{
3029+
struct bitmap_index *bitmap_git;
3030+
unsigned char *map;
3031+
3032+
if (!(bitmap_git = prepare_bitmap_git(r)))
3033+
die(_("failed to prepare bitmap indexes"));
3034+
/*
3035+
* If the table lookup extension is not used,
3036+
* prepare_bitmap_git has already called load_bitmap_entries_v1(),
3037+
* making it impossible to corrupt the bitmap.
3038+
*/
3039+
if (!bitmap_git->table_lookup)
3040+
return 0;
3041+
3042+
/*
3043+
* bitmap_git->map is read-only;
3044+
* to corrupt it, we need a writable memory block.
3045+
*/
3046+
map = bitmap_git->map;
3047+
bitmap_git->map = xmalloc(bitmap_git->map_size);
3048+
if (!bitmap_git->map)
3049+
return 0;
3050+
memcpy(bitmap_git->map, map, bitmap_git->map_size);
3051+
3052+
do_corrupt(bitmap_git);
3053+
if (!load_bitmap_entries_v1(bitmap_git))
3054+
die(_("load corrupt bitmap successfully"));
3055+
3056+
free(bitmap_git->map);
3057+
bitmap_git->map = map;
3058+
free_bitmap_index(bitmap_git);
3059+
3060+
return 0;
3061+
}
3062+
3063+
static void do_corrupt_commit_pos(struct bitmap_index *bitmap_git)
3064+
{
3065+
uint32_t *commit_pos_ptr;
3066+
3067+
commit_pos_ptr = (uint32_t *)(bitmap_git->map + bitmap_git->map_pos);
3068+
*commit_pos_ptr = (uint32_t)-1;
3069+
}
3070+
3071+
static void do_corrupt_xor_offset(struct bitmap_index *bitmap_git)
3072+
{
3073+
uint8_t *xor_offset_ptr;
3074+
3075+
xor_offset_ptr = (uint8_t *)(bitmap_git->map + bitmap_git->map_pos +
3076+
sizeof(uint32_t));
3077+
*xor_offset_ptr = MAX_XOR_OFFSET + 1;
3078+
}
3079+
3080+
int test_bitmap_load_corrupt(struct repository *r)
3081+
{
3082+
int res = 0;
3083+
if ((res = bitmap_corrupt_then_load(r, do_corrupt_commit_pos)))
3084+
return res;
3085+
if ((res = bitmap_corrupt_then_load(r, do_corrupt_xor_offset)))
3086+
return res;
3087+
return res;
3088+
}
3089+
30253090
int rebuild_bitmap(const uint32_t *reposition,
30263091
struct ewah_bitmap *source,
30273092
struct bitmap *dest)

pack-bitmap.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ int test_bitmap_hashes(struct repository *r);
8585
int test_bitmap_pseudo_merges(struct repository *r);
8686
int test_bitmap_pseudo_merge_commits(struct repository *r, uint32_t n);
8787
int test_bitmap_pseudo_merge_objects(struct repository *r, uint32_t n);
88+
int test_bitmap_load_corrupt(struct repository *r);
8889

8990
struct list_objects_filter_options;
9091

t/helper/test-bitmap.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ static int bitmap_dump_pseudo_merges(void)
2020
return test_bitmap_pseudo_merges(the_repository);
2121
}
2222

23+
static int bitmap_load_corrupt(void)
24+
{
25+
return test_bitmap_load_corrupt(the_repository);
26+
}
27+
2328
static int bitmap_dump_pseudo_merge_commits(uint32_t n)
2429
{
2530
return test_bitmap_pseudo_merge_commits(the_repository, n);
@@ -40,6 +45,8 @@ int cmd__bitmap(int argc, const char **argv)
4045
return bitmap_dump_hashes();
4146
if (argc == 2 && !strcmp(argv[1], "dump-pseudo-merges"))
4247
return bitmap_dump_pseudo_merges();
48+
if (argc == 2 && !strcmp(argv[1], "load-corrupt"))
49+
return bitmap_load_corrupt();
4350
if (argc == 3 && !strcmp(argv[1], "dump-pseudo-merge-commits"))
4451
return bitmap_dump_pseudo_merge_commits(atoi(argv[2]));
4552
if (argc == 3 && !strcmp(argv[1], "dump-pseudo-merge-objects"))
@@ -48,6 +55,7 @@ int cmd__bitmap(int argc, const char **argv)
4855
usage("\ttest-tool bitmap list-commits\n"
4956
"\ttest-tool bitmap dump-hashes\n"
5057
"\ttest-tool bitmap dump-pseudo-merges\n"
58+
"\ttest-tool bitmap load-corrupt\n"
5159
"\ttest-tool bitmap dump-pseudo-merge-commits <n>\n"
5260
"\ttest-tool bitmap dump-pseudo-merge-objects <n>");
5361

t/t5310-pack-bitmaps.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,21 @@ test_bitmap_cases () {
486486
grep "ignoring extra bitmap" trace2.txt
487487
)
488488
'
489+
490+
test_expect_success 'load corrupt bitmap' '
491+
git init repo &&
492+
test_when_finished "rm -fr repo" && (
493+
cd repo &&
494+
git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
495+
496+
echo "Hello world" > hello_world.txt &&
497+
git add hello_world.txt &&
498+
git commit -am "add hello_world.txt" &&
499+
500+
git repack -adb &&
501+
test-tool bitmap load-corrupt
502+
)
503+
'
489504
}
490505

491506
test_bitmap_cases

0 commit comments

Comments
 (0)