为什么在传递函数'pthread_create()'之后变量'sum'的值会发生变化?

时间:2018-08-18 13:20:07

标签: c pthreads verification

我声明了一个变量sum并初始化为 0 ,然后通过函数func()将其传递给函数pthread_create()。我没想到的是变量sum不再等于 0 (如您所见,函数assert()失败了)。我的Ubuntu版本是16.04.4 LTS。

  

当我仅使用 GCC 进行编译时,就可以了,该错误是由 CBMC 报告的。

static void * func(void * arg)
{
  double *sum = (double *)arg;

  assert(*sum == 0.0);  //failed in CBMC
  return ((void *) 0);
}

int main(){

  double sum=0.0;
  pthread_t thr1;

  pthread_create(&thr1, NULL, &func, (void *)&sum );
  pthread_join(thr1, NULL);

  printf("sum = %f\n", sum);
  assert(sum == 0.0);   //successful in CBMC

  return 0;
}
  

以下是 CBMC 报告的警告:

State 70 file pthreads4.c line 24 function main thread 1
----------------------------------------------------
  arg=&sum!0@1 (0000010000000000000000000000000000000000000000000000000000000000)

State 71 file pthreads4.c line 12 function func thread 1
----------------------------------------------------
  sum=((double *)NULL) (0000000000000000000000000000000000000000000000000000000000000000)

State 72 file pthreads4.c line 12 function func thread 1
----------------------------------------------------
  sum=&sum!0@1 (0000010000000000000000000000000000000000000000000000000000000000)

Violated property:
  file pthreads4.c line 14 function func
  assertion *sum == 0
  IEEE_FLOAT_EQUAL(*sum, (double)0)

VERIFICATION FAILED

0 个答案:

没有答案