From f9b9b390fb673cee2d733106db077efda65bb304 Mon Sep 17 00:00:00 2001 From: Zihao Yu Date: Wed, 20 Oct 2021 14:17:36 +0800 Subject: [PATCH] fix dead recursion when __NATIVE_USE_KLIB__ is defined --- am/src/native/trm.c | 12 ++++++++++-- klib/src/stdlib.c | 6 ++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/am/src/native/trm.c b/am/src/native/trm.c index 1325c30..95c9295 100644 --- a/am/src/native/trm.c +++ b/am/src/native/trm.c @@ -1,5 +1,6 @@ #include #include +#include void __am_platform_dummy(); void __am_exit_platform(int code); @@ -13,9 +14,16 @@ void putch(char ch) { } void halt(int code) { - printf("Exit (%d)\n", code); + const char *fmt = "Exit code = 40h\n"; + for (const char *p = fmt; *p; p++) { + char ch = *p; + if (ch == '0' || ch == '4') { + ch = "0123456789abcdef"[(code >> (ch - '0')) & 0xf]; + } + putch(ch); + } __am_exit_platform(code); - printf("Should not reach here!\n"); + putstr("Should not reach here!\n"); while (1); } diff --git a/klib/src/stdlib.c b/klib/src/stdlib.c index 1d9e45c..382635d 100644 --- a/klib/src/stdlib.c +++ b/klib/src/stdlib.c @@ -30,7 +30,13 @@ int atoi(const char* nptr) { } void *malloc(size_t size) { + // On native, malloc() will be called during initializaion of C runtime. + // Therefore do not call panic() here, else it will yield a dead recursion: + // panic() -> putchar() -> (glibc) -> malloc() -> panic() +#if !(defined(__ISA_NATIVE__) && defined(__NATIVE_USE_KLIB__)) panic("Not implemented"); +#endif + return NULL; } void free(void *ptr) {