Skip to content

Commit 2e34efa

Browse files
committed
buf::oom tests: use custom allocator for oom failures
Create a custom allocator for the `buf::oom` tests that will fail with out-of-memory errors in predictable ways. We were previously trying to guess the way that various allocators on various platforms would fail in a way such that `malloc`/`realloc` would return `NULL` (instead of aborting the application, or appearing suspicious to various instrumentation or static code analysis tools like valgrind.) Introduce a fake `malloc` and `realloc` that will return `NULL` on allocations requesting more than 100 bytes. Otherwise, we proxy to the default allocator. (It's important to use the _default_ allocator, not just call `malloc`, since the default allocator on Windows CI builds may be the debugging C runtime allocators which would not be compatible with a standard `malloc`.)
1 parent 305e801 commit 2e34efa

File tree

1 file changed

+39
-37
lines changed

1 file changed

+39
-37
lines changed

tests/buf/oom.c

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,59 @@
11
#include "clar_libgit2.h"
22
#include "buffer.h"
33

4-
/*
5-
* We want to use some ridiculous size that `malloc` will fail with
6-
* but that does not otherwise interfere with testing. On Linux, choose
7-
* a number that is large enough to fail immediately but small enough
8-
* that valgrind doesn't believe it to erroneously be a negative number.
9-
* On macOS, choose a number that is large enough to fail immediately
10-
* without having libc print warnings to stderr.
11-
*/
12-
#if defined(GIT_ARCH_64) && defined(__linux__)
13-
# define TOOBIG 0x0fffffffffffffff
14-
#elif defined(GIT_ARCH_64)
15-
# define TOOBIG 0xffffffffffffff00
16-
#endif
17-
18-
/**
19-
* If we make a ridiculously large request the first time we
20-
* actually allocate some space in the git_buf, the realloc()
21-
* will fail. And because the git_buf_grow() wrapper always
22-
* sets mark_oom, the code in git_buf_try_grow() will free
23-
* the internal buffer and set it to git_buf__oom.
24-
*
25-
* We initialized the internal buffer to (the static variable)
26-
* git_buf__initbuf. The purpose of this test is to make sure
27-
* that we don't try to free the static buffer.
28-
*
29-
* Skip this test entirely on 32-bit platforms; a buffer large enough
30-
* to guarantee malloc failures is so large that valgrind considers
31-
* it likely to be an error.
32-
*/
4+
/* Override default allocators with ones that will fail predictably. */
5+
6+
static git_allocator std_alloc;
7+
static git_allocator oom_alloc;
8+
9+
static void *oom_malloc(size_t n, const char *file, int line)
10+
{
11+
/* Reject any allocation of more than 100 bytes */
12+
return (n > 100) ? NULL : std_alloc.gmalloc(n, file, line);
13+
}
14+
15+
static void *oom_realloc(void *p, size_t n, const char *file, int line)
16+
{
17+
/* Reject any allocation of more than 100 bytes */
18+
return (n > 100) ? NULL : std_alloc.grealloc(p, n, file, line);
19+
}
20+
21+
void test_buf_oom__initialize(void)
22+
{
23+
git_stdalloc_init_allocator(&std_alloc);
24+
git_stdalloc_init_allocator(&oom_alloc);
25+
26+
oom_alloc.gmalloc = oom_malloc;
27+
oom_alloc.grealloc = oom_realloc;
28+
29+
cl_git_pass(git_libgit2_opts(GIT_OPT_SET_ALLOCATOR, &oom_alloc));
30+
}
31+
32+
void test_buf_oom__cleanup(void)
33+
{
34+
cl_git_pass(git_libgit2_opts(GIT_OPT_SET_ALLOCATOR, NULL));
35+
}
36+
3337
void test_buf_oom__grow(void)
3438
{
35-
#ifdef GIT_ARCH_64
3639
git_buf buf = GIT_BUF_INIT;
3740

38-
git_buf_clear(&buf);
41+
cl_git_pass(git_buf_grow(&buf, 42));
42+
cl_assert(!git_buf_oom(&buf));
3943

40-
cl_assert(git_buf_grow(&buf, TOOBIG) == -1);
44+
cl_assert(git_buf_grow(&buf, 101) == -1);
4145
cl_assert(git_buf_oom(&buf));
4246

4347
git_buf_dispose(&buf);
44-
#else
45-
cl_skip();
46-
#endif
4748
}
4849

4950
void test_buf_oom__grow_by(void)
5051
{
5152
git_buf buf = GIT_BUF_INIT;
5253

53-
buf.size = SIZE_MAX-10;
54+
cl_git_pass(git_buf_grow_by(&buf, 42));
55+
cl_assert(!git_buf_oom(&buf));
5456

55-
cl_assert(git_buf_grow_by(&buf, 50) == -1);
57+
cl_assert(git_buf_grow_by(&buf, 101) == -1);
5658
cl_assert(git_buf_oom(&buf));
5759
}

0 commit comments

Comments
 (0)