Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improper locking bugs(e.g., deadlocks) on the lock #1329

Closed
ryancaicse opened this issue Sep 14, 2021 · 6 comments · Fixed by #1330
Closed

Improper locking bugs(e.g., deadlocks) on the lock #1329

ryancaicse opened this issue Sep 14, 2021 · 6 comments · Fixed by #1330
Assignees

Comments

@ryancaicse
Copy link

Hi, developers, thank you for your checking. It seems the lock fd->refs->lock is not released correctly when start < 1 in the function cram_get_ref?

htslib/cram/cram_io.c

Lines 3419 to 3443 in 575db47

pthread_mutex_lock(&fd->refs->lock);
if (r->length == 0) {
if (cram_populate_ref(fd, id, r) == -1) {
hts_log_error("Failed to populate reference for id %d", id);
pthread_mutex_unlock(&fd->refs->lock);
pthread_mutex_unlock(&fd->ref_lock);
return NULL;
}
r = fd->refs->ref_id[id];
if (fd->unsorted)
cram_ref_incr_locked(fd->refs, id);
}
/*
* We now know that we the filename containing the reference, so check
* for limits. If it's over half the reference we'll load all of it in
* memory as this will speed up subsequent calls.
*/
if (end < 1)
end = r->length;
if (end >= r->length)
end = r->length;
if (start < 1)
return NULL;

Best,

@ryancaicse
Copy link
Author

Also, for the lock fd->ref_lock

pthread_mutex_lock(&fd->ref_lock);

@jkbonfield
Copy link
Contributor

jkbonfield commented Sep 14, 2021

Thanks for the bug report. It looks like a real issue.

I'm curious if you tripped over this with real data. I don't believe start should ever be < 1 on valid files, which is likely why we've never triggered it, but as we have an error case where we bail out here then we should also free the locks.

Edit: I see start isn't modified, so we could just move the check to the top along side id == -1 and return before we've done any locking. Seems like a simple fix.

@ryancaicse
Copy link
Author

@jkbonfield Thanks.
They are found by code scan. Yes, it looks like, the programs come to the corner cases where the locks are not released properly.

@jkbonfield jkbonfield self-assigned this Sep 14, 2021
jkbonfield added a commit to jkbonfield/htslib that referenced this issue Sep 15, 2021
This fixes samtools#1329, which was discovered by code scanning and reported
by Github @ryancaicse.

I do not believe it is likely to be triggered, but the value of this
file can sometimes come from a CRAM file so it is possible malformed
data could lead to a threading deadlock. (Untested)
whitwham pushed a commit that referenced this issue Sep 28, 2021
This fixes #1329, which was discovered by code scanning and reported
by Github @ryancaicse.

I do not believe it is likely to be triggered, but the value of this
file can sometimes come from a CRAM file so it is possible malformed
data could lead to a threading deadlock. (Untested)
@daviesrob
Copy link
Member

@ryancaicse Out of interest, what did you use to find this?

@ryancaicse
Copy link
Author

@daviesrob Thank you for your interest. I used Pinpoint. Its code technique could be found here.

@daviesrob
Copy link
Member

Thanks, I'll take a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants