Update Marl to 7b43abfc4

Changes:
    7b43abfc4 Add rv64 support (#208)
    555c93755 kokoro: Fix ASAN build (#209)

Commands:
    ./third_party/update-marl.sh

Bug: b/140546382
Change-Id: Iddebac993a50e56ed1312f166b53f4030651f3fc
diff --git a/third_party/marl/CMakeLists.txt b/third_party/marl/CMakeLists.txt
index 015732d..e171c90 100644
--- a/third_party/marl/CMakeLists.txt
+++ b/third_party/marl/CMakeLists.txt
@@ -160,10 +160,12 @@
         ${MARL_SRC_DIR}/osfiber_asm_arm.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_x64.S
         ${MARL_SRC_DIR}/osfiber_asm_x86.S
         ${MARL_SRC_DIR}/osfiber_mips64.c
         ${MARL_SRC_DIR}/osfiber_ppc64.c
+        ${MARL_SRC_DIR}/osfiber_rv64.c
         ${MARL_SRC_DIR}/osfiber_x64.c
         ${MARL_SRC_DIR}/osfiber_x86.c
     )
@@ -238,6 +240,10 @@
         target_compile_definitions(${target} PRIVATE "MARL_DEBUG_ENABLED=1")
     endif()
 
+    if(CMAKE_SYSTEM_PROCESSOR MATCHES "rv*")
+        target_link_libraries(${target} INTERFACE atomic) #explicitly use -latomic for RISC-V linking
+    endif()
+
     target_include_directories(${target} PUBLIC $<BUILD_INTERFACE:${MARL_INCLUDE_DIR}>)
 endfunction(marl_set_target_options)
 
diff --git a/third_party/marl/README.md b/third_party/marl/README.md
index 1c1778b..ea1a89e 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, x86 and x64).
+Marl supports Windows, macOS, Linux, FreeBSD, Fuchsia, Android and iOS (arm, aarch64, 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/kokoro/ubuntu/presubmit.sh b/third_party/marl/kokoro/ubuntu/presubmit.sh
index 375dd91..08627ef 100755
--- a/third_party/marl/kokoro/ubuntu/presubmit.sh
+++ b/third_party/marl/kokoro/ubuntu/presubmit.sh
@@ -19,7 +19,9 @@
 SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd )"
 ROOT_DIR="$( cd "${SCRIPT_DIR}/../.." >/dev/null 2>&1 && pwd )"
 
+# --privileged is required for some sanitizer builds, as they seem to require PTRACE privileges
 docker run --rm -i \
+  --privileged \
   --volume "${ROOT_DIR}:${ROOT_DIR}" \
   --workdir "${ROOT_DIR}" \
   --env BUILD_SYSTEM=${BUILD_SYSTEM} \
diff --git a/third_party/marl/kokoro/ubuntu/release.sh b/third_party/marl/kokoro/ubuntu/release.sh
index 2e69f97..484b1ea 100755
--- a/third_party/marl/kokoro/ubuntu/release.sh
+++ b/third_party/marl/kokoro/ubuntu/release.sh
@@ -19,7 +19,9 @@
 SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd )"
 ROOT_DIR="$( cd "${SCRIPT_DIR}/../.." >/dev/null 2>&1 && pwd )"
 
+# --privileged is required for some sanitizer builds, as they seem to require PTRACE privileges
 docker run --rm -i \
+  --privileged \
   --volume "${ROOT_DIR}:${ROOT_DIR}" \
   --volume "${KOKORO_ARTIFACTS_DIR}:/mnt/artifacts" \
   --workdir "${ROOT_DIR}" \
diff --git a/third_party/marl/src/osfiber_asm.h b/third_party/marl/src/osfiber_asm.h
index fe8679a..e9ce773 100644
--- a/third_party/marl/src/osfiber_asm.h
+++ b/third_party/marl/src/osfiber_asm.h
@@ -34,6 +34,8 @@
 #include "osfiber_asm_ppc64.h"
 #elif defined(__mips__) && _MIPS_SIM == _ABI64
 #include "osfiber_asm_mips64.h"
+#elif defined(__riscv) && __riscv_xlen == 64
+#include "osfiber_asm_rv64.h"
 #else
 #error "Unsupported target"
 #endif
