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

Initial address sanitisation support #47

Merged
merged 1 commit into from
Nov 14, 2024
Merged

Initial address sanitisation support #47

merged 1 commit into from
Nov 14, 2024

Conversation

hmelder
Copy link
Contributor

@hmelder hmelder commented Nov 8, 2024

Introducing address sanitization support

For more information on address sanitization read https://clang.llvm.org/docs/AddressSanitizer.html or https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html#index-fsanitize_003daddress.

Address sanitization can be turned on with the new asan flag in GNUstep Make: make asan=yes.

Caveats

The GNUstep Base library currently leaks memory upon program exit which makes analysis a bit cumbersome. We should improve on this, and encourage users of GNUstep make to use sanitization.

Demo

include $(GNUSTEP_MAKEFILES)/common.make

TOOL_NAME = demo

demo_OBJC_FILES = demo.m

-include GNUmakefile.preamble

include $(GNUSTEP_MAKEFILES)/tool.make

-include GNUmakefile.postamble
#import <Foundation/Foundation.h>

int main() {
	NSAutoreleasePool *arp = [NSAutoreleasePool new];
	[arp drain];
	return 0;
}
$ make asan=yes
$ ./obj/demo

=================================================================
==42402==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 40 byte(s) in 1 object(s) allocated from:
    #0 0xaaaadfe673d0 in calloc (/home/hmelder/example-gnustep-project/obj/demo+0xd73d0) (BuildId: af853ff0eca3f6f0e09b182830f87b00b389c0ea)
    #1 0xffffaf3c8df0 in allocate_class /home/hmelder/libobjc2/gc_none.c:19:3
    #2 0xffffaf3cb8ac in class_createInstance /home/hmelder/libobjc2/runtime.c:361:11
    #3 0xffffaf6d36dc in NSAllocateObject (/usr/gnustep/lib/libgnustep-base.so.1.30+0x2d36dc) (BuildId: 4e1ccc5c5276fba035c2814c49a9837ee5e5811e)
    #4 0xffffaf63dd40 in _c_NSDataStatic__allocWithZone_ NSData.m
    #5 0xffffaf637914 in _c_NSData__dataWithBytesNoCopy_length_ NSData.m
    #6 0xffffaf746088 in _i_NSString__dataUsingEncoding_allowLossyConversion_ NSString.m
    #7 0xffffaf745e10 in _i_NSString__dataUsingEncoding_ NSString.m
    #8 0xffffaf745748 in _i_NSString__getCString_maxLength_encoding_ NSString.m
    #9 0xffffaf6d32b8 in NSClassFromString (/usr/gnustep/lib/libgnustep-base.so.1.30+0x2d32b8) (BuildId: 4e1ccc5c5276fba035c2814c49a9837ee5e5811e)
    #10 0xffffaf5bfb8c in setup GSString.m
    #11 0xffffaf5bf958 in _c_GSPlaceholderString__initialize GSString.m
    #12 0xffffaf3c7e10 in objc_send_initialize /home/hmelder/libobjc2/dtable.c:859:2
    #13 0xffffaf3ccdf8 in objc_msg_lookup_internal /home/hmelder/libobjc2/sendmsg2.c:112:4
    #14 0xffffaf3ccdf8 in slowMsgLookup /home/hmelder/libobjc2/sendmsg2.c:163:9
    #15 0xffffaf3d05f8 in objc_msgSend_stret /home/hmelder/libobjc2/objc_msgSend.aarch64.S:210
    #16 0xffffaf73b354 in _c_NSString__initialize NSString.m
    #17 0xffffaf3c7e10 in objc_send_initialize /home/hmelder/libobjc2/dtable.c:859:2
    #18 0xffffaf3ccdf8 in objc_msg_lookup_internal /home/hmelder/libobjc2/sendmsg2.c:112:4
    #19 0xffffaf3ccdf8 in slowMsgLookup /home/hmelder/libobjc2/sendmsg2.c:163:9
    #20 0xffffaf3d05f8 in objc_msgSend_stret /home/hmelder/libobjc2/objc_msgSend.aarch64.S:210
    #21 0xffffaf6d3b2c in _c_NSObject__initialize NSObject.m
    #22 0xffffaf3c7e10 in objc_send_initialize /home/hmelder/libobjc2/dtable.c:859:2
    #23 0xffffaf3c7c58 in objc_send_initialize /home/hmelder/libobjc2/dtable.c:745:3
    #24 0xffffaf3ccdf8 in objc_msg_lookup_internal /home/hmelder/libobjc2/sendmsg2.c:112:4
    #25 0xffffaf3ccdf8 in slowMsgLookup /home/hmelder/libobjc2/sendmsg2.c:163:9
    #26 0xffffaf3d05f8 in objc_msgSend_stret /home/hmelder/libobjc2/objc_msgSend.aarch64.S:210
    #27 0xaaaadfea691c in main (/home/hmelder/example-gnustep-project/obj/demo+0x11691c) (BuildId: af853ff0eca3f6f0e09b182830f87b00b389c0ea)
    #28 0xffffaf112290  (/lib/aarch64-linux-gnu/libc.so.6+0x22290) (BuildId: 5c8f998f04145b99f2b808e5679fee4bb785e2a5)
    #29 0xffffaf112374 in __libc_start_main (/lib/aarch64-linux-gnu/libc.so.6+0x22374) (BuildId: 5c8f998f04145b99f2b808e5679fee4bb785e2a5)
    #30 0xaaaadfdc472c in _start (/home/hmelder/example-gnustep-project/obj/demo+0x3472c) (BuildId: af853ff0eca3f6f0e09b182830f87b00b389c0ea)

