From 30034ff8310e953fdeef78227cd068604fec6d66 Mon Sep 17 00:00:00 2001 From: saturneric Date: Sun, 3 Dec 2023 04:25:28 -0800 Subject: feat: add some memory utils --- src/core/function/SecureMemoryAllocator.cpp | 44 +++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/core/function/SecureMemoryAllocator.cpp (limited to 'src/core/function/SecureMemoryAllocator.cpp') diff --git a/src/core/function/SecureMemoryAllocator.cpp b/src/core/function/SecureMemoryAllocator.cpp new file mode 100644 index 00000000..09390305 --- /dev/null +++ b/src/core/function/SecureMemoryAllocator.cpp @@ -0,0 +1,44 @@ +/** + * Copyright (C) 2021 Saturneric + * + * This file is part of GpgFrontend. + * + * GpgFrontend is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GpgFrontend 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GpgFrontend. If not, see . + * + * The initial version of the source code is inherited from + * the gpg4usb project, which is under GPL-3.0-or-later. + * + * All the source code of GpgFrontend was modified and released by + * Saturneric starting on May 12, 2021. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#include "SecureMemoryAllocator.h" + +#include + +namespace GpgFrontend { + +auto SecurityMemoryAllocator::Allocate(std::size_t size) -> void* { + return mi_malloc(size); +} + +auto SecurityMemoryAllocator::Reallocate(void* ptr, std::size_t size) -> void* { + return mi_realloc(ptr, size); +} + +void SecurityMemoryAllocator::Deallocate(void* p) { mi_free(p); } +} // namespace GpgFrontend \ No newline at end of file -- cgit v1.2.3 From 42264ed0d7a3c91fbe9f307984964ffc9e5fe65c Mon Sep 17 00:00:00 2001 From: saturneric Date: Wed, 13 Dec 2023 18:01:06 +0800 Subject: refactor: improve the structure of main,core and test module --- src/core/function/SecureMemoryAllocator.cpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'src/core/function/SecureMemoryAllocator.cpp') diff --git a/src/core/function/SecureMemoryAllocator.cpp b/src/core/function/SecureMemoryAllocator.cpp index 09390305..e213ccbc 100644 --- a/src/core/function/SecureMemoryAllocator.cpp +++ b/src/core/function/SecureMemoryAllocator.cpp @@ -33,12 +33,24 @@ namespace GpgFrontend { auto SecurityMemoryAllocator::Allocate(std::size_t size) -> void* { - return mi_malloc(size); + auto* addr = mi_malloc(size); + SPDLOG_TRACE("secure memory allocator trys to alloc memory, address: {}", + static_cast(addr)); + return addr; } auto SecurityMemoryAllocator::Reallocate(void* ptr, std::size_t size) -> void* { - return mi_realloc(ptr, size); + auto* addr = mi_realloc(ptr, size); + SPDLOG_TRACE( + "secure memory allocator trys to realloc memory, " + "old address: {}, new address: {}", + static_cast(ptr), static_cast(addr)); + return addr; } -void SecurityMemoryAllocator::Deallocate(void* p) { mi_free(p); } +void SecurityMemoryAllocator::Deallocate(void* p) { + SPDLOG_TRACE("secure memory allocator trys to free memory, address: {}", + static_cast(p)); + mi_free(p); +} } // namespace GpgFrontend \ No newline at end of file -- cgit v1.2.3 From d74765b42206a83c0ade8617e8100794eb169f37 Mon Sep 17 00:00:00 2001 From: saturneric Date: Wed, 13 Dec 2023 20:02:24 +0800 Subject: feat: mimalloc support valgrind --- src/core/function/SecureMemoryAllocator.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/core/function/SecureMemoryAllocator.cpp') diff --git a/src/core/function/SecureMemoryAllocator.cpp b/src/core/function/SecureMemoryAllocator.cpp index e213ccbc..b9201942 100644 --- a/src/core/function/SecureMemoryAllocator.cpp +++ b/src/core/function/SecureMemoryAllocator.cpp @@ -32,14 +32,14 @@ namespace GpgFrontend { -auto SecurityMemoryAllocator::Allocate(std::size_t size) -> void* { +auto SecureMemoryAllocator::Allocate(std::size_t size) -> void* { auto* addr = mi_malloc(size); SPDLOG_TRACE("secure memory allocator trys to alloc memory, address: {}", static_cast(addr)); return addr; } -auto SecurityMemoryAllocator::Reallocate(void* ptr, std::size_t size) -> void* { +auto SecureMemoryAllocator::Reallocate(void* ptr, std::size_t size) -> void* { auto* addr = mi_realloc(ptr, size); SPDLOG_TRACE( "secure memory allocator trys to realloc memory, " @@ -48,7 +48,7 @@ auto SecurityMemoryAllocator::Reallocate(void* ptr, std::size_t size) -> void* { return addr; } -void SecurityMemoryAllocator::Deallocate(void* p) { +void SecureMemoryAllocator::Deallocate(void* p) { SPDLOG_TRACE("secure memory allocator trys to free memory, address: {}", static_cast(p)); mi_free(p); -- cgit v1.2.3 From f5cf83e4b3fdf1e9ae82b00f39e45e189809c419 Mon Sep 17 00:00:00 2001 From: saturneric Date: Fri, 15 Dec 2023 17:04:59 +0800 Subject: fix: slove some issues on memory and intilizations --- src/core/function/SecureMemoryAllocator.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'src/core/function/SecureMemoryAllocator.cpp') diff --git a/src/core/function/SecureMemoryAllocator.cpp b/src/core/function/SecureMemoryAllocator.cpp index b9201942..2c584753 100644 --- a/src/core/function/SecureMemoryAllocator.cpp +++ b/src/core/function/SecureMemoryAllocator.cpp @@ -48,9 +48,6 @@ auto SecureMemoryAllocator::Reallocate(void* ptr, std::size_t size) -> void* { return addr; } -void SecureMemoryAllocator::Deallocate(void* p) { - SPDLOG_TRACE("secure memory allocator trys to free memory, address: {}", - static_cast(p)); - mi_free(p); -} +void SecureMemoryAllocator::Deallocate(void* p) { mi_free(p); } + } // namespace GpgFrontend \ No newline at end of file -- cgit v1.2.3 From ee1a6bba20d71e710b169269565202d26794f44e Mon Sep 17 00:00:00 2001 From: saturneric Date: Sat, 23 Dec 2023 00:34:46 +0800 Subject: fix: slove compilation issue on macos --- src/core/function/SecureMemoryAllocator.cpp | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'src/core/function/SecureMemoryAllocator.cpp') diff --git a/src/core/function/SecureMemoryAllocator.cpp b/src/core/function/SecureMemoryAllocator.cpp index 2c584753..02efcbe0 100644 --- a/src/core/function/SecureMemoryAllocator.cpp +++ b/src/core/function/SecureMemoryAllocator.cpp @@ -28,19 +28,31 @@ #include "SecureMemoryAllocator.h" +#ifndef MACOS #include +#endif namespace GpgFrontend { auto SecureMemoryAllocator::Allocate(std::size_t size) -> void* { +#ifndef MACOS auto* addr = mi_malloc(size); +#else + auto* addr = malloc(size); +#endif + SPDLOG_TRACE("secure memory allocator trys to alloc memory, address: {}", static_cast(addr)); return addr; } auto SecureMemoryAllocator::Reallocate(void* ptr, std::size_t size) -> void* { +#ifndef MACOS auto* addr = mi_realloc(ptr, size); +#else + auto* addr = realloc(ptr, size); +#endif + SPDLOG_TRACE( "secure memory allocator trys to realloc memory, " "old address: {}, new address: {}", @@ -48,6 +60,12 @@ auto SecureMemoryAllocator::Reallocate(void* ptr, std::size_t size) -> void* { return addr; } -void SecureMemoryAllocator::Deallocate(void* p) { mi_free(p); } +void SecureMemoryAllocator::Deallocate(void* p) { +#ifndef MACOS + mi_free(p); +#else + free(p); +#endif +} } // namespace GpgFrontend \ No newline at end of file -- cgit v1.2.3 From 644aa4397b03dbef73f8bfedc13925b51cad836b Mon Sep 17 00:00:00 2001 From: saturneric Date: Fri, 5 Jan 2024 20:55:15 +0800 Subject: feat: integrate logging api to core --- src/core/function/SecureMemoryAllocator.cpp | 8 -------- 1 file changed, 8 deletions(-) (limited to 'src/core/function/SecureMemoryAllocator.cpp') diff --git a/src/core/function/SecureMemoryAllocator.cpp b/src/core/function/SecureMemoryAllocator.cpp index 02efcbe0..692c36c5 100644 --- a/src/core/function/SecureMemoryAllocator.cpp +++ b/src/core/function/SecureMemoryAllocator.cpp @@ -40,9 +40,6 @@ auto SecureMemoryAllocator::Allocate(std::size_t size) -> void* { #else auto* addr = malloc(size); #endif - - SPDLOG_TRACE("secure memory allocator trys to alloc memory, address: {}", - static_cast(addr)); return addr; } @@ -52,11 +49,6 @@ auto SecureMemoryAllocator::Reallocate(void* ptr, std::size_t size) -> void* { #else auto* addr = realloc(ptr, size); #endif - - SPDLOG_TRACE( - "secure memory allocator trys to realloc memory, " - "old address: {}, new address: {}", - static_cast(ptr), static_cast(addr)); return addr; } -- cgit v1.2.3