Chapel: chpl_library_finalize() appears to halt program and cause seg fault 11

Created on 27 Oct 2017  路  8Comments  路  Source: chapel-lang/chapel

More exciting ways to seg fault! I used to do this to Postgres all the time.

I am building off this project to try and use an integrated C / Chapel system. Here are my files

Makefile

EXEC=connector
LIB=worker

library: worker.chpl
    chpl --library -o libworker $<

connector: connector.c
    `$(CHPL_HOME)/util/config/compileline --compile` -L. -l$(LIB) `$(CHPL_HOME)/util/config/compileline --libraries` -Iworker.h -v -o $(EXEC) $<

run:
    ./$(EXEC)

clean:
    rm lib$(LIB).a $(EXEC)

all: library connector run

worker.h

int doWork();
char* sayWork();

connector.c

#include <stdio.h>
#include "worker.h"
extern void chpl_library_init();
extern void chpl_library_finalize(void);

int tryInt() {
  int w;
  chpl_library_init();
  w = doWork();
  printf("Work is %d \n", w);
  chpl_library_finalize();
  printf("I'm done with this...");
  return w;
}

int main(int argc, char* argv[]) {
  printf("Heading in to tryInt");
  tryInt();
  return 0;
}

and worker.chpl

export proc doWork() {
  var i: int = 17;
  return i;
}

export proc sayWork() {
  var s: string = "Say, is that work?";
  return s;
}

Running this gives me

"/Library/Developer/CommandLineTools/usr/bin/ld" -demangle -lto_library /Library/Developer/CommandLineTools/usr/lib/libLTO.dylib -no_deduplicate -dynamic -arch x86_64 -macosx_version_min 10.12.0 -o connector -L. -L/Users/buddha/github/chapel/third-party/qthread/install/darwin-clang-native-flat/lib -L/Users/buddha/github/chapel/third-party/jemalloc/install/darwin-clang-native/lib -L/Users/buddha/github/chapel/third-party/gmp/install/darwin-clang-native/lib -L/Users/buddha/github/chapel/third-party/hwloc/install/darwin-clang-native-flat/lib -L/Users/buddha/github/chapel/third-party/re2/install/darwin-clang-native/lib -L/Users/buddha/github/chapel/lib/darwin/clang/arch-native/loc-flat/comm-none/tasks-qthreads/tmr-generic/unwind-none/mem-jemalloc/atomics-intrinsics/gmp/hwloc/re2/fs-none -L/Users/buddha/github/chapel/third-party/hwloc/install/darwin-clang-native-flat/lib -L/Users/buddha/github/chapel/third-party/jemalloc/install/darwin-clang-native/lib -lworker -rpath /Users/buddha/github/chapel/third-party/qthread/install/darwin-clang-native-flat/lib -rpath /Users/buddha/github/chapel/third-party/gmp/install/darwin-clang-native/lib -rpath /Users/buddha/github/chapel/third-party/hwloc/install/darwin-clang-native-flat/lib -rpath /Users/buddha/github/chapel/third-party/re2/install/darwin-clang-native/lib -lchpl -lm -lgmp -lchpl -lqthread -ljemalloc -lhwloc -lm -lre2 -lpthread /var/folders/g5/4zwyfk513hd94tjs1fm5mdq00000gn/T/connector-5f08f2.o -lSystem /Library/Developer/CommandLineTools/usr/lib/clang/9.0.0/lib/darwin/libclang_rt.osx.a
./connector
make: *** [run] Segmentation fault: 11
Runtime Won't fix / Ain't broke Feature Request user issue

All 8 comments

