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

io.format causes segfault with i(X) parameter #88

Closed
testock opened this issue May 4, 2020 · 8 comments
Closed

io.format causes segfault with i(X) parameter #88

testock opened this issue May 4, 2020 · 8 comments

Comments

@testock
Copy link

testock commented May 4, 2020

When compiled with mercury-20.01 to the default grade on MacOS 10.15.4, the following line causes a segfault:
io.format("Successful test: %s has %d solutions\n", [s(Code), i(Solution)], !IO)
...
Successful test: 1 has *** Mercury runtime: caught segmentation violation *** This may have been caused by a stack overflow, due to unbounded recursion. exiting from signal handler zsh: segmentation fault ./count
The problems seems to be caused by the integer processing. I tried the later rotd releases and had the same problem, however, when compiled using the java grade, the program runs as expected.
Further, the problem seems to be only with integer and float values. Strings, that is s(X), work fine.

@juliensf
Copy link
Member

juliensf commented May 5, 2020

Does your program work as expected if you compile with --no-optimize-format-calls?

@testock
Copy link
Author

testock commented May 5, 2020

No, it also fails. (I'm using a different test program now)
format_test.m.txt

Without --no-optimize-format-calls::

MacBook-Pro-2% mmc --make format_test
Making Mercury/int3s/format_test.int3
Making Mercury/ints/format_test.int
Making Mercury/cs/format_test.c
Making Mercury/os/format_test.o
Making format_test
MacBook-Pro-2% ./format_test
Try to print s("test"): test
Try to print s("4"): 4
Try to print i(4):
*** Mercury runtime: caught segmentation violation ***
This may have been caused by a stack overflow, due to unbounded recursion.
exiting from signal handler
zsh: segmentation fault ./format_test

With --no-optimize-format-calls:

MacBook-Pro-2% rm -r Mercury format_test format_test.mh format_test.err
MacBook-Pro-2% mmc --make --no-optimize-format-calls -E format_test
Making Mercury/int3s/format_test.int3
Making Mercury/ints/format_test.int
Making Mercury/cs/format_test.c
Making Mercury/os/format_test.o
Making format_test
MacBook-Pro-2% ./format_test

*** Mercury runtime: caught segmentation violation ***
This may have been caused by a stack overflow, due to unbounded recursion.
exiting from signal handler
zsh: segmentation fault ./format_test

`

@juliensf
Copy link
Member

juliensf commented May 5, 2020

It looks like you have one of the low-level C grades as your default grade? Which one is it? Also, what C compiler (and version thereof) are you using?

(I've tried the test program with macOS 10.12 using clang 4.2.1 -- clang 9.0.0 in Apples number -- with both the hlc.gc and none.gc grades and it works fine.)

@testock
Copy link
Author

testock commented May 5, 2020

According to the -V output, the grade is hlc.gc.
mmc-V.txt
I checked usr/local/mercury-20.01/lib/mercury/conf/Mercury.config says:
conf.zip

MERCURY_STDLIB_DIR=/usr/local/mercury-20.01/lib/mercury
MERCURY_DEFAULT_OPT_LEVEL=-O2
MERCURY_DEFAULT_GRADE=hlc.gc <------
MERCURY_C_COMPILER=gcc
MERCURY_C_COMPILER_TYPE=clang_4_2_1 <------
MERCURY_MATH_LIB=-lm
MERCURY_JAVA_COMPILER=/usr/bin/javac
MERCURY_JAVA_INTERPRETER=/usr/bin/java
MERCURY_TARGET_ARCH=x86_64-apple-darwin19.2.0 <-----

@testock
Copy link
Author

testock commented May 5, 2020

I just checked and it looks like the Apple number for clang is 11.0.0 (sorry I didn't put this in my previous remark):
MacBook-Pro-2% clang --version
Apple clang version 11.0.0 (clang-1100.0.33.8)
Target: x86_64-apple-darwin19.4.0
Thread model: posix

@testock
Copy link
Author

testock commented May 5, 2020

It works now. I upgraded XCode and command line tools and rebuilt mercury. Apparently there was a problem with the clang 11.0.0 and stack alignment. Other projects have reported similar issues. Clang 11.0.3 seems to work fine.

Thanks, and apologies for the inconvenience.

@testock testock closed this as completed May 5, 2020
@zsomogyi
Copy link
Contributor

zsomogyi commented May 5, 2020

Could you please send us a link to someplace that describes those problems with clang 11.0.0?
I would like to find out exactly which clang versions are affected, so we can add an entry to
Mercury's autoconfiguration to warn users who would be affected.

@testock
Copy link
Author

testock commented May 5, 2020

No problem. Here is the discussion that I found:
jedisct1/libsodium#848 (comment)

Yesterday when I checked with lldb I got the stack alignment error they describe:

MacBook-Pro-2% lldb format_test
(lldb) target create "format_test"
Current executable set to '/Users/testock/repositories/p99/tmp/format_test' (x86_64).
(lldb) run
Process 5959 launched: '/Users/testock/repositories/p99/tmp/format_test' (x86_64)
Try to print s("test"): test
Try to print s("4"): 4
Process 5959 stopped

  • thread Fix bug 271 #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
    frame #0: 0x00007fff719d470a libdyld.dylib stack_not_16_byte_aligned_error
    libdyld.dylib stack_not_16_byte_aligned_error:
    -> 0x7fff719d470a <+0>: movdqa %xmm0, (%rsp)
    0x7fff719d470f <+5>: int3
    0x7fff719d4710 <+6>: nop

libdyld.dylib`_dyld_fast_stub_entry:
0x7fff719d4711 <+0>: pushq %rbp
Target 0: (format_test) stopped.

The steps for the fix were as follows:

  1. Upgrade XCode to 11.4
  2. Remove the command line tools: sudo rm -rf /Library/Developer/CommandLineTools
  3. Reinstall the command line tools: sudo xcode-select --install
  4. Rebuild / install mercury.

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

No branches or pull requests

3 participants