Skip to content

Two potential errors(e.g., deadlock) due to the unreleased lock &mutex_mmap #417

@true-ryan

Description

@true-ryan

Dear developers:
Thank you for your checking. The lock &mutex_mmap may not be released correctly when the condition posix_memalign(&p, box86_pagesize, size) and p==(void*)-1 return 1. The fix is to insert pthread_mutex_lock(&mutex_mmap); // the lock before returning.

pthread_mutex_lock(&mutex_mmap);

uintptr_t AllocDynarecMap(dynablock_t* db, int size)
{
    if(!size)
        return 0;
    if(size>MMAPSIZE-2*sizeof(blockmark_t)) {
        #ifndef USE_MMAP
        pthread_mutex_lock(&mutex_mmap); // the lock
        void *p = NULL;
        if(posix_memalign(&p, box86_pagesize, size)) {
            dynarec_log(LOG_INFO, "Cannot create dynamic map of %d bytes\n", size);
            return 0; /error 1, return with unreleased lock
        }
        mprotect(p, size, PROT_READ | PROT_WRITE | PROT_EXEC);
        #else
        void* p = mmap(NULL, size, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
        if(p==(void*)-1) {
            dynarec_log(LOG_INFO, "Cannot create dynamic map of %d bytes\n", size);
            return 0;  //error 2, return with unreleased lock
        }
       ...;
    return ret;
}

Best,

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions