Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GNU ObjC] Clang 14 regression: access violation when building with ARC and optimization #56056

Closed
triplef opened this issue Jun 15, 2022 · 2 comments

Comments

@triplef
Copy link
Member

triplef commented Jun 15, 2022

Building the following Objective-C code using using Automatic Reference Counting and the GNU Objective-C 2.0 runtime and -O1 or better optimization using Clang 14.0.5 causes an access violation at runtime.

This does not happen with Clang 13.0.1, or when using -O0, or when not using ARC.

#import "objc/runtime.h"

__attribute__((objc_root_class))
@interface Test {
  id isa;
}
+ (id)new;
@end

@implementation Test
+ (id)new {
  return class_createInstance(self, 0);
}
@end

int main(void) {
  [Test new];
  return 0;
}

Building:

"C:\Program Files\LLVM-14\bin\clang" -IC:\libobjc2\include -LC:\libobjc2\lib -fobjc-runtime=gnustep-2.0 -fobjc-arc -fuse-ld=lld -lobjc -O2 -o test.exe test.m

Result:

* thread #1, stop reason = Exception 0xc0000005 encountered at address 0x7ffb16aea405: Access violation reading location 0xffffffffffffffff
    frame #0: 0x00007ffb16aea405 objc.dll`objc_retain + 149
objc.dll`objc_retain:
->  0x7ffb16aea405 <+149>: movq   (%rax), %rcx
    0x7ffb16aea408 <+152>: movl   $0x4000, %edx             ; imm = 0x4000
    0x7ffb16aea40d <+157>: callq  0x7ffb16aeacb0            ; objc_storeWeak + 1472
    0x7ffb16aea412 <+162>: movl   %eax, 0x54(%rsp)
@triplef
Copy link
Member Author

triplef commented Jun 15, 2022

Interestingly the access violation does not happen when using NSObject from GNUstep Base instead of the locally defined Test class, and it even works to call [Test new] after [NSObject new]. However calling [Test new] before [NSObject new] still reproduces the bug.

#import "objc/runtime.h"
#import <Foundation/Foundation.h>

__attribute__((objc_root_class))
@interface Test {
  id isa;
}
+ (id)new;
@end

@implementation Test
+ (id)new {
  return class_createInstance(self, 0);
}
@end

// does not crash:
int main(void) {
  [NSObject new];
  [Test new];
  return 0;
}

// crashes:
/*
int main(void) {
  [Test new];
  [NSObject new];
  return 0;
}
*/

Built with the following using the pre-built GNUstep toolchain:

"C:\Program Files\LLVM-14\bin\clang" -I C:\GNUstep\x64\Debug\include -L C:\GNUstep\x64\Debug\lib -fobjc-runtime=gnustep-2.0 -lobjc -fobjc-arc -fuse-ld=lld -fexceptions -fobjc-exceptions -fblocks -DGNUSTEP -DGNUSTEP_WITH_DLL -DGNUSTEP_RUNTIME=1 -D_NONFRAGILE_ABI=1 -D_NATIVE_OBJC_EXCEPTIONS -lobjc -lgnustep-base -O2 -o test.exe test.m

@davidchisnall
Copy link
Contributor

I believe that this is caused by libobjc2 not having the ns_returns_retained attribute on class_createInstance. Closing and fixing it over there. Please reopen if this turns out to be a clang bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants