Skip to content

[MSP430] Initial fixes for built-ins on MSP430 #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 18 commits into from

Conversation

atrosinenko
Copy link
Contributor

@atrosinenko atrosinenko commented May 6, 2020

Here are mostly guessed fixes to compiler-rt that make it compile for MSP430 target. These are definitely WIP at the time of writing.

It can be compiled with something like

llvm_build=/path/to/llvm/build-directory
sysroot=/path/to/msp430gcc

cflags="-target msp430 -mmcu=msp430g2553 --gcc-toolchain=$sysroot -I$sysroot/include -L$sysroot/include"

cmake "$@" ../compiler-rt -G Ninja \
    -DCOMPILER_RT_DEFAULT_TARGET_TRIPLE="msp430-none-elf" \
    -DCOMPILER_RT_EMULATOR="$sysroot/bin/msp430-elf-run" \
    -DCOMPILER_RT_INCLUDE_TESTS=ON \
    -DCOMPILER_RT_BUILD_BUILTINS=ON \
    -DCMAKE_C_COMPILER=$llvm_build/bin/clang \
    -DCMAKE_CXX_COMPILER=$llvm_build/bin/clang++ \
    -DCMAKE_AR=$llvm_build/bin/llvm-ar \
    -DCMAKE_NM=$llvm_build/bin/llvm-nm \
    -DCMAKE_RANLIB=$llvm_build/bin/llvm-ranlib \
    -DLLVM_CONFIG_PATH=$llvm_build/bin/llvm-config \
    -DCMAKE_C_FLAGS="$cflags" \
    -DCMAKE_CXX_FLAGS="$cflags" \
    -DCMAKE_ASM_FLAGS="$cflags" \
    -DCOMPILER_RT_BAREMETAL_BUILD=ON

Prior to #20 the __ELF__ macro have to be set as well.

@atrosinenko atrosinenko force-pushed the compiler-rt-msp430 branch from 111a0a5 to 2e2c7ac Compare May 8, 2020 15:49
@atrosinenko
Copy link
Contributor Author

I have managed to run most of compiler-rt unit tests via msp430-elf-run simulator (shipped with gdb, see updated PR description) when I forgot dropping some -lgcc from linker command line. :) Without -lgcc, there are obviously lots of unresolved references to __mspabi_XYZ. According to MSP430TargetLowering, most of them are just renamed libgcc symbols, so what is the preferred way to map these names?

@asl
Copy link
Contributor

asl commented May 8, 2020

Create an alias? Similar to ARM EABI functions?

@atrosinenko
Copy link
Contributor Author

Yes, I expected something similar to other targets. As I understand, sometimes it is defined just like the following:

COMPILER_RT_ABI di_int __muldi3(di_int a, di_int b) {
dwords x;
x.all = a;
dwords y;
y.all = b;
dwords r;
r.all = __muldsi3(x.s.low, y.s.low);
r.s.high += x.s.high * y.s.low + x.s.low * y.s.high;
return r.all;
}
#if defined(__ARM_EABI__)
COMPILER_RT_ALIAS(__muldi3, __aeabi_lmul)
#endif

at the end of common .c file (not taking into account native asm implementations naturally residing under 'builtins/arm/'). I would prefer to describe all these aliases in a single .c or .S file under builtins/msp430 instead, but this is probably against the policy of separate files for separate functions. On the other hand, spreading such tiny aliases across lots of files may be quite hard to review and may be not very maintainable.

Ideally, I expect some autogenerated file that would be aligned with MSP430TargetLowering by construction (still, I'm not sure it is possible in general case).

@asl
Copy link
Contributor

asl commented May 8, 2020

Just put into separate files :)

@atrosinenko
Copy link
Contributor Author

Just to not perform massive rewriting afterwards :) Do I get it right, separate files builtins/msp430/__mspabi_XYZ.S, each containing a standard copyright header and a couple of macroses aliasing it to existing generic implementation for now? (Except for some msp-specific routines, of course)

@asl
Copy link
Contributor

asl commented May 8, 2020

Ok. In msp430 directory

@atrosinenko
Copy link
Contributor Author

