From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qt1-x82d.google.com (mail-qt1-x82d.google.com [IPv6:2607:f8b0:4864:20::82d]) by sourceware.org (Postfix) with ESMTPS id 1942D3858408 for ; Mon, 20 Dec 2021 15:01:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 1942D3858408 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-qt1-x82d.google.com with SMTP id j17so9994177qtx.2 for ; Mon, 20 Dec 2021 07:01:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:in-reply-to:date:message-id:mime-version; bh=287Klnc/dmMkotYSbYsqFYRRr20wdbDh7+m1+zQg4Oc=; b=lpRoifO5the9xCaN7gtOcG3XM9Dc3vb6IPWkTcjxIC2zXwWr4yRS46vzPPfnLzQS/e 2FMKDL67/lhCWZOrNO++I0s52jXDpf6o7B1Tclaios1zbaY4fBb/9AXzHJOVOJxmxCht ogCrHM5+EjDCdbAVyii9iYRJ9fr7DBpq4+oQxCvdCH/DZi0SadEQL4Igx/N665im4xTr zWFUvCaVrHFM4mNqz5rYKGhHzsMNANCGd/29kKLMVocrT7SpQwc66ja7glqhZpEdL168 1iDgH1gzs3hYJLgDH5D0QYsLAqrT0EU1d0yqTI33EEaPuKbKDGIa8nJzYZfvYvA4h8au yLXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:in-reply-to:date:message-id :mime-version; bh=287Klnc/dmMkotYSbYsqFYRRr20wdbDh7+m1+zQg4Oc=; b=N3M2mB0GModCaOYxX4zP0YQim07ofv1LOG7B95CUbw/j+LVCCawQXpqM+gX5aWE67+ vnjG/7ARqFg8fuQVuDIqCPAp+cqyLMol+I0DvKEfzUE45nTRyjtwInFLgrWO2AwaAXFy Z9OEDG7f7IqUkeHFeZcxU4nyOqzw289Lg7E73eJD4AiT1W+vIHK8w6gYgB64WVvqzkgs u2uGLbG/uyuvWlxHYCYXfSHp+a2MtrnNqBxILEPeQWilsuJD3HL4TaohgsVBEMvaaKVZ yZG95lV76lQef+vDk5jNPEvJGHRElsNDHHZQKDf5k6+c88cuuY+jomZuIoK4ip7uSs3z nPNQ== X-Gm-Message-State: AOAM533krCWlaeylgnZrp9UzUSirOAbmLFL4PqNXTgZ+pDIpHndGh1Z4 bp+8GCvKZgina/VDnEABk6oICPxUYuY= X-Google-Smtp-Source: ABdhPJx1idaz2X7aUkN4ATDbk1nm7g69jrkSEjOZB8zmqsuZiud92+j+aMepmLSc5Vnvl5QyNNmx3w== X-Received: by 2002:ac8:7f88:: with SMTP id z8mr12706291qtj.365.1640012499456; Mon, 20 Dec 2021 07:01:39 -0800 (PST) Received: from davidm-laptop (host-24-138-77-139.public.eastlink.ca. [24.138.77.139]) by smtp.gmail.com with ESMTPSA id o1sm15168323qtw.24.2021.12.20.07.01.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Dec 2021 07:01:38 -0800 (PST) From: David McFarland To: cygwin-developers@cygwin.com Cc: corngood@gmail.com Subject: Re: Fibers and cygtls In-Reply-To: Date: Mon, 20 Dec 2021 11:01:36 -0400 Message-ID: MIME-Version: 1.0 Content-Type: text/plain X-Spam-Status: No, score=-3.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, WEIRD_PORT autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: cygwin-developers@cygwin.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Cygwin core component developers mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 20 Dec 2021 15:01:43 -0000 > > Bottom line is, before trying to use Windows fibers, or before letting > > boost use Windows functions on Cygwin (sigh), check what other POSIX or > > BSD systems provide to implement coroutines. If there's an API, let's > > implement this API in Cygwin and then use that from user space. > > There's also an implementation (ucontext) using makecontext, which may > be the best option. I'll try that next. I've been at this on-and-off since my last message. I tried the ucontext-based implementation of coroutines in boost, but unfortunately ucontext seems to break exceptions. The only reference I could find to this was: https://stackoverflow.com/questions/30725276/modifying-the-stack-on-windows-tib-and-exceptions > On a related note, I wondered how Cygwin dealt with this for ucontext. > The source here > http://szupervigyor.ddsi.hu/source/in/openjdk-6-6b18-1.8.13/cacao-0.99.4/src/vm/jit/i386/cygwin/ucontext.c > uses GetThreadContext/SetThreadContext to implement ucontext. However, > from experimentation I see that this also fails when an exception is > thrown from inside a new context. In fact the SetThreadContext call > doesn't even update the TIB block! This seems to match what I'm seeing. The boost unit tests fail with ucontext, seemingly because the exception handlers are wrong after switching contexts. They pass using fcontext, but only if I apply the TLS patches I shared here: https://cygwin.com/pipermail/cygwin-developers/2020-September/011970.html Here's a test program: test-context.cc: ===== #include #include #include static ucontext_t uctx_main, uctx_func1; #define handle_error(msg) \ do { perror(msg); exit(EXIT_FAILURE); } while (0) struct test_ex {}; static void func1(void) { try { printf("func1: throw\n"); throw test_ex(); } catch(test_ex const&) { printf("func1: caught\n"); } printf("func1: swapcontext(&uctx_func1, &uctx_main)\n"); if (swapcontext(&uctx_func1, &uctx_main) == -1) handle_error("swapcontext"); } const size_t stack_size = 16 * 1024; void test (char *stack) { if (getcontext(&uctx_func1) == -1) handle_error("getcontext"); uctx_func1.uc_stack.ss_sp = stack; uctx_func1.uc_stack.ss_size = stack_size; uctx_func1.uc_link = &uctx_main; makecontext(&uctx_func1, func1, 0); printf("main: swapcontext(&uctx_main, &uctx_func1)\n"); if (swapcontext(&uctx_main, &uctx_func1) == -1) handle_error("swapcontext"); } int main(int argc, char *argv[]) { char on_stack[stack_size]; test(on_stack); char *from_malloc = (char*)malloc(stack_size); test(from_malloc); printf("main: exiting\n"); exit(EXIT_SUCCESS); } ===== When the stack provided to makecontext is allocated on the main thread stack, it works, but when the stack is allocated via malloc, weird stuff happens: When run from a shell it just exits with 0 when the exception is thrown. This is of particular concern because it may be causing e.g. boost unit tests to fail silently. With gdb attached (with or without cygwin-exceptions enabled): > func1: throw > gdb: unknown target exception 0x20474343 at 0x7ffca0da4f69 > > Thread 1 "test-context" received signal ?, Unknown signal. > 0x00007ffca0da4f69 in RaiseException () from /cygdrive/c/WINDOWS/System32/KERNELBASE.dll > (gdb) bt > #0 0x00007ffca0da4f69 in RaiseException () from /cygdrive/c/WINDOWS/System32/KERNELBASE.dll > #1 0x00000003ffabcca1 in _Unwind_RaiseException (exc=) at /usr/src/debug/gcc-11.2.0-1/libgcc/unwind-seh.c:334 > #2 0x00000003fd067f1b in __cxxabiv1::__cxa_throw (obj=, tinfo=0x1004030c0 <__fu0__ZTVN10__cxxabiv117__class_type_infoE>, dest=0x0) at /usr/src/debug/gcc-11.2.0-1/libstdc++-v3/libsupc++/eh_throw.cc:90 > #3 0x00000001004010b9 in func1 () at test-context.cc:15 > #4 0x0000000180068fac in swapcontext (oucp=0x0, ucp=0x0) at ../../../../winsup/cygwin/exceptions.cc:1935 > #5 0x0000000100407000 in ?? () > #6 0x0000000000000000 in ?? () > Backtrace stopped: previous frame inner to this frame (corrupt stack?) > (gdb) c > Continuing. > [Thread 11348.0x2fa0 exited with code 541541187] > [Thread 11348.0x2bec exited with code 541541187] > [Thread 11348.0xd50 exited with code 541541187] > [Thread 11348.0xacc exited with code 541541187] > > Program terminated with signal ?, Unknown signal. > The program no longer exists. With strace: > func1: throw > 680 409603 [main] test-context 1515 fhandler_console::write: 13 = fhandler_console::write(...) > 1123 410726 [main] test-context 1515 write: 13 = write(1, 0x80005C2B0, 13) > --- Process 1724 (pid: 1515) thread 5548 exited with status 0x20474343 > --- Process 1724 (pid: 1515) thread 10792 exited with status 0x20474343 > --- Process 1724 (pid: 1515) thread 2568 exited with status 0x20474343 > --- Process 1724 (pid: 1515) thread 12124 exited with status 0x20474343 > --- Process 1724 (pid: 1515) exited with status 0x20474343 I assume this has something to do with the stack still being in the valid range in the TIB? My goal here is to get nix working on cygwin. It depends on boost coroutines, which can use ucontext, but it uses malloc to allocate heaps by default. Anyone have any thoughts on how I could proceed? For now I'll keep digging into exception handling and context switching. Cheers, Dave