// SPDX-License-Identifier: GPL-3.0-or-later #include #include #define CHECK(test) do { if (!(test)) return __LINE__; } while (0) #define BUDGET 240 // Keep guard bytes immediately before and after the exact array given to the heap. static byte guarded[BUDGET + 2]; static byte alternate[48]; // Materialize logical negation as an int result. This also checks the // optimized CMOC call sequence, which differs from a branch on success. static int initializationFailed(byte *storage, unsigned capacity) { return !vcc_heap_init(storage, capacity); } static int qualifies(void) { byte stackGuard[15]; byte *pool = guarded + 1, *p, *q, *other, *left, *middle, *right, *end; unsigned i; memset(guarded, 0xB9, sizeof(guarded)); memset(alternate, 0xCD, sizeof(alternate)); for (i = 0; i < sizeof(stackGuard); ++i) stackGuard[i] = (byte)(i + 97); // No hidden default heap, and a range wrapping through zero is not usable. free(NULL); CHECK(!malloc(1) && !calloc(1, 2) && !realloc(NULL, 3)); CHECK(!vcc_heap_init(NULL, BUDGET) && !vcc_heap_init(pool, 15)); CHECK(!vcc_heap_init((void *)0xFFF0, 32)); CHECK(initializationFailed(alternate, 15) == 1); CHECK(initializationFailed(alternate, 16) == 0); p = (byte *)malloc(1); CHECK(p && p >= alternate && p < alternate + 16); free(p); CHECK(vcc_heap_init(pool, BUDGET)); CHECK(!malloc(0) && !calloc(0, 3) && !calloc(3, 0)); CHECK(!calloc(256, 256) && !calloc(32769U, 2) && !malloc(65535U)); p = (byte *)calloc(4, 9); CHECK(p && p >= pool && p + 36 <= pool + BUDGET); for (i = 0; i < 36; ++i) { CHECK(p[i] == 0); p[i] = (byte)(33 + i); } // A rejected reinitialization leaves both the old block and new region alone. memset(alternate, 0xCD, sizeof(alternate)); CHECK(!vcc_heap_init(alternate, sizeof(alternate))); CHECK(!vcc_heap_init(pool, BUDGET)); for (i = 0; i < sizeof(alternate); ++i) CHECK(alternate[i] == 0xCD); other = (byte *)realloc(NULL, 24); CHECK(other); memset(other, 0x3A, 24); CHECK(!realloc(p, 65535U)); q = (byte *)realloc(p, 68); CHECK(q); for (i = 0; i < 36; ++i) CHECK(q[i] == (byte)(33 + i)); for (i = 0; i < 24; ++i) CHECK(other[i] == 0x3A); CHECK(!realloc(q, BUDGET)); // Exhaustion even though the integer size fits. for (i = 0; i < 36; ++i) CHECK(q[i] == (byte)(33 + i)); p = (byte *)realloc(q, 11); CHECK(p); for (i = 0; i < 11; ++i) CHECK(p[i] == (byte)(33 + i)); CHECK(realloc(other, 0) == NULL); free(p); // Four live objects divide the pool. The two separate holes cannot hold // 64 bytes until the middle object is freed and the three holes coalesce. left = (byte *)malloc(40); middle = (byte *)malloc(40); right = (byte *)malloc(40); end = (byte *)malloc(40); CHECK(left && middle && right && end); memset(end, 0x37, 40); free(left); free(right); CHECK(!malloc(64)); free(middle); p = (byte *)malloc(90); CHECK(p); memset(p, 0xAA, 90); for (i = 0; i < 40; ++i) CHECK(end[i] == 0x37); free(end); free(p); // After several fill/release cycles the pool must regain its original room. for (i = 0; i < 50; ++i) { p = (byte *)malloc(200); CHECK(p && p >= pool && p + 200 <= pool + BUDGET); memset(p, 0x35, 200); free(p); } p = (byte *)calloc(100, 2); CHECK(p); for (i = 0; i < 200; ++i) CHECK(!p[i]); free(p); CHECK(!vcc_heap_init(NULL, BUDGET)); p = (byte *)malloc(200); CHECK(p && p >= pool && p + 200 <= pool + BUDGET); free(p); CHECK(vcc_heap_init(alternate, sizeof(alternate))); p = (byte *)malloc(16); CHECK(p && p >= alternate && p + 16 <= alternate + sizeof(alternate)); free(p); CHECK(guarded[0] == 0xB9 && guarded[BUDGET + 1] == 0xB9); for (i = 0; i < sizeof(stackGuard); ++i) CHECK(stackGuard[i] == (byte)(i + 97)); return 0; } int main(void) { int line; cls(1); line = qualifies(); if (line) printf("FAIL LINE %d\n", line); else printf("BOUNDED HEAP PASSED\n"); return line != 0; }