[Sony Xperia XZ Premium ROMs, Kernels, Recoveries, ] LSS4181:
The bluetooth is indeed an issue on AEX's side (the GSI build was incomplete with the bluetooth stack left out, which caused some kind of drama on the respective thread). Don't know which GSI build is the best (fewer bugs, no major issue like AEX's missing Bluetooth stack). May give official RR GSI another try (though when I first flashed it, it didn't boot, probably because I might have to flash OEM binaries after flashing the Treble zip).

As for Magisk, I need not only root but also Magisk Hide (a recent game I've been playing detects root as well as USB debugging) as well as some other systemless features, to allow certain functionalities to persist across GSI updates (since they're unconditionally flashed via fastboot which would not honor addon.d, so I would have to reflash everything every update).

Either the issue is somewhere else or it's because only few people actually tried GSI, the Magisk bootloop issue has not been reported yet in Magisk's GitHub.

AVB 1.0 might be the culprit as back then when I built and flashed AOSP, the kernel image was also signed with AVB 1.0 and also bootlooped when I flashed Magisk. Magisk would sign the image back with "test keys", which probably don't work in our cases. That's why I've been asking if it's possible to build an unsigned image (that isn't AVB signed) and see if it makes a difference.

My previous device, ASUS Zenfone 2, had similar issues with Magisk. While it's not AVB 1.0, it additionally requires flashing a image signature patch zip, otherwise the phone would boot directly to recovery.

Normally, a rejected boot image (due to Magisk patches) would lead to the bootloader try booting the recovery. However, since Xperia devices don't really have a dedicated recovery partition (it's built into stock kernel), its bootloader probably doesn't have the logic to boot to recovery and thus it keeps booting the boot image (which keeps failing).

