From 6fc141772c85d33640c32cea83bf7236ca64df7a Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Sat, 6 Feb 2016 02:01:51 +0300 Subject: [PATCH 1/2] Implement static initialization guards (#500) --- cores/esp8266/abi.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/cores/esp8266/abi.cpp b/cores/esp8266/abi.cpp index ff865d9ca8..eeb3be2bb7 100644 --- a/cores/esp8266/abi.cpp +++ b/cores/esp8266/abi.cpp @@ -25,7 +25,10 @@ extern "C" { #include "osapi.h" #include "mem.h" } +#include +#include +using __cxxabiv1::__guard; void *operator new(size_t size) { size = ((size + 3) & ~((size_t)0x3)); @@ -56,6 +59,34 @@ void __cxa_deleted_virtual(void) { panic(); } +typedef struct { + uint8_t guard; + uint8_t ps; +} guard_t; + +extern "C" int __cxa_guard_acquire(__guard* pg) +{ + uint8_t ps = xt_rsil(15); + if (reinterpret_cast(pg)->guard) { + xt_wsr_ps(ps); + return 0; + } + reinterpret_cast(pg)->ps = ps; + return 1; +} + +extern "C" void __cxa_guard_release(__guard* pg) +{ + reinterpret_cast(pg)->guard = 1; + xt_wsr_ps(reinterpret_cast(pg)->ps); +} + +extern "C" void __cxa_guard_abort(__guard* pg) +{ + xt_wsr_ps(reinterpret_cast(pg)->ps); +} + + namespace std { void __throw_bad_function_call() { panic(); From 737f6c28ea8c3d47d5c4fa3fd82da8657df17295 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Sat, 6 Feb 2016 02:06:44 +0300 Subject: [PATCH 2/2] Remove forced alignment in operators new and delete This alignment prevents umm_malloc to detect buffer overruns which fall within padding introduced by new/new[]. Allocated memory will be aligned by design of umm_malloc, so we don't need to pad anything here. Also fixed some formatting/newlines and removed unused header files. --- cores/esp8266/abi.cpp | 49 +++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/cores/esp8266/abi.cpp b/cores/esp8266/abi.cpp index eeb3be2bb7..9143b5bece 100644 --- a/cores/esp8266/abi.cpp +++ b/cores/esp8266/abi.cpp @@ -19,43 +19,41 @@ #include #include #include -extern "C" { -#include "ets_sys.h" -#include "os_type.h" -#include "osapi.h" -#include "mem.h" -} #include #include using __cxxabiv1::__guard; -void *operator new(size_t size) { - size = ((size + 3) & ~((size_t)0x3)); - return os_malloc(size); +void *operator new(size_t size) +{ + return malloc(size); } -void *operator new[](size_t size) { - size = ((size + 3) & ~((size_t)0x3)); - return os_malloc(size); +void *operator new[](size_t size) +{ + return malloc(size); } -void operator delete(void * ptr) { - os_free(ptr); +void operator delete(void * ptr) +{ + free(ptr); } -void operator delete[](void * ptr) { - os_free(ptr); +void operator delete[](void * ptr) +{ + free(ptr); } extern "C" void __cxa_pure_virtual(void) __attribute__ ((__noreturn__)); extern "C" void __cxa_deleted_virtual(void) __attribute__ ((__noreturn__)); -void __cxa_pure_virtual(void) { +void __cxa_pure_virtual(void) +{ panic(); } -void __cxa_deleted_virtual(void) { +void __cxa_deleted_virtual(void) +{ panic(); } @@ -87,20 +85,25 @@ extern "C" void __cxa_guard_abort(__guard* pg) } -namespace std { -void __throw_bad_function_call() { +namespace std +{ +void __throw_bad_function_call() +{ panic(); } -void __throw_length_error(char const*) { +void __throw_length_error(char const*) +{ panic(); } -void __throw_bad_alloc() { +void __throw_bad_alloc() +{ panic(); } -void __throw_logic_error(const char* str) { +void __throw_logic_error(const char* str) +{ panic(); } }