From what I'm seeing, the segfault is because chpl_library_init() expects argc and argv arguments which you aren't passing in (see http://chapel-lang.org/docs/master/technotes/libraries.html?highlight=chpl_library_init). So that routine is accessing some garbage values on the stack and segfaulting.

If it's reasonable, you might #include chpl-init.h in your connector.c file rather than using manual extern declarations of the chpl_library_*() functions, which would've probably resulted in a C compilation error.

If I change your code to:

  const char* dummyArg0 = "";
  chpl_library_init(0, &dummyArg0);

I get:

./connector
Heading in to tryInt
Trying init
Trying doWork()
Work is 17 

[edited for clarity once I realized "segfault" and "halting the program" were two distinct issues]

Note that chpl_library_finalize()currently exits program execution, so it needs to be the last thing called (at least with how it's currently implemented.) So in your example printf("I'm done with this..."); will never run

If that's a problem, feel free to make a new "chpl_library_finalize() exits" issue

Why does it exit the program? That seems weird.

It's probably not intentional that it terminates the program. I think when chpl_library_finalize() was introduced in https://github.com/chapel-lang/chapel/pull/945 it just hooked into our existing shutdown code, which happens to call exit.

I _think_ we could rework our runtime code to not call exit on finalize, but I'm not sure how much work that will be. It should be pretty straight forward for single-locale, but I think it'll be a lot more difficult to coordinate for multi-locale programs.

That said it seems like a bug that chpl_library_finalize() exits. If that's a big deal for you I can dig deeper now, otherwise I'll probably just file an issue for a rainy day.

As a workaround, you could also probably just not call chpl_library_finalize() (either "ever" or "until you're ready to exit").

The big downside to not shutting down our runtime is that we'll still be using resources which could have a negative performance impact on other code (e.g. we could tie up a lot memory, the comm handler will still run every now-and-again, and more importantly our qthreads will be pinned to cores, which will give them priority scheduling, and significantly hurt the performance of other threads.)

Now I'm getting a compilation error similar to https://github.com/chapel-lang/chapel/issues/7666

#include <stdio.h>
#include "worker.h"
#include "chpl-init.h"

int tryInt() {
  int w;
  const char* dummyArg0 = "";
  chpl_library_init(0, &dummyArg0);
  w = doWork();
  printf("Work is %d \n", w);
  chpl_library_finalize();
  printf("I'm done with this...");
  return w;
}

int main(int argc, char* argv[]) {
  printf("Heading in to tryInt");
  tryInt();
  printf("Done trying Int, did it work?");
  tryInt();

  return 0;
}```

```chapel
// worker.chpl 
export proc doWork() {
  var i: int = 17;
  return i;
}
// Makefile
EXEC=connector
LIB=worker

library: worker.chpl
    chpl --library -o libworker $<

connector: connector.c
    `$(CHPL_HOME)/util/config/compileline --compile` -L. -l$(LIB) `$(CHPL_HOME)/util/config/compileline --libraries` -Iworker.h -v -o $(EXEC) $<

run:
    ./$(EXEC)

clean:
    rm lib$(LIB).a $(EXEC)

all: library connector run

And the Grand Finale!

> make connector
`/Users/buddha/github/chapel/util/config/compileline --compile` -L. -lworker `/Users/buddha/github/chapel/util/config/compileline --libraries` -Iworker.h -v -o connector connector.c
Apple LLVM version 9.0.0 (clang-900.0.38)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
Found CUDA installation: /usr/local/cuda, version 7.5
 "/Library/Developer/CommandLineTools/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.12.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name connector.c -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -fno-strict-return -masm-verbose -munwind-tables -target-cpu penryn -target-linker-version 302.3 -v -dwarf-column-info -debugger-tuning=lldb -resource-dir /Library/Developer/CommandLineTools/usr/lib/clang/9.0.0 -D _POSIX_C_SOURCE -I /Users/buddha/github/chapel/third-party/qthread/install/darwin-clang-native-flat/include -I /Users/buddha/github/chapel/third-party/hwloc/install/darwin-clang-native-flat/include -D "CHPL_TASKS_MODEL_H=\"tasks-qthreads.h\"" -D "CHPL_THREADS_MODEL_H=\"threads-none.h\"" -D CHPL_JEMALLOC_PREFIX=chpl_je_ -D CHPL_HAS_GMP -I /Users/buddha/github/chapel/third-party/qthread/install/darwin-clang-native-flat/include -I . -I /Users/buddha/github/chapel/modules/packages -I /Users/buddha/github/chapel/runtime//include/localeModels/flat -I /Users/buddha/github/chapel/runtime//include/localeModels -I /Users/buddha/github/chapel/runtime//include/comm/none -I /Users/buddha/github/chapel/runtime//include/comm -I /Users/buddha/github/chapel/runtime//include/tasks/qthreads -I /Users/buddha/github/chapel/runtime//include/threads/none -I /Users/buddha/github/chapel/runtime//include -I /Users/buddha/github/chapel/runtime//include/qio -I /Users/buddha/github/chapel/runtime//include/atomics/intrinsics -I /Users/buddha/github/chapel/runtime//include/mem/jemalloc -I /Users/buddha/github/chapel/third-party/utf8-decoder -I /Users/buddha/github/chapel/runtime//../build/runtime/darwin/clang/arch-native/loc-flat/comm-none/tasks-qthreads/tmr-generic/unwind-none/mem-jemalloc/atomics-intrinsics/gmp/hwloc/re2/fs-none/include -I /Users/buddha/github/chapel/third-party/jemalloc/install/darwin-clang-native/include -I /Users/buddha/github/chapel/third-party/gmp/install/darwin-clang-native/include -I /Users/buddha/github/chapel/third-party/hwloc/install/darwin-clang-native-flat/include -I worker.h -Wno-unused -Wno-uninitialized -Wno-pointer-sign -Wno-tautological-compare -fdebug-compilation-dir /Users/buddha/github/buddha314/chapppl -ferror-limit 19 -fmessage-length 164 -fwrapv -stack-protector 1 -fblocks -fobjc-runtime=macosx-10.12.0 -fencode-extended-block-signature -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/g5/4zwyfk513hd94tjs1fm5mdq00000gn/T/connector-d46375.o -x c connector.c
clang -cc1 version 9.0.0 (clang-900.0.38) default target x86_64-apple-darwin16.7.0
ignoring nonexistent directory "/Users/buddha/github/chapel/runtime//include/comm/none"
ignoring nonexistent directory "/Users/buddha/github/chapel/runtime//include/threads/none"
ignoring nonexistent directory "worker.h"
ignoring duplicate directory "/Users/buddha/github/chapel/third-party/qthread/install/darwin-clang-native-flat/include"
ignoring duplicate directory "/Users/buddha/github/chapel/third-party/hwloc/install/darwin-clang-native-flat/include"
#include "..." search starts here:
#include <...> search starts here:
 /Users/buddha/github/chapel/third-party/qthread/install/darwin-clang-native-flat/include
 /Users/buddha/github/chapel/third-party/hwloc/install/darwin-clang-native-flat/include
 .
 /Users/buddha/github/chapel/modules/packages
 /Users/buddha/github/chapel/runtime//include/localeModels/flat
 /Users/buddha/github/chapel/runtime//include/localeModels
 /Users/buddha/github/chapel/runtime//include/comm
 /Users/buddha/github/chapel/runtime//include/tasks/qthreads
 /Users/buddha/github/chapel/runtime//include
 /Users/buddha/github/chapel/runtime//include/qio
 /Users/buddha/github/chapel/runtime//include/atomics/intrinsics
 /Users/buddha/github/chapel/runtime//include/mem/jemalloc
 /Users/buddha/github/chapel/third-party/utf8-decoder
 /Users/buddha/github/chapel/runtime//../build/runtime/darwin/clang/arch-native/loc-flat/comm-none/tasks-qthreads/tmr-generic/unwind-none/mem-jemalloc/atomics-intrinsics/gmp/hwloc/re2/fs-none/include
 /Users/buddha/github/chapel/third-party/jemalloc/install/darwin-clang-native/include
 /Users/buddha/github/chapel/third-party/gmp/install/darwin-clang-native/include
 /usr/local/include
 /Library/Developer/CommandLineTools/usr/lib/clang/9.0.0/include
 /Library/Developer/CommandLineTools/usr/include
 /usr/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)
End of search list.
In file included from connector.c:3:
/Users/buddha/github/chapel/runtime//include/chpl-init.h:34:33: error: unknown type name 'c_fn_ptr'
void chpl_execute_module_deinit(c_fn_ptr deinitFun);
                                ^
connector.c:8:24: warning: passing 'const char **' to parameter of type 'char **' discards qualifiers in nested pointer types
      [-Wincompatible-pointer-types-discards-qualifiers]
  chpl_library_init(0, &dummyArg0);
                       ^~~~~~~~~~
/Users/buddha/github/chapel/runtime//include/chpl-init.h:36:40: note: passing argument to parameter 'argv' here
void chpl_library_init(int argc, char* argv[]);
                                       ^
1 warning and 1 error generated.
make: *** [connector] Error 1

Closing in favor of #9290, which has more information on the halt stuff (and focuses on it instead of considering multiple issues)

Was this page helpful?
0 / 5 - 0 ratings