【发布时间】:2014-02-22 15:13:28
【问题描述】:
似乎我在 libc 中遇到了一个可能的错误。我有以下代码:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
struct bla
{
int a,b,c,d;
};
pthread_t tid;
void print (const char *s, const struct bla *fp);
void * thr_fn1 ( void * arg);
int main()
{
struct bla *bla_main;
pthread_create (&tid,NULL,thr_fn1,NULL);
pthread_join (tid, (void *) &bla_main);
print ("Old thread: \n",bla_main);
return 0;
}
void print (const char *s, const struct bla *bla_print)
{
printf ("%s\n",s);
printf ("Struct address: %p\n",bla_print);
printf ("fp.a = %d\n",bla_print->a);
printf ("fp.b = %d\n",bla_print->b);
printf ("fp.c = %d\n",bla_print->c);
printf ("fp.d = %d\n",bla_print->d);
}
void * thr_fn1 ( void * arg)
{
struct bla *bla_thr;
bla_thr= malloc(1);
bla_thr->a=1;
bla_thr->b=2;
bla_thr->c=3;
bla_thr->d=4;
print ("Thread 1:\n",bla_thr);
pthread_exit ((void *) bla_thr);
}
使用gcc -Wall -pthread file.c 完成编译,不会产生错误/警告。但是,当我尝试在我的 Raspberry Pi(32 位)上运行它时,我得到以下输出:
[alex@ArchPi code]$ ./a.out
Thread 1:
Struct address: 0xb6500468
fp.a = 1
fp.b = 2
fp.c = 3
fp.d = 4
a.out: malloc.c:2365: sysmalloc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.
Aborted (core dumped)
[alex@ArchPi code]$ file a.out
a.out: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.27, BuildID[sha1]=33e5d87872f0b40924a709fe266d47f9f011a06c, not stripped
我注意到,当我尝试在 Intel 处理器上运行它时会发生同样的事情,在编译步骤中使用 -m32 选项来生成 32 位可执行文件。
alex@debian:~/code$ ./a.out
Thread 1:
Struct address: 0x804e098
fp.a = 1
fp.b = 2
fp.c = 3
fp.d = 4
a.out: malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.
Aborted
alex@debian:~/code$ file a.out
a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.26, BuildID[sha1]=0x9966b205f3f6cd3d3a544ea010608e11346f6f9a, not stripped
但是,在 Intel 上运行该程序的 64 位可执行文件时不会发生这种情况。
alex@debian:~/code$ ./a.out
Thread 1:
Struct address: 0xb42130
fp.a = 1
fp.b = 2
fp.c = 3
fp.d = 4
Old thread:
Struct address: 0xb42130
fp.a = 1
fp.b = 2
fp.c = 3
fp.d = 4
alex@debian:~/code$ file a.out
a.out: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.26, BuildID[sha1]=0x4bb04ef61287bfe750a37427bb41b8b1578d74e1, not stripped
那么,这是 libc/malloc() 中的错误,还是我做错了什么? 如果您需要更多详细信息,请告诉我。
谢谢
【问题讨论】:
-
bla_thr= malloc(1);1 字节对于结构来说可能有点太小了。试试bla_thr= malloc( sizeof *bla_thr );。 -
为什么要投反对票?我不认为“这个问题没有显示任何研究工作;不清楚或没有用”...... OP 甚至尝试了不同的构建。至少说明为什么要投票。
-
也许您应该更改问题标题。最好陈述一个问题而不是一个可能的解决方案(libc 中的错误)。诸如“为什么不同的架构会表现出不同的行为?”之类的东西。有趣的是,当您这样表述时,未定义的行为显然会成为候选对象。
-
@stv: 听从了你的建议 ;)