ldc icon indicating copy to clipboard operation
ldc copied to clipboard

AddressSanitizer not work with NSString

Open calvin2021y opened this issue 3 years ago • 1 comments

	import core.attribute : selector;
	extern (Objective-C)
	interface Class
	{
	  NSString alloc() @selector("alloc") @nogc nothrow ;
	}
	extern (Objective-C)
	interface NSString
	{
	  NSString initWithUTF8String(in char* str) @selector("initWithUTF8String:") @nogc nothrow ;
	  void release() @selector("release") @nogc nothrow ;
	}
	extern (C) void NSLog(NSString, ...) @nogc nothrow ;
	extern (C) Class objc_lookUpClass(in char* name) @nogc nothrow ;
	void nslog(const(char)* msg) @nogc nothrow  {
		scope cls = objc_lookUpClass("NSString");
		scope str = cls.alloc().initWithUTF8String(msg);
		scope(exit) str.release();
		NSLog(str);
	}

extern(C) void main(){
	nslog("test".ptr);
}

ldc2 -betterC test.d -fsanitize=address -L-framework -LFoundation

test(51135,0x1121f0600) malloc: nano zone abandoned due to inability to preallocate reserved vm space.
2022-01-28 17:03:09.143 test[51135:276395] test
AddressSanitizer:DEADLYSIGNAL
=================================================================
==51135==ERROR: AddressSanitizer: SEGV on unknown address (pc 0x00010ca38d56 bp 0x7ff7b34ca970 sp 0x7ff7b34ca910 T0)
==51135==The signal is caused by a READ memory access.
==51135==Hint: this fault was caused by a dereference of a high value address (see register values below).  Disassemble the provided pc to learn which register was used.
    #0 0x10ca38d56 in _D4test5nslogFNaNbNiPxaZv+0x176 (test:x86_64+0x100003d56)
    #1 0x10ca38def in main+0xf (test:x86_64+0x100003def)
    #2 0x1121754fd in start+0x1cd (dyld:x86_64+0x54fd)

==51135==Register values:
rax = 0x1a3b71c0c440a1d0  rbx = 0x000000010ca45060  rcx = 0x0000100000000000  rdx = 0x0000000000000000  
rdi = 0x0000000000000001  rsi = 0x0000625000001038  rbp = 0x00007ff7b34ca970  rsp = 0x00007ff7b34ca910  
 r8 = 0x000000000000000d   r9 = 0x0000000000000000  r10 = 0xffffffffffffffff  r11 = 0x00000fffffffffff  
r12 = 0x00000001121f03a0  r13 = 0x00007ff7b34caa38  r14 = 0x000000010ca38de0  r15 = 0x00000001121dc010  
AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV (test:x86_64+0x100003d56) in _D4test5nslogFNaNbNiPxaZv+0x176
==51135==ABORTING
Abort trap: 6

calvin2021y avatar Jan 28 '22 09:01 calvin2021y

The documents:

An NSString object initialized by copying the bytes from nullTerminatedCString. The returned object may be different from the original receiver.

The same code run fine with IOS( AddressSanitizer enabled, test on IOS X86_64), just not work for macOS.

calvin2021y avatar Jan 28 '22 09:01 calvin2021y