Changeset 1b3e854 in mainline


Ignore:
Timestamp:
2011-05-21T14:29:50Z (13 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
faeb7cc
Parents:
955f2a5
Message:

add library function for explicit heap structure consistency check
use extensive heap consistency checks in the heap allocator tests

Location:
uspace
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/tester/mm/common.c

    r955f2a5 r1b3e854  
    135135}
    136136
     137static void check_consistency(const char *loc)
     138{
     139        /* Check heap consistency */
     140        void *prob = heap_check();
     141        if (prob != NULL) {
     142                TPRINTF("\nError: Heap inconsistency at %p in %s.\n",
     143                    prob, loc);
     144                TSTACKTRACE();
     145                error_flag = true;
     146        }
     147}
     148
    137149/** Checked malloc
    138150 *
     
    153165        /* Allocate the chunk of memory */
    154166        data = malloc(size);
     167        check_consistency("checked_malloc");
    155168        if (data == NULL)
    156169                return NULL;
     
    160173                TPRINTF("\nError: Allocated block overlaps with another "
    161174                    "previously allocated block.\n");
     175                TSTACKTRACE();
    162176                error_flag = true;
    163177        }
     
    198212        if (block->addr == NULL) {
    199213                free(block);
     214                check_consistency("alloc_block");
    200215                return NULL;
    201216        }
     
    228243        /* Free the memory */
    229244        free(block->addr);
     245        check_consistency("free_block (a)");
    230246        free(block);
     247        check_consistency("free_block (b)");
    231248}
    232249
     
    257274            pos < end; pos++)
    258275                *pos = block_expected_value(block, pos);
     276       
     277        check_consistency("fill_block");
    259278}
    260279
     
    273292                if (*pos != block_expected_value(block, pos)) {
    274293                        TPRINTF("\nError: Corrupted content of a data block.\n");
     294                        TSTACKTRACE();
    275295                        error_flag = true;
    276296                        return;
     
    296316        if (entry == NULL) {
    297317                TPRINTF("\nError: Corrupted list of allocated memory blocks.\n");
     318                TSTACKTRACE();
    298319                error_flag = true;
    299320        }
     
    325346        if (addr == NULL) {
    326347                free(area);
     348                check_consistency("map_area (a)");
    327349                return NULL;
    328350        }
     
    331353        if (area->addr == (void *) -1) {
    332354                free(area);
     355                check_consistency("map_area (b)");
    333356                return NULL;
    334357        }
     
    361384       
    362385        free(area);
     386        check_consistency("unmap_area");
    363387}
    364388
     
    389413            pos < end; pos++)
    390414                *pos = area_expected_value(area, pos);
    391 }
     415       
     416        check_consistency("fill_area");
     417}
  • uspace/app/tester/mm/malloc3.c

    r955f2a5 r1b3e854  
    236236                                        mem_area_t *area = map_area(AREA_SIZE);
    237237                                        RETURN_IF_ERROR;
     238                                       
    238239                                        if (area != NULL) {
    239240                                                TPRINTF("*");
  • uspace/app/tester/tester.h

    r955f2a5 r1b3e854  
    3838#include <sys/types.h>
    3939#include <bool.h>
     40#include <stacktrace.h>
    4041
    4142#define IPC_TEST_SERVICE  10240
     
    5960                if (!test_quiet) { \
    6061                        fprintf(stderr, (format), ##__VA_ARGS__); \
     62                } \
     63        } while (0)
     64
     65#define TSTACKTRACE() \
     66        do { \
     67                if (!test_quiet) { \
     68                        stacktrace_print(); \
    6169                } \
    6270        } while (0)
  • uspace/lib/c/generic/malloc.c

    r955f2a5 r1b3e854  
    904904}
    905905
     906void *heap_check(void)
     907{
     908        futex_down(&malloc_futex);
     909       
     910        if (first_heap_area == NULL) {
     911                futex_up(&malloc_futex);
     912                return (void *) -1;
     913        }
     914       
     915        /* Walk all heap areas */
     916        for (heap_area_t *area = first_heap_area; area != NULL;
     917            area = area->next) {
     918               
     919                /* Check heap area consistency */
     920                if ((area->magic != HEAP_AREA_MAGIC) ||
     921                    ((void *) area != area->start) ||
     922                    (area->start >= area->end) ||
     923                    (((uintptr_t) area->start % PAGE_SIZE) != 0) ||
     924                    (((uintptr_t) area->end % PAGE_SIZE) != 0)) {
     925                        futex_up(&malloc_futex);
     926                        return (void *) area;
     927                }
     928               
     929                /* Walk all heap blocks */
     930                for (heap_block_head_t *head = (heap_block_head_t *)
     931                    AREA_FIRST_BLOCK_HEAD(area); (void *) head < area->end;
     932                    head = (heap_block_head_t *) (((void *) head) + head->size)) {
     933                       
     934                        /* Check heap block consistency */
     935                        if (head->magic != HEAP_BLOCK_HEAD_MAGIC) {
     936                                futex_up(&malloc_futex);
     937                                return (void *) head;
     938                        }
     939                       
     940                        heap_block_foot_t *foot = BLOCK_FOOT(head);
     941                       
     942                        if ((foot->magic != HEAP_BLOCK_FOOT_MAGIC) ||
     943                            (head->size != foot->size)) {
     944                                futex_up(&malloc_futex);
     945                                return (void *) foot;
     946                        }
     947                }
     948        }
     949       
     950        futex_up(&malloc_futex);
     951       
     952        return NULL;
     953}
     954
    906955/** @}
    907956 */
  • uspace/lib/c/include/malloc.h

    r955f2a5 r1b3e854  
    4646extern void *realloc(const void *addr, const size_t size);
    4747extern void free(const void *addr);
     48extern void *heap_check(void);
    4849
    4950#endif
Note: See TracChangeset for help on using the changeset viewer.