Skip to content

Commit

Permalink
Add ABI wrapper for lseek and read.
Browse files Browse the repository at this point in the history
Change-Id: I6966eac26431373e6eccd70f9b31386daac5717d
  • Loading branch information
yjzhang111 committed Apr 11, 2024
1 parent fd3e046 commit bec97aa
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 5 deletions.
7 changes: 5 additions & 2 deletions starboard/common/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<char*>(data) + bytes_read, size - bytes_read);
if (rv <= 0) {
break;
}
Expand Down
2 changes: 1 addition & 1 deletion starboard/common/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
3 changes: 3 additions & 0 deletions starboard/elf_loader/exported_symbols.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -477,6 +478,7 @@ ExportedSymbols::ExportedSymbols() {
map_["gettimeofday"] =
reinterpret_cast<const void*>(&__abi_wrap_gettimeofday);
map_["gmtime_r"] = reinterpret_cast<const void*>(&__abi_wrap_gmtime_r);
map_["lseek"] = reinterpret_cast<const void*>(&__abi_wrap_lseek);
map_["mmap"] = reinterpret_cast<const void*>(&__abi_wrap_mmap);
map_["pthread_cond_broadcast"] =
reinterpret_cast<const void*>(&__abi_wrap_pthread_cond_broadcast);
Expand Down Expand Up @@ -520,6 +522,7 @@ ExportedSymbols::ExportedSymbols() {
reinterpret_cast<const void*>(&__abi_wrap_pthread_once);
map_["pthread_self"] =
reinterpret_cast<const void*>(&__abi_wrap_pthread_self);
map_["read"] = reinterpret_cast<const void*>(&__abi_wrap_read);
map_["stat"] = reinterpret_cast<const void*>(&__abi_wrap_stat);
map_["time"] = reinterpret_cast<const void*>(&__abi_wrap_time);

Expand Down
4 changes: 2 additions & 2 deletions starboard/nplb/posix_compliance/posix_file_read_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
Expand Down
3 changes: 3 additions & 0 deletions starboard/shared/modular/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
34 changes: 34 additions & 0 deletions starboard/shared/modular/cobalt_layer_posix_file_abi_wrappers.cc
Original file line number Diff line number Diff line change
@@ -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 <unistd.h>

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
Original file line number Diff line number Diff line change
@@ -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 <sys/types.h>
#include <unistd.h>

musl_off_t __abi_wrap_lseek(int fildes, musl_off_t offset, int whence) {
return static_cast<off_t>(lseek(fildes, static_cast<off_t>(offset), whence));
}

ssize_t __abi_wrap_read(int fildes, void* buf, size_t nbyte) {
return read(int fildes, void* buf, size_t nbyte);
}
62 changes: 62 additions & 0 deletions starboard/shared/modular/starboard_layer_posix_file_abi_wrappers.h
Original file line number Diff line number Diff line change
@@ -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 <stdint.h>
#include <sys/types.h>

#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_
1 change: 1 addition & 0 deletions starboard/tools/api_leak_detector/api_leak_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
'free',
'freeifaddrs',
'freeaddrinfo',
'fstat',
'gettimeofday',
'getifaddrs',
'getaddrinfo',
Expand Down

0 comments on commit bec97aa

Please sign in to comment.