From bec97aa4e36370f6be5aa812cab9c1a981818483 Mon Sep 17 00:00:00 2001 From: Yijia Zhang Date: Wed, 10 Apr 2024 17:57:08 -0700 Subject: [PATCH] Add ABI wrapper for lseek and read. Change-Id: I6966eac26431373e6eccd70f9b31386daac5717d --- starboard/common/file.cc | 7 ++- starboard/common/file.h | 2 +- starboard/elf_loader/exported_symbols.cc | 3 + .../posix_compliance/posix_file_read_test.cc | 4 +- starboard/shared/modular/BUILD.gn | 3 + .../cobalt_layer_posix_file_abi_wrappers.cc | 34 ++++++++++ ...starboard_layer_posix_file_abi_wrappers.cc | 26 ++++++++ .../starboard_layer_posix_file_abi_wrappers.h | 62 +++++++++++++++++++ .../api_leak_detector/api_leak_detector.py | 1 + 9 files changed, 137 insertions(+), 5 deletions(-) create mode 100644 starboard/shared/modular/cobalt_layer_posix_file_abi_wrappers.cc create mode 100644 starboard/shared/modular/starboard_layer_posix_file_abi_wrappers.cc create mode 100644 starboard/shared/modular/starboard_layer_posix_file_abi_wrappers.h diff --git a/starboard/common/file.cc b/starboard/common/file.cc index b2be9a18c50e..36e6eec41bce 100644 --- a/starboard/common/file.cc +++ b/starboard/common/file.cc @@ -46,14 +46,17 @@ bool DirectoryCloseLogFailure(const char* path, SbDirectory dir) { } // namespace -ssize_t ReadAll(int fd, char* data, int size) { +ssize_t ReadAll(int fd, void* data, int size) { if (fd < 0 || size < 0) { return -1; } ssize_t bytes_read = 0; ssize_t rv; do { - rv = read(fd, data + bytes_read, size - bytes_read); + // Needs to cast void* to char* as MSVC returns error for pointer + // arithmetic. + rv = + read(fd, reinterpret_cast(data) + bytes_read, size - bytes_read); if (rv <= 0) { break; } diff --git a/starboard/common/file.h b/starboard/common/file.h index 58e2e0221c4f..d9c1bbc0ef73 100644 --- a/starboard/common/file.h +++ b/starboard/common/file.h @@ -26,7 +26,7 @@ namespace starboard { -ssize_t ReadAll(int fd, char* data, int size); +ssize_t ReadAll(int fd, void* data, int size); void RecordFileWriteStat(int write_file_result); diff --git a/starboard/elf_loader/exported_symbols.cc b/starboard/elf_loader/exported_symbols.cc index 58a13ffb2e47..ed9688a4708f 100644 --- a/starboard/elf_loader/exported_symbols.cc +++ b/starboard/elf_loader/exported_symbols.cc @@ -50,6 +50,7 @@ #include "starboard/once.h" #include "starboard/player.h" #if SB_API_VERSION >= 16 +#include "starboard/shared/modular/starboard_layer_posix_file_abi_wrappers.h" #include "starboard/shared/modular/starboard_layer_posix_mmap_abi_wrappers.h" #include "starboard/shared/modular/starboard_layer_posix_pthread_abi_wrappers.h" #include "starboard/shared/modular/starboard_layer_posix_stat_abi_wrappers.h" @@ -477,6 +478,7 @@ ExportedSymbols::ExportedSymbols() { map_["gettimeofday"] = reinterpret_cast(&__abi_wrap_gettimeofday); map_["gmtime_r"] = reinterpret_cast(&__abi_wrap_gmtime_r); + map_["lseek"] = reinterpret_cast(&__abi_wrap_lseek); map_["mmap"] = reinterpret_cast(&__abi_wrap_mmap); map_["pthread_cond_broadcast"] = reinterpret_cast(&__abi_wrap_pthread_cond_broadcast); @@ -520,6 +522,7 @@ ExportedSymbols::ExportedSymbols() { reinterpret_cast(&__abi_wrap_pthread_once); map_["pthread_self"] = reinterpret_cast(&__abi_wrap_pthread_self); + map_["read"] = reinterpret_cast(&__abi_wrap_read); map_["stat"] = reinterpret_cast(&__abi_wrap_stat); map_["time"] = reinterpret_cast(&__abi_wrap_time); diff --git a/starboard/nplb/posix_compliance/posix_file_read_test.cc b/starboard/nplb/posix_compliance/posix_file_read_test.cc index 4d76a17dff03..fee22ca3bc5f 100644 --- a/starboard/nplb/posix_compliance/posix_file_read_test.cc +++ b/starboard/nplb/posix_compliance/posix_file_read_test.cc @@ -34,14 +34,14 @@ class PosixFileReadTest : public testing::Test {}; class PosixRead { public: - static int Read(int file, void* buf, size_t nbyte) { + static ssize_t Read(int file, void* buf, size_t nbyte) { return read(file, buf, nbyte); } }; class PosixReadAll { public: - static int Read(int file, char* data, int size) { + static ssize_t Read(int file, void* data, int size) { return ReadAll(file, data, size); } }; diff --git a/starboard/shared/modular/BUILD.gn b/starboard/shared/modular/BUILD.gn index 6003a286e6d0..a8562a40fab3 100644 --- a/starboard/shared/modular/BUILD.gn +++ b/starboard/shared/modular/BUILD.gn @@ -17,6 +17,8 @@ if (sb_is_modular || sb_is_evergreen_compatible) { source_set("starboard_layer_posix_abi_wrappers") { sources = [ + "starboard_layer_posix_file_abi_wrappers.cc", + "starboard_layer_posix_file_abi_wrappers.h", "starboard_layer_posix_mmap_abi_wrappers.cc", "starboard_layer_posix_mmap_abi_wrappers.h", "starboard_layer_posix_pthread_abi_wrappers.cc", @@ -37,6 +39,7 @@ if (sb_is_modular && !sb_is_evergreen && current_toolchain == cobalt_toolchain) { source_set("cobalt_layer_posix_abi_wrappers") { sources = [ + "cobalt_layer_posix_file_abi_wrappers.cc", "cobalt_layer_posix_mmap_abi_wrappers.cc", "cobalt_layer_posix_pthread_abi_wrappers.cc", "cobalt_layer_posix_stat_abi_wrappers.cc", diff --git a/starboard/shared/modular/cobalt_layer_posix_file_abi_wrappers.cc b/starboard/shared/modular/cobalt_layer_posix_file_abi_wrappers.cc new file mode 100644 index 000000000000..3afc5c22d8b8 --- /dev/null +++ b/starboard/shared/modular/cobalt_layer_posix_file_abi_wrappers.cc @@ -0,0 +1,34 @@ +// Copyright 2024 The Cobalt Authors. All Rights Reserved. +// +// 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 +// +// http://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 SB_API_VERSION >= 16 + +#include + +extern "C" { + +off_t __abi_wrap_lseek(int fildes, off_t offset, int whence); + +off_t lseek(int fildes, off_t offset, int whence) { + return __abi_wrap_lseek(fildes, offset, whence); +} + +ssize_t __abi_wrap_read(int fildes, void* buf, size_t nbyte); + +ssize_t read(int fildes, void* buf, size_t nbyte) { + return __abi_wrap_read(int fildes, void* buf, size_t nbyte); +} +} + +#endif // SB_API_VERSION >= 16 diff --git a/starboard/shared/modular/starboard_layer_posix_file_abi_wrappers.cc b/starboard/shared/modular/starboard_layer_posix_file_abi_wrappers.cc new file mode 100644 index 000000000000..b83526765d43 --- /dev/null +++ b/starboard/shared/modular/starboard_layer_posix_file_abi_wrappers.cc @@ -0,0 +1,26 @@ +// Copyright 2024 The Cobalt Authors. All Rights Reserved. +// +// 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 +// +// http://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. + +#include "starboard/shared/modular/starboard_layer_posix_file_abi_wrappers.h" + +#include +#include + +musl_off_t __abi_wrap_lseek(int fildes, musl_off_t offset, int whence) { + return static_cast(lseek(fildes, static_cast(offset), whence)); +} + +ssize_t __abi_wrap_read(int fildes, void* buf, size_t nbyte) { + return read(int fildes, void* buf, size_t nbyte); +} diff --git a/starboard/shared/modular/starboard_layer_posix_file_abi_wrappers.h b/starboard/shared/modular/starboard_layer_posix_file_abi_wrappers.h new file mode 100644 index 000000000000..b2a6433cb2c1 --- /dev/null +++ b/starboard/shared/modular/starboard_layer_posix_file_abi_wrappers.h @@ -0,0 +1,62 @@ +// Copyright 2024 The Cobalt Authors. All Rights Reserved. +// +// 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 +// +// http://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. + +#ifndef STARBOARD_SHARED_MODULAR_STARBOARD_LAYER_POSIX_FILE_ABI_WRAPPERS_H_ +#define STARBOARD_SHARED_MODULAR_STARBOARD_LAYER_POSIX_FILE_ABI_WRAPPERS_H_ + +#include +#include + +#include "starboard/export.h" + +// The `__abi_wrap_lseek` function converts from the musl's off_t +// type to the platform's off_t which may have different sizes as +// the definition is platform specific. +// The `__abi_wrap_read` function converts ssize_t to int32_t or +// int64_t depending on the platform. +// +// The wrapper is used by all modular builds, including Evergreen. +// +// For Evergreen-based modular builds, we will rely on the exported_symbols.cc +// mapping logic to map calls to file IO functions to `__abi_wrap_` file IO +// functions. +// +// For non-Evergreen modular builds, the Cobalt-side shared library will be +// compiled with code that remaps calls to file IO functions to `__abi_wrap_` +// file IO functions. + +// A matching type for the off_t definition in musl. +typedef int64_t musl_off_t; + +#if SB_IS(ARCH_ARM64) || SB_IS(ARCH_X64) +typedef int64_t ssize_t; +#else +typedef int32_t ssize_t; +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +SB_EXPORT musl_off_t __abi_wrap_lseek(int fildes, + musl_off_t offset, + int whence); + +SB_EXPORT ssize_t __abi_wrap_read(int fildes, void* buf, size_t nbyte); + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif // STARBOARD_SHARED_MODULAR_STARBOARD_LAYER_POSIX_FILE_ABI_WRAPPERS_H_ diff --git a/starboard/tools/api_leak_detector/api_leak_detector.py b/starboard/tools/api_leak_detector/api_leak_detector.py index c5948213c3e5..8cf727a11712 100755 --- a/starboard/tools/api_leak_detector/api_leak_detector.py +++ b/starboard/tools/api_leak_detector/api_leak_detector.py @@ -96,6 +96,7 @@ 'free', 'freeifaddrs', 'freeaddrinfo', + 'fstat', 'gettimeofday', 'getifaddrs', 'getaddrinfo',