After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 783220 - Add ASan and UBSan to GJS
Add ASan and UBSan to GJS
Status: RESOLVED FIXED
Product: gjs
Classification: Bindings
Component: general
unspecified
Other Linux
: Normal normal
: ---
Assigned To: Claudio André
gjs-maint
Depends on:
Blocks:
 
 
Reported: 2017-05-29 23:52 UTC by Claudio André
Modified: 2017-08-25 03:39 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Add sanitizer options (2.24 KB, patch)
2017-05-29 23:54 UTC, Claudio André
none Details | Review
Add sanitizer build options (1.95 KB, patch)
2017-08-15 02:47 UTC, Claudio André
reviewed Details | Review
maint: add ASAN Address Sanitizer (2.80 KB, patch)
2017-08-21 00:52 UTC, Philip Chimento
committed Details | Review
Add sanitizer to doc (1.85 KB, patch)
2017-08-24 02:21 UTC, Claudio André
committed Details | Review

Description Claudio André 2017-05-29 23:52:57 UTC
This patch adds ASan and UBSan to GJS configure and build process. The patch is safe since nothing will change except the user enable one of the new options (checks):
* --enable-asan
* --enable-ubsan

As I told once, ASan detects memory leaks in GJS [1].
I never had luck with UBSan in general (I guess it was not stable). Anyway, it worked just fine for GJS; no warnings or errors.

----
[1] in fact, it is complicated. We are facing problems while calling routines in linked libraries. It is deeper.
Comment 1 Claudio André 2017-05-29 23:54:41 UTC
Created attachment 352833 [details] [review]
Add sanitizer options
Comment 2 Philip Chimento 2017-06-11 04:49:20 UTC
Review of attachment 352833 [details] [review]:

Really nice! When running the tests with asan, there is a use-after-free which actually may be related to bug 781799, so this is really helpful.

I had to remove -Wno-undefined from libgjs_la_LDFLAGS in Makefile.am for this to work. Otherwise it fails with undefined symbols while linking libgjs.la. I am pretty sure that's not what I'm supposed to do :-) Perhaps the sanitizer flags need to be added to there instead of to AM_LDFLAGS? In that case I would suggest doing AC_SUBST([SAN_FLAGS]) in configure.ac, and adding $(SAN_FLAGS) in the appropriate places in each make target.

Here as well, consider adding a paragraph of instructions to doc/Hacking.md.

::: configure.ac
@@ +212,3 @@
+
+AS_IF([test "x$enable_asan" = "xyes"], [
+    AX_CHECK_COMPILE_FLAG([-fsanitize=address], [

I think this can be simplified using AX_APPEND_COMPILE_FLAGS (https://www.gnu.org/software/autoconf-archive/ax_append_compile_flags.html#ax_append_compile_flags). Maybe something like this, although you would have to split it into two separate variables.

    AX_APPEND_COMPILE_FLAGS([-fsanitize=address -fno-omit-frame-pointer -g], [SAN_CFLAGS])
    AX_APPEND_LINK_FLAGS([-fsanitize=address -fno-omit-frame-pointer -g], [SAN_LDFLAGS])

@@ +221,3 @@
+    ])
+    GJS_CFLAGS="$GJS_CFLAGS $SAN_FLAGS"
+    AX_CHECK_COMPILE_FLAG([-fsanitize=address], [

Should be AM_LDFLAGS, otherwise we will clobber the user's linker flags (see for more information https://www.gnu.org/software/automake/manual/html_node/Flag-Variables-Ordering.html)

@@ +227,3 @@
+  [AS_HELP_STRING([--enable-ubsan], [Build with undefined behavior sanitizer support @<:@default: no@:>@])])
+
+	    AX_CHECK_COMPILE_FLAG([-fno-omit-frame-pointer -g], [

Is it possible to have --enable-asan and --enable-ubsan at the same time? If so, then you should not overwrite SAN_FLAGS from asan with ubsan. If not, then you should check for the case where they are both specified and exit with AC_MSG_ERROR.
Comment 3 Claudio André 2017-08-15 02:44:08 UTC
(In reply to Philip Chimento from comment #2)
> 
> I had to remove -Wno-undefined from libgjs_la_LDFLAGS in Makefile.am for
> this to work. Otherwise it fails with undefined symbols while linking
> libgjs.la.
I tested this inside Travis CI and I saw no errors. If you add -ldl to LD_FLAGS does it work?
Anyway: I've got a new test machine, I will try to reproduce locally.

I'm attaching a new patch. I will start my testing using it because it is clear. Later, I can try the approach you suggested.
Comment 4 Claudio André 2017-08-15 02:47:13 UTC
Created attachment 357600 [details] [review]
Add sanitizer build options
Comment 5 Philip Chimento 2017-08-17 02:21:57 UTC
I'm still futzing around trying to get it to work with Clang, but one suggestion I thought of in the meantime is combining the two flags. It looks like they are meant to be able to be used together, so no reason to enable one and not the other, right?

In any case I found the answer to the -no-undefined question at https://github.com/google/sanitizers/issues/380
Comment 6 Philip Chimento 2017-08-21 00:51:48 UTC
I think you were right about LDFLAGS versus AM_LDFLAGS before. Otherwise the sanitizer flags are not added when compiling GIRs with g-ir-scanner.

Here's a patch that works for me. Let me know if it works for you as well, and I'll commit it.
Comment 7 Philip Chimento 2017-08-21 00:52:03 UTC
Created attachment 358038 [details] [review]
maint: add ASAN Address Sanitizer

AddressSanitizer (or ASan) is a programming tool that detects memory
corruption bugs such as buffer overflows or use after free. AddressSanitizer
is based on compiler instrumentation.

UndefinedBehaviorSanitizer (or UBSan) is a fast undefined behavior
detector. It modifies the program at compile-time to catch errors
such as using misaligned or null pointer and signed integer overflow.
Comment 8 Philip Chimento 2017-08-21 00:52:53 UTC
Review of attachment 357600 [details] [review]:

Updated with no-undefined fix and LDFLAGS.
Comment 9 Philip Chimento 2017-08-21 21:56:46 UTC
Oh yeah, and also let me know what you think of combining the two flags into one.

(If you are able to check today, then we can include it in today's 1.49.91. Otherwise we can put it in 1.49.92 in two weeks.
Comment 10 Claudio André 2017-08-24 02:20:35 UTC
The new patch seems to work fine to me (sorry, I'm having troubles in my dev machine).
- You can combine both flags or use any one of them. 
  - While testing, I prefer to not combine them. But, this is personal, anyone else might prefer to test using, e.g. --enable-sanitizers.

Your call.

BTW: ASAN and UBSAN are becoming better. An updated compiler is advised (yes, a warning about ancient LTS distros).
Please, let me know if you want a note in about this.
Comment 11 Claudio André 2017-08-24 02:21:12 UTC
Created attachment 358295 [details] [review]
Add sanitizer to doc

BTW: there is some moz38 inside docs.
Comment 12 Philip Chimento 2017-08-25 03:32:12 UTC
Review of attachment 358295 [details] [review]:

+1
Comment 13 Philip Chimento 2017-08-25 03:39:15 UTC
I squashed both together. Thanks!
Attachment 358038 [details] pushed as 9883073 - maint: add ASAN Address Sanitizer

I also fixed the mozjs38 in Hacking.md, thanks for pointing it out.