You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on May 28, 2025. It is now read-only.
Repository navigation
This repository was archived by the owner on May 28, 2025. It is now read-only.
Message sends to nil return structs return garbage values. #1365
Without a compiler fix, we cannot address the general case here.
We can, however, address the <=24-byte VFP aggregate case and the single floating point value cases.
Since getting a memset(0)'d struct from a nil message send is relatively new (compared to the rest of Objective-C), the real bug is that a single float or double does not get cleared on ARM. Fixing this should be sufficient for most cases.
changed the title [-]Message sends to nil return structs return garbage values on ARM[/-][+]Message sends to nil return structs return garbage values.[/+]on Jan 9, 2017
@interfaceX
- (T)message;
@end
...
T value = [x message];
For non-pointer-sized aggregate returns, the Mac Objective-C codegen shipped with clang emits code like the following:
T value;
static T _global_T_zeroValue_1; // zero-initialized by virtue of being static; C++ ctors called.if (likely(nil != receiver)) {
objc_msgSend_stret(&value, x, @selector(message));
} else {
memcpy(&value, _global_T_zeroValue_1, sizeof(T));
}
Whereas our code generator--and, indeed, clang's Mac codegen before iOS 5.0 or 6.0--emits code like this:
T value;
if (likely(nil != receiver)) {
objc_msgSend_stret(&value, x, @selector(message));
} else {
T *temporary = alloca(sizeof(T)); // <- never initializedmemcpy(&value, temporary, sizeof(T));
}
// else: nothing?
Since we never end up in objc_msgSend*, we don't get a chance to stomp the return value with nil.
For pointer-sized returns, the check is never made.
This may be a bug in CGObjCGNU. The comment here indicates that it should "fill in the 0 value".
However, it follows up by saying
// The language spec says the result of this kind of message send is// undefined, but lots of people seem to have forgotten to read that// paragraph and insist on sending messages to nil that have structure// returns. With GCC, this generates a random return value (whatever happens// to be on the stack / in those registers at the time) on most platforms,// and generates an illegal instruction trap on SPARC. With LLVM it corrupts// the stack.
As such, I'm bumping this to Area-Dev Tools and SubArea-Compiler.
This looks like it's specific to the MS version of clang: the initialisation is correct here (which hasn't been modified upstream for 2 years). With upstream clang, this code:
struct X { int x,y,z,a,b,c; };
@interfaceX
- (struct X)y;
@endintex(X*x)
{
[x y];
}
Generates this IR:
definei32@ex(%0* %x) #0 {
entry:
%retval = allocai32, align4%x.addr = alloca%0*, align8%tmp = alloca%struct.X, align4%null = alloca%struct.X, align4; struct X value initialised to 0 here.store%struct.Xzeroinitializer, %struct.X*%null, align4store%0* %x, %0** %x.addr, align8%0 = load%0*, %0** %x.addr, align8%1 = icmpeq%0* %0, nullbri1%1, label%continue, label%msgSend
msgSend: ; preds = %entry%2 = bitcast%0* %0toi8*callvoidbitcast (i8* (i8*, ...)* @objc_msgSend_strettovoid (%struct.X*, i8*, i8*)*)(%struct.X*sret%tmp, i8*%2, i8*bitcast ([2 x { i8*, i8* }]* @.objc_selector_listtoi8*)), !GNUObjCMessageSend!2brlabel%continue
continue: ; preds = %msgSend, %entry; Using the zero-initialised version if we didn't do the message send.%3 = phi%struct.X* [ %tmp, %msgSend ], [ %null, %entry ]
%4 = loadi32, i32*%retval, align4reti32%4
}
Note, however, that the ObjC language ref makes this explicitly undefined behaviour, because NeXT considered this approach and decided it was too expensive (back in the days of 25MHz CPUs and 8MB of RAM). I added this code path to clang 6-7 years ago because it makes portable code quite tricky to write, because whether objc_msgSend or objc_msgSend_stret is called depends on the ABI and, in particular, a lot of code broke on SPARC as a result of it treating things as stret that weren't on most other platforms (and people are far more likely to complain if code works on i386 OS X and not on SPARC OpenBSD with GNUstep than vice versa).
Apple adopted it a little bit later, but I think it wasn't until OS X 10.7 or 10.8 that this behaviour became reliable, and code that expects to run with older versions of Apple's toolchain shouldn't rely on it.
Activity
DHowett-MSFT commentedon Nov 14, 2016
Specifically: message sends to nil which return floats or uniform float aggregates will return garbage values on ARM.
Root Cause
objc_msgSend zeroes out
r0andr1ifr0is nil, but it does nothing ford0-d3.Fix
However, I have not had time to validate this fix.
DHowett-MSFT commentedon Nov 14, 2016
On the reference platform, Clang emits an
if(unlikely(!receiver)) { memset(&dest, 0, sizeof(dest)) }before every stret-possible message send.DHowett-MSFT commentedon Jan 6, 2017
Without a compiler fix, we cannot address the general case here.
We can, however, address the <=24-byte VFP aggregate case and the single floating point value cases.
Since getting a
memset(0)'d struct from a nil message send is relatively new (compared to the rest of Objective-C), the real bug is that a single float or double does not get cleared on ARM. Fixing this should be sufficient for most cases.rajsesh commentedon Jan 9, 2017
Dustin L. Howett (MSFT) (@DHowett-MSFT) I have a repro of this on x86 as well.
[-]Message sends to nil return structs return garbage values on ARM[/-][+]Message sends to nil return structs return garbage values.[/+]DHowett-MSFT commentedon Jan 9, 2017
It turns out that we can't fix this in libobjc2.
Assuming the following:
For non-pointer-sized aggregate returns, the Mac Objective-C codegen shipped with clang emits code like the following:
Whereas our code generator--and, indeed, clang's Mac codegen before iOS 5.0 or 6.0--emits code like this:
Since we never end up in
objc_msgSend*, we don't get a chance to stomp the return value with nil.For pointer-sized returns, the check is never made.
This may be a bug in CGObjCGNU. The comment here indicates that it should "fill in the 0 value".
However, it follows up by saying
As such, I'm bumping this to
Area-Dev ToolsandSubArea-Compiler.14 remaining items
davidchisnall commentedon May 23, 2017
This looks like it's specific to the MS version of clang: the initialisation is correct here (which hasn't been modified upstream for 2 years). With upstream clang, this code:
Generates this IR:
Note, however, that the ObjC language ref makes this explicitly undefined behaviour, because NeXT considered this approach and decided it was too expensive (back in the days of 25MHz CPUs and 8MB of RAM). I added this code path to clang 6-7 years ago because it makes portable code quite tricky to write, because whether
objc_msgSendorobjc_msgSend_stretis called depends on the ABI and, in particular, a lot of code broke on SPARC as a result of it treating things as stret that weren't on most other platforms (and people are far more likely to complain if code works on i386 OS X and not on SPARC OpenBSD with GNUstep than vice versa).Apple adopted it a little bit later, but I think it wasn't until OS X 10.7 or 10.8 that this behaviour became reliable, and code that expects to run with older versions of Apple's toolchain shouldn't rely on it.