From 7d356b1ddab0150b7347a5952e3bff2a04704fd4 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Thu, 13 Jul 2017 20:09:53 +0200 Subject: added an exit function added Linux syscall stubs for exit and write adapted all tests added a printf test made stdio work on Linux or AbaOs syscalls --- tests/libc/Makefile | 70 +++++++++++++++++++++++++++++++--------------- tests/libc/kernel_stub.asm | 48 +++++++++++++++++++++++++++++++ tests/libc/kernel_stub.c | 8 +++++- tests/libc/kernel_stub.h | 27 ++++++++++++++++++ tests/libc/test_abort.c | 6 ++++ tests/libc/test_itoa.c | 14 +++++----- tests/libc/test_malloc.c | 6 ++-- tests/libc/test_printf.c | 15 ++++++++++ tests/libc/test_strlcat.c | 11 ++++---- tests/libc/test_strlcpy.c | 7 +++-- 10 files changed, 171 insertions(+), 41 deletions(-) create mode 100644 tests/libc/kernel_stub.asm create mode 100644 tests/libc/kernel_stub.h create mode 100644 tests/libc/test_abort.c create mode 100644 tests/libc/test_printf.c (limited to 'tests') diff --git a/tests/libc/Makefile b/tests/libc/Makefile index 43620d7..59fa715 100644 --- a/tests/libc/Makefile +++ b/tests/libc/Makefile @@ -1,53 +1,79 @@ CC := gcc -CFLAGS := -std=c99 -m32 -ffreestanding -O0 -g -Wall -Werror -INCLUDES = -I../../src/libc -I../../src/kernel -LD := ld +DEFINES = -DOS_LINUX +INCLUDES = -I. -I../../src/libc -I../../src/kernel +CFLAGS := -std=c99 -m32 -ffreestanding -O0 -g -Wall -Werror $(INCLUDES) $(DEFINES) +# tcc misses crt1.o from gcc?! and fails with _start symbol missing on -nostdlib, +# this seems very wrong.. +# TODO: for now test sanity of library tests with gcc and enabling -nostdlib +#LDFLAGS := -nostdlib +NASMFLAGS := -f elf32 +NASM := nasm all: test -test_strlcpy: test_strlcpy.o ../../src/libc/string.o - $(CC) -o test_strlcpy test_strlcpy.o ../../src/libc/string.o +test_strlcpy: test_strlcpy.o ../../src/libc/string.o ../../src/libc/stdlib.o ../../src/libc/stdio.o ../../src/kernel/memorymanagement.o kernel_stub.o kernel_stub_asm.o + $(CC) -o test_strlcpy test_strlcpy.o ../../src/libc/string.o ../../src/libc/stdlib.o ../../src/libc/stdio.o ../../src/kernel/memorymanagement.o kernel_stub.o kernel_stub_asm.o test_strlcpy.o: test_strlcpy.c - $(CC) $(CFLAGS) $(INCLUDES) -c -o test_strlcpy.o test_strlcpy.c + $(CC) $(CFLAGS) -c -o test_strlcpy.o test_strlcpy.c -test_strlcat: test_strlcat.o ../../src/libc/string.o - $(CC) -o test_strlcat test_strlcat.o ../../src/libc/string.o +test_strlcat: test_strlcat.o ../../src/libc/string.o ../../src/libc/stdlib.o ../../src/libc/stdio.o ../../src/kernel/memorymanagement.o kernel_stub.o kernel_stub_asm.o + $(CC) $(LDFLAGS) -o test_strlcat test_strlcat.o ../../src/libc/string.o ../../src/libc/stdlib.o ../../src/libc/stdio.o ../../src/kernel/memorymanagement.o kernel_stub.o kernel_stub_asm.o test_strlcat.o: test_strlcat.c - $(CC) $(CFLAGS) $(INCLUDES) -c -o test_strlcat.o test_strlcat.c + $(CC) $(CFLAGS) -c -o test_strlcat.o test_strlcat.c -test_itoa: test_itoa.o ../../src/libc/stdlib.o ../../src/libc/string.o ../../src/kernel/memorymanagement.o kernel_stub.o - $(CC) -o test_itoa test_itoa.o ../../src/libc/stdlib.o ../../src/libc/string.o ../../src/kernel/memorymanagement.o kernel_stub.o +test_itoa: test_itoa.o ../../src/libc/stdlib.o ../../src/libc/stdio.o ../../src/libc/string.o ../../src/kernel/memorymanagement.o kernel_stub.o kernel_stub_asm.o + $(CC) $(LDFLAGS) -o test_itoa test_itoa.o ../../src/libc/stdlib.o ../../src/libc/stdio.o ../../src/libc/string.o ../../src/kernel/memorymanagement.o kernel_stub.o kernel_stub_asm.o test_itoa.o: test_itoa.c ../../src/libc/stdlib.h - $(CC) $(CFLAGS) $(INCLUDES) -c -o test_itoa.o test_itoa.c + $(CC) $(CFLAGS) -c -o test_itoa.o test_itoa.c -test_malloc: test_malloc.o ../../src/libc/stdlib.o ../../src/libc/string.o ../../src/kernel/memorymanagement.o kernel_stub.o - $(CC) -o test_malloc test_malloc.o ../../src/libc/stdlib.o ../../src/libc/string.o ../../src/kernel/memorymanagement.o kernel_stub.o +test_malloc: test_malloc.o ../../src/libc/stdlib.o ../../src/libc/stdio.o ../../src/libc/string.o ../../src/kernel/memorymanagement.o kernel_stub.o kernel_stub_asm.o + $(CC) $(LDFLAGS) -o test_malloc test_malloc.o ../../src/libc/stdio.o ../../src/libc/stdlib.o ../../src/libc/string.o ../../src/kernel/memorymanagement.o kernel_stub.o kernel_stub_asm.o test_malloc.o: test_malloc.c ../../src/libc/stdlib.h - $(CC) $(CFLAGS) $(INCLUDES) -c -o test_malloc.o test_malloc.c + $(CC) $(CFLAGS) -c -o test_malloc.o test_malloc.c + +test_printf: test_printf.o ../../src/libc/stdlib.o ../../src/libc/stdio.o ../../src/libc/string.o ../../src/kernel/memorymanagement.o kernel_stub.o kernel_stub_asm.o + $(CC) $(LDFLAGS) -o test_printf test_printf.o ../../src/libc/stdio.o ../../src/libc/stdlib.o ../../src/libc/string.o ../../src/kernel/memorymanagement.o kernel_stub.o kernel_stub_asm.o + +test_printf.o: test_printf.c ../../src/libc/stdlib.h + $(CC) $(CFLAGS) -c -o test_printf.o test_printf.c + +test_abort: test_abort.o ../../src/libc/stdlib.o ../../src/libc/stdio.o ../../src/libc/string.o ../../src/kernel/memorymanagement.o kernel_stub.o kernel_stub_asm.o + $(CC) $(LDFLAGS) -o test_abort test_abort.o ../../src/libc/stdio.o ../../src/libc/stdlib.o ../../src/libc/string.o ../../src/kernel/memorymanagement.o kernel_stub.o kernel_stub_asm.o + +test_abort.o: test_abort.c ../../src/libc/stdlib.h + $(CC) $(CFLAGS) -c -o test_abort.o test_abort.c ../../src/libc/string.o: ../../src/libc/string.c - $(CC) $(CFLAGS) $(INCLUDES) -c -o ../../src/libc/string.o ../../src/libc/string.c + $(CC) $(CFLAGS) -c -o ../../src/libc/string.o ../../src/libc/string.c ../../src/libc/stdlib.o: ../../src/libc/stdlib.c - $(CC) $(CFLAGS) $(INCLUDES) -c -o ../../src/libc/stdlib.o ../../src/libc/stdlib.c + $(CC) $(CFLAGS) -c -o ../../src/libc/stdlib.o ../../src/libc/stdlib.c + +../../src/libc/stdio.o: ../../src/libc/stdio.c + $(CC) $(CFLAGS) -c -o ../../src/libc/stdio.o ../../src/libc/stdio.c ../../src/kernel/memorymanagement.o: ../../src/kernel/memorymanagement.c - $(CC) $(CFLAGS) $(INCLUDES) -c -o ../../src/kernel/memorymanagement.o ../../src/kernel/memorymanagement.c + $(CC) $(CFLAGS) -c -o ../../src/kernel/memorymanagement.o ../../src/kernel/memorymanagement.c kernel_stub.o: kernel_stub.c - $(CC) $(CFLAGS) $(INCLUDES) -c -o kernel_stub.o kernel_stub.c + $(CC) $(CFLAGS) -c -o kernel_stub.o kernel_stub.c + +kernel_stub_asm.o: kernel_stub.asm + $(NASM) kernel_stub.asm $(NASMFLAGS) -o kernel_stub_asm.o -test: test_strlcpy test_strlcat test_itoa test_malloc +test: test_strlcpy test_strlcat test_itoa test_malloc test_abort test_printf ./test_strlcpy ./test_strlcat ./test_itoa ./test_malloc + ./test_printf + -./test_abort clean: - -rm -f test_strlcpy test_strlcat test_itoa test_malloc - -rm -f ../../src/libc/stdlib.o ../../src/libc/string.o ../../src/kernel/memorymanagement.o + -rm -f test_strlcpy test_strlcat test_itoa test_malloc test_printf test_abort + -rm -f ../../src/libc/stdlib.o ../../src/libc/string.o ../../src/libc/stdio.o ../../src/kernel/memorymanagement.o -rm -f *.o diff --git a/tests/libc/kernel_stub.asm b/tests/libc/kernel_stub.asm new file mode 100644 index 0000000..c9335a8 --- /dev/null +++ b/tests/libc/kernel_stub.asm @@ -0,0 +1,48 @@ +[bits 32] + +global syscall0 +global syscall1 +global syscall2 +global syscall3 + +; long syscall1( long n ); +syscall0: + push ebp + mov ebp, esp + mov eax, [ebp+8] + int 0x80 + leave + ret + +; long syscall1( long n, long a1 ); +syscall1: + push ebp + mov ebp, esp + mov eax, [ebp+8] + mov ebx, [ebp+12] + int 0x80 + leave + ret + +; long syscall2( long n, long a1, long a2 ); +syscall2: + push ebp + mov ebp, esp + mov eax, [ebp+8] + mov ebx, [ebp+12] + mov ecx, [ebp+16] + int 0x80 + leave + ret + +; long syscall3( long n, long a1, long a2, long a3 ); +syscall3: + push ebp + mov ebp, esp + mov eax, [ebp+8] + mov ebx, [ebp+12] + mov ecx, [ebp+16] + mov edx, [ebp+20] + int 0x80 + leave + ret diff --git a/tests/libc/kernel_stub.c b/tests/libc/kernel_stub.c index c859be0..610c3d3 100644 --- a/tests/libc/kernel_stub.c +++ b/tests/libc/kernel_stub.c @@ -3,6 +3,12 @@ #include "stdio.h" #include "stdlib.h" +// abaos has a kernel_panic function +// TODO: we should not need this stub, rather, libc functions should +// use other means to abort the process (e.g. with abort), then the +// kernel should catch this exception and issue the kernel_panic itself. +// anyway: the kernel has to decide whether it dies in panic or it +// kills the culprid child an merely goes his way.. void kernel_panic( const char *format, ... ) { (void)printf( "\n*** KERNEL PANIC ***\n" ); @@ -13,6 +19,6 @@ void kernel_panic( const char *format, ... ) va_end( args ); puts( "" ); - abort( ); + exit( 255 ); } diff --git a/tests/libc/kernel_stub.h b/tests/libc/kernel_stub.h new file mode 100644 index 0000000..467ad20 --- /dev/null +++ b/tests/libc/kernel_stub.h @@ -0,0 +1,27 @@ +#ifndef KERNEL_STUB_H +#define KERNEL_STUB_H + +// abaos has a kernel_panic function +// TODO: we should not need this stub, rather, libc functions should +// use other means to abort the process (e.g. with abort), then the +// kernel should catch this exception and issue the kernel_panic itself. +// anyway: the kernel has to decide whether it dies in panic or it +// kills the culprid child an merely goes his way.. +void kernel_panic( const char *format, ... ); + +// definitions to emulate kernel functions by calling Linux kernel +// syscalls directly (careful: works for 32-bit Intel only!) + +// those are defined in sys/syscall.h +#define __NR_exit 1 +#define __NR_write 4 + +// defined in unistd.h +#define STDOUT_FILENO 1 + +long syscall0( long n ); +long syscall1( long n, long a1 ); +long syscall2( long n, long a1, long a2 ); +long syscall3( long n, long a1, long a2, long a3 ); + +#endif diff --git a/tests/libc/test_abort.c b/tests/libc/test_abort.c new file mode 100644 index 0000000..da5c0f2 --- /dev/null +++ b/tests/libc/test_abort.c @@ -0,0 +1,6 @@ +#include "stdlib.h" + +int main( void ) +{ + abort( ); +} diff --git a/tests/libc/test_itoa.c b/tests/libc/test_itoa.c index 837b77a..837fd01 100644 --- a/tests/libc/test_itoa.c +++ b/tests/libc/test_itoa.c @@ -9,26 +9,26 @@ int main( void ) // simple conversion without sign res = itoa( 568876, buf, 10 ); - if( strcmp( buf, "568876" ) ) return 1; + if( strcmp( buf, "568876" ) ) exit( 1 ); // test if the returned pointer points to the buffer - if( strcmp( res, buf ) ) return 1; + if( strcmp( res, buf ) ) exit( 1 ); // conversion with sign res = itoa( -568876, buf, 10 ); - if( strcmp( buf, "-568876" ) ) return 1; + if( strcmp( buf, "-568876" ) ) exit( 1 ); // convert upper limit res = itoa( INT_MAX, buf, 10 ); - if( strcmp( buf, "2147483647" ) ) return 1; + if( strcmp( buf, "2147483647" ) ) exit( 1 ); // convert lower limit res = itoa( INT_MIN+1, buf, 10 ); - if( strcmp( buf, "-2147483647" ) ) return 1; + if( strcmp( buf, "-2147483647" ) ) exit( 1 ); // convert to hex res = itoa( 568876, buf, 16 ); - if( strcmp( buf, "8AE2C" ) ) return 1; + if( strcmp( buf, "8AE2C" ) ) exit( 1 ); - return 0; + exit( 0 ); } diff --git a/tests/libc/test_malloc.c b/tests/libc/test_malloc.c index 0f74fad..aaff2da 100644 --- a/tests/libc/test_malloc.c +++ b/tests/libc/test_malloc.c @@ -17,8 +17,8 @@ int main( void ) s = (char *)malloc( strlen( s1 ) + 1 ); size_t n = strlcpy( s, s1, strlen( s1 ) + 1 ); - if( n != 11 ) return 1; - if( strcmp( s, s1 ) != 0 ) return 1; + if( n != 11 ) exit( 1 ); + if( strcmp( s, s1 ) != 0 ) exit( 1 ); - return 0; + exit( 0 ); } diff --git a/tests/libc/test_printf.c b/tests/libc/test_printf.c new file mode 100644 index 0000000..d2fc849 --- /dev/null +++ b/tests/libc/test_printf.c @@ -0,0 +1,15 @@ +#include "string.h" +#include "stdlib.h" +#include "stdio.h" + +int main( void ) +{ + char *s = "test_string"; + int i = 47; + char c = 'X'; + + printf( "string '%s', decimal %d, hex 0x%X, character '%c'\n", + s, i, i, c ); + + exit( 0 ); +} diff --git a/tests/libc/test_strlcat.c b/tests/libc/test_strlcat.c index 074cb98..5a1bfc9 100644 --- a/tests/libc/test_strlcat.c +++ b/tests/libc/test_strlcat.c @@ -1,4 +1,5 @@ #include "string.h" +#include "stdlib.h" int main( void ) { @@ -9,11 +10,11 @@ int main( void ) *d = '\0'; n = strlcat( d, s1, 15 ); - if( n != 11 ) return 1; - if( strcmp( d, s1 ) != 0 ) return 1; + if( n != 11 ) exit( 1 ); + if( strcmp( d, s1 ) != 0 ) exit( 1 ); n = strlcat( d, s2, 15 ); - if( n != 14 ) return 1; - if( strcmp( d, "test_stringapp" ) != 0 ) return 1; + if( n != 14 ) exit( 1 ); + if( strcmp( d, "test_stringapp" ) != 0 ) exit( 1 ); - return 0; + exit( 0 ); } diff --git a/tests/libc/test_strlcpy.c b/tests/libc/test_strlcpy.c index a23a5bc..ac7acf9 100644 --- a/tests/libc/test_strlcpy.c +++ b/tests/libc/test_strlcpy.c @@ -1,4 +1,5 @@ #include "string.h" +#include "stdlib.h" int main( void ) { @@ -8,8 +9,8 @@ int main( void ) // copy into too small string n = strlcpy( d, s, 4 ); - if( n != 11 ) return 1; - if( strcmp( d, "tes" ) ) return 1; + if( n != 11 ) exit( 1 ); + if( strcmp( d, "tes" ) ) exit( 1 ); - return 0; + exit( 0 ); } -- cgit v1.2.3-54-g00ecf