Skip to content

Commit

Permalink
Merge pull request #300 from insertinterestingnamehere/msan
Browse files Browse the repository at this point in the history
Memory Sanitizer Fixes
  • Loading branch information
insertinterestingnamehere authored Oct 17, 2024
2 parents 5fca9c6 + 80695e1 commit 1e9775a
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ jobs:
scheduler: [nemesis, sherwood, distrib]
topology: [hwloc, binders, no]
use_libcxx: [false] # disable testing on libcxx since its effect seems very limited for now.
exclude:
- sanitizer: memory
topology: hwloc
- sanitizer: memory
topology: binders
env:
CC: clang-19
CXX: clang++-19
Expand Down
6 changes: 6 additions & 0 deletions include/qt_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
#define Q_UNUSED(x) x
#endif

#if defined(__has_feature)
#if __has_feature(memory_sanitizer)
#define QTHREAD_MSAN
#endif
#endif

#define TLS_DECL(type, name) thread_local type name
#define TLS_DECL_INIT(type, name) thread_local type name = 0
#define TLS_GET(name) name
Expand Down
3 changes: 3 additions & 0 deletions src/fastcontext/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "qthread/common.h"

#include "qt_asserts.h"
#include "qt_macros.h"
#include "qt_prefetch.h"
#include "qt_visibility.h"

Expand Down Expand Up @@ -149,7 +150,9 @@ QT_SKIP_THREAD_SANITIZER int INTERNAL qt_swapctxt(uctxt_t *oucp, uctxt_t *ucp) {
if (getcontext(oucp) == 0) {
#if ((QTHREAD_ASSEMBLY_ARCH == QTHREAD_IA32) || \
(QTHREAD_ASSEMBLY_ARCH == QTHREAD_AMD64))
#ifndef QTHREAD_MSAN
Q_PREFETCH((void *)ucp->mc.mc_esp, 1, 3);
#endif
#endif
setcontext(ucp);
}
Expand Down
1 change: 1 addition & 0 deletions src/threadqueues/distrib_threadqueues.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ static qt_threadqueue_t *alloc_threadqueue(void) {
t->t = qt_malloc(sizeof(qt_threadqueue_internal) * t->num_queues);
t->w_inds =
qt_calloc(qlib->nshepherds * qlib->nworkerspershep, sizeof(w_ind));
atomic_store_explicit(&t->numwaiters, 0ull, memory_order_relaxed);
return t;
}

Expand Down
17 changes: 9 additions & 8 deletions test/basics/tasklocal_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <stdlib.h>
#include <unistd.h>

int *main_local = NULL;

static aligned_t use_default_space(void *arg) {
unsigned int size = 64 * sizeof(int);

Expand All @@ -15,10 +17,9 @@ static aligned_t use_default_space(void *arg) {
qthread_size_tasklocal());
iprintf("use_default_space(): local_int_arr at %p\n", local_int_arr);
local_int_arr = (int *)qthread_get_tasklocal(size);
test_check(42 != local_int_arr[42]);
iprintf(
"use_default_space(): retrieved tasklocal data successfully (42 != %d)\n",
local_int_arr[42]);
test_check(main_local != local_int_arr);
iprintf("use_default_space(): retrieved tasklocal data successfully "
"(distinct from main thread's local)\n");
local_int_arr = NULL;

return 0;
Expand All @@ -35,10 +36,9 @@ static aligned_t use_allocated_space(void *arg) {
qthread_size_tasklocal());
iprintf("use_allocated_space(): local_int_arr at %p\n", local_int_arr);
local_int_arr = (int *)qthread_get_tasklocal(size);
test_check(42 != local_int_arr[42]);
iprintf(
"use_allocated_space(): retrieved tasklocal data successfully (42 != %d)\n",
local_int_arr[42]);
test_check(main_local != local_int_arr);
iprintf("use_allocated_space(): retrieved tasklocal data successfully "
"(distinct from main thread's local)\n");
local_int_arr = NULL;

return 0;
Expand Down Expand Up @@ -121,6 +121,7 @@ int main(int argc, char *argv[]) {
iprintf("main(): size_tasklocal() is %d\n", qthread_size_tasklocal());
iprintf("main(): local_int_arr at %p\n", local_int_arr);
local_int_arr = (int *)qthread_get_tasklocal(size);
main_local = local_int_arr;
test_check(42 == local_int_arr[42]);
iprintf("main(): retrieved tasklocal data successfully (42 == %d)\n",
local_int_arr[42]);
Expand Down
17 changes: 9 additions & 8 deletions test/basics/tasklocal_data_no_argcopy.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <stdlib.h>
#include <unistd.h>

int *main_local = NULL;

static aligned_t use_default_space(void *arg) {
unsigned int size = 64 * sizeof(int);

Expand All @@ -16,10 +18,9 @@ static aligned_t use_default_space(void *arg) {
qthread_size_tasklocal());
iprintf("use_default_space(): local_int_arr at %p\n", local_int_arr);
local_int_arr = (int *)qthread_get_tasklocal(size);
test_check(42 != local_int_arr[42]);
iprintf(
"use_default_space(): retrieved tasklocal data successfully (42 != %d)\n",
local_int_arr[42]);
test_check(main_local != local_int_arr);
iprintf("use_default_space(): retrieved tasklocal data successfully "
"(distinct from main thread's local)\n");
local_int_arr = NULL;

return 0;
Expand All @@ -36,10 +37,9 @@ static aligned_t use_allocated_space(void *arg) {
qthread_size_tasklocal());
iprintf("use_allocated_space(): local_int_arr at %p\n", local_int_arr);
local_int_arr = (int *)qthread_get_tasklocal(size);
test_check(42 != local_int_arr[42]);
iprintf(
"use_allocated_space(): retrieved tasklocal data successfully (42 != %d)\n",
local_int_arr[42]);
test_check(main_local != local_int_arr);
iprintf("use_allocated_space(): retrieved tasklocal data successfully "
"(distinct from main thread's local)\n");
local_int_arr = NULL;

return 0;
Expand Down Expand Up @@ -123,6 +123,7 @@ int main(int argc, char *argv[]) {
iprintf("main(): size_tasklocal() is %d\n", qthread_size_tasklocal());
iprintf("main(): local_int_arr at %p\n", local_int_arr);
local_int_arr = (int *)qthread_get_tasklocal(size);
main_local = local_int_arr;
test_check(42 == local_int_arr[42]);
iprintf("main(): retrieved tasklocal data successfully (42 == %d)\n",
local_int_arr[42]);
Expand Down
2 changes: 1 addition & 1 deletion test/features/qpool.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ int main(int argc, char *argv[]) {
fprintf(stderr, "qpool_alloc() failed!\n");
exit(-1);
}
iprintf("allocated: %p (%lu)\n", (void *)rets, (unsigned long)*rets);
iprintf("allocated: %p\n", (void *)rets);
*rets = 1;
if (*rets != 1) {
fprintf(stderr, "assigning a value to the allocated memory failed!\n");
Expand Down

0 comments on commit 1e9775a

Please sign in to comment.