Skip to content

Dangerous conflict potential of user and system code #385

@tnowotny

Description

@tnowotny

I was working with pygenn and defined a connectivity init snippet like so:

ors_orns_connect = create_custom_sparse_connect_init_snippet_class(
    "or_type_specific",
    row_build_code=
        """
        const unsigned int row_length= $(num_post)/$(num_pre);
        const unsigned int offset= $(id_pre)*row_length;
        for (unsigned int i= 0; i < row_length; i++) {
            $(addSynapse, (offset + i));
        }
        $(endRow);
        """,
    calc_max_row_len_func=create_cmlf_class(
        lambda num_pre, num_post, pars: int(num_post/num_pre))()
)

However, when running in the single-threaded CPU backend this was code generated into another loop that also used i as its loop index:

     // Synapse groups with sparse connectivity
     {
        // merged synapse connectivity init group 0
        for(unsigned int g = 0; g < 1; g++) {
            const auto *group = &mergedSynapseConnectivityInitGroup0[g]; 
            memset(group->rowLength, 0, group->numSrcNeurons * sizeof(unsigned int));
            for (unsigned int i = 0; i < group->numSrcNeurons; i++) {
                // Build sparse connectivity
                while(true) {
                    
                    const unsigned int row_length= group->numTrgNeurons/group->numSrcNeurons;
                    const unsigned int offset= i*row_length;
                    for (unsigned int i= 0; i < row_length; i++) {
                        do {
                        const unsigned int idx = (i * group->rowStride) + group->rowLength[i];
                        group->ind[idx] = (offset + i);
                        group->rowLength[i]++;
                    }
                    while(false);
                    }
                    break;
                    
                }
            }
        }
    }

This obviously did not work and ended in a segmentation fault.

I wonder whether as a rule, we should always use something like __i or other convention for code-generated indices that are highly unlikely to be chosen by a user (this is what brian 2 does afaik).

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions