Skip to content

RF edges generating incorrectly for local struct members #74

Description

@Daniel-A-W

Ran into this issue trying to verify a program, I've written up a small MWE below.

The reader thread should be picking up its own store to f.wait, but the tool reports a read from uninitialised memory.
If run with -cache-instructions, it gives an internal error.
Also, the execution time estimate is shown as "-nans".

Replacing the local declaration with a globally declared variable (but not adding any additional writes) fixes the issue.
Replacing the struct with a single atomic int also appears to fix it.

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

struct flag {
   _Atomic unsigned int wait ;
};

#define FLAGS 1

_Atomic (struct flag*) flags[FLAGS];
pthread_t writer;
pthread_t reader;

void* read(void* i) {
  struct flag f;
  _Atomic struct flag* p = &f;
  atomic_store_explicit(&flags[(int)i], p, memory_order_release);
  atomic_store_explicit(&f.wait, 1, memory_order_release);
  while (atomic_load_explicit(&f.wait, memory_order_relaxed));
  pthread_join(&writer, NULL);
}

void* write(void* p) {
  for(int i = 0; i < FLAGS; i++) {
    struct flag* f;
    do {f = atomic_load_explicit(&flags[i], memory_order_acquire);}
    while (f == NULL);
    atomic_store_explicit(&f->wait, 0, memory_order_relaxed);
  }
}

int main() {
	pthread_create(&writer, NULL, &write, NULL);
	pthread_create(&reader, NULL, &read, (void*)0);
	pthread_join(reader, NULL);
	pthread_join(writer, NULL);
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions