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

Attribute errors in threads to the correct thread #101

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

fosslinux
Copy link
Contributor

@fosslinux fosslinux commented Aug 20, 2024

This trivial program:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void *thread(void *data) {
	char *s = malloc(8 * sizeof(char));
	s[10] = 0;
	free(s);
	return 0;
}

int main(void) {
	pthread_t thread_data;
	pthread_create(&thread_data, NULL, thread, NULL);
	pthread_join(thread_data, NULL);
}

Previously outputted

=================================================================

Runtime error: malloc buffer overflow
dcc explanation: access past the end of malloc'ed memory.
  Make sure you have allocated enough memory for the size of your struct/array.
  A common error is to use the size of a pointer instead of the size of the struct or array.

  For more information see: https://comp1511unsw.github.io/dcc/malloc_sizeof.html

Execution stopped in main() in a.c at line 15:

int main(void) {
	pthread_t thread_data;
	pthread_create(&thread_data, NULL, thread, NULL);
-->	pthread_join(thread_data, NULL);
}

Values when execution stopped:

thread_data = 127271036778176

Which is clearly incorrect, as the error occurs within the thread.
This set of changes makes DCC correctly trace the error back to the originating thread, for ASAN and signal errors.

Now it outputs:

=================================================================

Runtime error: malloc buffer overflow
dcc explanation: access past the end of malloc'ed memory.
  Make sure you have allocated enough memory for the size of your struct/array.
  A common error is to use the size of a pointer instead of the size of the struct or array.

  For more information see: https://comp1511unsw.github.io/dcc/malloc_sizeof.html

Execution stopped in thread(data=NULL) in a.c at line 7:

void *thread(void *data) {
	char *s = malloc(8 * sizeof(char));
-->	s[10] = 0;
	free(s);
	return 0;
}

Values when execution stopped:

data = NULL
s = "<8 uninitialized values>"
s[10] = 0 = '\0'

Motivation: I found this very very confusing the first few times I ran across this bug in COMP1521 this term, so I fixed it 😄

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 this pull request may close these issues.

1 participant