Bug 8061

Summary: print 64 bit data in pthread always becomes strange data
Product: uClibc Reporter: BigZJK <zhao.junkui>
Component: stdioAssignee: unassigned
Status: RESOLVED INVALID    
Severity: minor CC: uclibc-cvs
Priority: P5    
Version: 0.9.33.2   
Target Milestone: 0.9.33.2   
Hardware: Other   
OS: Linux   
Host: Target: armv7-unknown-gnueabi
Build:
Attachments: test file

Description BigZJK 2015-04-29 07:43:18 UTC
when i try to printf a long long data in pthread,the printf result is always wrong. But the same data printf in the main process,the result is correct? And the result of stroll interface is wrong(always the max long long)?

test result:
/ # iptables -h
pthread_entry_zjk:HIGH 203e235e
pthread_entry_zjk:LOW 2702123
pthread_entry_zjk:HIGH 203e235e
pthread_entry_zjk:LOW 2702123
pthread_entry_zjk:0
pthread_entry_zjk:175676819675021312
pthread_entry_zjk:270212300000000
pthread_entry_zjk:175676819675021312
pthread_entry_zjk:175676819675021312
pthread_entry_zjk:175676819675021312
pthread_entry_zjk:3689067335152182110
pthread_entry_zjk_lijishu:175676819675021312
iptables-main:2323333344444555555
iptables-main:7fffffffffffffff
iptables-main:7fffffffffffffff
iptables-main:2323333344444555555
iptables-main:2323333344444555555
iptables-main:2323333344444555555
iptables-main:2323333344444555555

code following:
(i test in iptables open source code)

int
main(int argc, char *argv[])
{
	...
	ret = do_command(argc, argv, &table, &handle);
	if (ret) {
		ret = iptc_commit(handle);
		iptc_free(handle);
	}

	...
	exit(!ret);
}


long long test_zjk10 = 0;
long long test_zjk = 0;
long long test_zjk16 = 0;
long long test_zjk17 = 0;

long long test_zjk10_p  = 0;
long long test_zjk16_p = 0;
long long test_zjk_p  = 0;
char test_zjk_arr_p[100] = {0};


pthread_mutex_t mutex_zjk = PTHREAD_MUTEX_INITIALIZER;
void pthread_entry_zjk(void)
{
	char test[] = "2323333344444555555";
	char test1[] = "2323333344444555555";
	test_zjk_p = atoll("2323333344444555555");
	test_zjk10_p = strtoll(&test[0], NULL,10);
	test_zjk16_p= strtoll(&test1[0], NULL,10);

	pthread_mutex_lock(&mutex_zjk);
		
	printf("pthread_entry_zjk:%lld\n",0);
	printf("pthread_entry_zjk:%lld\n",test_zjk10_p);
	printf("pthread_entry_zjk:%llx\n",test_zjk16_p);
	printf("pthread_entry_zjk:%lld\n",test_zjk_p);
	printf("pthread_entry_zjk:%lld\n",test_zjk_p);
	printf("pthread_entry_zjk:%lld\n",test_zjk_p);
	sprintf(test_zjk_arr_p,"%lld",test_zjk10_p);
	printf("pthread_entry_zjk:%s\n",test_zjk_arr_p);
	printf("pthread_entry_zjk_lijishu:%lld\n",2323333344444555555);
	
	pthread_mutex_unlock(&mutex_zjk);
}

int do_command(int argc, char *argv[], char **table, struct iptc_handle **handle)
{
	...
	pthread_t id_zjk;
	pthread_create(&id_zjk,NULL,pthread_entry_zjk,NULL);
	sleep(40);
	test_zjk = atoll("2323333344444555555");
	test_zjk10 = strtoll(&test[0], NULL,10);
	test_zjk16 = strtoll(&test1[0], NULL,16);
	printf("iptables-main:%lld\n",test_zjk10);
	printf("iptables-main:%llx\n",test_zjk16);
	printf("iptables-main:%llx\n",test_zjk17);
	printf("iptables-main:%lld\n",test_zjk);
	printf("iptables-main:%lld\n",test_zjk);
	printf("iptables-main:%lld\n",test_zjk);
	sprintf(test_zjk_arr,"%lld",test_zjk10);
	printf("iptables-main:%s\n",test_zjk_arr);
	...
}
Comment 1 BigZJK 2015-04-29 08:02:54 UTC
platform arm
OS uClinux
Comment 2 Bernhard Reutner-Fischer 2015-04-29 10:21:38 UTC
Can you please provide a small, self-contained test along instructions on how to compile and run it that demonstrates the issue?
Comment 3 BigZJK 2015-04-30 09:11:41 UTC
(In reply to comment #2)
> Can you please provide a small, self-contained test along instructions on how
> to compile and run it that demonstrates the issue?

arch: armr7
os  : uClinux
cross_compiler: arm-unknown-eabi gcc4.8.3

command line cmds like following:
/ # iptables -h
Comment 4 Bernhard Reutner-Fischer 2015-04-30 13:31:18 UTC
(In reply to comment #3)
> (In reply to comment #2)
> > Can you please provide a small, self-contained test along instructions on how
> > to compile and run it that demonstrates the issue?
> 
> arch: armr7
> os  : uClinux
> cross_compiler: arm-unknown-eabi gcc4.8.3
> 
> command line cmds like following:
> / # iptables -h

Please provide a small, self-contained (no non-system headers) C testcase and how you compile this C testcase (arm-unknown-eabi -o bug bug.c -pthread -marmv99) and how i can run the testcase (maybe ./bug).


The partial code you pasted here does not compile.
thanks,
Comment 5 BigZJK 2015-05-04 06:47:35 UTC
Created attachment 6021 [details]
test file

Actually there are two strange places.
Refer to our project code, I wrote two test code.
First test the Strtoull API with 16 hex, it can run, but the result is not correct,whatever the num changes, Strtoull always return 0x7fffffffffffffff.
Second test printf code in Pthread, but the code throws a segmentation fault(i can`t understand why,the same code of creating pthread can run in our project, althrough the printf is always strange)?
The test platform X86-64, os:ubuntu.
Test code in the accessory
Comment 6 BigZJK 2015-05-08 02:48:33 UTC
(In reply to comment #5)
> Created attachment 6021 [details]
> test file
> 
> Actually there are two strange places.
> Refer to our project code, I wrote two test code.
> First test the Strtoull API with 16 hex, it can run, but the result is not
> correct,whatever the num changes, Strtoull always return 0x7fffffffffffffff.
> Second test printf code in Pthread, but the code throws a segmentation fault(i
> can`t understand why,the same code of creating pthread can run in our project,
> althrough the printf is always strange)?
> The test platform X86-64, os:ubuntu.
> Test code in the accessory

sorry, now we resolved it, maybe it`s not uClibc`s problem, but the complier gcc:
in my project, when i create a pthread, it`s stack was not 8b aligned, then the result always become strange.Just keep the stack base address 8b aligned, it`s ok.