Skip to content

Commit

Permalink
[native] Switch C++ standard to C++23 (#9325)
Browse files Browse the repository at this point in the history
Whenever `size_t` or `ssize_t` types are used and an integer literal
is involved, use the `uz` or `z` literal suffix.  This is to make the
intent explicit as well as to avoid hidden type conversions.  Despite
being more verbose, this makes the code more type-safe.
  • Loading branch information
grendello authored Oct 25, 2024
1 parent 46f9145 commit aa668a5
Show file tree
Hide file tree
Showing 33 changed files with 181 additions and 185 deletions.
2 changes: 1 addition & 1 deletion src/native/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ ensure_variable_set(XA_LIB_TOP_DIR)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${OUTPUT_PATH}/${ANDROID_RID}" CACHE PATH "" FORCE)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${OUTPUT_PATH}/${ANDROID_RID}" CACHE PATH "" FORCE)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

Expand Down
10 changes: 5 additions & 5 deletions src/native/monodroid/archive-dso-stub-config.hh.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
namespace xamarin::android {
struct ArchiveDSOStubConfig
{
static inline constexpr size_t PayloadSectionAlignment = @ARCHIVE_DSO_STUB_PAYLOAD_SECTION_ALIGNMENT@;
static inline constexpr size_t SectionHeaderEntrySize = @SECTION_HEADER_ENTRY_SIZE@;
static inline constexpr size_t SectionHeaderEntryCount = @SECTION_HEADER_ENTRY_COUNT@;
static inline constexpr uint32_t PayloadSectionOffset = @PAYLOAD_SECTION_OFFSET@;
static inline constexpr size_t PayloadSectionAlignment = @ARCHIVE_DSO_STUB_PAYLOAD_SECTION_ALIGNMENT@uz;
static inline constexpr size_t SectionHeaderEntrySize = @SECTION_HEADER_ENTRY_SIZE@uz;
static inline constexpr size_t SectionHeaderEntryCount = @SECTION_HEADER_ENTRY_COUNT@uz;
static inline constexpr uint32_t PayloadSectionOffset = @PAYLOAD_SECTION_OFFSET@uz;

// We know that payload section is the last one in the binary, this is an index into
// the section header table.
static inline constexpr size_t PayloadSectionIndex = SectionHeaderEntryCount - 1;
static inline constexpr size_t PayloadSectionIndex = SectionHeaderEntryCount - 1uz;
};
}
56 changes: 27 additions & 29 deletions src/native/monodroid/embedded-assemblies-zip.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

using namespace xamarin::android::internal;

using read_count_type = size_t;