PS: Replacing the kernel zImage with the official TWRP 3.2.3 one (via Android Image Kitchen) onto your recovery (Twrp-Q-v2_1) allowed it to properly boot (I don't experience freezes like before this time). However, it now shows as TWRP 3.2.3 and the CPU temperature is the same weird number as the official one (kernel-related, I think). Did you build the new recovery with TWRP 3.2.3, perhaps? I couldn't really distinguish much until I actually saw the options to backup/flash OEM image and such (which I didn't recall seeing it available on official build, and I just successfully made a backup of the OEM image). Is that what you added in the new recovery build? Not sure about the original build, though (or whether the freeze issue has been fixed). (EDIT: It seems the new recovery image is already based on TWRP 3.2.3 and it seems to be having the same official TWRP traits (it no longer freezes), so that was unnecessary...

EDIT: It seems we either need to build an image without AVB (I don't think CarbonROM has AVB, or enforced it, since when flashing Magisk against it, it never mentioned about AVB), or find ways to disable AVB completely (in case the phone does require AVB by default on Pie and wouldn't boot without it, or is signed with different keys, which Magisk did, as it signs a different "test key").

EDIT 2: Trying to get AOSP building but I'm getting this error at the beginning of the build process, even after exporting LC_ALL=C (and LC_TIME=C because the former somehow didn't set everything), and run the build process in a bash shell as well as a Python2 build environment. I'm working it on Manjaro Linux (and could previously build CarbonROM with the help of ScriBt).

FAILED: out/soong/.intermediates/external/selinux/checkpolicy/checkpolicy/linux_glibc_x86_64/gen/lex/external/selinux/checkpolicy/policy_scan.c
prebuilts/misc/linux-x86/flex/flex-2.5.39 -oout/soong/.intermediates/external/selinux/checkpolicy/checkpolicy/linux_glibc_x86_64/gen/lex/external/selinux/checkpolicy/policy_scan.c external/selinux/checkpolicy/policy_scan.l
flex-2.5.39: loadlocale.c:130: _nl_intern_locale_data: Assertion `cnt < (sizeof (_nl_value_type_LC_TIME) / sizeof (_nl_value_type_LC_TIME[0]))' failed.

EDIT 3: OK... it seems LC_ALL=C doesn't work anymore on my distro. I ended up making the prebuilt flex binary a symlink to my system's own flex and, although dirty, it works... for now...

EDIT 4: At around 70% I'm having a LOT of errors... it seems for some reason I couldn't build AOSP correctly anymore...

FAILED: out/soong/.intermediates/frameworks/compile/mclinker/lib/Script/libmcldScript/android_arm64_armv8-a_cortex-a73_core_static/obj/.intermediates/frameworks/compile/mclinker/lib/Script/libmcldScript/android_arm64_armv8-a_cortex-a73_core_static/gen/lex/frameworks/compile/mclinker/lib/Script/ScriptScanner.o
PWD=/proc/self/cwd prebuilts/clang/host/linux-x86/clang-4691093/bin/clang++ -c -Iframeworks/compile/mclinker/include -Iframeworks/compile/mclinker/include/mcld/Script -Iframeworks/compile/mclinker/lib/Script -Werror=implicit-function-declaration -DANDROID -fmessage-length=0 -W -Wall -Wno-unused -Winit-self -Wpointer-arith -no-canonical-prefixes -DNDEBUG -UDEBUG -fno-exceptions -Wno-multichar -O2 -g -fno-strict-aliasing -fdebug-prefix-map=/proc/self/cwd= -D__compiler_offsetof=__builtin_offsetof -Werror=int-conversion -Wno-reserved-id-macro -Wno-format-pedantic -Wno-unused-command-line-argument -fcolor-diagnostics -Wno-expansion-to-defined -Wno-zero-as-null-pointer-constant -fdebug-prefix-map=$PWD/= -ffunction-sections -fdata-sections -fno-short-enums -funwind-tables -fstack-protector-strong -Wa,--noexecstack -D_FORTIFY_SOURCE=2 -Wstrict-aliasing=2 -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Werror=date-time -Werror=format-security -nostdlibinc -march=armv8-a -mcpu=cortex-a53 -Iout/soong/.intermediates/frameworks/compile/mclinker/lib/Script/libmcldScript/android_arm64_armv8-a_cortex-a73_core_static/gen/yacc/frameworks/compile/mclinker/lib/Script -Iexternal/llvm/include -Iexternal/llvm/device/include -Iout/soong/.intermediates/external/llvm/llvm-gen-attributes/gen -Iout/soong/.intermediates/external/llvm/llvm-gen-intrinsics/gen -Iexternal/libcxx/include -Iexternal/libcxxabi/include -Isystem/core/include -Isystem/media/audio/include -Ihardware/libhardware/include -Ihardware/libhardware_legacy/include -Ihardware/ril/include -Ilibnativehelper/include -Iframeworks/native/include -Iframeworks/native/opengl/include -Iframeworks/av/include -isystem bionic/libc/include -isystem bionic/libc/kernel/uapi -isystem bionic/libc/kernel/uapi/asm-arm64 -isystem bionic/libc/kernel/android/scsi -isystem bionic/libc/kernel/android/uapi -Ilibnativehelper/include_jni -D_GNU_SOURCE -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -fomit-frame-pointer -Wall -W -Wno-cast-qual -Wno-sign-compare -Wno-unused-parameter -Wno-missing-field-initializers -Wwrite-strings -Werror -Dsprintf=sprintf -Wno-error=unused-lambda-capture -Wno-error=user-defined-warnings -target aarch64-linux-android -Bprebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9/aarch64-linux-android/bin -DANDROID_STRICT -fPIC -D_USING_LIBCXX -std=gnu++14 -Wsign-promo -Wno-inconsistent-missing-override -Wno-null-dereference -D_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS -Wno-thread-safety-negative -Wno-gnu-include-next -fvisibility-inlines-hidden -Wno-sign-promo -std=c++11 -Wall -Wno-unused-parameter -Wno-unused-private-field -Wno-unused-const-variable -Werror -Woverloaded-virtual -fno-rtti -Werror=int-to-pointer-cast -Werror=pointer-to-int-cast -Werror=address-of-temporary -Werror=return-type -Wno-tautological-constant-compare -Wno-null-pointer-arithmetic -Wno-enum-compare -Wno-enum-compare-switch -MD -MF out/soong/.intermediates/frameworks/compile/mclinker/lib/Script/libmcldScript/android_arm64_armv8-a_cortex-a73_core_static/obj/.intermediates/frameworks/compile/mclinker/lib/Script/libmcldScript/android_arm64_armv8-a_cortex-a73_core_static/gen/lex/frameworks/compile/mclinker/lib/Script/ScriptScanner.o.d -o out/soong/.intermediates/frameworks/compile/mclinker/lib/Script/libmcldScript/android_arm64_armv8-a_cortex-a73_core_static/obj/.intermediates/frameworks/compile/mclinker/lib/Script/libmcldScript/android_arm64_armv8-a_cortex-a73_core_static/gen/lex/frameworks/compile/mclinker/lib/Script/ScriptScanner.o out/soong/.intermediates/frameworks/compile/mclinker/lib/Script/libmcldScript/android_arm64_armv8-a_cortex-a73_core_static/gen/lex/frameworks/compile/mclinker/lib/Script/ScriptScanner.cpp
out/soong/.intermediates/frameworks/compile/mclinker/lib/Script/libmcldScript/android_arm64_armv8-a_cortex-a73_core_static/gen/lex/frameworks/compile/mclinker/lib/Script/ScriptScanner.cpp:1487:8: error: member reference type 'std::istream *' (aka 'basic_istream<char> *') is a pointer; did you mean to use '->'?
yyin.rdbuf(std::cin.rdbuf());
~~~~^
->
out/soong/.intermediates/frameworks/compile/mclinker/lib/Script/libmcldScript/android_arm64_armv8-a_cortex-a73_core_static/gen/lex/frameworks/compile/mclinker/lib/Script/ScriptScanner.cpp:1490:9: error: member reference type 'std::ostream *' (aka 'basic_ostream<char> *') is a pointer; did you mean to use '->'?
yyout.rdbuf(std::cout.rdbuf());
~~~~~^
->
out/soong/.intermediates/frameworks/compile/mclinker/lib/Script/libmcldScript/android_arm64_armv8-a_cortex-a73_core_static/gen/lex/frameworks/compile/mclinker/lib/Script/ScriptScanner.cpp:2274:50: error: member reference type 'std::istream *' (aka 'basic_istream<char> *') is a pointer; did you mean to use '->'?
YY_CURRENT_BUFFER_LVALUE->yy_input_file = yyin.rdbuf();
~~~~^
->
out/soong/.intermediates/frameworks/compile/mclinker/lib/Script/libmcldScript/android_arm64_armv8-a_cortex-a73_core_static/gen/lex/frameworks/compile/mclinker/lib/Script/ScriptScanner.cpp:2389:2: error: cannot initialize a member subobject of type 'std::istream *' (aka 'basic_istream<char> *') with an rvalue of type 'basic_streambuf<std::__1::basic_ios<char, std::__1::char_traits<char> >::char_type, std::__1::basic_ios<char, std::__1::char_traits<char> >::traits_type> *' (aka 'basic_streambuf<char, std::__1::char_traits<char> > *')
yyin(arg_yyin ? arg_yyin->rdbuf() : std::cin.rdbuf()),
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
out/soong/.intermediates/frameworks/compile/mclinker/lib/Script/libmcldScript/android_arm64_armv8-a_cortex-a73_core_static/gen/lex/frameworks/compile/mclinker/lib/Script/ScriptScanner.cpp:2390:2: error: cannot initialize a member subobject of type 'std::ostream *' (aka 'basic_ostream<char> *') with an rvalue of type 'basic_streambuf<std::__1::basic_ios<char, std::__1::char_traits<char> >::char_type, std::__1::basic_ios<char, std::__1::char_traits<char> >::traits_type> *' (aka 'basic_streambuf<char, std::__1::char_traits<char> > *')
yyout(arg_yyout ? arg_yyout->rdbuf() : std::cout.rdbuf())
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
out/soong/.intermediates/frameworks/compile/mclinker/lib/Script/libmcldScript/android_arm64_armv8-a_cortex-a73_core_static/gen/lex/frameworks/compile/mclinker/lib/Script/ScriptScanner.cpp:2392:2: error: use of undeclared identifier 'ctor_common'
ctor_common();
^
out/soong/.intermediates/frameworks/compile/mclinker/lib/Script/libmcldScript/android_arm64_armv8-a_cortex-a73_core_static/gen/lex/frameworks/compile/mclinker/lib/Script/ScriptScanner.cpp:2397:14: error: out-of-line definition of 'yyFlexLexer' does not match any declaration in 'yyFlexLexer'
yyFlexLexer::yyFlexLexer( std::istream& arg_yyin, std::ostream& arg_yyout ):
^~~~~~~~~~~
out/soong/.intermediates/frameworks/compile/mclinker/lib/Script/libmcldScript/android_arm64_armv8-a_cortex-a73_core_static/gen/lex/frameworks/compile/mclinker/lib/Script/ScriptScanner.cpp:22:25: note: expanded from macro 'yyFlexLexer'
#define yyFlexLexer yyFlexLexer
^~~~~~~~~~~
out/soong/.intermediates/frameworks/compile/mclinker/lib/Script/libmcldScript/android_arm64_armv8-a_cortex-a73_core_static/gen/lex/frameworks/compile/mclinker/lib/Script/ScriptScanner.cpp:2388:27: note: type of 1st parameter of member declaration does not match definition ('std::istream *' (aka 'basic_istream<char> *') vs 'std::istream &' (aka 'basic_istream<char> &'))
yyFlexLexer::yyFlexLexer( std::istream* arg_yyin, std::ostream* arg_yyout ):
^
out/soong/.intermediates/frameworks/compile/mclinker/lib/Script/libmcldScript/android_arm64_armv8-a_cortex-a73_core_static/gen/lex/frameworks/compile/mclinker/lib/Script/ScriptScanner.cpp:2398:2: error: cannot initialize a member subobject of type 'std::istream *' (aka 'basic_istream<char> *') with an rvalue of type 'basic_streambuf<std::__1::basic_ios<char, std::__1::char_traits<char> >::char_type, std::__1::basic_ios<char, std::__1::char_traits<char> >::traits_type> *' (aka 'basic_streambuf<char, std::__1::char_traits<char> > *')
yyin(arg_yyin.rdbuf()),
^ ~~~~~~~~~~~~~~~~
out/soong/.intermediates/frameworks/compile/mclinker/lib/Script/libmcldScript/android_arm64_armv8-a_cortex-a73_core_static/gen/lex/frameworks/compile/mclinker/lib/Script/ScriptScanner.cpp:2399:2: error: cannot initialize a member subobject of type 'std::ostream *' (aka 'basic_ostream<char> *') with an rvalue of type 'basic_streambuf<std::__1::basic_ios<char, std::__1::char_traits<char> >::char_type, std::__1::basic_ios<char, std::__1::char_traits<char> >::traits_type> *' (aka 'basic_streambuf<char, std::__1::char_traits<char> > *')
yyout(arg_yyout.rdbuf())
^ ~~~~~~~~~~~~~~~~~
out/soong/.intermediates/frameworks/compile/mclinker/lib/Script/libmcldScript/android_arm64_armv8-a_cortex-a73_core_static/gen/lex/frameworks/compile/mclinker/lib/Script/ScriptScanner.cpp:2401:2: error: use of undeclared identifier 'ctor_common'
ctor_common();
^
out/soong/.intermediates/frameworks/compile/mclinker/lib/Script/libmcldScript/android_arm64_armv8-a_cortex-a73_core_static/gen/lex/frameworks/compile/mclinker/lib/Script/ScriptScanner.cpp:2406:19: error: out-of-line definition of 'ctor_common' does not match any declaration in 'yyFlexLexer'
void yyFlexLexer::ctor_common()
^~~~~~~~~~~
out/soong/.intermediates/frameworks/compile/mclinker/lib/Script/libmcldScript/android_arm64_armv8-a_cortex-a73_core_static/gen/lex/frameworks/compile/mclinker/lib/Script/ScriptScanner.cpp:2444:19: error: out-of-line definition of 'switch_streams' does not match any declaration in 'yyFlexLexer'
void yyFlexLexer::switch_streams( std::istream& new_in, std::ostream& new_out )
^~~~~~~~~~~~~~
frameworks/compile/mclinker/include/mcld/Script/FlexLexer.h:133:31: note: type of 1st parameter of member declaration does not match definition ('std::istream *' (aka 'basic_istream<char> *') vs 'std::istream &' (aka 'basic_istream<char> &'))
virtual void switch_streams(FLEX_STD istream* new_in,
^
frameworks/compile/mclinker/include/mcld/Script/FlexLexer.h:62:18: note: expanded from macro 'FLEX_STD'
#define FLEX_STD std::
^
out/soong/.intermediates/frameworks/compile/mclinker/lib/Script/libmcldScript/android_arm64_armv8-a_cortex-a73_core_static/gen/lex/frameworks/compile/mclinker/lib/Script/ScriptScanner.cpp:2448:41: error: no viable conversion from 'std::istream' (aka 'basic_istream<char>') to 'std::istream *' (aka 'basic_istream<char> *')
yy_switch_to_buffer( yy_create_buffer( new_in, YY_BUF_SIZE ) );
^~~~~~
frameworks/compile/mclinker/include/mcld/Script/FlexLexer.h:125:62: note: passing argument to parameter 's' here
struct yy_buffer_state* yy_create_buffer(FLEX_STD istream* s, int size);
^
out/soong/.intermediates/frameworks/compile/mclinker/lib/Script/libmcldScript/android_arm64_armv8-a_cortex-a73_core_static/gen/lex/frameworks/compile/mclinker/lib/Script/ScriptScanner.cpp:2451:7: error: member reference type 'std::ostream *' (aka 'basic_ostream<char> *') is a pointer; did you mean to use '->'?
yyout.rdbuf(new_out.rdbuf());
~~~~~^
->
out/soong/.intermediates/frameworks/compile/mclinker/lib/Script/libmcldScript/android_arm64_armv8-a_cortex-a73_core_static/gen/lex/frameworks/compile/mclinker/lib/Script/ScriptScanner.cpp:2459:12: error: assigning to 'std::istream *' (aka 'basic_istream<char> *') from incompatible type 'std::istream **' (aka 'basic_istream<char> **'); remove &
new_in = &yyin;
^~~~~
out/soong/.intermediates/frameworks/compile/mclinker/lib/Script/libmcldScript/android_arm64_armv8-a_cortex-a73_core_static/gen/lex/frameworks/compile/mclinker/lib/Script/ScriptScanner.cpp:2463:13: error: assigning to 'std::ostream *' (aka 'basic_ostream<char> *') from incompatible type 'std::ostream **' (aka 'basic_ostream<char> **'); remove &
new_out = &yyout;
^~~~~~
out/soong/.intermediates/frameworks/compile/mclinker/lib/Script/libmcldScript/android_arm64_armv8-a_cortex-a73_core_static/gen/lex/frameworks/compile/mclinker/lib/Script/ScriptScanner.cpp:2466:17: error: no viable conversion from 'std::istream' (aka 'basic_istream<char>') to 'std::istream *' (aka 'basic_istream<char> *')
switch_streams(*new_in, *new_out);
^~~~~~~
out/soong/.intermediates/frameworks/compile/mclinker/lib/Script/libmcldScript/android_arm64_armv8-a_cortex-a73_core_static/gen/lex/frameworks/compile/mclinker/lib/Script/ScriptScanner.cpp:2456:49: note: passing argument to parameter 'new_in' here
void yyFlexLexer::switch_streams( std::istream* new_in, std::ostream* new_out )
^
out/soong/.intermediates/frameworks/compile/mclinker/lib/Script/libmcldScript/android_arm64_armv8-a_cortex-a73_core_static/gen/lex/frameworks/compile/mclinker/lib/Script/ScriptScanner.cpp:2475:11: error: member reference type 'std::istream *' (aka 'basic_istream<char> *') is a pointer; did you mean to use '->'?
if ( yyin.eof() || yyin.fail() )
~~~~^
->
out/soong/.intermediates/frameworks/compile/mclinker/lib/Script/libmcldScript/android_arm64_armv8-a_cortex-a73_core_static/gen/lex/frameworks/compile/mclinker/lib/Script/ScriptScanner.cpp:2475:25: error: member reference type 'std::istream *' (aka 'basic_istream<char> *') is a pointer; did you mean to use '->'?
if ( yyin.eof() || yyin.fail() )
~~~~^
->
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.

EDIT 5: It seems all the errors are flex-related. With my system's flex it doesn't have the LC_TIME assertion issue, but having all the errors above. For some reasons, export LC_ALL=C has no effect on this (on Manjaro/Arch). I can't even build flex 2.6.4 myself because it segfaults during make. (EDIT 6: It seems using the flex binary from here solves the problems I'm having... actually I remembered having replaced flex binary the last time I attempted building it... this should be essential for Arch-based Linux users as I'm not sure how it was on Ubuntu, yet it doesn't seem to have been formally included)

EDIT 7: For anyone having LC_ALL=C (flex-related) problems, just cherry-pick this commit from Lineage and the build should proceed.