Skip to content

[Regression] SIGSEGV on 1.6.2 on C function call, works on 1.0.0 up to 1.6.0  #19378

@arkanoid87

Description

@arkanoid87

Forum post:

https://forum.nim-lang.org/t/8796

Repo with minimal test case and C version to compare

https://github.com/arkanoid87/futhest

Re-posting forum thread here:

I've been scratching my head solving a SIGSEGV on my machine when using futhark. I've reduced the problem to a self contained Nim and C files with no external nim packages involved, just plain C linkage.

The C program compiles and runs correctly, the Nim program goes SIGSEGV while calling C function, but they should be 1:1

You can find dockerfiles for reproducible builds and Makefile here: https://github.com/arkanoid87/futhest.

shared header.h

int addition(int num1, int num2) {
    return num1+num2;
}

futhest.c

#include <assert.h>
#include <stdio.h>
#include "clang-c/Index.h"


enum CXChildVisitResult
visitor(CXCursor cursor, CXCursor parent, CXClientData clientData) {
    CXFile file;
    unsigned int line;
    unsigned int column;
    unsigned int offset;

    CXSourceLocation loc = clang_getCursorLocation(cursor);
    clang_getFileLocation(loc, &file, &line, &column, &offset);

    CXString filename = clang_getFileName(file);   
    printf("%s [%d:%d, %d]\n", clang_getCString(filename), line, column, offset);
    clang_disposeString(filename);

    return CXChildVisit_Continue;
}

int main (void) { 
    CXIndex Idx = clang_createIndex(0, 0);

    CXTranslationUnit TU = clang_parseTranslationUnit(Idx, 
        "src/header.h", NULL, 0, NULL, 0, 0);

    assert(TU != NULL);

    CXCursor cursor = clang_getTranslationUnitCursor(TU);

    clang_visitChildren(cursor, visitor, NULL);

    clang_disposeTranslationUnit(TU);

    printf("END");
}

C Output:

./futhest_c 
src/header.h [1:5, 4]
END⏎

futhest.nim

type
  CXIndex = pointer
  CXTranslationUnit = pointer
  CXCursor* {.bycopy.} = object
    kind*: byte
    xdata*: cint
    data*: array[3, pointer]

#[
CINDEX_LINKAGE CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
                                         int displayDiagnostics);
]#
proc createIndex*(excludeDeclarationsFromPCH: cint; displayDiagnostics: cint): CXIndex {.
    importc: "clang_createIndex", cdecl.}


#[
CINDEX_LINKAGE CXTranslationUnit
clang_parseTranslationUnit(CXIndex CIdx,
                           const char *source_filename,
                           const char *const *command_line_args,
                           int num_command_line_args,
                           struct CXUnsavedFile *unsaved_files,
                           unsigned num_unsaved_files,
                           unsigned options);
]#
proc parseTranslationUnit*(CIdx: CXIndex; source_filename: cstring;
                          command_line_args: cstringArray;
                          num_command_line_args: cint;
                          unsaved_files: pointer;
                          num_unsaved_files: cuint; options: cuint): CXTranslationUnit {.
    importc: "clang_parseTranslationUnit", cdecl.}


#[
  CINDEX_LINKAGE CXCursor clang_getTranslationUnitCursor(CXTranslationUnit);
]#
proc getTranslationUnitCursor*(a1: CXTranslationUnit): CXCursor {.
    importc: "clang_getTranslationUnitCursor", cdecl.}


#[
  CINDEX_LINKAGE void clang_disposeTranslationUnit(CXTranslationUnit);
]#
proc disposeTranslationUnit*(a1: CXTranslationUnit) {.importc: "clang_disposeTranslationUnit", cdecl.}


# ----------------------------------------------------------------------------


var
  index = createIndex(0, 0)
  unit = parseTranslationUnit(index, "src/header.h".cstring, nil, 0, nil, 0, 0)

doAssert not unit.isNil

var cursor = getTranslationUnitCursor(unit) # SIGSEGV

# never reached
disposeTranslationUnit(unit)

echo "END"

Nim output:

./futhest
Traceback (most recent call last)
/home/jack/nim/futhest/src/futhest.nim(57) futhest
SIGSEGV: Illegal storage access. (Attempt to read from nil?)
fish: “./futhest” terminated by signal SIGSEGV (Address boundary error)

Additional Information

  • 1.7.1 devel 9888a29: SIGSEGV
  • 1.6.2: SIGSEGV
  • 1.6.0: OK
  • 1.4.8: OK
  • 1.2.0: OK
  • 1.0.0: OK

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions