www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.ldc - get memcpy EXC_BAD_ACCESS only with LTO on multi

reply Innot Sagg <donovinsbbkgbi gmail.com> writes:
I am not sure is this a ldc2 bug or my code bug.  I don't get it 
without LTO. with LTO I get this error on macOS, linux, 
Android(cross build and termux).


the code around this error:

new_ptr = my_zalloc(new_size);
if (! new_ptr ) {
	errno = -2;
	return errno;
}
new_ptr = 1 ;
if ( m_location ) {
	assert(old_ptr);
         // printf("m_location=%p\n", m_location); // add this 
line will not get error;
         // printf("old_ptr=%p\n", old_ptr); // add this line will 
not get error;
	llvm_memcpy(new_ptr, old_ptr, m_location);
}


I am not able to reduce it since I change any things the error 
will gone. for example if I add any one of the printf there will 
no error.

The linux error:
Program received signal SIGSEGV, Segmentation fault.
__memmove_sse2_unaligned_erms () at 
../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:384
384	../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S: No 
such file or directory.

../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:384

size=<optimized out>, zero=<optimized out>) at /d/buffer.d:919


the android error:
Program received signal SIGSEGV, Segmentation fault.
0x0000007fbf2bf10c in memcpy () from /system/lib64/libc.so


size=<optimized out>, zero=<optimized out>) at 
/data/data/com.termux/files/home/d/buffer.d:919

the maxOS error:

EXC_BAD_ACCESS (code=1, address=0x7ffeefc00000)

libsystem_platform.dylib`_platform_memmove$VARIANT$Haswell + 41




the old_ptr is a 512 byte memory block,  m_location = 512.

LTO with -O0, -O1 -O2, -O3 trigger this error. (-O1 on linux no 
error, some of my debug code only work with -O0)

Add printf before the memcpy, or remove LTO will made the error 
go away.

test with ldc2-1.23.0-beta1 and ldc2-1.22.0, same results.

any suggestion how to deal with this case ?
Jul 17 2020
parent reply Kagamin <spam here.lot> writes:
Maybe the compiler can understand that new_ptr=1 and remove the 
copy.
Jul 17 2020
parent reply Innot Sagg <donovinsbbkgbi gmail.com> writes:
On Saturday, 18 July 2020 at 03:33:30 UTC, Kagamin wrote:
 Maybe the compiler can understand that new_ptr=1 and remove the 
 copy.
Thanks for replay. "new_ptr=1" is typo, the code already remove that line. If i remove bitfields from Buffer struct, the error will gone. This code will trigger LTO error: struct Buffer { ubyte* m_data; union { ulong m_placeholder; uint m_location; mixin(bitfields!( uint, "", 32 , uint, "m_size", 31 , bool, "is_new", 1 , )) ; } } replace it to this will be no error: struct Buffer { ubyte* m_data; uint m_location; uint m_size; bool is_new; } I am still not able to reduce it into minimal test case, because any small change in the code will made the errno disappear.
Jul 17 2020
parent reply Kagamin <spam here.lot> writes:
AFAIK, ldc supports memory sanitizers, try to use them.
Jul 17 2020
parent reply Innot Sagg <donovinsbbkgbi gmail.com> writes:
On Saturday, 18 July 2020 at 03:58:22 UTC, Kagamin wrote:
 AFAIK, ldc supports memory sanitizers, try to use them.
Add "-fsanitize=address" made the errno disappear.
Jul 17 2020
parent reply Kagamin <spam here.lot> writes:
Try -fsanitize=undefined
Jul 17 2020
parent reply Innot Sagg <donovinsbbkgbi gmail.com> writes:
On Saturday, 18 July 2020 at 04:55:44 UTC, Kagamin wrote:
 Try -fsanitize=undefined
Error: Unrecognized -fsanitize value 'undefined'.
Jul 17 2020
parent Innot Sagg <donovinsbbkgbi gmail.com> writes:
On Saturday, 18 July 2020 at 04:55:44 UTC, Kagamin wrote:
 Try -fsanitize=undefined
After add -fsanitize=address, lto build no problem. Build without lto raise problem macOS: Undefined symbols for architecture x86_64: "___asan_version_mismatch_check_apple_clang_1103", referenced from: _asan.module_ctor in libmyhelp.a(mycode.o) ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) libmyhelp.a is the c static build with clang -fsanitize=address. If also pass -fsanitize=address to the clang when it called by ldc doing the link work. (ldc2 lto or non-lto same error) I get this runtime error: ================================================================= ==52571==ERROR: AddressSanitizer: unknown-crash on address 0x1c8000000000 at pc 0x000100129ff8 bp 0x7ffeefbfdda0 sp 0x7ffeefbfd560 WRITE of size 1536 at 0x1c8000000000 thread T0 AddressSanitizer:DEADLYSIGNAL ================================================================= ==52571==ERROR: AddressSanitizer: BUS on unknown address 0x624000000010 (pc 0x000100117c21 bp 0x7ffeefbfb320 sp 0x7ffeefbfb300 T16777215) <empty stack> ==52571==Register values: rax = 0x0000624000000000 rbx = 0x000000010271f060 rcx = 0x000061d000000070 rdx = 0x00007ffeefbfb3f8 rdi = 0x000000010271f060 rsi = 0x000000010271f0e0 rbp = 0x00007ffeefbfb320 rsp = 0x00007ffeefbfb300 r8 = 0x0000000000000828 r9 = 0x00000fffffffffff r10 = 0x0000000000000000 r11 = 0xffffffffffffffff r12 = 0x00007ffeefbfb3f8 r13 = 0x00007ffeefbfb3f8 r14 = 0x0000000000000828 r15 = 0x000061d000000070 AddressSanitizer can not provide additional info. SUMMARY: AddressSanitizer: BUS AddressSanitizer:DEADLYSIGNAL AddressSanitizer: nested bug in the same thread, aborting.
Jul 17 2020