Bug 172073 - [WTF] Make ThreadMessage portable
Summary: [WTF] Make ThreadMessage portable
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: New Bugs (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Yusuke Suzuki
URL:
Keywords:
Depends on: 171865
Blocks:
  Show dependency treegraph
 
Reported: 2017-05-13 12:13 PDT by Yusuke Suzuki
Modified: 2017-06-11 17:47 PDT (History)
12 users (show)

See Also:


Attachments
Patch (17.55 KB, patch)
2017-05-13 12:15 PDT, Yusuke Suzuki
no flags Details | Formatted Diff | Diff
Patch (18.54 KB, patch)
2017-05-13 12:21 PDT, Yusuke Suzuki
no flags Details | Formatted Diff | Diff
Patch (18.54 KB, patch)
2017-05-13 12:22 PDT, Yusuke Suzuki
no flags Details | Formatted Diff | Diff
Patch (17.57 KB, patch)
2017-06-09 04:24 PDT, Yusuke Suzuki
no flags Details | Formatted Diff | Diff
Patch (16.21 KB, patch)
2017-06-11 10:05 PDT, Yusuke Suzuki
no flags Details | Formatted Diff | Diff
Patch (16.20 KB, patch)
2017-06-11 10:06 PDT, Yusuke Suzuki
keith_miller: review+
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Yusuke Suzuki 2017-05-13 12:13:59 PDT
[WTF] Make ThreadMessage portable
Comment 1 Yusuke Suzuki 2017-05-13 12:15:42 PDT
Created attachment 310023 [details]
Patch
Comment 2 Yusuke Suzuki 2017-05-13 12:21:33 PDT
Created attachment 310024 [details]
Patch
Comment 3 Yusuke Suzuki 2017-05-13 12:22:23 PDT
Created attachment 310025 [details]
Patch
Comment 4 Yusuke Suzuki 2017-05-13 12:23:30 PDT
Interesting thing is suspending / resuming threads is more portable than emitting signals :)
Comment 5 Mark Lam 2017-05-15 11:03:35 PDT
Comment on attachment 310025 [details]
Patch

The mach exception patch has been rolled out.  Let's hold off on this patch until that gets resolved first.
Comment 6 Yusuke Suzuki 2017-06-09 04:24:46 PDT
Created attachment 312424 [details]
Patch
Comment 7 Yusuke Suzuki 2017-06-09 05:56:32 PDT
Now, the mach exception patch is landed.
I think this patch is ready to be reviewed.
Comment 8 Mark Lam 2017-06-11 08:52:12 PDT
Comment on attachment 312424 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=312424&action=review

Looks good in general but I have some questions and concerns.

> Source/JavaScriptCore/runtime/MachineContext.h:39
> +void*& stackPointer(PlatformRegisters&);

Why make this only specific to #if OS(WINDOWS) || HAVE(MACHINE_CONTEXT)?  What's the rationale?

I suspect that this will break the build for platform that are !(OS(WINDOWS) || HAVE(MACHINE_CONTEXT)).  For example, the VMTraps and SigillCrashAnalyzer's SignalContext constructor initializer lists use this function.  One option is to fix them to use the const PlatformRegister& form.  Another option is to disable VMTraps and SigillCrashAnalyzer for !(OS(WINDOWS) || HAVE(MACHINE_CONTEXT)).  I like the second option better.  What do you think?

> Source/JavaScriptCore/runtime/MachineContext.h:121
> +#else

Since this #else is so far away from its corresponding #if, can you add a comment like so:
#else // not OS(WINDOWS) || HAVE(MACHINE_CONTEXT)

> Source/JavaScriptCore/runtime/MachineContext.h:126
> +#endif

Ditto, add a comment:
#endif // OS(WINDOWS) || HAVE(MACHINE_CONTEXT)

> Source/WTF/wtf/ThreadingPthreads.cpp:438
> +#endif

nit: use a comment to pair with the #if please.
#endif // OS(DARWIN)

> Source/WTF/wtf/ThreadingPthreads.cpp:458
> +void Thread::setRegisters(const PlatformRegisters& registers)

I see this defined but not used anywhere.  What's the motivation for introducing this?
Comment 9 Mark Lam 2017-06-11 08:56:11 PDT
Comment on attachment 312424 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=312424&action=review

> Source/WTF/wtf/ThreadMessage.cpp:-201
> -
> -#endif // USE(PTHREADS)

The whole of ThreadMessage.h appears to be duplicated below when I click on the "100 above" link.  Can you make sure there's no duplication?
Comment 10 Mark Lam 2017-06-11 09:04:13 PDT
Comment on attachment 312424 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=312424&action=review

Some of my comments seemed to have just disappeared.  I'll try to reconstruct them as much as possible.

> Source/WTF/wtf/ThreadingPthreads.cpp:172
> +#if HAVE(MACHINE_CONTEXT)
> +    registersFromUContext(userContext) = thread->m_platformRegisters;
> +#endif

Is it reasonable to expect HAVE(MACHINE_CONTEXT) for pthreads?  If so, should we add a:

#else
#error "Unable to modify platform registers because !HAVE(MACHINE_CONTEXT)

This way, there's no silent failure catching anyone by surprise.

> Source/WTF/wtf/ThreadingPthreads.cpp:465
> +    ASSERT_WITH_MESSAGE(m_suspendCount, "We can get registers only if the thread is suspended.");

