kendryte / kendryte-standalone-sdk

Standalone SDK for kendryte K210
https://kendryte.com
Apache License 2.0
440 stars 161 forks source link

reculock implementation is not multicore safe #96

Open jkbmtx opened 4 years ago

jkbmtx commented 4 years ago

BUG REPORT

I observed that the reculock implementation in locks.c file is not safe for use by multiple cores. reculock array is a shared resource that is not protected from simultaneous usage by separate cores. If two cores use two distinct recursive locks in parallel with high frequency, corruption occurs and system crashes.

Expected behavior

The system works in a stable manner.

Actual behavior

System crashes.

Test code


reculock_t reculock[LOCK_MAX_NUM];

...

static reculock_t *get_free_reculock(void)
{
    reculock_t *ret = NULL;
    for(uint32_t i = 0; i < LOCK_MAX_NUM; i++)
      {
        // we should make sure that this logic does not run in parallel on both cores
        if(reculock[i].lock == NULL){
          ret = &reculock[i];
          break;
        }
      }

    return ret;
}

Document version

present master

Hardware

KD233

System

Linux