force_inline bool
EmbeddedAssemblies::zip_load_entry_common (size_t entry_index, std::vector<uint8_t> const& buf, dynamic_local_string<SENSIBLE_PATH_MAX> &entry_name, ZipEntryLoadState &state) noexcept
{
Expand Down Expand Up @@ -157,7 +155,7 @@ EmbeddedAssemblies::zip_load_individual_assembly_entries (std::vector<uint8_t> c
// However, clang-tidy can't know that the value is owned by Mono and we must not free it, thus the suppression.
//
// NOLINTNEXTLINE(clang-analyzer-unix.Malloc)
for (size_t i = 0; i < num_entries; i++) {
for (size_t i = 0uz; i < num_entries; i++) {
bool interesting_entry = zip_load_entry_common (i, buf, entry_name, state);
if (!interesting_entry) {
continue;
Expand Down Expand Up @@ -260,7 +258,7 @@ EmbeddedAssemblies::zip_load_assembly_store_entries (std::vector<uint8_t> const&
bool assembly_store_found = false;

log_debug (LOG_ASSEMBLY, "Looking for assembly stores in APK ('%s)", assembly_store_file_path.data ());
for (size_t i = 0; i < num_entries; i++) {
for (size_t i = 0uz; i < num_entries; i++) {
if (all_required_zip_entries_found ()) {
need_to_scan_more_apks = false;
break;
Expand Down Expand Up @@ -341,17 +339,17 @@ EmbeddedAssemblies::zip_load_entries (int fd, const char *apk_name, [[maybe_unus
.file_name = apk_name,
.prefix = prefix,
.prefix_len = prefix_len,
.buf_offset = 0,
.compression_method = 0,
.local_header_offset = 0,
.data_offset = 0,
.file_size = 0,
.buf_offset = 0uz,
.compression_method = 0u,
.local_header_offset = 0u,
.data_offset = 0u,
.file_size = 0u,
.bundled_assemblies_slow_path = false,
.max_assembly_name_size = 0,
.max_assembly_file_name_size = 0,
.max_assembly_name_size = 0u,
.max_assembly_file_name_size = 0u,
};

ssize_t nread = read (fd, buf.data (), static_cast<read_count_type>(buf.size ()));
ssize_t nread = read (fd, buf.data (), buf.size ());
if (static_cast<size_t>(nread) != cd_size) {
Helpers::abort_application (
LOG_ASSEMBLY,
Expand Down Expand Up @@ -424,14 +422,14 @@ EmbeddedAssemblies::zip_read_cd_info (int fd, uint32_t& cd_offset, uint32_t& cd_
}

std::array<uint8_t, ZIP_EOCD_LEN> eocd;
ssize_t nread = ::read (fd, eocd.data (), static_cast<read_count_type>(eocd.size ()));
ssize_t nread = ::read (fd, eocd.data (), eocd.size ());
if (nread < 0 || nread != eocd.size ()) {
log_error (LOG_ASSEMBLY, "Failed to read EOCD from the APK: %s (nread: %d; errno: %d)", std::strerror (errno), nread, errno);
return false;
}

size_t index = 0; // signature
std::array<uint8_t, 4> signature;
size_t index = 0uz; // signature
std::array<uint8_t, 4uz> signature;

if (!zip_read_field (eocd, index, signature)) {
log_error (LOG_ASSEMBLY, "Failed to read EOCD signature");
Expand All @@ -443,7 +441,7 @@ EmbeddedAssemblies::zip_read_cd_info (int fd, uint32_t& cd_offset, uint32_t& cd_
}

// Most probably a ZIP with comment
constexpr size_t alloc_size = 65535 + ZIP_EOCD_LEN; // 64k is the biggest comment size allowed
constexpr size_t alloc_size = 65535uz + ZIP_EOCD_LEN; // 64k is the biggest comment size allowed
ret = ::lseek (fd, static_cast<off_t>(-alloc_size), SEEK_END);
if (ret < 0) {
log_error (LOG_ASSEMBLY, "Unable to seek into the file to find ECOD before APK comment: %s (ret: %d; errno: %d)", std::strerror (errno), ret, errno);
Expand All @@ -452,7 +450,7 @@ EmbeddedAssemblies::zip_read_cd_info (int fd, uint32_t& cd_offset, uint32_t& cd_

std::vector<uint8_t> buf (alloc_size);

nread = ::read (fd, buf.data (), static_cast<read_count_type>(buf.size ()));
nread = ::read (fd, buf.data (), buf.size ());

if (nread < 0 || static_cast<size_t>(nread) != alloc_size) {
log_error (LOG_ASSEMBLY, "Failed to read EOCD and comment from the APK: %s (nread: %d; errno: %d)", std::strerror (errno), nread, errno);
Expand All @@ -462,7 +460,7 @@ EmbeddedAssemblies::zip_read_cd_info (int fd, uint32_t& cd_offset, uint32_t& cd_
// We scan from the end to save time
bool found = false;
const uint8_t* data = buf.data ();
for (ssize_t i = static_cast<ssize_t>(alloc_size - (ZIP_EOCD_LEN + 2)); i >= 0; i--) {
for (ssize_t i = static_cast<ssize_t>(alloc_size - (ZIP_EOCD_LEN + 2)); i >= 0z; i--) {
if (memcmp (data + i, ZIP_EOCD_MAGIC.data (), sizeof(ZIP_EOCD_MAGIC)) != 0)
continue;

Expand All @@ -482,8 +480,8 @@ EmbeddedAssemblies::zip_read_cd_info (int fd, uint32_t& cd_offset, uint32_t& cd_
bool
EmbeddedAssemblies::zip_adjust_data_offset (int fd, ZipEntryLoadState &state)
{
static constexpr size_t LH_FILE_NAME_LENGTH_OFFSET = 26;
static constexpr size_t LH_EXTRA_LENGTH_OFFSET = 28;
static constexpr size_t LH_FILE_NAME_LENGTH_OFFSET = 26uz;
static constexpr size_t LH_EXTRA_LENGTH_OFFSET = 28uz;

off_t result = ::lseek (fd, static_cast<off_t>(state.local_header_offset), SEEK_SET);
if (result < 0) {
Expand Down Expand Up @@ -534,9 +532,9 @@ template<size_t BufSize>
bool
EmbeddedAssemblies::zip_extract_cd_info (std::array<uint8_t, BufSize> const& buf, uint32_t& cd_offset, uint32_t& cd_size, uint16_t& cd_entries)
{
constexpr size_t EOCD_TOTAL_ENTRIES_OFFSET = 10;
constexpr size_t EOCD_CD_SIZE_OFFSET = 12;
constexpr size_t EOCD_CD_START_OFFSET = 16;
constexpr size_t EOCD_TOTAL_ENTRIES_OFFSET = 10uz;
constexpr size_t EOCD_CD_SIZE_OFFSET = 12uz;
constexpr size_t EOCD_CD_START_OFFSET = 16uz;

static_assert (BufSize >= ZIP_EOCD_LEN, "Buffer too short for EOCD");

Expand Down Expand Up @@ -627,12 +625,12 @@ EmbeddedAssemblies::zip_read_field (T const& buf, size_t index, size_t count, dy
bool
EmbeddedAssemblies::zip_read_entry_info (std::vector<uint8_t> const& buf, dynamic_local_string<SENSIBLE_PATH_MAX>& file_name, ZipEntryLoadState &state)
{
constexpr size_t CD_COMPRESSION_METHOD_OFFSET = 10;
constexpr size_t CD_UNCOMPRESSED_SIZE_OFFSET = 24;
constexpr size_t CD_FILENAME_LENGTH_OFFSET = 28;
constexpr size_t CD_EXTRA_LENGTH_OFFSET = 30;
constexpr size_t CD_LOCAL_HEADER_POS_OFFSET = 42;
constexpr size_t CD_COMMENT_LENGTH_OFFSET = 32;
constexpr size_t CD_COMPRESSION_METHOD_OFFSET = 10uz;
constexpr size_t CD_UNCOMPRESSED_SIZE_OFFSET = 24uz;
constexpr size_t CD_FILENAME_LENGTH_OFFSET = 28uz;
constexpr size_t CD_EXTRA_LENGTH_OFFSET = 30uz;
constexpr size_t CD_LOCAL_HEADER_POS_OFFSET = 42uz;
constexpr size_t CD_COMMENT_LENGTH_OFFSET = 32uz;

size_t index = state.buf_offset;
zip_ensure_valid_params (buf, index, ZIP_CENTRAL_LEN);
Expand Down
16 changes: 8 additions & 8 deletions src/native/monodroid/embedded-assemblies.cc
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ EmbeddedAssemblies::map_runtime_file (XamarinAndroidBundledAssembly& file) noexc
if (Util::should_log (LOG_ASSEMBLY) && map_info.area != nullptr) [[unlikely]] {
const char *p = (const char*) file.data;

std::array<char, 9> header;
for (size_t j = 0; j < header.size () - 1; ++j)
std::array<char, 9uz> header;
for (size_t j = 0uz; j < header.size () - 1uz; ++j)
header[j] = isprint (p [j]) ? p [j] : '.';
header [header.size () - 1] = '\0';

Expand Down Expand Up @@ -349,7 +349,7 @@ EmbeddedAssemblies::individual_assemblies_open_from_bundles (dynamic_local_strin

MonoAssembly *a = nullptr;

for (size_t i = 0; i < application_config.number_of_assemblies_in_apk; i++) {
for (size_t i = 0uz; i < application_config.number_of_assemblies_in_apk; i++) {
a = load_bundled_assembly (bundled_assemblies [i], name, abi_name, loader_data, ref_only);
if (a != nullptr) {
return a;
Expand Down Expand Up @@ -575,7 +575,7 @@ EmbeddedAssemblies::binary_search (const Key *key, const Entry *base, size_t nme
force_inline const TypeMapModuleEntry*
EmbeddedAssemblies::binary_search (uint32_t key, const TypeMapModuleEntry *arr, uint32_t n) noexcept
{
ssize_t left = -1;
ssize_t left = -1z;
ssize_t right = static_cast<ssize_t>(n);
ssize_t middle;

Expand Down Expand Up @@ -1010,7 +1010,7 @@ EmbeddedAssemblies::typemap_load_index (TypeMapIndexHeader &header, size_t file_
}

uint8_t *p = data.get ();
for (size_t i = 0; i < type_map_count; i++) {
for (size_t i = 0uz; i < type_map_count; i++) {
type_maps[i].assembly_name = reinterpret_cast<char*>(p);
p += entry_size;
}
Expand Down Expand Up @@ -1043,7 +1043,7 @@ EmbeddedAssemblies::typemap_load_index (int dir_fd, const char *dir_path, const
bool
EmbeddedAssemblies::typemap_load_file (BinaryTypeMapHeader &header, const char *dir_path, const char *file_path, int file_fd, TypeMap &module)
{
size_t alloc_size = Helpers::add_with_overflow_check<size_t> (header.assembly_name_length, 1);
size_t alloc_size = Helpers::add_with_overflow_check<size_t> (header.assembly_name_length, 1uz);
module.assembly_name = new char[alloc_size];

ssize_t nread = do_read (file_fd, module.assembly_name, header.assembly_name_length);
Expand Down Expand Up @@ -1086,7 +1086,7 @@ EmbeddedAssemblies::typemap_load_file (BinaryTypeMapHeader &header, const char *
TypeMapEntry *cur;

constexpr uint32_t INVALID_TYPE_INDEX = std::numeric_limits<uint32_t>::max ();
for (size_t i = 0; i < module.entry_count; i++) {
for (size_t i = 0uz; i < module.entry_count; i++) {
cur = const_cast<TypeMapEntry*> (&module.java_to_managed[i]);
cur->from = reinterpret_cast<char*>(java_pos);

Expand Down Expand Up @@ -1291,7 +1291,7 @@ EmbeddedAssemblies::register_from_filesystem (const char *lib_dir_path,bool look
)
);

size_t assembly_count = 0;
size_t assembly_count = 0uz;
do {
errno = 0;
dirent *cur = readdir (lib_dir);
Expand Down
14 changes: 7 additions & 7 deletions src/native/monodroid/embedded-assemblies.hh
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ namespace xamarin::android::internal {
}

template<typename Key, typename Entry, int (*compare)(const Key*, const Entry*), bool use_extra_size = false>
static const Entry* binary_search (const Key *key, const Entry *base, size_t nmemb, size_t extra_size = 0) noexcept;
static const Entry* binary_search (const Key *key, const Entry *base, size_t nmemb, size_t extra_size = 0uz) noexcept;

#if defined (DEBUG)
static int compare_type_name (const char *type_name, const TypeMapEntry *entry) noexcept;
Expand Down Expand Up @@ -390,7 +390,7 @@ namespace xamarin::android::internal {
}

template<bool IsSatelliteAssembly>
static void unmangle_name (dynamic_local_string<SENSIBLE_PATH_MAX> &name, size_t start_idx = 0) noexcept
static void unmangle_name (dynamic_local_string<SENSIBLE_PATH_MAX> &name, size_t start_idx = 0uz) noexcept
{
constexpr size_t mangled_data_size = get_mangled_data_size<IsSatelliteAssembly> ();
if (name.length () <= mangled_data_size) {
Expand Down Expand Up @@ -423,8 +423,8 @@ namespace xamarin::android::internal {

bool register_debug_symbols;
bool have_and_want_debug_symbols;
size_t bundled_assembly_index = 0;
size_t number_of_found_assemblies = 0;
size_t bundled_assembly_index = 0uz;
size_t number_of_found_assemblies = 0uz;

#if defined (DEBUG)
TypeMappingInfo *java_to_managed_maps;
Expand All @@ -436,10 +436,10 @@ namespace xamarin::android::internal {

md_mmap_info runtime_config_blob_mmap{};
void *runtime_config_data = nullptr;
size_t runtime_config_data_size = 0;
size_t runtime_config_data_size = 0uz;
bool runtime_config_blob_found = false;
uint32_t number_of_mapped_assembly_stores = 0;
uint32_t number_of_zip_dso_entries = 0;
uint32_t number_of_mapped_assembly_stores = 0u;
uint32_t number_of_zip_dso_entries = 0u;
bool need_to_scan_more_apks = true;

AssemblyStoreIndexEntry *assembly_store_hashes;
Expand Down
10 changes: 5 additions & 5 deletions src/native/monodroid/jni-remapping.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ JniRemapping::lookup_replacement_type (const char *jniSimpleReference) noexcept
}

size_t ref_len = strlen (jniSimpleReference);
for (size_t i = 0; i < application_config.jni_remapping_replacement_type_count; i++) {
for (size_t i = 0uz; i < application_config.jni_remapping_replacement_type_count; i++) {
JniRemappingTypeReplacementEntry const& entry = jni_remapping_type_replacements[i];

if (equal (entry.name, jniSimpleReference, ref_len)) {
Expand All @@ -51,7 +51,7 @@ JniRemapping::lookup_replacement_method_info (const char *jniSourceType, const c
size_t source_type_len = strlen (jniSourceType);

const JniRemappingIndexTypeEntry *type = nullptr;
for (size_t i = 0; i < application_config.jni_remapping_replacement_method_index_entry_count; i++) {
for (size_t i = 0uz; i < application_config.jni_remapping_replacement_method_index_entry_count; i++) {
JniRemappingIndexTypeEntry const& entry = jni_remapping_method_replacement_index[i];

if (!equal (entry.name, jniSourceType, source_type_len)) {
Expand All @@ -67,9 +67,9 @@ JniRemapping::lookup_replacement_method_info (const char *jniSourceType, const c
}

size_t method_name_len = strlen (jniMethodName);
size_t signature_len = jniMethodSignature == nullptr ? 0 : strlen (jniMethodSignature);
size_t signature_len = jniMethodSignature == nullptr ? 0uz : strlen (jniMethodSignature);

for (size_t i = 0; i < type->method_count; i++) {
for (size_t i = 0uz; i < type->method_count; i++) {
JniRemappingIndexMethodEntry const& entry = type->methods[i];

if (!equal (entry.name, jniMethodName, method_name_len)) {
Expand All @@ -89,7 +89,7 @@ JniRemapping::lookup_replacement_method_info (const char *jniSourceType, const c
sig_end--;
}

if (equal (entry.signature, jniMethodSignature, static_cast<size_t>(sig_end - jniMethodSignature) + 1)) {
if (equal (entry.signature, jniMethodSignature, static_cast<size_t>(sig_end - jniMethodSignature) + 1uz)) {
return &type->methods[i].replacement;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/native/monodroid/mono-image-loader.hh
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ namespace xamarin::android::internal {
force_inline static ssize_t find_index (hash_t hash) noexcept
{
ssize_t idx = Search::binary_search (hash, assembly_image_cache_hashes, number_of_cache_index_entries);
return idx >= 0 ? static_cast<ssize_t>(assembly_image_cache_indices[idx]) : -1;
return idx >= 0 ? static_cast<ssize_t>(assembly_image_cache_indices[idx]) : -1z;

}
#endif // def USE_CACHE
Expand Down
2 changes: 1 addition & 1 deletion src/native/monodroid/monodroid-glue-internal.hh
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ namespace xamarin::android::internal

private:
static constexpr std::string_view base_apk_name { "/base.apk" };
static constexpr size_t SMALL_STRING_PARSE_BUFFER_LEN = 50;
static constexpr size_t SMALL_STRING_PARSE_BUFFER_LEN = 50uz;
static constexpr bool is_running_on_desktop = false;

public:
Expand Down
Loading

0 comments on commit aa668a5

Please sign in to comment.