Indirect leak of 17 byte(s) in 1 object(s) allocated from:
    #0 0xaaaadfe67204 in malloc (/home/hmelder/example-gnustep-project/obj/demo+0xd7204) (BuildId: af853ff0eca3f6f0e09b182830f87b00b389c0ea)
    #1 0xffffaf7a6f70 in default_malloc NSZone.m
    #2 0xffffaf7a6d50 in NSZoneMalloc (/usr/gnustep/lib/libgnustep-base.so.1.30+0x3a6d50) (BuildId: 4e1ccc5c5276fba035c2814c49a9837ee5e5811e)
    #3 0xffffaf8091cc in GSFromUnicode (/usr/gnustep/lib/libgnustep-base.so.1.30+0x4091cc) (BuildId: 4e1ccc5c5276fba035c2814c49a9837ee5e5811e)
    #4 0xffffaf746058 in _i_NSString__dataUsingEncoding_allowLossyConversion_ NSString.m
    #5 0xffffaf745e10 in _i_NSString__dataUsingEncoding_ NSString.m
    #6 0xffffaf745748 in _i_NSString__getCString_maxLength_encoding_ NSString.m
    #7 0xffffaf6d32b8 in NSClassFromString (/usr/gnustep/lib/libgnustep-base.so.1.30+0x2d32b8) (BuildId: 4e1ccc5c5276fba035c2814c49a9837ee5e5811e)
    #8 0xffffaf5bfb8c in setup GSString.m
    #9 0xffffaf5bf958 in _c_GSPlaceholderString__initialize GSString.m
    #10 0xffffaf3c7e10 in objc_send_initialize /home/hmelder/libobjc2/dtable.c:859:2
    #11 0xffffaf3ccdf8 in objc_msg_lookup_internal /home/hmelder/libobjc2/sendmsg2.c:112:4
    #12 0xffffaf3ccdf8 in slowMsgLookup /home/hmelder/libobjc2/sendmsg2.c:163:9
    #13 0xffffaf3d05f8 in objc_msgSend_stret /home/hmelder/libobjc2/objc_msgSend.aarch64.S:210
    #14 0xffffaf73b354 in _c_NSString__initialize NSString.m
    #15 0xffffaf3c7e10 in objc_send_initialize /home/hmelder/libobjc2/dtable.c:859:2
    #16 0xffffaf3ccdf8 in objc_msg_lookup_internal /home/hmelder/libobjc2/sendmsg2.c:112:4
    #17 0xffffaf3ccdf8 in slowMsgLookup /home/hmelder/libobjc2/sendmsg2.c:163:9
    #18 0xffffaf3d05f8 in objc_msgSend_stret /home/hmelder/libobjc2/objc_msgSend.aarch64.S:210
    #19 0xffffaf6d3b2c in _c_NSObject__initialize NSObject.m
    #20 0xffffaf3c7e10 in objc_send_initialize /home/hmelder/libobjc2/dtable.c:859:2
    #21 0xffffaf3c7c58 in objc_send_initialize /home/hmelder/libobjc2/dtable.c:745:3
    #22 0xffffaf3ccdf8 in objc_msg_lookup_internal /home/hmelder/libobjc2/sendmsg2.c:112:4
    #23 0xffffaf3ccdf8 in slowMsgLookup /home/hmelder/libobjc2/sendmsg2.c:163:9
    #24 0xffffaf3d05f8 in objc_msgSend_stret /home/hmelder/libobjc2/objc_msgSend.aarch64.S:210
    #25 0xaaaadfea691c in main (/home/hmelder/example-gnustep-project/obj/demo+0x11691c) (BuildId: af853ff0eca3f6f0e09b182830f87b00b389c0ea)
    #26 0xffffaf112290  (/lib/aarch64-linux-gnu/libc.so.6+0x22290) (BuildId: 5c8f998f04145b99f2b808e5679fee4bb785e2a5)
    #27 0xffffaf112374 in __libc_start_main (/lib/aarch64-linux-gnu/libc.so.6+0x22374) (BuildId: 5c8f998f04145b99f2b808e5679fee4bb785e2a5)
    #28 0xaaaadfdc472c in _start (/home/hmelder/example-gnustep-project/obj/demo+0x3472c) (BuildId: af853ff0eca3f6f0e09b182830f87b00b389c0ea)

@rfm
Copy link
Contributor

rfm commented Nov 8, 2024

I tried changing gnustep-base to free memory at exit several years ago, but gave up because it was just too much work (for little gain) and nobody seemed interested in helping.
I'd quite like to look at it again though ... improve the usability of the cleanup methods and try to use them s lot more

Copy link
Contributor

@rfm rfm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good and works well for me.
I did wonder if some configure time checks for whether the feature is available (and a warning if you try to use it when it isn't) would be good, but I don't think it's worth the effort as this would only be used by experienced developers.

@hmelder hmelder merged commit 23485f8 into master Nov 14, 2024
@hmelder hmelder deleted the sanitiser branch November 14, 2024 11:28
@rfm
Copy link
Contributor

rfm commented Nov 15, 2024

FYI I fixed a typo and added another way (environment variable) to turn on asan.
Using an environment variable lets us easily turn it on for the test suite; if we build library code with asan then we need to build the testcases with asan too.

At the moment I'm working on fixing leaks in the tests for the base library, but it's slow because there are lot of them.

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

Successfully merging this pull request may close these issues.

2 participants