The problem with aliasing via msp430/*.S was that I cannot alias a symbol from another object file (this can be done, but with linker script, not just another mspabi-aliases.o...).

On the other hand, it seems not too handy to spread aliases for every new target across .c files with corresponding generic implementations. What if just add an ALIAS<defined function name> macro "invocation" for every libcall? Then only a single "generic" header needs an adjustment for every new target, while all target-specific aliases can reside in a single header, not cluttering every generic .c file.

@asl
Copy link
Contributor

asl commented May 12, 2020

Sorry, I do not follow.

@atrosinenko
Copy link
Contributor Author

While googling how to emit some symbol as an alias to another one, I have found some suggestions on using .set to_symbol, from_symbol. But, as I understood, there exist some generic issues for referencing a symbol not defined in the same object file (relocations don't work for that cases, at least generically, they say...) and wasting 4*renamed_symbol_count bytes for this (br original_symbol) may be not very good for such low-memory target.

@atrosinenko
Copy link
Contributor Author

So, there exist other possibilities: either the most trivial one - use assignments via a linker script or alias in the correct translation unit. Linker script may require some non-trivial building. On the other hand, aliasing in the generic C-sources are already used for ARM target. My proposed approach just make it possible to localize changes for new target support in a couple of files instead of spreading them over at most O(libcall count) files.

PS:

  1. My patch still have to touch every required libcall source file
  2. In the latest commit of this PR, there is only a single file manually patched as an example
  3. ARM aliases may probably be refactored that way if appropriate

@asl
Copy link
Contributor

asl commented May 12, 2020

We cannot use linker script in general. It may be custom or not used at all. So, may I suggest us to go simple for now and mimic what ARM EABI is doing?

@atrosinenko atrosinenko force-pushed the compiler-rt-msp430 branch from 2393552 to cb4c23b Compare May 12, 2020 18:46
@atrosinenko
Copy link
Contributor Author

Current check-builtins status:

Failing Tests (36):
  Builtins-msp430-linux :: absvdi2_test.c
  Builtins-msp430-linux :: absvsi2_test.c
  Builtins-msp430-linux :: addtf3_test.c
  Builtins-msp430-linux :: ashldi3_test.c
  Builtins-msp430-linux :: ashrdi3_test.c
  Builtins-msp430-linux :: clzsi2_test.c
  Builtins-msp430-linux :: compiler_rt_logb_test.c
  Builtins-msp430-linux :: compiler_rt_logbf_test.c
  Builtins-msp430-linux :: ctzsi2_test.c
  Builtins-msp430-linux :: divdc3_test.c
  Builtins-msp430-linux :: divdf3_test.c
  Builtins-msp430-linux :: divsc3_test.c
  Builtins-msp430-linux :: divsf3_test.c
  Builtins-msp430-linux :: divtc3_test.c
  Builtins-msp430-linux :: fixdfdi_test.c
  Builtins-msp430-linux :: fixsfdi_test.c
  Builtins-msp430-linux :: fixunsdfdi_test.c
  Builtins-msp430-linux :: fixunssfdi_test.c
  Builtins-msp430-linux :: floatdidf_test.c
  Builtins-msp430-linux :: floatdisf_test.c
  Builtins-msp430-linux :: floatundidf_test.c
  Builtins-msp430-linux :: floatundisf_test.c
  Builtins-msp430-linux :: lshrdi3_test.c
  Builtins-msp430-linux :: muldc3_test.c
  Builtins-msp430-linux :: mulodi4_test.c
  Builtins-msp430-linux :: mulsc3_test.c
  Builtins-msp430-linux :: mulvdi3_test.c
  Builtins-msp430-linux :: paritydi2_test.c
  Builtins-msp430-linux :: paritysi2_test.c
  Builtins-msp430-linux :: popcountdi2_test.c
  Builtins-msp430-linux :: popcountsi2_test.c
  Builtins-msp430-linux :: powidf2_test.c
  Builtins-msp430-linux :: powisf2_test.c
  Builtins-msp430-linux :: subtf3_test.c
  Builtins-msp430-linux :: truncdfsf2_test.c
  Builtins-msp430-linux :: udivmoddi4_test.c


Testing Time: 2.04s
  Unsupported Tests  : 95
  Expected Passes    : 71
  Expected Failures  :  1
  Unexpected Failures: 36

@asl
Copy link
Contributor

asl commented May 12, 2020

I would ignore all FP tests for now and try to understand the integer ones.

@atrosinenko
Copy link
Contributor Author

Not surprisingly, my draft implementations for msp430-specific shift LibCalls were wrong :)

After they were fixed:

  Unsupported Tests  : 95
  Expected Passes    : 82
  Expected Failures  :  1
  Unexpected Failures: 31

Specifically, the following tests were fixed:

  Builtins-msp430-linux :: ashldi3_test.c
  Builtins-msp430-linux :: ashrdi3_test.c
  Builtins-msp430-linux :: clzsi2_test.c
  Builtins-msp430-linux :: ctzsi2_test.c
  Builtins-msp430-linux :: lshrdi3_test.c

and 6 other were added (for these 6 shift LibCalls).

@atrosinenko
Copy link
Contributor Author

Shouldn't this be a part of COMPILER_RT_ABI?

According to MSP430 EABI, "6.3 Special Register Conventions for Helper Functions", there is only a small subset of __mspabi_* functions that operate on two 64-bit arguments - they use this optimization. So this is probably correct but slightly misleading: CallingConv::MSP430_BUILTIN is that "modified" convention and not the one used for every LibCall. It would probably be better to leave it as-is and add an explanation.

@asl
Copy link
Contributor

asl commented May 14, 2020

What will break if we'd use this CC for all builtins?

@atrosinenko
Copy link
Contributor Author

Right now, for example, I have adjusted MSP430CallingConv.td by adding

  CCIfCC<"CallingConv::MSP430_BUILTIN", CCPromoteToType<i64>>,

Still it is just guessed in many ways (but seems to ultimately work...), so there may exist more correct way to express this. I agree, using MSP430_BUILTIN for, well, all MSP430 builtins would make sense :)

But I'm not sure this can be trivially achieved without specially marking LibCalls that need such an adjustment. For example, part 6.3 lists "The following functions take 2 64-bit values". Among others, there is __mspabi_srlll function. But even in MSP430 EABI Table 10, it is declared as uint64 __mspabi_srlll(uint64 x, int16 n);, so one cannot just match on LibCall with two i64 arguments. This still may be achievable but it would require further investigations.

@atrosinenko
Copy link
Contributor Author

  Unsupported Tests  : 95
  Expected Passes    : 94
  Expected Failures  :  1
  Unexpected Failures: 22


// Returns: a / b

COMPILER_RT_ABI hi_int __divhi3(hi_int a, hi_int b) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have it msp430 specific for now?


// Returns: a % b

COMPILER_RT_ABI hi_int __modhi3(hi_int a, hi_int b) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have it msp430 specific?

@asl
Copy link
Contributor

asl commented May 14, 2020

Can we have *hi builtins move to msp430 folder, please?

@atrosinenko atrosinenko force-pushed the compiler-rt-msp430 branch from 963477c to 7abf2b7 Compare May 14, 2020 15:45
@atrosinenko
Copy link
Contributor Author

Can we have *hi builtins move to msp430 folder, please?

Done. I have left their names unchanged (that is, traditional compiler-rt name aliased to _mspabi*). As I understand, now they are "staging" but someday they may probably become part of 16-bit target support (and then be moved to common directory).

@atrosinenko
Copy link
Contributor Author

Failing Tests (11):
  Builtins-msp430-linux :: addtf3_test.c
  Builtins-msp430-linux :: comparedf2_test.c
  Builtins-msp430-linux :: compiler_rt_logbf_test.c
  Builtins-msp430-linux :: divdc3_test.c
  Builtins-msp430-linux :: divsc3_test.c
  Builtins-msp430-linux :: divtc3_test.c
  Builtins-msp430-linux :: muldc3_test.c
  Builtins-msp430-linux :: mulsc3_test.c
  Builtins-msp430-linux :: mulvdi3_test.c
  Builtins-msp430-linux :: subtf3_test.c
  Builtins-msp430-linux :: udivmoddi4_test.c

Testing Time: 9.08s
  Unsupported Tests  :  95
  Expected Passes    : 105
  Expected Failures  :   1
  Unexpected Failures:  11

@asl
Copy link
Contributor

asl commented May 15, 2020

I would concentrate on udivmoddi4_test.c

@atrosinenko
Copy link
Contributor Author

I would concentrate on udivmoddi4_test.c

Chances are, it will "just work" when I ultimately manage to link it. The problem is enabling optimization refuses to "just work", so I'm investigating it.

/old_ssd/ast/soft/msp430gcc/lib/gcc/msp430-elf/8.3.1/../../../../bin/msp430-elf-ld: /old_ssd/ast/msp430-projects/llvm-project/build-compiler-rt-msp430/test/builtins/Unit/MSP430LinuxConfig/Output/udivmoddi4_test.c.tmp section `.data' will not fit in region `RAM'
/old_ssd/ast/soft/msp430gcc/lib/gcc/msp430-elf/8.3.1/../../../../bin/msp430-elf-ld: section __interrupt_vector_31 loaded at [000000000000fffe,000000000000ffff] overlaps section .data loaded at [00000000000049a8,00000000000a58d5]
/old_ssd/ast/soft/msp430gcc/lib/gcc/msp430-elf/8.3.1/../../../../bin/msp430-elf-ld: region `RAM' overflowed by 616676 bytes
clang-11: error: msp430-elf-ld command failed with exit code 1 (use -v to see invocation)

@asl
Copy link
Contributor

asl commented May 15, 2020

Why is it so large? What is so special in it as compared to other tests?

This patch is mostly guessed and does not have any tests yet.
Current LLVM code relies on these registers being callee-saved for
msp430_builtin calling convention, too. The MSP430 EABI document does
not explicitly declare them as caller-saved in that case, as well.
atrosinenko pushed a commit that referenced this pull request Aug 25, 2020
When `Target::GetEntryPointAddress()` calls `exe_module->GetObjectFile()->GetEntryPointAddress()`, and the returned
`entry_addr` is valid, it can immediately be returned.

However, just before that, an `llvm::Error` value has been setup, but in this case it is not consumed before returning, like is done further below in the function.

In https://bugs.freebsd.org/248745 we got a bug report for this, where a very simple test case aborts and dumps core:

```
* thread #1, name = 'testcase', stop reason = breakpoint 1.1
    frame #0: 0x00000000002018d4 testcase`main(argc=1, argv=0x00007fffffffea18) at testcase.c:3:5
   1	int main(int argc, char *argv[])
   2	{
-> 3	    return 0;
   4	}
(lldb) p argc
Program aborted due to an unhandled Error:
Error value was Success. (Note: Success values must still be checked prior to being destroyed).

Thread 1 received signal SIGABRT, Aborted.
thr_kill () at thr_kill.S:3
3	thr_kill.S: No such file or directory.
(gdb) bt
#0  thr_kill () at thr_kill.S:3
#1  0x00000008049a0004 in __raise (s=6) at /usr/src/lib/libc/gen/raise.c:52
#2  0x0000000804916229 in abort () at /usr/src/lib/libc/stdlib/abort.c:67
#3  0x000000000451b5f5 in fatalUncheckedError () at /usr/src/contrib/llvm-project/llvm/lib/Support/Error.cpp:112
#4  0x00000000019cf008 in GetEntryPointAddress () at /usr/src/contrib/llvm-project/llvm/include/llvm/Support/Error.h:267
#5  0x0000000001bccbd8 in ConstructorSetup () at /usr/src/contrib/llvm-project/lldb/source/Target/ThreadPlanCallFunction.cpp:67
#6  0x0000000001bcd2c0 in ThreadPlanCallFunction () at /usr/src/contrib/llvm-project/lldb/source/Target/ThreadPlanCallFunction.cpp:114
#7  0x00000000020076d4 in InferiorCallMmap () at /usr/src/contrib/llvm-project/lldb/source/Plugins/Process/Utility/InferiorCallPOSIX.cpp:97
#8  0x0000000001f4be33 in DoAllocateMemory () at /usr/src/contrib/llvm-project/lldb/source/Plugins/Process/FreeBSD/ProcessFreeBSD.cpp:604
#9  0x0000000001fe51b9 in AllocatePage () at /usr/src/contrib/llvm-project/lldb/source/Target/Memory.cpp:347
#10 0x0000000001fe5385 in AllocateMemory () at /usr/src/contrib/llvm-project/lldb/source/Target/Memory.cpp:383
#11 0x0000000001974da2 in AllocateMemory () at /usr/src/contrib/llvm-project/lldb/source/Target/Process.cpp:2301
#12 CanJIT () at /usr/src/contrib/llvm-project/lldb/source/Target/Process.cpp:2331
#13 0x0000000001a1bf3d in Evaluate () at /usr/src/contrib/llvm-project/lldb/source/Expression/UserExpression.cpp:190
#14 0x00000000019ce7a2 in EvaluateExpression () at /usr/src/contrib/llvm-project/lldb/source/Target/Target.cpp:2372
#15 0x0000000001ad784c in EvaluateExpression () at /usr/src/contrib/llvm-project/lldb/source/Commands/CommandObjectExpression.cpp:414
#16 0x0000000001ad86ae in DoExecute () at /usr/src/contrib/llvm-project/lldb/source/Commands/CommandObjectExpression.cpp:646
#17 0x0000000001a5e3ed in Execute () at /usr/src/contrib/llvm-project/lldb/source/Interpreter/CommandObject.cpp:1003
#18 0x0000000001a6c4a3 in HandleCommand () at /usr/src/contrib/llvm-project/lldb/source/Interpreter/CommandInterpreter.cpp:1762
#19 0x0000000001a6f98c in IOHandlerInputComplete () at /usr/src/contrib/llvm-project/lldb/source/Interpreter/CommandInterpreter.cpp:2760
#20 0x0000000001a90b08 in Run () at /usr/src/contrib/llvm-project/lldb/source/Core/IOHandler.cpp:548
#21 0x00000000019a6c6a in ExecuteIOHandlers () at /usr/src/contrib/llvm-project/lldb/source/Core/Debugger.cpp:903
#22 0x0000000001a70337 in RunCommandInterpreter () at /usr/src/contrib/llvm-project/lldb/source/Interpreter/CommandInterpreter.cpp:2946
#23 0x0000000001d9d812 in RunCommandInterpreter () at /usr/src/contrib/llvm-project/lldb/source/API/SBDebugger.cpp:1169
#24 0x0000000001918be8 in MainLoop () at /usr/src/contrib/llvm-project/lldb/tools/driver/Driver.cpp:675
#25 0x000000000191a114 in main () at /usr/src/contrib/llvm-project/lldb/tools/driver/Driver.cpp:890```

Fix the incorrect error catch by only instantiating an `Error` object if it is necessary.

Reviewed By: JDevlieghere

Differential Revision: https://reviews.llvm.org/D86355
@asl asl deleted the branch master January 24, 2023 18:10
@asl asl closed this Jan 24, 2023
atrosinenko pushed a commit that referenced this pull request Nov 15, 2023
…tePluginObject

After llvm/llvm-project#68052 this function changed from returning
a nullptr with `return {};` to returning Expected and hitting `llvm_unreachable` before it could
do so.

I gather that we're never supposed to call this function, but on Windows we actually do call
this function because `interpreter->CreateScriptedProcessInterface()` returns
`ScriptedProcessInterface` not `ScriptedProcessPythonInterface`. Likely because
`target_sp->GetDebugger().GetScriptInterpreter()` also does not return a Python related class.

The previously XFAILed test crashed with:
```
 # .---command stderr------------
 # | PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
 # | Stack dump:
 # | 0.  Program arguments: c:\\users\\tcwg\\david.spickett\\build-llvm\\bin\\lldb-test.exe ir-memory-map C:\\Users\\tcwg\\david.spickett\\build-llvm\\tools\\lldb\\test\\Shell\\Expr\\Output\\TestIRMemoryMapWindows.test.tmp C:\\Users\\tcwg\\david.spickett\\llvm-project\\lldb\\test\\Shell\\Expr/Inputs/ir-memory-map-basic
 # | 1.  HandleCommand(command = "run")
 # | Exception Code: 0xC000001D
 # | #0 0x00007ff696b5f588 lldb_private::ScriptedProcessInterface::CreatePluginObject(class llvm::StringRef, class lldb_private::ExecutionContext &, class std::shared_ptr<class lldb_private::StructuredData::Dictionary>, class lldb_private::StructuredData::Generic *) C:\Users\tcwg\david.spickett\llvm-project\lldb\include\lldb\Interpreter\Interfaces\ScriptedProcessInterface.h:28:0
 # | #1 0x00007ff696b1d808 llvm::Expected<std::shared_ptr<lldb_private::StructuredData::Generic> >::operator bool C:\Users\tcwg\david.spickett\llvm-project\llvm\include\llvm\Support\Error.h:567:0
 # | #2 0x00007ff696b1d808 lldb_private::ScriptedProcess::ScriptedProcess(class std::shared_ptr<class lldb_private::Target>, class std::shared_ptr<class lldb_private::Listener>, class lldb_private::ScriptedMetadata const &, class lldb_private::Status &) C:\Users\tcwg\david.spickett\llvm-project\lldb\source\Plugins\Process\scripted\ScriptedProcess.cpp:115:0
 # | #3 0x00007ff696b1d124 std::shared_ptr<lldb_private::ScriptedProcess>::shared_ptr C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.35.32124\include\memory:1478:0
 # | #4 0x00007ff696b1d124 lldb_private::ScriptedProcess::CreateInstance(class std::shared_ptr<class lldb_private::Target>, class std::shared_ptr<class lldb_private::Listener>, class lldb_private::FileSpec const *, bool) C:\Users\tcwg\david.spickett\llvm-project\lldb\source\Plugins\Process\scripted\ScriptedProcess.cpp:61:0
 # | #5 0x00007ff69699c8f4 std::_Ptr_base<lldb_private::Process>::_Move_construct_from C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.35.32124\include\memory:1237:0
 # | #6 0x00007ff69699c8f4 std::shared_ptr<lldb_private::Process>::shared_ptr C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.35.32124\include\memory:1534:0
 # | #7 0x00007ff69699c8f4 std::shared_ptr<lldb_private::Process>::operator= C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.35.32124\include\memory:1594:0
 # | #8 0x00007ff69699c8f4 lldb_private::Process::FindPlugin(class std::shared_ptr<class lldb_private::Target>, class llvm::StringRef, class std::shared_ptr<class lldb_private::Listener>, class lldb_private::FileSpec const *, bool) C:\Users\tcwg\david.spickett\llvm-project\lldb\source\Target\Process.cpp:396:0
 # | #9 0x00007ff6969bd708 std::_Ptr_base<lldb_private::Process>::_Move_construct_from C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.35.32124\include\memory:1237:0
 # | #10 0x00007ff6969bd708 std::shared_ptr<lldb_private::Process>::shared_ptr C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.35.32124\include\memory:1534:0
 # | #11 0x00007ff6969bd708 std::shared_ptr<lldb_private::Process>::operator= C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.35.32124\include\memory:1594:0
 # | #12 0x00007ff6969bd708 lldb_private::Target::CreateProcess(class std::shared_ptr<class lldb_private::Listener>, class llvm::StringRef, class lldb_private::FileSpec const *, bool) C:\Users\tcwg\david.spickett\llvm-project\lldb\source\Target\Target.cpp:215:0
 # | #13 0x00007ff696b13af0 std::_Ptr_base<lldb_private::Process>::_Ptr_base C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.35.32124\include\memory:1230:0
 # | #14 0x00007ff696b13af0 std::shared_ptr<lldb_private::Process>::shared_ptr C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.35.32124\include\memory:1524:0
 # | #15 0x00007ff696b13af0 lldb_private::PlatformWindows::DebugProcess(class lldb_private::ProcessLaunchInfo &, class lldb_private::Debugger &, class lldb_private::Target &, class lldb_private::Status &) C:\Users\tcwg\david.spickett\llvm-project\lldb\source\Plugins\Platform\Windows\PlatformWindows.cpp:495:0
 # | #16 0x00007ff6969cf590 std::_Ptr_base<lldb_private::Process>::_Move_construct_from C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.35.32124\include\memory:1237:0
 # | #17 0x00007ff6969cf590 std::shared_ptr<lldb_private::Process>::shared_ptr C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.35.32124\include\memory:1534:0
 # | #18 0x00007ff6969cf590 std::shared_ptr<lldb_private::Process>::operator= C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.35.32124\include\memory:1594:0
 # | #19 0x00007ff6969cf590 lldb_private::Target::Launch(class lldb_private::ProcessLaunchInfo &, class lldb_private::Stream *) C:\Users\tcwg\david.spickett\llvm-project\lldb\source\Target\Target.cpp:3274:0
 # | #20 0x00007ff696fff82c CommandObjectProcessLaunch::DoExecute(class lldb_private::Args &, class lldb_private::CommandReturnObject &) C:\Users\tcwg\david.spickett\llvm-project\lldb\source\Commands\CommandObjectProcess.cpp:258:0
 # | #21 0x00007ff696fab6c0 lldb_private::CommandObjectParsed::Execute(char const *, class lldb_private::CommandReturnObject &) C:\Users\tcwg\david.spickett\llvm-project\lldb\source\Interpreter\CommandObject.cpp:751:0
 # `-----------------------------
 # error: command failed with exit status: 0xc000001d
```

That might be a bug on the Windows side, or an artifact of how our build is setup,
but whatever it is, having `CreatePluginObject` return an error and
the caller check it, fixes the failing test.

The built lldb can run the script command to use Python, but I'm not sure if that means
anything.
kovdan01 pushed a commit that referenced this pull request Mar 14, 2024
TestCases/Misc/Linux/sigaction.cpp fails because dlsym() may call malloc
on failure. And then the wrapped malloc appears to access thread local
storage using global dynamic accesses, thus calling
___interceptor___tls_get_addr, before REAL(__tls_get_addr) has
been set, so we get a crash inside ___interceptor___tls_get_addr. For
example, this can happen when looking up __isoc23_scanf which might not
exist in some libcs.

Fix this by marking the thread local variable accessed inside the
debug checks as "initial-exec", which does not require __tls_get_addr.

This is probably a better alternative to llvm/llvm-project#83886.

This fixes a different crash but is related to llvm/llvm-project#46204.

Backtrace:
```
#0 0x0000000000000000 in ?? ()
#1 0x00007ffff6a9d89e in ___interceptor___tls_get_addr (arg=0x7ffff6b27be8) at /path/to/llvm/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp:2759
#2 0x00007ffff6a46bc6 in __sanitizer::CheckedMutex::LockImpl (this=0x7ffff6b27be8, pc=140737331846066) at /path/to/llvm/compiler-rt/lib/sanitizer_common/sanitizer_mutex.cpp:218
#3 0x00007ffff6a448b2 in __sanitizer::CheckedMutex::Lock (this=0x7ffff6b27be8, this@entry=0x730000000580) at /path/to/llvm/compiler-rt/lib/tsan/rtl/../../sanitizer_common/sanitizer_mutex.h:129
#4 __sanitizer::Mutex::Lock (this=0x7ffff6b27be8, this@entry=0x730000000580) at /path/to/llvm/compiler-rt/lib/tsan/rtl/../../sanitizer_common/sanitizer_mutex.h:167
#5 0x00007ffff6abdbb2 in __sanitizer::GenericScopedLock<__sanitizer::Mutex>::GenericScopedLock (mu=0x730000000580, this=<optimized out>) at /path/to/llvm/compiler-rt/lib/tsan/rtl/../../sanitizer_common/sanitizer_mutex.h:383
#6 __sanitizer::SizeClassAllocator64<__tsan::AP64>::GetFromAllocator (this=0x7ffff7487dc0 <__tsan::allocator_placeholder>, stat=stat@entry=0x7ffff570db68, class_id=11, chunks=chunks@entry=0x7ffff5702cc8, n_chunks=n_chunks@entry=128) at /path/to/llvm/compiler-rt/lib/tsan/rtl/../../sanitizer_common/sanitizer_allocator_primary64.h:207
#7 0x00007ffff6abdaa0 in __sanitizer::SizeClassAllocator64LocalCache<__sanitizer::SizeClassAllocator64<__tsan::AP64> >::Refill (this=<optimized out>, c=c@entry=0x7ffff5702cb8, allocator=<optimized out>, class_id=<optimized out>)
 at /path/to/llvm/compiler-rt/lib/tsan/rtl/../../sanitizer_common/sanitizer_allocator_local_cache.h:103
#8 0x00007ffff6abd731 in __sanitizer::SizeClassAllocator64LocalCache<__sanitizer::SizeClassAllocator64<__tsan::AP64> >::Allocate (this=0x7ffff6b27be8, allocator=0x7ffff5702cc8, class_id=140737311157448)
 at /path/to/llvm/compiler-rt/lib/tsan/rtl/../../sanitizer_common/sanitizer_allocator_local_cache.h:39
#9 0x00007ffff6abc397 in __sanitizer::CombinedAllocator<__sanitizer::SizeClassAllocator64<__tsan::AP64>, __sanitizer::LargeMmapAllocatorPtrArrayDynamic>::Allocate (this=0x7ffff5702cc8, cache=0x7ffff6b27be8, size=<optimized out>, size@entry=175, alignment=alignment@entry=16)
 at /path/to/llvm/compiler-rt/lib/tsan/rtl/../../sanitizer_common/sanitizer_allocator_combined.h:69
#10 0x00007ffff6abaa6a in __tsan::user_alloc_internal (thr=0x7ffff7ebd980, pc=140737331499943, sz=sz@entry=175, align=align@entry=16, signal=true) at /path/to/llvm/compiler-rt/lib/tsan/rtl/tsan_mman.cpp:198
#11 0x00007ffff6abb0d1 in __tsan::user_alloc (thr=0x7ffff6b27be8, pc=140737331846066, sz=11, sz@entry=175) at /path/to/llvm/compiler-rt/lib/tsan/rtl/tsan_mman.cpp:223
#12 0x00007ffff6a693b5 in ___interceptor_malloc (size=175) at /path/to/llvm/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp:666
#13 0x00007ffff7fce7f2 in malloc (size=175) at ../include/rtld-malloc.h:56
#14 __GI__dl_exception_create_format (exception=exception@entry=0x7fffffffd0d0, objname=0x7ffff7fc3550 "/path/to/llvm/compiler-rt/cmake-build-all-sanitizers/lib/linux/libclang_rt.tsan-x86_64.so",
 fmt=fmt@entry=0x7ffff7ff2db9 "undefined symbol: %s%s%s") at ./elf/dl-exception.c:157
#15 0x00007ffff7fd50e8 in _dl_lookup_symbol_x (undef_name=0x7ffff6af868b "__isoc23_scanf", undef_map=<optimized out>, ref=0x7fffffffd148, symbol_scope=<optimized out>, version=<optimized out>, type_class=0, flags=2, skip_map=0x7ffff7fc35e0) at ./elf/dl-lookup.c:793
--Type <RET> for more, q to quit, c to continue without paging--
#16 0x00007ffff656d6ed in do_sym (handle=<optimized out>, name=0x7ffff6af868b "__isoc23_scanf", who=0x7ffff6a3bb84 <__interception::InterceptFunction(char const*, unsigned long*, unsigned long, unsigned long)+36>, vers=vers@entry=0x0, flags=flags@entry=2) at ./elf/dl-sym.c:146
#17 0x00007ffff656d9dd in _dl_sym (handle=<optimized out>, name=<optimized out>, who=<optimized out>) at ./elf/dl-sym.c:195
#18 0x00007ffff64a2854 in dlsym_doit (a=a@entry=0x7fffffffd3b0) at ./dlfcn/dlsym.c:40
#19 0x00007ffff7fcc489 in __GI__dl_catch_exception (exception=exception@entry=0x7fffffffd310, operate=0x7ffff64a2840 <dlsym_doit>, args=0x7fffffffd3b0) at ./elf/dl-catch.c:237
#20 0x00007ffff7fcc5af in _dl_catch_error (objname=0x7fffffffd368, errstring=0x7fffffffd370, mallocedp=0x7fffffffd367, operate=<optimized out>, args=<optimized out>) at ./elf/dl-catch.c:256
#21 0x00007ffff64a2257 in _dlerror_run (operate=operate@entry=0x7ffff64a2840 <dlsym_doit>, args=args@entry=0x7fffffffd3b0) at ./dlfcn/dlerror.c:138
#22 0x00007ffff64a28e5 in dlsym_implementation (dl_caller=<optimized out>, name=<optimized out>, handle=<optimized out>) at ./dlfcn/dlsym.c:54
#23 ___dlsym (handle=<optimized out>, name=<optimized out>) at ./dlfcn/dlsym.c:68
#24 0x00007ffff6a3bb84 in __interception::GetFuncAddr (name=0x7ffff6af868b "__isoc23_scanf", trampoline=140737311157448) at /path/to/llvm/compiler-rt/lib/interception/interception_linux.cpp:42
#25 __interception::InterceptFunction (name=0x7ffff6af868b "__isoc23_scanf", ptr_to_real=0x7ffff74850e8 <__interception::real___isoc23_scanf>, func=11, trampoline=140737311157448)
 at /path/to/llvm/compiler-rt/lib/interception/interception_linux.cpp:61
#26 0x00007ffff6a9f2d9 in InitializeCommonInterceptors () at /path/to/llvm/compiler-rt/lib/tsan/rtl/../../sanitizer_common/sanitizer_common_interceptors.inc:10315
```

Reviewed By: vitalybuka, MaskRay

Pull Request: llvm/llvm-project#83890
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants