Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/kernel/tests/test_heap.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / kernel / tests / test_heap.c` 58 lines ยท 2273 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* test_heap.c -- kernel heap: alignment, zeroing, reuse, coalescing, 18 * exhaustion and rejection of bad frees. */ 19 #include 20 #include 21 #include 22 #include "ck_test.h" 23 #include "heap.h" 24 25 int main(void) 26 { 27 size_t size = 64 * 1024; 28 uint8_t *mem = aligned_alloc(4096, size); 29 struct ck_heap h; 30 CHECK(ck_heap_init(&h, mem, size) == 0); 31 size_t free0 = ck_heap_free_bytes(&h); 32 uint8_t *a = ck_heap_alloc(&h, 1), *b = ck_heap_alloc(&h, 100), *c = ck_heap_alloc(&h, 4000); 33 CHECK(a && b && c && ((uintptr_t)a & 15) == 0 && ((uintptr_t)b & 15) == 0 && ((uintptr_t)c & 15) == 0); 34 CHECK(a + 1 <= b || b + 100 <= a); 35 memset(b, 0xab, 100); 36 CHECK(ck_heap_free(&h, b) == 0); 37 CHECK(ck_heap_free(&h, b) == -1); /* double free */ 38 CHECK(ck_heap_free(&h, a + 8) == -1); /* interior pointer */ 39 CHECK(ck_heap_free(&h, mem + size + 64) == -1); /* foreign */ 40 uint8_t *d = ck_heap_alloc(&h, 100); 41 int zero = 1; 42 for (int i = 0; i < 100; i++) 43 zero &= d[i] == 0; 44 CHECK(d == b && zero); 45 CHECK(ck_heap_check(&h) == 0); 46 CHECK(ck_heap_free(&h, a) == 0 && ck_heap_free(&h, c) == 0 && ck_heap_free(&h, d) == 0); 47 CHECK(ck_heap_free_bytes(&h) == free0); /* fully coalesced */ 48 CHECK(ck_heap_alloc(&h, size) == NULL); 49 CHECK(ck_heap_alloc(&h, (size_t)-1) == NULL); 50 void *big = ck_heap_alloc(&h, free0 - 64); 51 CHECK(big != NULL); 52 CHECK(ck_heap_check(&h) == 0); 53 /* Corrupt a header: the walk notices. */ 54 ((uint64_t *)big)[-1] ^= 1; 55 CHECK(ck_heap_check(&h) != 0); 56 free(mem); 57 return ck_t_verdict("CK_HEAP"); 58 }