Hi,

I have the follow flex code using Boehm garbage collector.
http://www.hboehm.info/gc/gcinterface.html

The program is compiled with the following commands.

$ flex -o main.l.c main.l
$ clang -I. -DGC_DEBUG -Wall -pedantic -g -c -o main.l.o main.l.c #
rapidstring.h is in .
$ clang main.l.o -lgc -lfl -o main.l.exe
$ dsymutil main.l.exe

rapidstring.h can be downloaded here.

https://raw.githubusercontent.com/boyerjohn/rapidstring/master/include/rapidstring.h

The following commands show that without using valgrind, the program
runs OK. But if valgrind is used, the program will cause a
segmentation fault.

Is it because valgrind does not work with a garbage collector?

$ ./main.l.exe <<EOF
1a23b


456c
EOF
tok = 1000, yylval= 1
tok = 1001, yylval= a
tok = 1000, yylval= 23
tok = 1001, yylval= b
tok = 1000, yylval= 456
tok = 1001, yylval= c

$ valgrind ./main.l.exe <<EOF
1a23b


456c
EOF
==44938== Memcheck, a memory error detector
==44938== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==44938== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info
==44938== Command: ./main.l.exe
==44938==
==44938== Syscall param __pthread_sigmask(set) points to uninitialised byte(s)
==44938==    at 0x10068CB96: __pthread_sigmask (in
/usr/lib/system/libsystem_kernel.dylib)
==44938==    by 0x1006C3674: pthread_sigmask (in
/usr/lib/system/libsystem_pthread.dylib)
==44938==    by 0x1000D0225: GC_start_mark_threads_inner (in
/usr/local/Cellar/bdw-gc/8.0.2/lib/libgc.1.dylib)
==44938==    by 0x1000C0060: GC_init (in
/usr/local/Cellar/bdw-gc/8.0.2/lib/libgc.1.dylib)
==44938==    by 0x100003133: main (main.l:33)
==44938==  Address 0x1048a42e4 is on thread 1's stack
==44938==  in frame #2, created by GC_start_mark_threads_inner (???:)
==44938==
==44938== Thread 2:
==44938== Invalid read of size 4
==44938==    at 0x1006C35BA: _pthread_body (in
/usr/lib/system/libsystem_pthread.dylib)
==44938==    by 0x1006C350C: _pthread_start (in
/usr/lib/system/libsystem_pthread.dylib)
==44938==    by 0x1006C2BF8: thread_start (in
/usr/lib/system/libsystem_pthread.dylib)
==44938==  Address 0x18 is not stack'd, malloc'd or (recently) free'd
==44938==
==44938==
==44938== Process terminating with default action of signal 11 (SIGSEGV)
==44938==  Access not within mapped region at address 0x18
==44938==    at 0x1006C35BA: _pthread_body (in
/usr/lib/system/libsystem_pthread.dylib)
==44938==    by 0x1006C350C: _pthread_start (in
/usr/lib/system/libsystem_pthread.dylib)
==44938==    by 0x1006C2BF8: thread_start (in
/usr/lib/system/libsystem_pthread.dylib)
==44938==  If you believe this happened as a result of a stack
==44938==  overflow in your program's main thread (unlikely but
==44938==  possible), you can try to increase the size of the
==44938==  main thread stack using the --main-stacksize= flag.
==44938==  The main thread stack size used in this run was 8388608.
==44938==
==44938== HEAP SUMMARY:
==44938==     in use at exit: 19,932 bytes in 162 blocks
==44938==   total heap usage: 183 allocs, 21 frees, 28,380 bytes allocated
==44938==
==44938== LEAK SUMMARY:
==44938==    definitely lost: 0 bytes in 0 blocks
==44938==    indirectly lost: 2,064 bytes in 1 blocks
==44938==      possibly lost: 0 bytes in 0 blocks
==44938==    still reachable: 200 bytes in 6 blocks
==44938==         suppressed: 17,668 bytes in 155 blocks
==44938== Rerun with --leak-check=full to see details of leaked memory
==44938==
==44938== For counts of detected and suppressed errors, rerun with: -v
==44938== Use --track-origins=yes to see where uninitialised values come from
==44938== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 4 from 4)
Segmentation fault: 11



$ cat main.l
  // clang-format off
%{
        // clang-format on
#define TOK_NUMBER 1000
#define TOK_STRING 1001
#include <gc.h>
#define RS_MALLOC GC_MALLOC
#define RS_REALLOC GC_REALLOC
#define RS_FREE GC_FREE
#include <rapidstring.h>
        typedef struct {
                int num;
                rapidstring str;
        } YYSTYPE;
        // clang-format off
%}
%option nodefault noinput nounput
%option reentrant bison-bridge

%%
[[:digit:]]+                    yylval->num=atoi(yytext); return TOK_NUMBER;
[[:alpha:]]+                    {
        // clang-format on
        rs_cpy(&yylval->str, yytext);
        return TOK_STRING;
        // clang-format off
}
.|\n
%%

int main() {
        // clang-format on
        GC_INIT();

        yyscan_t scanner;
        yylex_init(&scanner);
        int tok;
        YYSTYPE lval;
        rs_init(&lval.str);
        while ((tok = yylex(&lval, scanner))) {
                if (tok == TOK_NUMBER) {
                        printf("tok = %d, yylval= %d\n", tok,
                               yyget_lval(scanner)->num);
                } else if (tok == TOK_STRING) {
                        printf("tok = %d, yylval= %s\n", tok,
                               rs_data(&yyget_lval(scanner)->str));
                }
        }
        yylex_destroy(scanner);
        return 0;
}

-- 
Regards,
Peng


_______________________________________________
Valgrind-users mailing list
Valgrind-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/valgrind-users

Reply via email to