Skip to content

Commit 1154157

Browse files
committed
[Crypto] PSA: Heap is now needed for RCP builds
1 parent 2a9bb52 commit 1154157

File tree

10 files changed

+52
-27
lines changed

10 files changed

+52
-27
lines changed

src/core/BUILD.gn

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,8 @@ openthread_radio_sources = [
761761
"common/error.hpp",
762762
"common/frame_builder.cpp",
763763
"common/frame_builder.hpp",
764+
"common/heap.cpp",
765+
"common/heap.hpp",
764766
"common/log.cpp",
765767
"common/random.cpp",
766768
"common/string.cpp",
@@ -786,6 +788,8 @@ openthread_radio_sources = [
786788
"radio/radio_callbacks.cpp",
787789
"radio/radio_platform.cpp",
788790
"thread/link_quality.cpp",
791+
"utils/heap.cpp",
792+
"utils/heap.hpp",
789793
"utils/parse_cmdline.cpp",
790794
"utils/power_calibration.cpp",
791795
]

src/core/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ set(RADIO_COMMON_SOURCES
279279
common/binary_search.cpp
280280
common/error.cpp
281281
common/frame_builder.cpp
282+
common/heap.cpp
282283
common/log.cpp
283284
common/random.cpp
284285
common/string.cpp
@@ -289,6 +290,7 @@ set(RADIO_COMMON_SOURCES
289290
crypto/aes_ecb.cpp
290291
crypto/crypto_platform_mbedtls.cpp
291292
crypto/crypto_platform_psa.cpp
293+
crypto/mbedtls.cpp
292294
crypto/storage.cpp
293295
diags/factory_diags.cpp
294296
instance/instance.cpp
@@ -304,6 +306,7 @@ set(RADIO_COMMON_SOURCES
304306
radio/radio_callbacks.cpp
305307
radio/radio_platform.cpp
306308
thread/link_quality.cpp
309+
utils/heap.cpp
307310
utils/parse_cmdline.cpp
308311
utils/power_calibration.cpp
309312
)

src/core/common/heap.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void *CAlloc(size_t aCount, size_t aSize) { return otPlatCAlloc(aCount, aSize);
4444

4545
void Free(void *aPointer) { otPlatFree(aPointer); }
4646

47-
#else
47+
#elif OPENTHREAD_CONFIG_HEAP_INTERNAL_ENABLE
4848

4949
void *CAlloc(size_t aCount, size_t aSize) { return Instance::GetHeap().CAlloc(aCount, aSize); }
5050

src/core/common/message.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ uint16_t MessagePool::GetFreeBufferCount(void) const
156156
uint16_t rval;
157157

158158
#if OPENTHREAD_CONFIG_MESSAGE_USE_HEAP_ENABLE
159-
#if !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
159+
#if OPENTHREAD_CONFIG_HEAP_INTERNAL_ENABLE
160160
rval = static_cast<uint16_t>(Instance::GetHeap().GetFreeSize() / sizeof(Buffer));
161161
#else
162162
rval = NumericLimits<uint16_t>::kMax;
@@ -175,7 +175,7 @@ uint16_t MessagePool::GetTotalBufferCount(void) const
175175
uint16_t rval;
176176

177177
#if OPENTHREAD_CONFIG_MESSAGE_USE_HEAP_ENABLE
178-
#if !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
178+
#if OPENTHREAD_CONFIG_HEAP_INTERNAL_ENABLE
179179
rval = static_cast<uint16_t>(Instance::GetHeap().GetCapacity() / sizeof(Buffer));
180180
#else
181181
rval = NumericLimits<uint16_t>::kMax;

src/core/config/misc.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
*/
4545

4646
#include "config/coap.h"
47+
#include "config/crypto.h"
4748
#include "config/srp_server.h"
4849

4950
/**
@@ -316,6 +317,22 @@
316317
#define OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE 0
317318
#endif
318319

320+
/**
321+
* @def OPENTHREAD_CONFIG_HEAP_INTERNAL_ENABLE
322+
*
323+
* Enable the internal heap.
324+
*
325+
* This configuration is automatically enabled by default when an external heap is disabled.
326+
* It is applicable for FTD and MTD builds. Additionally, if PSA library is used, it requires
327+
* heap memory also for Radio build.
328+
*
329+
*/
330+
#ifndef OPENTHREAD_CONFIG_HEAP_INTERNAL_ENABLE
331+
#define OPENTHREAD_CONFIG_HEAP_INTERNAL_ENABLE \
332+
(!OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE && \
333+
(OPENTHREAD_MTD || OPENTHREAD_FTD || (OPENTHREAD_CONFIG_CRYPTO_LIB == OPENTHREAD_CONFIG_CRYPTO_LIB_PSA)))
334+
#endif
335+
319336
/**
320337
* @def OPENTHREAD_CONFIG_DTLS_APPLICATION_DATA_MAX_LENGTH
321338
*

src/core/instance/instance.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,12 @@ static uint64_t gMultiInstanceRaw[MULTI_INSTANCE_SIZE];
5757

5858
#endif
5959

60-
#if OPENTHREAD_MTD || OPENTHREAD_FTD
61-
#if !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
60+
#if OPENTHREAD_CONFIG_HEAP_INTERNAL_ENABLE
6261
OT_DEFINE_ALIGNED_VAR(sHeapRaw, sizeof(Utils::Heap), uint64_t);
6362
Utils::Heap *Instance::sHeap{nullptr};
6463
#endif
64+
65+
#if OPENTHREAD_MTD || OPENTHREAD_FTD
6566
#if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE
6667
bool Instance::sDnsNameCompressionEnabled = true;
6768
#endif
@@ -289,7 +290,7 @@ Instance::Instance(void)
289290
#endif
290291
}
291292

292-
#if (OPENTHREAD_MTD || OPENTHREAD_FTD) && !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
293+
#if OPENTHREAD_CONFIG_HEAP_INTERNAL_ENABLE
293294
Utils::Heap &Instance::GetHeap(void)
294295
{
295296
if (nullptr == sHeap)

src/core/instance/instance.hpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,12 @@
6060
#include "common/timer.hpp"
6161
#include "common/type_traits.hpp"
6262
#include "common/uptime.hpp"
63+
#include "crypto/mbedtls.hpp"
6364
#include "diags/factory_diags.hpp"
6465
#include "instance/extension.hpp"
6566
#include "mac/link_raw.hpp"
6667
#include "radio/radio.hpp"
68+
#include "utils/heap.hpp"
6769
#include "utils/otns.hpp"
6870
#include "utils/power_calibration.hpp"
6971
#include "utils/static_counter.hpp"
@@ -78,7 +80,6 @@
7880
#include "common/code_utils.hpp"
7981
#include "common/notifier.hpp"
8082
#include "common/settings.hpp"
81-
#include "crypto/mbedtls.hpp"
8283
#include "crypto/storage.hpp"
8384
#include "mac/mac.hpp"
8485
#include "mac/wakeup_tx_scheduler.hpp"
@@ -136,7 +137,6 @@
136137
#include "thread/tmf.hpp"
137138
#include "utils/channel_manager.hpp"
138139
#include "utils/channel_monitor.hpp"
139-
#include "utils/heap.hpp"
140140
#include "utils/history_tracker.hpp"
141141
#include "utils/jam_detector.hpp"
142142
#include "utils/link_metrics_manager.hpp"
@@ -329,6 +329,15 @@ class Instance : public otInstance, private NonCopyable
329329
*/
330330
void Finalize(void);
331331

332+
#if OPENTHREAD_CONFIG_HEAP_INTERNAL_ENABLE
333+
/**
334+
* Returns a reference to the Heap object.
335+
*
336+
* @returns A reference to the Heap object.
337+
*/
338+
static Utils::Heap &GetHeap(void);
339+
#endif
340+
332341
#if OPENTHREAD_MTD || OPENTHREAD_FTD
333342
/**
334343
* Deletes all the settings stored in non-volatile memory, and then triggers a platform reset.
@@ -345,15 +354,6 @@ class Instance : public otInstance, private NonCopyable
345354
*/
346355
Error ErasePersistentInfo(void);
347356

348-
#if !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
349-
/**
350-
* Returns a reference to the Heap object.
351-
*
352-
* @returns A reference to the Heap object.
353-
*/
354-
static Utils::Heap &GetHeap(void);
355-
#endif
356-
357357
#if OPENTHREAD_CONFIG_COAP_API_ENABLE
358358
/**
359359
* Returns a reference to application COAP object.
@@ -449,7 +449,7 @@ class Instance : public otInstance, private NonCopyable
449449
static LogLevel sLogLevel;
450450
#endif
451451

452-
#if (OPENTHREAD_MTD || OPENTHREAD_FTD) && !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
452+
#if OPENTHREAD_CONFIG_HEAP_INTERNAL_ENABLE
453453
static Utils::Heap *sHeap;
454454
#endif
455455

@@ -471,7 +471,7 @@ class Instance : public otInstance, private NonCopyable
471471
TimerMicro::Scheduler mTimerMicroScheduler;
472472
#endif
473473

474-
#if OPENTHREAD_MTD || OPENTHREAD_FTD
474+
#if OPENTHREAD_MTD || OPENTHREAD_FTD || (OPENTHREAD_CONFIG_CRYPTO_LIB == OPENTHREAD_CONFIG_CRYPTO_LIB_PSA)
475475
// Random::Manager is initialized before other objects. Note that it
476476
// requires MbedTls which itself may use Heap.
477477
Crypto::MbedTls mMbedTls;

src/core/utils/heap.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
#include "heap.hpp"
3535

36-
#if !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
36+
#if OPENTHREAD_CONFIG_HEAP_INTERNAL_ENABLE
3737

3838
#include <string.h>
3939

@@ -219,4 +219,4 @@ void Heap::Free(void *aPointer)
219219
} // namespace Utils
220220
} // namespace ot
221221

222-
#endif // !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
222+
#endif // OPENTHREAD_CONFIG_HEAP_INTERNAL_ENABLE

src/core/utils/heap.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
#include "openthread-core-config.h"
3838

39-
#if !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
39+
#if OPENTHREAD_CONFIG_HEAP_INTERNAL_ENABLE
4040

4141
#include <stddef.h>
4242
#include <stdint.h>
@@ -323,6 +323,6 @@ class Heap : private NonCopyable
323323
} // namespace Utils
324324
} // namespace ot
325325

326-
#endif // !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
326+
#endif // OPENTHREAD_CONFIG_HEAP_INTERNAL_ENABLE
327327

328328
#endif // OT_UTILS_HEAP_HPP_

tests/unit/test_heap.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
namespace ot {
4242

43-
#if !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
43+
#if OPENTHREAD_CONFIG_HEAP_INTERNAL_ENABLE
4444

4545
/**
4646
* Verifies single variable allocating and freeing.
@@ -173,15 +173,15 @@ void RunTimerTests(void)
173173
TestAllocateMultiple();
174174
}
175175

176-
#endif // !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
176+
#endif // OPENTHREAD_CONFIG_HEAP_INTERNAL_ENABLE
177177

178178
} // namespace ot
179179

180180
int main(void)
181181
{
182-
#if !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
182+
#if OPENTHREAD_CONFIG_HEAP_INTERNAL_ENABLE
183183
ot::RunTimerTests();
184184
printf("All tests passed\n");
185-
#endif // !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
185+
#endif // OPENTHREAD_CONFIG_HEAP_INTERNAL_ENABLE
186186
return 0;
187187
}

0 commit comments

Comments
 (0)