This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Compiler/MSP430FR6989: "Attempted to read Past end of memory"

Part Number: MSP430FR6989

Tool/software: TI C/C++ Compiler

My program has a series of mallocs, the first few seem to allocate just fine but after overshooting a certain area of memory it appears to think these areas are out of reach.

The heap size is set to 1 Kb and there are no heap allocations previous to these. The memory map is the default for this device.

I've confirmed that this is not simply a quirk in the IDE watches, the program does not behave as it should when it refers to these "out of range" areas of memory.

Silicon model is set to mspx with the code and data models set to large and restricted respectively.

  • Hi Timothy
    Could you please help to provide the part of source code on mallocs both which is OK and which is out of reach?
    B.R.
    Xiaodong Li
  • It's very verbose and I don't know how much help it would be, I've included the header as an attachment

    const u1 * bytePntr;
    
    void read(u1 *ptr, size_t size, size_t nmemb){
        size_t x = size*nmemb;
        u4 i = 0;
        while(x--){
            ptr[i++] = *(bytePntr++);
    
        }
    }
    
    
    rDex * readBytes(){
        bytePntr = bytes;
        rDex * rdex = malloc(sizeof(rDex));
        read((u1*)&rdex->sizes,SIZESSTRUCT_LOADER_SIZE,1);
    
        rdex->field_id_items = malloc(rdex->sizes.field_ids_size*FIELDIDITEM_LOADER_SIZE);
        for(u2 i=0;i<rdex->sizes.field_ids_size;i++){
            read((u1*)rdex->field_id_items+i,FIELDIDITEM_LOADER_SIZE,1);
        }
    
        rdex->type_ids = malloc(rdex->sizes.type_ids_size*U2);
    
        for(u1 i=0;i<rdex->sizes.type_ids_size;i++){
            read((u1*)&rdex->type_ids[i],U2,1);
        }
        rdex->method_id_items = malloc(rdex->sizes.method_ids_size*sizeof(method_id_item));
        for(u1 i=0;i<rdex->sizes.method_ids_size;i++){
            read((u1*)&rdex->method_id_items[i].class_idx[0],U2,1);
            read((u1*)&rdex->method_id_items[i].name_idx[0],U2,1);
            read(&rdex->method_id_items[i].is_main,U1,1);
            read(&rdex->method_id_items[i].is_ObjInit,U1,1);
        }
    
        rdex->classDefs = malloc(sizeof(class_def_item)*rdex->sizes.class_defs_size);
        for(u1 i=0;i<rdex->sizes.class_defs_size;i++){
            read((u1*)&rdex->classDefs[i].class_idx[0],U2,1);
            read(& rdex->classDefs[i].class_data_off,U1,1);
            if(rdex->classDefs[i].class_data_off!=0) {
                rdex->classDefs[i].classData = malloc(sizeof(class_data_item));
                read(&rdex->classDefs[i].classData->static_fields_size, U1, 1);
                read(&rdex->classDefs[i].classData->instance_fields_size, U1, 1);
                read(&rdex->classDefs[i].classData->direct_methods_size, U1, 1);
                read(&rdex->classDefs[i].classData->virtual_methods_size, U1, 1);
    
                rdex->classDefs[i].classData->instance_fields = malloc(rdex->classDefs[i].classData->instance_fields_size*sizeof(encoded_field));
                for(u1 j=0;j<rdex->classDefs[i].classData->instance_fields_size;j++) {
                    read((u1*)&rdex->classDefs[i].classData->instance_fields[j].field_idx_diff,U2, 1);
                }
    
                rdex->classDefs[i].classData->static_fields = malloc(rdex->classDefs[i].classData->static_fields_size*sizeof(encoded_field));
                for(u1 j=0;j<rdex->classDefs[i].classData->static_fields_size;j++) {
                    read((u1*)&rdex->classDefs[i].classData->static_fields[j].field_idx_diff,U2, 1);
                }
    
                if(rdex->classDefs[i].classData->direct_methods_size>0){
                    rdex->classDefs[i].classData->direct_methods = malloc(rdex->classDefs[i].classData->direct_methods_size*sizeof(encoded_method));
                    for(int j=0;j<rdex->classDefs[i].classData->direct_methods_size;j++){
                        read((u1*)&rdex->classDefs[i].classData->direct_methods[j].method_idx_diff,U1,1);
    
                        (rdex->classDefs[i].classData->direct_methods+j)->methodCode = malloc(sizeof(code_item));
                        read((u1*)(rdex->classDefs[i].classData->direct_methods+j)->methodCode,CODEITEMSIZE_LOADER,1);
    
                        (rdex->classDefs[i].classData->direct_methods+j)->methodCode->insns = malloc((rdex->classDefs[i].classData->direct_methods+j)->methodCode->insns_size[0]);
                        read((u1*)(rdex->classDefs[i].classData->direct_methods+j)->methodCode->insns,U1,(rdex->classDefs[i].classData->direct_methods+j)->methodCode->insns_size[0]);
                    }
                }
    
                if(rdex->classDefs[i].classData->virtual_methods_size>0){
                    rdex->classDefs[i].classData->virtual_methods = malloc(rdex->classDefs[i].classData->virtual_methods_size*sizeof(encoded_method));
                    for(int j=0;j<rdex->classDefs[i].classData->virtual_methods_size;j++){
                        read((u1*)&rdex->classDefs[i].classData->virtual_methods[j].method_idx_diff,U1,1);
    
                        (rdex->classDefs[i].classData->virtual_methods+j)->methodCode = malloc(sizeof(code_item));
                        read((u1*)(rdex->classDefs[i].classData->virtual_methods+j)->methodCode,CODEITEMSIZE_LOADER,1);
    
                        (rdex->classDefs[i].classData->virtual_methods+j)->methodCode->insns = malloc((rdex->classDefs[i].classData->virtual_methods+j)->methodCode->insns_size[0]);
                        read((u1*)(rdex->classDefs[i].classData->virtual_methods+j)->methodCode->insns,U1,(rdex->classDefs[i].classData->virtual_methods+j)->methodCode->insns_size[0]);
                    }
            }
            }
        }
    
        rdex->strings = malloc(sizeof(String)*rdex->sizes.string_ids_size);
        for(int i=0;i<rdex->sizes.string_ids_size;i++){
            read((u1*)rdex->strings+i,STRINGSTRUCTSIZE_LOADER,1);
         
            if(rdex->strings[i].len>0){
                rdex->strings[i].string_data = malloc(rdex->strings[i].len);
                read((u1*)rdex->strings[i].string_data, U1, rdex->strings[i].len);
            }
        }
     
        return rdex;
    }
    

    definitions.h

  • line 41: rdex->classDefs[i].classData = malloc(sizeof(class_data_item));

    Is where the first problem arises.

    Here is also a copy the linker .cmd file (default) for this device.

    github.com/.../lnk_msp430fr6989.cmd

    EDIT: the header file includ7318.definitions.hed in the last post was the incorrect one, below is the proper one

  • Thanks for your reply! Please give me some time to check and discuss with the compiler expert.
  • Every call to malloc must always check for whether it returned NULL.  For details, I recommend you perform a web search on the term malloc check null.  

    Once you do that, I'm confident you will find out that some call to malloc is returning NULL.  This is usually because you didn't allocate enough memory for the heap during the build.  The compiler option --heap=number sets the size of the heap to that number of bytes.

    Thanks and regards,

    -George