From 6ded32ff2d7c39df5d9fc0499023b1a17311019c Mon Sep 17 00:00:00 2001 From: Akilan Date: Wed, 8 Apr 2026 13:55:55 +0100 Subject: [PATCH] added glibc --- Tests/isa/CPrograms/Benchmarks/glibc/build.sh | 13 + Tests/isa/CPrograms/Benchmarks/glibc/glibc.c | 227 ++++++++++++++++++ Tests/isa/CPrograms/build-assembler.sh | 2 +- Tests/isa/CPrograms/malloc.c | 92 +++++-- 4 files changed, 316 insertions(+), 18 deletions(-) create mode 100644 Tests/isa/CPrograms/Benchmarks/glibc/build.sh create mode 100644 Tests/isa/CPrograms/Benchmarks/glibc/glibc.c diff --git a/Tests/isa/CPrograms/Benchmarks/glibc/build.sh b/Tests/isa/CPrograms/Benchmarks/glibc/build.sh new file mode 100644 index 0000000..d34742d --- /dev/null +++ b/Tests/isa/CPrograms/Benchmarks/glibc/build.sh @@ -0,0 +1,13 @@ +scp ../../start.S home-1:/home/akilan/cheri/output/sdk/bin/ +# scp main.c home-1:/home/akilan/cheri/output/sdk/bin/ +scp ../../malloc.c home-1:/home/akilan/cheri/output/sdk/bin/ + +scp glibc.c home-1:/home/akilan/cheri/output/sdk/bin/ + +ssh home-1 'cd /home/akilan/cheri/output/sdk/bin/ && ./clang --target=riscv64-unknown-elf -march=rv64gcxcheri -mabi=lp64d -nostdlib -nostartfiles -fno-builtin-malloc -mcmodel=medany -Wl,-Ttext=0x80000000 -o testC start.S malloc.c glibc.c' + +# ssh home-1 'cd /home/akilan/cheri/output/sdk/bin/ && ./llvm-objdump -d testC' + +scp home-1:/home/akilan/cheri/output/sdk/bin/testC ../../../ + + diff --git a/Tests/isa/CPrograms/Benchmarks/glibc/glibc.c b/Tests/isa/CPrograms/Benchmarks/glibc/glibc.c new file mode 100644 index 0000000..6952f32 --- /dev/null +++ b/Tests/isa/CPrograms/Benchmarks/glibc/glibc.c @@ -0,0 +1,227 @@ +/* Benchmark malloc and free functions. + Copyright (C) 2019-2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +// modified by Daan Leijen to fit the bench suite and add lifo/fifo free order. + +// #include +// #include +// #include +// #include +// #include "malloc.h" + +#include +#include +#include + +// #define malloc MALLOCCHERI +// #define free FREECHERI + +// #include "bench-timing.h" +// #include "json-lib.h" + +void free(void * __capability ptr); +void * __capability malloc(size_t size); + + +/* Benchmark the malloc/free performance of a varying number of blocks of a + given size. This enables performance tracking of the t-cache and fastbins. + It tests 3 different scenarios: single-threaded using main arena, + multi-threaded using thread-arena, and main arena with SINGLE_THREAD_P + false. */ + +// source: https://github.com/daanx/mimalloc-bench/blob/master/bench/glibc-bench/bench-malloc-thread.c + +#define NUM_ITERS 20 +#define NUM_ALLOCS 4 +#define MAX_ALLOCS 1600 + +// Daan: disable timing +typedef long timing_t; +#define TIMING_NOW(s) +#define TIMING_DIFF(e,start,stop) + + +typedef struct +{ + size_t iters; + size_t size; + int n; + timing_t elapsed; +} malloc_args; + +static void +do_benchmark(malloc_args *args, char * __capability * __capability arr) +{ + // timing_t start, stop; + size_t iters = args->iters; + size_t size = args->size; + int n = args->n; + +// TIMING_NOW (start); + + for (int j = 0; j < iters; j++) + { + for (int i = 0; i < n; i++) { + arr[i] = malloc (size); + for(int g = 0; g < size; g++) { arr[i][g] =(char)g; } + } + + // free half in fifo order + for (int i = 0; i < n/2; i++) { + free (arr[i]); + arr[i] = NULL; + } + + // and the other half in lifo order + for(int i = n-1; i >= n/2; i--) { + free(arr[i]); + arr[i] = NULL; + } + } + +// TIMING_NOW (stop); + +// TIMING_DIFF (args->elapsed, start, stop); +} + +static malloc_args tests[3][NUM_ALLOCS]; +static int allocs[NUM_ALLOCS] = { 25, 100, 400, MAX_ALLOCS }; + +// static void * +// thread_test (void *p) +// { +// char **arr = (char**)p; + +// /* Run benchmark multi-threaded. */ +// for (int i = 0; i < NUM_ALLOCS; i++) +// do_benchmark (&tests[2][i], arr); + +// return p; +// } + +void +bench (unsigned long size) +{ + size_t iters = NUM_ITERS; + // char**arr = (char**)malloc (MAX_ALLOCS * sizeof (void*)); + char * __capability * __capability arr = malloc(MAX_ALLOCS * sizeof(char * __capability)); + + for (int t = 0; t < 3; t++) + for (int i = 0; i < NUM_ALLOCS; i++) + { + tests[t][i].n = allocs[i]; + tests[t][i].size = size; + tests[t][i].iters = iters / allocs[i]; + + /* Do a quick warmup run. */ + if (t == 0) + do_benchmark (&tests[0][i], arr); + } + + /* Run benchmark single threaded in main_arena. */ + for (int i = 0; i < NUM_ALLOCS; i++) + do_benchmark (&tests[0][i], arr); + + /* Run benchmark in a thread_arena. */ +// pthread_t t; +// pthread_create (&t, NULL, thread_test, (void*)arr); +// pthread_join (t, NULL); + + /* Repeat benchmark in main_arena with SINGLE_THREAD_P == false. */ + // for (int i = 0; i < NUM_ALLOCS; i++) + // do_benchmark (&tests[1][i], arr); + + free (arr); + arr = NULL; + + /* + json_ctx_t json_ctx; + + json_init (&json_ctx, 0, stdout); + + json_document_begin (&json_ctx); + + json_attr_string (&json_ctx, "timing_type", TIMING_TYPE); + + json_attr_object_begin (&json_ctx, "functions"); + + json_attr_object_begin (&json_ctx, "malloc"); + + char s[100]; + double iters2 = iters; + + json_attr_object_begin (&json_ctx, ""); + json_attr_double (&json_ctx, "malloc_block_size", size); + + struct rusage usage; + getrusage (RUSAGE_SELF, &usage); + json_attr_double (&json_ctx, "max_rss", usage.ru_maxrss); + + for (int i = 0; i < NUM_ALLOCS; i++) + { + sprintf (s, "main_arena_st_allocs_%04d_time", allocs[i]); + json_attr_double (&json_ctx, s, tests[0][i].elapsed / iters2); + } + + for (int i = 0; i < NUM_ALLOCS; i++) + { + sprintf (s, "main_arena_mt_allocs_%04d_time", allocs[i]); + json_attr_double (&json_ctx, s, tests[1][i].elapsed / iters2); + } + + for (int i = 0; i < NUM_ALLOCS; i++) + { + sprintf (s, "thread_arena__allocs_%04d_time", allocs[i]); + json_attr_double (&json_ctx, s, tests[2][i].elapsed / iters2); + } + + json_attr_object_end (&json_ctx); + + json_attr_object_end (&json_ctx); + + json_attr_object_end (&json_ctx); + + json_document_end (&json_ctx); + */ +} + +// static void usage (const char *name) +// { +// fprintf (stderr, "%s: \n", name); +// exit (1); +// } + +int main (void) +{ +// INITREGULARALLOC(); + long size = 3; +// if (argc == 2) +// size = strtol (argv[1], NULL, 0); + +// if (argc > 2 || size <= 0) +// usage (argv[0]); + + // bench (size); + // bench (2*size); + // bench (4*size); + // bench (8*size); + // bench (16*size); + // bench (32*size); + + return 0; +} \ No newline at end of file diff --git a/Tests/isa/CPrograms/build-assembler.sh b/Tests/isa/CPrograms/build-assembler.sh index 4f40ed1..757cf57 100644 --- a/Tests/isa/CPrograms/build-assembler.sh +++ b/Tests/isa/CPrograms/build-assembler.sh @@ -10,7 +10,7 @@ scp malloc.c home-1:/home/akilan/cheri/output/sdk/bin/ # ssh home-1 'cd /home/akilan/cheri/output/sdk/bin/ && ./clang --target=riscv64-unknown-elf -march=rv64gcxcheri -mabi=l64pc128 -nostdlib -nostartfiles -Wl,-Ttext=0x80000000 -o testC start.S main.c' -ssh home-1 'cd /home/akilan/cheri/output/sdk/bin/ && ./clang --target=riscv64-unknown-elf -march=rv64gcxcheri -mabi=lp64d -nostdlib -nostartfiles -Wl,-Ttext=0x80000000 -o testC start.S main.c' +# ssh home-1 'cd /home/akilan/cheri/output/sdk/bin/ && ./clang --target=riscv64-unknown-elf -march=rv64gcxcheri -mabi=lp64d -nostdlib -nostartfiles -Wl,-Ttext=0x80000000 -o testC start.S main.c' # Malloc test implementation ssh home-1 'cd /home/akilan/cheri/output/sdk/bin/ && ./clang --target=riscv64-unknown-elf -march=rv64gcxcheri -mabi=lp64d -nostdlib -nostartfiles -fno-builtin-malloc -mcmodel=medany -Wl,-Ttext=0x80000000 -o testC start.S malloc.c' diff --git a/Tests/isa/CPrograms/malloc.c b/Tests/isa/CPrograms/malloc.c index 5b105ba..d817fbb 100644 --- a/Tests/isa/CPrograms/malloc.c +++ b/Tests/isa/CPrograms/malloc.c @@ -4,11 +4,31 @@ #include "riscv_test.h" -#define HEAP_SIZE 4096 +// TODO: Write track allocated and free memory avaliable +// Also to calculate the distance between the virtual and +// physical memory. + +// [1, 2, 3. ....... n] -> physical +// |__| (Distance -1 between address) +// | | (Distance 1 between address) +// [1, 2, 3. ....... n] -> virtual + +#define HEAP_SIZE 10096 + +#define PHYS_BASE 0x80000000 +#define VIRT_BASE 0xFFFFFFFF80000000 static char heap[HEAP_SIZE]; static size_t bump = 0; +static void * __capability free_list = NULL; + +typedef struct free_node { + void * __capability next; +} free_node_t; + + + // Add delta value for TLB translation static inline void * __capability add_delta(void * __capability cap, int offset) { void * __capability result; @@ -55,23 +75,61 @@ void bump_reset(void) { bump = 0; } -int main(void) { - char * __capability a = malloc(30); - char * __capability b = malloc(16); +// Can only free if it's in a stack +// Need to write a free †hat can work +// in any sequence +// In regular malloc the freelist +// is maintained from mmap. +void free(void * __capability ptr) { + if (!ptr) return; + uintptr_t addr = cheri_address_get(ptr); + size_t size = cheri_length_get(ptr); - // if (!a || !b) return -1; - - a[0] = 'A'; - a[1] = 'C'; - b[0] = 'B'; - - // This will fault (out-of-bounds) - // a[20] = 'X'; - - if (a[1] != 'C') { - while (1); + // Check if this is the most recent allocation + if ((char *)addr + size == heap + bump) { + bump -= size; } - return 0; -} \ No newline at end of file + // *ptr = NULL; +} + +// Quick tests +// int main(void) { +// char * __capability a = malloc(30); +// char * __capability b = malloc(16); + + +// // if (!a || !b) return -1; + +// a[0] = 'A'; +// a[1] = 'C'; +// b[0] = 'B'; + +// // This will fault (out-of-bounds) +// // a[20] = 'X'; + +// if (a[1] != 'C') { +// while (1); +// } + +// if (b[0] != 'B') { +// while (1); +// } + +// free(&b); +// b = NULL; + +// if (b[0] == 'B') { +// while (1); +// } + +// char * __capability c = malloc(16); + +// // if (c[0] != 'B') { +// // while (1); +// // } + + +// return 0; +// } \ No newline at end of file