Thread overview
question about mecca fiber with address sanitizer
Dec 13, 2020
Calvin P
Dec 13, 2020
kinke
Dec 14, 2020
Calvin P
Dec 14, 2020
Johan
Dec 14, 2020
Johan
Dec 15, 2020
Calvin P
Dec 15, 2020
Johan
Dec 15, 2020
Calvin P
December 13, 2020
I try use Mecca style fiber with address sanitizer, get this question:


1) Mecca fiber use switchTo, not like DRuntime use switchIn and switchOut
2) __sanitizer_start_switch_fiber need a pointer to the bottom of the destination stack and its size.


My question is when Mecca fiber switchTo into main thread, how to get the main thread stackBottom and stackSize,  there is a getStackBottom from https://wiki.dlang.org/LDC_inline_IR, but only work for windows. I need a solution for linux and macOS.

I appreciate for any kind tips for my problem.
December 13, 2020
On Sunday, 13 December 2020 at 16:17:16 UTC, Calvin P wrote:
> how to get the main thread stackBottom and stackSize,  there is a getStackBottom from https://wiki.dlang.org/LDC_inline_IR, but only work for windows. I need a solution for linux and macOS.

druntime should contain all of that already, see getStack{Bottom,Top} in core.thread.osthread.
December 14, 2020
On Sunday, 13 December 2020 at 22:00:42 UTC, kinke wrote:
> On Sunday, 13 December 2020 at 16:17:16 UTC, Calvin P wrote:
>> how to get the main thread stackBottom and stackSize,  there is a getStackBottom from https://wiki.dlang.org/LDC_inline_IR, but only work for windows. I need a solution for linux and macOS.
>
> druntime should contain all of that already, see getStack{Bottom,Top} in core.thread.osthread.

Thanks very much, I should look source code more hard.


Base on document: https://github.com/llvm-mirror/compiler-rt/blob/master/include/sanitizer/common_interface_defs.h#L313,  NULL fake stack pointer pass to __sanitizer_start_switch_fiber destroy the current fake stack.

https://github.com/ldc-developers/druntime/blob/ldc/src/core/thread/fiber.d#L1929,  the NULL is pass to __sanitizer_finish_switch_fiber.


If I am correct, DRuntime will never destroy the fake stack?






December 14, 2020
On Monday, 14 December 2020 at 07:30:10 UTC, Calvin P wrote:
>
> Base on document: https://github.com/llvm-mirror/compiler-rt/blob/master/include/sanitizer/common_interface_defs.h#L313,  NULL fake stack pointer pass to __sanitizer_start_switch_fiber destroy the current fake stack.
>
> https://github.com/ldc-developers/druntime/blob/ldc/src/core/thread/fiber.d#L1929,  the NULL is pass to __sanitizer_finish_switch_fiber.
>
>
> If I am correct, DRuntime will never destroy the fake stack?

Great that you are working on this!

Perhaps you're right.
On this line
https://github.com/ldc-developers/druntime/blob/a261a84e4c6b208c65ae39da0ae844ae3dd06744/src/core/thread/fiber.d#L1971 , perhaps the first argument should be `(m_state == State.TERM) ? null : &__fake_stack`


I worked on this, but I don't remember all the details any more. I recommend making testcases and carefully check what is working / what is not. We can add such tests to our testsuite. See for example: https://github.com/ldc-developers/ldc/blob/master/tests/sanitizers/asan_fiber.d

cheers,
  Johan






December 14, 2020
On Monday, 14 December 2020 at 19:09:16 UTC, Johan wrote:
>
> I worked on this, but I don't remember all the details any more. I recommend making testcases and carefully check what is working / what is not. We can add such tests to our testsuite. See for example: https://github.com/ldc-developers/ldc/blob/master/tests/sanitizers/asan_fiber.d

A testcase with FakeStake enabled:
https://github.com/ldc-developers/ldc/blob/master/tests/sanitizers/asan_fiber_main.d


December 15, 2020
On Monday, 14 December 2020 at 19:10:41 UTC, Johan wrote:
> On Monday, 14 December 2020 at 19:09:16 UTC, Johan wrote:
>>
>> I worked on this, but I don't remember all the details any more. I recommend making testcases and carefully check what is working / what is not. We can add such tests to our testsuite. See for example: https://github.com/ldc-developers/ldc/blob/master/tests/sanitizers/asan_fiber.d
>
> A testcase with FakeStake enabled:
> https://github.com/ldc-developers/ldc/blob/master/tests/sanitizers/asan_fiber_main.d

Hi @Johan, Thanks for the tips.

With export ASAN_OPTIONS=detect_stack_use_after_return=1,  I get this results for https://github.com/ldc-developers/ldc/blob/master/tests/sanitizers/asan_fiber.d.


On my Mecca style fiber, all my own unittest passed on macOS, but on linux I get wired error at random:

https://gist.github.com/InnotSagg/9217ebd4d9b50e9b77ae8ae24b6cc51b

The error line is on function declaration, only get with linux + nolto + norelease.

I am not sure it related with https://github.com/dlang/dmd/pull/12012,  this error stop my work for days and I have no clue.



December 15, 2020
On Monday, 14 December 2020 at 19:09:16 UTC, Johan wrote:
> On Monday, 14 December 2020 at 07:30:10 UTC, Calvin P wrote:
> cheers,
>   Johan


My fiber is free too soon, add delay to free fix the problem.
December 15, 2020
On Tuesday, 15 December 2020 at 04:42:41 UTC, Calvin P wrote:
> On Monday, 14 December 2020 at 19:10:41 UTC, Johan wrote:
>> On Monday, 14 December 2020 at 19:09:16 UTC, Johan wrote:
>>>
>>> I worked on this, but I don't remember all the details any more. I recommend making testcases and carefully check what is working / what is not. We can add such tests to our testsuite. See for example: https://github.com/ldc-developers/ldc/blob/master/tests/sanitizers/asan_fiber.d
>>
>> A testcase with FakeStake enabled:
>> https://github.com/ldc-developers/ldc/blob/master/tests/sanitizers/asan_fiber_main.d
>
> Hi @Johan, Thanks for the tips.
>
> With export ASAN_OPTIONS=detect_stack_use_after_return=1,  I get this results for https://github.com/ldc-developers/ldc/blob/master/tests/sanitizers/asan_fiber.d.
>
>
> On my Mecca style fiber, all my own unittest passed on macOS, but on linux I get wired error at random:
>
> https://gist.github.com/InnotSagg/9217ebd4d9b50e9b77ae8ae24b6cc51b

It's not clear to me. asan_fiber.d is supposed to crash with a stack overflow error, so seems to work fine to me.

-Johan