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);
}
오류/경고가 표시되지 않는 컴파일이 수행됩니다. 그러나, 내가하려고 할 때 내 라즈베리 파이 (32 비트)에서 실행하기 위해, 나는 다음과 같은 출력을 얻을 : 내가 인텔 프로세서에서 실행하려고 할 때 같은 일을 사용하여 발생하는 것으로 나타났습니다
[[email protected] 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)
[[email protected] 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
을 컴파일 단계에 대한 -m32
옵션을 사용하여 32 비트 실행 파일을 생성합니다. 인텔에 대한 프로그램의 64 비트 실행 파일을 실행하는 동안
[email protected]:~/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
[email protected]:~/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
그러나이 발생하지 않습니다.
[email protected]:~/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
[email protected]:~/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);을 시도해보십시오. – wildplasser
왜 downvoting? 나는 "이 질문은 어떤 연구 노력도 보여주지 않는다. 그것은 불분명하거나 유용하지 않다"라고 생각하지 않는다. ... OP는 다른 빌드를 시도해 보았다. 적어도 왜 downvoting 말해. – Seki
아마도 질문 제목을 변경해야합니다. 해결책 (libc의 버그)보다는 질문을하는 것이 가장 좋습니다. "다른 아키텍처가 다른 행동을 보이는 이유는 무엇입니까?" 흥미롭게도, 당신이 그것을이 방법으로 표현할 때, 정의되지 않은 행동이 명백한 후보가됩니다. – stv