Closed hernanponcedeleon closed 2 years ago
Hi, @hernanponcedeleon, I don't think it is used or checked by SMACK. Relevant APIs are *ThreadLocalMode*
and I don't find them.
I think the following two programs show the problem.
user@e5f3c9c70c7e:~/smack$ cat test.c
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <pthread.h>
#include <stdatomic.h>
#include <assert.h>
int myindex = 0;
void *thread_n(void *arg)
{
myindex++;
assert(myindex == 1);
return NULL;
}
int main()
{
pthread_t t1, t2;
pthread_create(&t1, NULL, thread_n, (void *) 0);
pthread_create(&t2, NULL, thread_n, (void *) 1);
return 0;
}
user@e5f3c9c70c7e:~/smack$ smack --pthread test.c
SMACK program verifier version 2.8.0
...
SMACK found an error.
The above one is correct because both threads increment the same global variable.
user@e5f3c9c70c7e:~/smack$ cat test_local.c
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <pthread.h>
#include <stdatomic.h>
#include <assert.h>
__thread int myindex = 0;
void *thread_n(void *arg)
{
myindex++;
assert(myindex == 1);
return NULL;
}
int main()
{
pthread_t t1, t2;
pthread_create(&t1, NULL, thread_n, (void *) 0);
pthread_create(&t2, NULL, thread_n, (void *) 1);
return 0;
}
user@e5f3c9c70c7e:~/smack$ smack --pthread test_local.c
SMACK program verifier version 2.8.0
...
SMACK found an error.
This one however is not because variables are thread local.
@shaobo-he would it be possible to add support for thread local variables in smack? We are starting to see more benchmarks using those and this is causing wrong verification results.
@shaobo-he would it be possible to add support for thread local variables in smack? We are starting to see more benchmarks using those and this is causing wrong verification results.
I'm going to look into it this week. This may take a while since I'm not an expert in pthreads.
@hernanponcedeleon I thought about it for a while and concluded it's not trivial at all to implement this. So, I was wondering if it's hard to rewrite thread-local variables into local variables of functions running inside the threads.
Actually, let me share my current thoughts. Thread-local variables can be encoded as an array from thread ids to values. So, when a memory access happens, we have to query if the memory address refers to a thread-local variable. If so, we fetch the current thread id and further get/set the value otherwise get/set the value normally. This would incur overhead for each memory access encoding. Though there may be some optimizations, I doubt their generality.
@zvonimir @MontyCarter any thoguhts?
Thanks a lot @shaobo-he for making the effort, I really appreciate.
Unfortunately, I don't think this is really possible. The benchmarks we are trying to verify come from the Linux Kernel which heavily use thread local storage.
Would at least be possible to "tag" global variables coming from thread local one? My understanding is that you have this information at the LLVM level. I think and annotation like
const {:allocSize 8, :treadLocal} tindex: ref;
would be sufficient for us.
Thanks a lot @shaobo-he for making the effort, I really appreciate.
Unfortunately, I don't think this is really possible. The benchmarks we are trying to verify come from the Linux Kernel which heavily use thread local storage.
Would at least be possible to "tag" global variables coming from thread local one? My understanding is that you have this information at the LLVM level. I think and annotation like
const {:allocSize 8, :treadLocal} tindex: ref;
would be sufficient for us.
Thank you for the feedback. Let me implement this.
@hernanponcedeleon I implemented it via commit 9cbbfd9. It adds {:threadLocal}
attributes for thread-local varaibles. Please let me know if it works for you.
Looks good! Thanks @shaobo-he
Can smack handle thread local variables? I have the following C code
The corresponding llvm code clearly marks
tindex
asthread_local
However I don't see any related difference in the boogie code generated by smack between
threshold_reached
andtindex
, neither in the constant definitionnor in the static initialisation
which suggests that smack just ignores the tag. Am I right?