heap: fix wrong memory region check

A memory region starts from REGION_START and ends at
(REGION_START+SIZE-1).

Prior to this change, the check assumes a to-be-added region starting from REGION_START is invalid. Let's take an easy example:

A memory region:  0x1000~0x10ff
new added region: 0x1000~0x1020

This will be valid.

Valid conditions and invalid conditions are illustrated in the code comment
pull/11508/head
Armando 2022-03-22 16:46:37 +08:00
rodzic cfecf5917b
commit 32408b718f
2 zmienionych plików z 26 dodań i 25 usunięć

Wyświetl plik

@ -1,16 +1,8 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "heap_private.h"
#include <assert.h>
#include <string.h>
@ -180,28 +172,38 @@ esp_err_t heap_caps_add_region_with_caps(const uint32_t caps[], intptr_t start,
//Check if region overlaps the start and/or end of an existing region. If so, the
//region is invalid (or maybe added twice)
/*
* assume that in on region, start must be less than end (cannot equal to) !!
* Specially, the 4th scenario can be allowed. For example, allocate memory from heap,
* We assume that in any region, the "start" must be stictly less than the end.
* Specially, the 3rd scenario can be allowed. For example, allocate memory from heap,
* then change the capability and call this function to create a new region for special
* application.
* In the following chart, 'start = start' and 'end = end' is contained in 3rd scenario.
* In the following chart, 'start = start' and 'end = end' is contained in 4th scenario.
* This all equal scenario is incorrect because the same region cannot be add twice. For example,
* add the .bss memory to region twice, if not do the check, it will cause exception.
*
* the existing heap region s(tart) e(nd)
* |----------------------|
* 1.add region [Correct] (s1<s && e1<=s) |-----|
* 2.add region [Incorrect] (s2<=s && s<e2<=e) |---------------|
* 3.add region [Incorrect] (s3<=s && e<e3) |-------------------------------------|
* 4 add region [Correct] (s<s4<e && s<e4<=e) |-------|
* 5.add region [Incorrect] (s<s5<e && e<e5) |----------------------------|
* 6.add region [Correct] (e<=s6 && e<e6) |----|
*
* 1.add region (e1<s) |-----| correct: bool condition_1 = end < heap->start;
*
* 2.add region (s2<s && e2>=s) |-----------------| wrong: bool condition_2 = start < heap->start && end >= heap->start;
* |---------------------------------| wrong
*
* 3.add region (s3>=s && e3<e) |---------------| correct: bool condition_3 = start >= heap->start && end < heap->end;
* |--------------| correct
*
* 4.add region (s4<e && e4>=e) |----------------------| wrong: bool condition_4 = start < heap->end && end >= heap->end;
* |---------------------| wrong
*
* 5.add region (s5>=e) |----| correct: bool condition_5 = start >= heap->end;
*/
heap_t *heap;
SLIST_FOREACH(heap, &registered_heaps, next) {
if ((start <= heap->start && end > heap->start)
|| (start < heap->end && end > heap->end)) {
bool condition_2 = start < heap->start && end >= heap->start; //if 1, wrong
bool condition_4 = start < heap->end && end >= heap->end; //if 1, wrong
bool wrong_region = condition_2 || condition_4;
if (wrong_region) {
return ESP_FAIL;
}
}

Wyświetl plik

@ -999,7 +999,6 @@ components/hal/twai_hal_iram.c
components/hal/uart_hal.c
components/hal/uart_hal_iram.c
components/hal/usb_hal.c
components/heap/heap_caps_init.c
components/heap/heap_private.h
components/heap/heap_task_info.c
components/heap/heap_tlsf.c