typo: /get/set/.
Comment 11 Yusuke Suzuki 2017-06-11 09:54:10 PDT
Comment on attachment 312424 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=312424&action=review

Thanks!

>> Source/JavaScriptCore/runtime/MachineContext.h:39
>> +void*& stackPointer(PlatformRegisters&);
> 
> Why make this only specific to #if OS(WINDOWS) || HAVE(MACHINE_CONTEXT)?  What's the rationale?
> 
> I suspect that this will break the build for platform that are !(OS(WINDOWS) || HAVE(MACHINE_CONTEXT)).  For example, the VMTraps and SigillCrashAnalyzer's SignalContext constructor initializer lists use this function.  One option is to fix them to use the const PlatformRegister& form.  Another option is to disable VMTraps and SigillCrashAnalyzer for !(OS(WINDOWS) || HAVE(MACHINE_CONTEXT)).  I like the second option better.  What do you think?

We still have `void* stackPointer(const PlatformRegisters&);`. `void*& stackPointer(PlatformRegisters&);` is prepared to perform the operation like,

MachineContext::stackPointer(registers) = newValue;

But the above thing should be effective only in `OS(WINDOWS) || HAVE(MACHINE_CONTEXT)` environment. HAVE(MACHINE_CONTEXT) means that we know how to modify MachineContext.
If HAVE(MACHINE_CONTEXT) is false (and OS(WINDOWS) is false), our PlatformRegister just has approximate stack pointer. Modifying it by using the above form does not have any meaningful effect.

So I think we should not expose `void*& stackPointer(PlatformRegisters&);` function in `OS(WINDOWS) || HAVE(MACHINE_CONTEXT)` environment.

VMTrap depends on HAVE(MACHINE_CONTEXT). And SigillAnalyzer is only enabled in Darwin environment right now. So I think this change does not cause build failures.

>> Source/JavaScriptCore/runtime/MachineContext.h:121
>> +#else
> 
> Since this #else is so far away from its corresponding #if, can you add a comment like so:
> #else // not OS(WINDOWS) || HAVE(MACHINE_CONTEXT)

Nice, fixed.

>> Source/JavaScriptCore/runtime/MachineContext.h:126
>> +#endif
> 
> Ditto, add a comment:
> #endif // OS(WINDOWS) || HAVE(MACHINE_CONTEXT)

Done.

>> Source/WTF/wtf/ThreadMessage.cpp:-201
>> -#endif // USE(PTHREADS)
> 
> The whole of ThreadMessage.h appears to be duplicated below when I click on the "100 above" link.  Can you make sure there's no duplication?

Oh, that's strange... They are removed completely. I hope the updated patch's diff does not show it.

>> Source/WTF/wtf/ThreadingPthreads.cpp:172
>> +#endif
> 
> Is it reasonable to expect HAVE(MACHINE_CONTEXT) for pthreads?  If so, should we add a:
> 
> #else
> #error "Unable to modify platform registers because !HAVE(MACHINE_CONTEXT)
> 
> This way, there's no silent failure catching anyone by surprise.

Or, now, we do not use setRegister(). So, this functionality is not necessary.
We can drop this feature simply. Dropped this phase.

BTW, HAVE(MACHINE_CONTEXT) can be false in USE(PTHREAD) environment if we do not know much about machine context internals.
For example, on non-JIT-supported ISA, we do not have accessors for machine context. In this case, HAVE(MACHINE_CONTEXT) becomes false.

>> Source/WTF/wtf/ThreadingPthreads.cpp:438
>> +#endif
> 
> nit: use a comment to pair with the #if please.
> #endif // OS(DARWIN)

Fixed.

>> Source/WTF/wtf/ThreadingPthreads.cpp:458
>> +void Thread::setRegisters(const PlatformRegisters& registers)
> 
> I see this defined but not used anywhere.  What's the motivation for introducing this?

This is originally introduced to be used in sendMessageUsingMach in the previous patch.
But now, the newly landed one does not include it. So, we can drop this implementation! :)
Dropped.

>> Source/WTF/wtf/ThreadingPthreads.cpp:465
>> +    ASSERT_WITH_MESSAGE(m_suspendCount, "We can get registers only if the thread is suspended.");
> 
> typo: /get/set/.

Fixed.
Comment 12 Yusuke Suzuki 2017-06-11 10:05:08 PDT
Created attachment 312605 [details]
Patch
Comment 13 Yusuke Suzuki 2017-06-11 10:06:22 PDT
Created attachment 312607 [details]
Patch
Comment 14 Keith Miller 2017-06-11 14:11:47 PDT
Comment on attachment 312607 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=312607&action=review

r=me with comment.

> Source/WTF/wtf/ThreadingPthreads.cpp:402
> +struct ThreadStateOption {

What's the reasoning behind calling this ThreadStateOption? What do you think about calling this ThreadStateMetadata?
Comment 15 Yusuke Suzuki 2017-06-11 17:40:42 PDT
Comment on attachment 312607 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=312607&action=review

>> Source/WTF/wtf/ThreadingPthreads.cpp:402
>> +struct ThreadStateOption {
> 
> What's the reasoning behind calling this ThreadStateOption? What do you think about calling this ThreadStateMetadata?

It's fine. Renamed :)
Comment 16 Yusuke Suzuki 2017-06-11 17:47:27 PDT
Committed r218080: <http://trac.webkit.org/changeset/218080>