Subject: bin/23429: systrace has problems with threaded apps
To: None <gnats-bugs@gnats.netbsd.org>
From: Christian Biere <christianbiere@gmx.de>
List: netbsd-bugs
Date: 11/14/2003 02:46:50
>Number:         23429
>Category:       bin
>Synopsis:       systrace has problems with threaded apps
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    bin-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Fri Nov 14 01:47:00 UTC 2003
>Closed-Date:
>Last-Modified:
>Originator:     Christian Biere
>Release:        NetBSD 1.6ZE
>Organization:
>Environment:
System:
NetBSD cyclonus 1.6ZE NetBSD 1.6ZE (STARSCREAM) #0: Sun Nov 9 23:53:01
CET 2003 bin@cyclonus:/usr/build/sys/arch/i386/compile/STARSCREAM i386
Architecture: i386
Machine: i386

>Description:
systrace seems to have a problem creating automatic policies (with -A)
for threaded applications.

I see ogg123 fail from time to time with assertions related to pthreads.
Wondering whether this is a bug in ogg123 or NetBSD's pthreads, I've
stumbled accross bugreport kern/23266:

	http://news.gw.com/netbsd.bugs/25991

Note, that I don't use USE_TOPDOWN_VM for my kernel - it's mostly a
GENERIC kernel with much unnecessary stuff removed. I've tried the
program and it worked without any problem. Anyway, then I tried to
systrace the program and discovered a problem which causes the same
assertion to fail.
I'm not sure whether the category for this PR is correct. It might be
a bug in the kernel, libpthreads or /bin/systrace. 

>How-To-Repeat:

$ systrace -Ad /tmp ./thread_test
Hit some keys and wonder why nothing happens. Abort with CTRL+C or the
like. Try again several times until you get this:

thread 1
assertion "t->pt_type != PT_THREAD_UPCALL" failed: file
"/usr/build/src/lib/libpthread/pthread_sa.c", line 110, function
"pthread__upcall"[...]
assertion "t->pt_type != PT_THREAD_UPCALL" failed: file
"/usr/build/src/lib/libpthread/pthread_sa.c", line 110, function
"pthread__upcall" assertion "t->pt_type != PT_THREAD_UPCALL" failed:
file "/usr/build/src/lib/libpthread/pthread_sa.c", line 110, function
"pthread__upcall" Abort trap

Well, after that the program seems to work fine. systrace seems to fail
at adding further necessary syscalls to the policy during the first
few invocations. You can see that by looking at the created policy file
after each invokation.
Here's a copy of the source code used to demonstrate kern/23266: 

#include <stdio.h>
#include <pthread.h>

void thread_func(int x)
{
    printf("thread %d\n", x);
    getchar();
}

int main()
{
    pthread_t t1;

    pthread_create(&t1, NULL, (void *)thread_func, (void *)1);
    pthread_join(t1, NULL);

    return (0);
}

Compiled with:
gcc -Wall -lpthread -o thread_test thread_test.c

>Fix:
>Release-Note:
>Audit-Trail:
>Unformatted: