From d4836a4426525afbd29349063dde8ba3b5e89246 Mon Sep 17 00:00:00 2001 From: Buke Po Date: Fri, 14 Jul 2017 23:37:12 +0800 Subject: [PATCH] [mbedtls] move heap into otInstance. (#1996) --- src/core/crypto/mbedtls.cpp | 12 ++++++++---- src/core/crypto/mbedtls.hpp | 4 ++++ src/core/openthread-instance.h | 2 ++ tests/unit/test_heap.cpp | 4 ++-- tests/unit/test_hmac_sha256.cpp | 24 ++++++++++++++---------- 5 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/core/crypto/mbedtls.cpp b/src/core/crypto/mbedtls.cpp index d5dcab7a3..244ac24b9 100644 --- a/src/core/crypto/mbedtls.cpp +++ b/src/core/crypto/mbedtls.cpp @@ -38,20 +38,22 @@ #include #include "heap.hpp" +#include "openthread-instance.h" +#include "openthread-single-instance.h" + +#if !OPENTHREAD_ENABLE_MULTIPLE_INSTANCES namespace ot { namespace Crypto { -static Heap sHeap; - static void *CAlloc(size_t aCount, size_t aSize) { - return sHeap.CAlloc(aCount, aSize); + return otGetInstance()->mMbedTlsHeap.CAlloc(aCount, aSize); } static void Free(void *aPointer) { - sHeap.Free(aPointer); + return otGetInstance()->mMbedTlsHeap.Free(aPointer); } MbedTls::MbedTls(void) @@ -61,3 +63,5 @@ MbedTls::MbedTls(void) } // namespace Crypto } // namespace ot + +#endif // #if !OPENTHREAD_ENABLE_MULTIPLE_INSTANCES diff --git a/src/core/crypto/mbedtls.hpp b/src/core/crypto/mbedtls.hpp index 7d4edfaac..d6e1c3cb7 100644 --- a/src/core/crypto/mbedtls.hpp +++ b/src/core/crypto/mbedtls.hpp @@ -34,6 +34,8 @@ #ifndef OT_MBEDTLS_HPP_ #define OT_MBEDTLS_HPP_ +#if !OPENTHREAD_ENABLE_MULTIPLE_INSTANCES + namespace ot { namespace Crypto { @@ -66,4 +68,6 @@ public: } // namespace Crypto } // namespace ot +#endif // #if !OPENTHREAD_ENABLE_MULTIPLE_INSTANCES + #endif // OT_MBEDTLS_HPP_ diff --git a/src/core/openthread-instance.h b/src/core/openthread-instance.h index 429afb541..5c4cd89ff 100644 --- a/src/core/openthread-instance.h +++ b/src/core/openthread-instance.h @@ -49,6 +49,7 @@ #include "api/link_raw.hpp" #endif #include "coap/coap.hpp" +#include "crypto/heap.hpp" #include "crypto/mbedtls.hpp" #include "net/ip6.hpp" #include "thread/thread_netif.hpp" @@ -79,6 +80,7 @@ typedef struct otInstance #if !OPENTHREAD_ENABLE_MULTIPLE_INSTANCES ot::Crypto::MbedTls mMbedTls; + ot::Crypto::Heap mMbedTlsHeap; #endif ot::Ip6::Ip6 mIp6; ot::ThreadNetif mThreadNetif; diff --git a/tests/unit/test_heap.cpp b/tests/unit/test_heap.cpp index 42cdb88b0..f00e00835 100644 --- a/tests/unit/test_heap.cpp +++ b/tests/unit/test_heap.cpp @@ -93,7 +93,7 @@ void TestAllocateRandomly(size_t aSizeLimit, unsigned int aSeed) do { - size_t size = sizeof(Node) + rand() % aSizeLimit; + size_t size = sizeof(Node) + static_cast(rand()) % aSizeLimit; Log("TestAllocateRandomly allocating %zu bytes...", size); last->mNext = static_cast(heap.CAlloc(1, size)); @@ -109,7 +109,7 @@ void TestAllocateRandomly(size_t aSizeLimit, unsigned int aSeed) ++nnodes; // 50% probability to randomly free a node. - size_t freeIndex = rand() % (nnodes * 2); + size_t freeIndex = static_cast(rand()) % (nnodes * 2); if (freeIndex > nnodes) { diff --git a/tests/unit/test_hmac_sha256.cpp b/tests/unit/test_hmac_sha256.cpp index 06d881137..0401e9215 100644 --- a/tests/unit/test_hmac_sha256.cpp +++ b/tests/unit/test_hmac_sha256.cpp @@ -63,19 +63,23 @@ void TestHmacSha256(void) }; otInstance *instance = testInitInstance(); - ot::Crypto::HmacSha256 hmac; - uint8_t hash[ot::Crypto::HmacSha256::kHashSize]; - VerifyOrQuit(instance != NULL, "Null OpenThread instance"); - - for (int i = 0; tests[i].key != NULL; i++) + // Make sure hmac is destructed before freeing instance. { - hmac.Start(reinterpret_cast(tests[i].key), static_cast(strlen(tests[i].key))); - hmac.Update(reinterpret_cast(tests[i].data), static_cast(strlen(tests[i].data))); - hmac.Finish(hash); + ot::Crypto::HmacSha256 hmac; + uint8_t hash[ot::Crypto::HmacSha256::kHashSize]; - VerifyOrQuit(memcmp(hash, tests[i].hash, sizeof(tests[i].hash)) == 0, - "HMAC-SHA-256 failed\n"); + VerifyOrQuit(instance != NULL, "Null OpenThread instance"); + + for (int i = 0; tests[i].key != NULL; i++) + { + hmac.Start(reinterpret_cast(tests[i].key), static_cast(strlen(tests[i].key))); + hmac.Update(reinterpret_cast(tests[i].data), static_cast(strlen(tests[i].data))); + hmac.Finish(hash); + + VerifyOrQuit(memcmp(hash, tests[i].hash, sizeof(tests[i].hash)) == 0, + "HMAC-SHA-256 failed\n"); + } } testFreeInstance(instance);