Update Marl to 2f129cf1b

Changes:
    2f129cf1b Use __loongarch_lp64 for identifying LoongArch LP64* ABIs
    9741a763b Fix style problems in the LoongArch support code
    28e42f9fb Guard "bound" accesses from Memory Sanitizer checks (#225)
    9929747c9 src/memory.cpp: Fix sizeof() (#221)

Commands:
    ./third_party/update-marl.sh

Bug: b/140546382
Change-Id: Id2cdee64a4015be6af2c9bfb59686df78e1e0c12
diff --git a/third_party/marl/CMakeLists.txt b/third_party/marl/CMakeLists.txt
index 1bd10d9..c84076d 100644
--- a/third_party/marl/CMakeLists.txt
+++ b/third_party/marl/CMakeLists.txt
@@ -158,16 +158,16 @@
         ${MARL_SRC_DIR}/osfiber_arm.c
         ${MARL_SRC_DIR}/osfiber_asm_aarch64.S
         ${MARL_SRC_DIR}/osfiber_asm_arm.S
+        ${MARL_SRC_DIR}/osfiber_asm_loongarch64.S
         ${MARL_SRC_DIR}/osfiber_asm_mips64.S
         ${MARL_SRC_DIR}/osfiber_asm_ppc64.S
         ${MARL_SRC_DIR}/osfiber_asm_rv64.S
-	${MARL_SRC_DIR}/osfiber_asm_loongarch64.S
         ${MARL_SRC_DIR}/osfiber_asm_x64.S
         ${MARL_SRC_DIR}/osfiber_asm_x86.S
+        ${MARL_SRC_DIR}/osfiber_loongarch64.c
         ${MARL_SRC_DIR}/osfiber_mips64.c
         ${MARL_SRC_DIR}/osfiber_ppc64.c
         ${MARL_SRC_DIR}/osfiber_rv64.c
-	${MARL_SRC_DIR}/osfiber_loongarch64.c
         ${MARL_SRC_DIR}/osfiber_x64.c
         ${MARL_SRC_DIR}/osfiber_x86.c
     )
@@ -177,11 +177,11 @@
     set_source_files_properties(
         ${MARL_SRC_DIR}/osfiber_asm_aarch64.S
         ${MARL_SRC_DIR}/osfiber_asm_arm.S
+        ${MARL_SRC_DIR}/osfiber_asm_loongarch64.S
         ${MARL_SRC_DIR}/osfiber_asm_mips64.S
         ${MARL_SRC_DIR}/osfiber_asm_ppc64.S
         ${MARL_SRC_DIR}/osfiber_asm_x64.S
         ${MARL_SRC_DIR}/osfiber_asm_x86.S
-	${MARL_SRC_DIR}/osfiber_asm_loongarch64.S
         PROPERTIES LANGUAGE C
     )
 endif(NOT MSVC)
diff --git a/third_party/marl/README.md b/third_party/marl/README.md
index beef96a..eb3bf53 100644
--- a/third_party/marl/README.md
+++ b/third_party/marl/README.md
@@ -8,7 +8,7 @@
 
 Marl uses a combination of fibers and threads to allow efficient execution of tasks that can block, while keeping a fixed number of hardware threads.
 
-Marl supports Windows, macOS, Linux, FreeBSD, Fuchsia, Android and iOS (arm, aarch64, mips64, ppc64, rv64, loongarch64, x86 and x64).
+Marl supports Windows, macOS, Linux, FreeBSD, Fuchsia, Android and iOS (arm, aarch64, loongarch64, mips64, ppc64, rv64, x86 and x64).
 
 Marl has no dependencies on other libraries (with an exception on googletest for building the optional unit tests).
 
diff --git a/third_party/marl/include/marl/scheduler.h b/third_party/marl/include/marl/scheduler.h
index e5509e6..b402155 100644
--- a/third_party/marl/include/marl/scheduler.h
+++ b/third_party/marl/include/marl/scheduler.h
@@ -487,6 +487,9 @@
   // it going to sleep.
   void onBeginSpinning(int workerId);
 
+  // setBound() sets the scheduler bound to the current thread.
+  static void setBound(Scheduler* scheduler);
+
   // The scheduler currently bound to the current thread.
   static thread_local Scheduler* bound;
 
diff --git a/third_party/marl/src/memory.cpp b/third_party/marl/src/memory.cpp
index b8cbaa2..a8a5bff 100644
--- a/third_party/marl/src/memory.cpp
+++ b/third_party/marl/src/memory.cpp
@@ -192,7 +192,7 @@
 // alignedFree() frees memory allocated by alignedMalloc.
 inline void alignedFree(void* ptr, size_t size) {
   void* base;
-  memcpy(&base, reinterpret_cast<uint8_t*>(ptr) + size, sizeof(size_t));
+  memcpy(&base, reinterpret_cast<uint8_t*>(ptr) + size, sizeof(void*));
   free(base);
 }
 
diff --git a/third_party/marl/src/osfiber_asm_loongarch64.S b/third_party/marl/src/osfiber_asm_loongarch64.S
index c6d835e..285c734 100644
--- a/third_party/marl/src/osfiber_asm_loongarch64.S
+++ b/third_party/marl/src/osfiber_asm_loongarch64.S
@@ -2,9 +2,9 @@
 //
 // Licensed under the Apache License. Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at 
+// You may obtain a copy of the License at
 //
-// 	https://www.apache.org/licenses/LICENSE-2.0
+//      https://www.apache.org/licenses/LICENSE-2.0
 //
 // Unless required by applicable law or agreed to in writing, software
 // distributed under the License is distributed on an "AS IS" BASIS,
@@ -12,7 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-#if defined(__loongarch__) && _LOONGARCH_SIM == _ABILP64
+#if defined(__loongarch_lp64)
 
 #define MARL_BUILD_ASM 1
 #include "osfiber_asm_loongarch64.h"
@@ -25,60 +25,60 @@
 .align 4
 marl_fiber_swap:
 
-	// Save context 'from'
+    // Save context 'from'
 
-	// Store callee-preserved registers
-	st.d $s0, $a0, MARL_REG_s0
-	st.d $s1, $a0, MARL_REG_s1
-	st.d $s2, $a0, MARL_REG_s2
-	st.d $s3, $a0, MARL_REG_s3
-	st.d $s4, $a0, MARL_REG_s4
-	st.d $s5, $a0, MARL_REG_s5
-	st.d $s6, $a0, MARL_REG_s6
-	st.d $s7, $a0, MARL_REG_s7
-	st.d $s8, $a0, MARL_REG_s8
+    // Store callee-preserved registers
+    st.d $s0, $a0, MARL_REG_s0
+    st.d $s1, $a0, MARL_REG_s1
+    st.d $s2, $a0, MARL_REG_s2
+    st.d $s3, $a0, MARL_REG_s3
+    st.d $s4, $a0, MARL_REG_s4
+    st.d $s5, $a0, MARL_REG_s5
+    st.d $s6, $a0, MARL_REG_s6
+    st.d $s7, $a0, MARL_REG_s7
+    st.d $s8, $a0, MARL_REG_s8
 
-	fst.d $fs0, $a0, MARL_REG_fs0
-	fst.d $fs1, $a0, MARL_REG_fs1
-	fst.d $fs2, $a0, MARL_REG_fs2
-	fst.d $fs3, $a0, MARL_REG_fs3
-	fst.d $fs4, $a0, MARL_REG_fs4
-	fst.d $fs5, $a0, MARL_REG_fs5
-	fst.d $fs6, $a0, MARL_REG_fs6
-	fst.d $fs7, $a0, MARL_REG_fs7
+    fst.d $fs0, $a0, MARL_REG_fs0
+    fst.d $fs1, $a0, MARL_REG_fs1
+    fst.d $fs2, $a0, MARL_REG_fs2
+    fst.d $fs3, $a0, MARL_REG_fs3
+    fst.d $fs4, $a0, MARL_REG_fs4
+    fst.d $fs5, $a0, MARL_REG_fs5
+    fst.d $fs6, $a0, MARL_REG_fs6
+    fst.d $fs7, $a0, MARL_REG_fs7
 
-	st.d $ra, $a0, MARL_REG_ra
-	st.d $sp, $a0, MARL_REG_sp
-	st.d $fp, $a0, MARL_REG_fp
+    st.d $ra, $a0, MARL_REG_ra
+    st.d $sp, $a0, MARL_REG_sp
+    st.d $fp, $a0, MARL_REG_fp
 
-	// Recover callee-preserved registers
-	ld.d $s0, $a1, MARL_REG_s0
-	ld.d $s1, $a1, MARL_REG_s1
-	ld.d $s2, $a1, MARL_REG_s2
-	ld.d $s3, $a1, MARL_REG_s3
-	ld.d $s4, $a1, MARL_REG_s4
-	ld.d $s5, $a1, MARL_REG_s5
-	ld.d $s6, $a1, MARL_REG_s6
-	ld.d $s7, $a1, MARL_REG_s7
-	ld.d $s8, $a1, MARL_REG_s8
+    // Recover callee-preserved registers
+    ld.d $s0, $a1, MARL_REG_s0
+    ld.d $s1, $a1, MARL_REG_s1
+    ld.d $s2, $a1, MARL_REG_s2
+    ld.d $s3, $a1, MARL_REG_s3
+    ld.d $s4, $a1, MARL_REG_s4
+    ld.d $s5, $a1, MARL_REG_s5
+    ld.d $s6, $a1, MARL_REG_s6
+    ld.d $s7, $a1, MARL_REG_s7
+    ld.d $s8, $a1, MARL_REG_s8
 
-	fld.d $fs0, $a1, MARL_REG_fs0
-	fld.d $fs1, $a1, MARL_REG_fs1
-	fld.d $fs2, $a1, MARL_REG_fs2
-	fld.d $fs3, $a1, MARL_REG_fs3
-	fld.d $fs4, $a1, MARL_REG_fs4
-	fld.d $fs5, $a1, MARL_REG_fs5
-	fld.d $fs6, $a1, MARL_REG_fs6
-	fld.d $fs7, $a1, MARL_REG_fs7
+    fld.d $fs0, $a1, MARL_REG_fs0
+    fld.d $fs1, $a1, MARL_REG_fs1
+    fld.d $fs2, $a1, MARL_REG_fs2
+    fld.d $fs3, $a1, MARL_REG_fs3
+    fld.d $fs4, $a1, MARL_REG_fs4
+    fld.d $fs5, $a1, MARL_REG_fs5
+    fld.d $fs6, $a1, MARL_REG_fs6
+    fld.d $fs7, $a1, MARL_REG_fs7
 
-	ld.d $ra, $a1, MARL_REG_ra
-	ld.d $sp, $a1, MARL_REG_sp
-	ld.d $fp, $a1, MARL_REG_fp
+    ld.d $ra, $a1, MARL_REG_ra
+    ld.d $sp, $a1, MARL_REG_sp
+    ld.d $fp, $a1, MARL_REG_fp
 
-	// Recover arguments
-	ld.d $a0, $a1, MARL_REG_a0
-	ld.d $a1, $a1, MARL_REG_a1
+    // Recover arguments
+    ld.d $a0, $a1, MARL_REG_a0
+    ld.d $a1, $a1, MARL_REG_a1
 
-	jr	$ra // Jump to the trampoline
+    jr $ra // Jump to the trampoline
 
-#endif // defined(__loongarch__) && _LOONGARCH_SIM == _ABILP64
+#endif // defined(__loongarch_lp64)
diff --git a/third_party/marl/src/osfiber_asm_loongarch64.h b/third_party/marl/src/osfiber_asm_loongarch64.h
index 2366c2b..7f4f188 100644
--- a/third_party/marl/src/osfiber_asm_loongarch64.h
+++ b/third_party/marl/src/osfiber_asm_loongarch64.h
@@ -2,9 +2,9 @@
 //
 // Licensed under the Apache License. Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at 
+// You may obtain a copy of the License at
 //
-// 	https://www.apache.org/licenses/LICENSE-2.0
+//      https://www.apache.org/licenses/LICENSE-2.0
 //
 // Unless required by applicable law or agreed to in writing, software
 // distributed under the License is distributed on an "AS IS" BASIS,
@@ -42,7 +42,7 @@
 // Procedure Call Standard for the LoongArch 64-bit Architecture
 // https://loongson.github.io/LoongArch-Documentation/LoongArch-ELF-ABI-EN.html
 struct marl_fiber_context {
-  // paramater registers (First two)
+  // parameter registers (First two)
   uintptr_t a0;
   uintptr_t a1;
 
@@ -73,50 +73,50 @@
 
 #ifdef __cplusplus
 #include <cstddef>
-static_assert(offsetof(marl_fiber_context, a0) == MARL_REG_a0, 
-	      "Bad register offset");
-static_assert(offsetof(marl_fiber_context, a1) == MARL_REG_a1, 
-	      "Bad register offset");
-static_assert(offsetof(marl_fiber_context, s0) == MARL_REG_s0, 
-	      "Bad register offset");
-static_assert(offsetof(marl_fiber_context, s1) == MARL_REG_s1, 
-	      "Bad register offset");
-static_assert(offsetof(marl_fiber_context, s2) == MARL_REG_s2, 
-	      "Bad register offset");
-static_assert(offsetof(marl_fiber_context, s3) == MARL_REG_s3, 
-	      "Bad register offset");
-static_assert(offsetof(marl_fiber_context, s4) == MARL_REG_s4, 
-	      "Bad register offset");
-static_assert(offsetof(marl_fiber_context, s5) == MARL_REG_s5, 
-	      "Bad register offset");
-static_assert(offsetof(marl_fiber_context, s6) == MARL_REG_s6, 
-	      "Bad register offset");
-static_assert(offsetof(marl_fiber_context, s7) == MARL_REG_s7, 
-	      "Bad register offset");
-static_assert(offsetof(marl_fiber_context, s8) == MARL_REG_s8, 
-	      "Bad register offset");
-static_assert(offsetof(marl_fiber_context, fs0) == MARL_REG_fs0, 
-	      "Bad register offset");
-static_assert(offsetof(marl_fiber_context, fs1) == MARL_REG_fs1, 
-	      "Bad register offset");
-static_assert(offsetof(marl_fiber_context, fs2) == MARL_REG_fs2, 
-	      "Bad register offset");
-static_assert(offsetof(marl_fiber_context, fs3) == MARL_REG_fs3, 
-	      "Bad register offset");
-static_assert(offsetof(marl_fiber_context, fs4) == MARL_REG_fs4, 
-	      "Bad register offset");
-static_assert(offsetof(marl_fiber_context, fs5) == MARL_REG_fs5, 
-	      "Bad register offset");
-static_assert(offsetof(marl_fiber_context, fs6) == MARL_REG_fs6, 
-	      "Bad register offset");
-static_assert(offsetof(marl_fiber_context, fs7) == MARL_REG_fs7, 
-	      "Bad register offset");
-static_assert(offsetof(marl_fiber_context, ra) == MARL_REG_ra, 
-	      "Bad register offset");
-static_assert(offsetof(marl_fiber_context, sp) == MARL_REG_sp, 
-	      "Bad register offset");
-static_assert(offsetof(marl_fiber_context, fp) == MARL_REG_fp, 
-	      "Bad register offset");
+static_assert(offsetof(marl_fiber_context, a0) == MARL_REG_a0,
+              "Bad register offset");
+static_assert(offsetof(marl_fiber_context, a1) == MARL_REG_a1,
+              "Bad register offset");
+static_assert(offsetof(marl_fiber_context, s0) == MARL_REG_s0,
+              "Bad register offset");
+static_assert(offsetof(marl_fiber_context, s1) == MARL_REG_s1,
+              "Bad register offset");
+static_assert(offsetof(marl_fiber_context, s2) == MARL_REG_s2,
+              "Bad register offset");
+static_assert(offsetof(marl_fiber_context, s3) == MARL_REG_s3,
+              "Bad register offset");
+static_assert(offsetof(marl_fiber_context, s4) == MARL_REG_s4,
+              "Bad register offset");
+static_assert(offsetof(marl_fiber_context, s5) == MARL_REG_s5,
+              "Bad register offset");
+static_assert(offsetof(marl_fiber_context, s6) == MARL_REG_s6,
+              "Bad register offset");
+static_assert(offsetof(marl_fiber_context, s7) == MARL_REG_s7,
+              "Bad register offset");
+static_assert(offsetof(marl_fiber_context, s8) == MARL_REG_s8,
+              "Bad register offset");
+static_assert(offsetof(marl_fiber_context, fs0) == MARL_REG_fs0,
+              "Bad register offset");
+static_assert(offsetof(marl_fiber_context, fs1) == MARL_REG_fs1,
+              "Bad register offset");
+static_assert(offsetof(marl_fiber_context, fs2) == MARL_REG_fs2,
+              "Bad register offset");
+static_assert(offsetof(marl_fiber_context, fs3) == MARL_REG_fs3,
+              "Bad register offset");
+static_assert(offsetof(marl_fiber_context, fs4) == MARL_REG_fs4,
+              "Bad register offset");
+static_assert(offsetof(marl_fiber_context, fs5) == MARL_REG_fs5,
+              "Bad register offset");
+static_assert(offsetof(marl_fiber_context, fs6) == MARL_REG_fs6,
+              "Bad register offset");
+static_assert(offsetof(marl_fiber_context, fs7) == MARL_REG_fs7,
+              "Bad register offset");
+static_assert(offsetof(marl_fiber_context, ra) == MARL_REG_ra,
+              "Bad register offset");
+static_assert(offsetof(marl_fiber_context, sp) == MARL_REG_sp,
+              "Bad register offset");
+static_assert(offsetof(marl_fiber_context, fp) == MARL_REG_fp,
+              "Bad register offset");
 #endif // __cplusplus
 
 #endif // MARL_BUILD_ASM
diff --git a/third_party/marl/src/osfiber_loongarch64.c b/third_party/marl/src/osfiber_loongarch64.c
index 4b5c4dc..b749337 100644
--- a/third_party/marl/src/osfiber_loongarch64.c
+++ b/third_party/marl/src/osfiber_loongarch64.c
@@ -2,9 +2,9 @@
 //
 // Licensed under the Apache License. Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at 
+// You may obtain a copy of the License at
 //
-// 	https://www.apache.org/licenses/LICENSE-2.0
+//      https://www.apache.org/licenses/LICENSE-2.0
 //
 // Unless required by applicable law or agreed to in writing, software
 // distributed under the License is distributed on an "AS IS" BASIS,
@@ -12,7 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-#if defined(__loongarch__)  && _LOONGARCH_SIM == _ABILP64
+#if defined(__loongarch_lp64)
 
 #include "osfiber_asm_loongarch64.h"
 
@@ -25,10 +25,10 @@
 
 MARL_EXPORT
 void marl_fiber_set_target(struct marl_fiber_context* ctx,
-			   void* stack,
-			   uint32_t stack_size,
-			   void (*target)(void*),
-			   void* arg) {
+                           void* stack,
+                           uint32_t stack_size,
+                           void (*target)(void*),
+                           void* arg) {
   uintptr_t* stack_top = (uintptr_t*)((uint8_t*)(stack) + stack_size);
   ctx->ra = (uintptr_t)&marl_fiber_trampoline;
   ctx->a0 = (uintptr_t)target;
@@ -36,4 +36,4 @@
   ctx->sp = ((uintptr_t)stack_top) & ~(uintptr_t)15;
 }
 
-#endif // defined(__loongarch__) && _LOONGARCH_SIM == _ABILP64
+#endif // defined(__loongarch_lp64)
diff --git a/third_party/marl/src/scheduler.cpp b/third_party/marl/src/scheduler.cpp
index af98159..c9ae3ee 100644
--- a/third_party/marl/src/scheduler.cpp
+++ b/third_party/marl/src/scheduler.cpp
@@ -87,19 +87,25 @@
 ////////////////////////////////////////////////////////////////////////////////
 thread_local Scheduler* Scheduler::bound = nullptr;
 
+CLANG_NO_SANITIZE_MEMORY
 Scheduler* Scheduler::get() {
   return bound;
 }
 
+CLANG_NO_SANITIZE_MEMORY
+void Scheduler::setBound(Scheduler* scheduler) {
+    bound = scheduler;
+}
+
 void Scheduler::bind() {
 #if !MEMORY_SANITIZER_ENABLED
   // thread_local variables in shared libraries are initialized at load-time,
   // but this is not observed by MemorySanitizer if the loader itself was not
   // instrumented, leading to false-positive unitialized variable errors.
   // See https://github.com/google/marl/issues/184
-  MARL_ASSERT(bound == nullptr, "Scheduler already bound");
+  MARL_ASSERT(get() == nullptr, "Scheduler already bound");
 #endif
-  bound = this;
+  setBound(this);
   {
     marl::lock lock(singleThreadedWorkers.mutex);
     auto worker = cfg.allocator->make_unique<Worker>(
@@ -111,22 +117,22 @@
 }
 
 void Scheduler::unbind() {
-  MARL_ASSERT(bound != nullptr, "No scheduler bound");
+  MARL_ASSERT(get() != nullptr, "No scheduler bound");
   auto worker = Worker::getCurrent();
   worker->stop();
   {
-    marl::lock lock(bound->singleThreadedWorkers.mutex);
+    marl::lock lock(get()->singleThreadedWorkers.mutex);
     auto tid = std::this_thread::get_id();
-    auto it = bound->singleThreadedWorkers.byTid.find(tid);
-    MARL_ASSERT(it != bound->singleThreadedWorkers.byTid.end(),
+    auto it = get()->singleThreadedWorkers.byTid.find(tid);
+    MARL_ASSERT(it != get()->singleThreadedWorkers.byTid.end(),
                 "singleThreadedWorker not found");
     MARL_ASSERT(it->second.get() == worker, "worker is not bound?");
-    bound->singleThreadedWorkers.byTid.erase(it);
-    if (bound->singleThreadedWorkers.byTid.empty()) {
-      bound->singleThreadedWorkers.unbind.notify_one();
+    get()->singleThreadedWorkers.byTid.erase(it);
+    if (get()->singleThreadedWorkers.byTid.empty()) {
+      get()->singleThreadedWorkers.unbind.notify_one();
     }
   }
-  bound = nullptr;
+  setBound(nullptr);
 }
 
 Scheduler::Scheduler(const Config& config)
@@ -380,7 +386,7 @@
           initFunc(id);
         }
 
-        Scheduler::bound = scheduler;
+        Scheduler::setBound(scheduler);
         Worker::current = this;
         mainFiber = Fiber::createFromCurrentThread(scheduler->cfg.allocator, 0);
         currentFiber = mainFiber.get();