diff --git a/third_party/marl/src/osfiber_asm_rv64.S b/third_party/marl/src/osfiber_asm_rv64.S
new file mode 100644
index 0000000..0c554f1
--- /dev/null
+++ b/third_party/marl/src/osfiber_asm_rv64.S
@@ -0,0 +1,100 @@
+// Copyright 2021 The Marl Authors.
+//
+// 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
+//
+//     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,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#if defined(__riscv) && __riscv_xlen == 64
+
+#define MARL_BUILD_ASM 1
+#include "osfiber_asm_rv64.h"
+
+// void marl_fiber_swap(marl_fiber_context* from, const marl_fiber_context* to)
+// a0: from
+// a1: to
+.text
+.global marl_fiber_swap
+.align 4
+marl_fiber_swap:
+
+    // Save context 'from'
+
+    // Store callee-preserved registers
+    sd  s0, MARL_REG_s0(a0)
+    sd  s1, MARL_REG_s1(a0)
+    sd  s2, MARL_REG_s2(a0)
+    sd  s3, MARL_REG_s3(a0)
+    sd  s4, MARL_REG_s4(a0)
+    sd  s5, MARL_REG_s5(a0)
+    sd  s6, MARL_REG_s6(a0)
+    sd  s7, MARL_REG_s7(a0)
+    sd  s8, MARL_REG_s8(a0)
+    sd  s9, MARL_REG_s9(a0)
+    sd  s10, MARL_REG_s10(a0)
+    sd  s11, MARL_REG_s11(a0)
+
+    fsd  fs0, MARL_REG_fs0(a0)
+    fsd  fs1, MARL_REG_fs1(a0)
+    fsd  fs2, MARL_REG_fs2(a0)
+    fsd  fs3, MARL_REG_fs3(a0)
+    fsd  fs4, MARL_REG_fs4(a0)
+    fsd  fs5, MARL_REG_fs5(a0)
+    fsd  fs6, MARL_REG_fs6(a0)
+    fsd  fs7, MARL_REG_fs7(a0)
+    fsd  fs8, MARL_REG_fs8(a0)
+    fsd  fs9, MARL_REG_fs9(a0)
+    fsd  fs10, MARL_REG_fs10(a0)
+    fsd  fs11, MARL_REG_fs11(a0)
+
+    sd  sp, MARL_REG_sp(a0)
+    // On RISC-V ra is caller-saved
+    // but we need ra to jump to the trampoline
+    sd  ra, MARL_REG_ra(a0)
+
+    move  t0, a1 // Store a1 in temporary register
+
+    // Recover callee-preserved registers
+    ld  s0, MARL_REG_s0(t0)
+    ld  s1, MARL_REG_s1(t0)
+    ld  s2, MARL_REG_s2(t0)
+    ld  s3, MARL_REG_s3(t0)
+    ld  s4, MARL_REG_s4(t0)
+    ld  s5, MARL_REG_s5(t0)
+    ld  s6, MARL_REG_s6(t0)
+    ld  s7, MARL_REG_s7(t0)
+    ld  s8, MARL_REG_s8(t0)
+    ld  s9, MARL_REG_s9(t0)
+    ld  s10, MARL_REG_s10(t0)
+    ld  s11, MARL_REG_s11(t0)
+
+    fld  fs0, MARL_REG_fs0(t0)
+    fld  fs1, MARL_REG_fs1(t0)
+    fld  fs2, MARL_REG_fs2(t0)
+    fld  fs3, MARL_REG_fs3(t0)
+    fld  fs4, MARL_REG_fs4(t0)
+    fld  fs5, MARL_REG_fs5(t0)
+    fld  fs6, MARL_REG_fs6(t0)
+    fld  fs7, MARL_REG_fs7(t0)
+    fld  fs8, MARL_REG_fs8(t0)
+    fld  fs9, MARL_REG_fs9(t0)
+    fld  fs10, MARL_REG_fs10(t0)
+    fld  fs11, MARL_REG_fs11(t0)
+
+    ld  sp, MARL_REG_sp(t0)
+    ld  ra, MARL_REG_ra(t0)
+
+    // Recover arguments
+    ld  a0, MARL_REG_a0(t0)
+    ld  a1, MARL_REG_a1(t0)
+
+    jr	ra // Jump to the trampoline
+
+#endif // defined(__riscv) && __riscv_xlen == 64
diff --git a/third_party/marl/src/osfiber_asm_rv64.h b/third_party/marl/src/osfiber_asm_rv64.h
new file mode 100644
index 0000000..2c7d19e
--- /dev/null
+++ b/third_party/marl/src/osfiber_asm_rv64.h
@@ -0,0 +1,145 @@
+// Copyright 2021 The Marl Authors.
+//
+// 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
+//
+//     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,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+
+#define MARL_REG_a0 0x00
+#define MARL_REG_a1 0x08
+#define MARL_REG_s0 0x10
+#define MARL_REG_s1 0x18
+#define MARL_REG_s2 0x20
+#define MARL_REG_s3 0x28
+#define MARL_REG_s4 0x30
+#define MARL_REG_s5 0x38
+#define MARL_REG_s6 0x40
+#define MARL_REG_s7 0x48
+#define MARL_REG_s8 0x50
+#define MARL_REG_s9 0x58
+#define MARL_REG_s10 0x60
+#define MARL_REG_s11 0x68
+#define MARL_REG_fs0 0x70
+#define MARL_REG_fs1 0x78
+#define MARL_REG_fs2 0x80
+#define MARL_REG_fs3 0x88
+#define MARL_REG_fs4 0x90
+#define MARL_REG_fs5 0x98
+#define MARL_REG_fs6 0xa0
+#define MARL_REG_fs7 0xa8
+#define MARL_REG_fs8 0xb0
+#define MARL_REG_fs9 0xb8
+#define MARL_REG_fs10 0xc0
+#define MARL_REG_fs11 0xc8
+#define MARL_REG_sp 0xd0
+#define MARL_REG_ra 0xd8
+
+#ifndef MARL_BUILD_ASM
+
+#include <stdint.h>
+
+struct marl_fiber_context {
+  // parameter registers (First two)
+  uintptr_t a0;
+  uintptr_t a1;
+
+  // callee-saved registers
+  uintptr_t s0;
+  uintptr_t s1;
+  uintptr_t s2;
+  uintptr_t s3;
+  uintptr_t s4;
+  uintptr_t s5;
+  uintptr_t s6;
+  uintptr_t s7;
+  uintptr_t s8;
+  uintptr_t s9;
+  uintptr_t s10;
+  uintptr_t s11;
+
+  uintptr_t fs0;
+  uintptr_t fs1;
+  uintptr_t fs2;
+  uintptr_t fs3;
+  uintptr_t fs4;
+  uintptr_t fs5;
+  uintptr_t fs6;
+  uintptr_t fs7;
+  uintptr_t fs8;
+  uintptr_t fs9;
+  uintptr_t fs10;
+  uintptr_t fs11;
+
+  uintptr_t sp;
+  uintptr_t ra;
+};
+
+#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, s9) == MARL_REG_s9,
+              "Bad register offset");
+static_assert(offsetof(marl_fiber_context, s10) == MARL_REG_s10,
+              "Bad register offset");
+static_assert(offsetof(marl_fiber_context, s11) == MARL_REG_s11,
+              "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, fs8) == MARL_REG_fs8,
+              "Bad register offset");
+static_assert(offsetof(marl_fiber_context, fs9) == MARL_REG_fs9,
+              "Bad register offset");
+static_assert(offsetof(marl_fiber_context, fs10) == MARL_REG_fs10,
+              "Bad register offset");
+static_assert(offsetof(marl_fiber_context, fs11) == MARL_REG_fs11,
+              "Bad register offset");
+static_assert(offsetof(marl_fiber_context, sp) == MARL_REG_sp,
+              "Bad register offset");
+static_assert(offsetof(marl_fiber_context, ra) == MARL_REG_ra,
+              "Bad register offset");
+#endif  // __cplusplus
+
+#endif  // MARL_BUILD_ASM
diff --git a/third_party/marl/src/osfiber_rv64.c b/third_party/marl/src/osfiber_rv64.c
new file mode 100644
index 0000000..47b32fc
--- /dev/null
+++ b/third_party/marl/src/osfiber_rv64.c
@@ -0,0 +1,39 @@
+// Copyright 2021 The Marl Authors.
+//
+// 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
+//
+//     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,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#if defined(__riscv) && __riscv_xlen == 64
+
+#include "osfiber_asm_rv64.h"
+
+#include "marl/export.h"
+
+MARL_EXPORT
+void marl_fiber_trampoline(void (*target)(void*), void* arg) {
+  target(arg);
+}
+
+MARL_EXPORT
+void marl_fiber_set_target(struct marl_fiber_context* ctx,
+                           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;
+  ctx->a1 = (uintptr_t)arg;
+  ctx->sp = ((uintptr_t)stack_top) & ~(uintptr_t)15;
+}
+
+#endif  // defined(__riscv) && __riscv_xlen == 64