0

MojoHandle -> uintptr_t

MojoHandle was recently changed from 32 bits to 64 bits wide in order
to have parity with ipcz handles. ipcz handles however have since been
standardized as pointer-sized rather than fixed at 64 bits.

This updates MojoHandle to be an alias for uintptr_t, in order to
maintain parity with current ipcz, once again breaking the Mojo
ABI. This is fine since the Mojo ABI only has one known project
depending on its stability (Chrome OS IME) and it's already using
an alternative (strictly 32-bit) ABI.

Bug: 1299283
Change-Id: Ibe97e8882b4f1b9f08e780c3acc48bec60f2ef60
Fixed: 1322162
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3804110
Commit-Queue: Ken Rockot <rockot@google.com>
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1031404}
This commit is contained in:
Ken Rockot
2022-08-04 08:19:01 +00:00
committed by Chromium LUCI CQ
parent eb0a5a1550
commit d61f19cc6a
10 changed files with 84 additions and 78 deletions

@ -38,7 +38,7 @@ bool ParamTraits<mojo::MessagePipeHandle>::Read(const base::Pickle* m,
void ParamTraits<mojo::MessagePipeHandle>::Log(const param_type& p,
std::string* l) {
l->append("mojo::MessagePipeHandle(");
LogParam(p.value(), l);
LogParam(static_cast<uint64_t>(p.value()), l);
l->append(")");
}
@ -88,7 +88,7 @@ bool ParamTraits<mojo::DataPipeConsumerHandle>::Read(const base::Pickle* m,
void ParamTraits<mojo::DataPipeConsumerHandle>::Log(const param_type& p,
std::string* l) {
l->append("mojo::DataPipeConsumerHandle(");
LogParam(p.value(), l);
LogParam(static_cast<uint64_t>(p.value()), l);
l->append(")");
}

@ -359,52 +359,52 @@ MojoResult MojoSetDefaultProcessErrorHandlerImpl(
} // extern "C"
MojoSystemThunks64 g_thunks = {sizeof(g_thunks),
MojoInitializeImpl,
MojoGetTimeTicksNowImpl,
MojoCloseImpl,
MojoQueryHandleSignalsStateImpl,
MojoCreateMessagePipeImpl,
MojoWriteMessageImpl,
MojoReadMessageImpl,
MojoFuseMessagePipesImpl,
MojoCreateMessageImpl,
MojoDestroyMessageImpl,
MojoSerializeMessageImpl,
MojoAppendMessageDataImpl,
MojoGetMessageDataImpl,
MojoSetMessageContextImpl,
MojoGetMessageContextImpl,
MojoNotifyBadMessageImpl,
MojoCreateDataPipeImpl,
MojoWriteDataImpl,
MojoBeginWriteDataImpl,
MojoEndWriteDataImpl,
MojoReadDataImpl,
MojoBeginReadDataImpl,
MojoEndReadDataImpl,
MojoCreateSharedBufferImpl,
MojoDuplicateBufferHandleImpl,
MojoMapBufferImpl,
MojoUnmapBufferImpl,
MojoGetBufferInfoImpl,
MojoCreateTrapImpl,
MojoAddTriggerImpl,
MojoRemoveTriggerImpl,
MojoArmTrapImpl,
MojoWrapPlatformHandleImpl,
MojoUnwrapPlatformHandleImpl,
MojoWrapPlatformSharedMemoryRegionImpl,
MojoUnwrapPlatformSharedMemoryRegionImpl,
MojoCreateInvitationImpl,
MojoAttachMessagePipeToInvitationImpl,
MojoExtractMessagePipeFromInvitationImpl,
MojoSendInvitationImpl,
MojoAcceptInvitationImpl,
MojoSetQuotaImpl,
MojoQueryQuotaImpl,
MojoShutdownImpl,
MojoSetDefaultProcessErrorHandlerImpl};
MojoSystemThunks2 g_thunks = {sizeof(g_thunks),
MojoInitializeImpl,
MojoGetTimeTicksNowImpl,
MojoCloseImpl,
MojoQueryHandleSignalsStateImpl,
MojoCreateMessagePipeImpl,
MojoWriteMessageImpl,
MojoReadMessageImpl,
MojoFuseMessagePipesImpl,
MojoCreateMessageImpl,
MojoDestroyMessageImpl,
MojoSerializeMessageImpl,
MojoAppendMessageDataImpl,
MojoGetMessageDataImpl,
MojoSetMessageContextImpl,
MojoGetMessageContextImpl,
MojoNotifyBadMessageImpl,
MojoCreateDataPipeImpl,
MojoWriteDataImpl,
MojoBeginWriteDataImpl,
MojoEndWriteDataImpl,
MojoReadDataImpl,
MojoBeginReadDataImpl,
MojoEndReadDataImpl,
MojoCreateSharedBufferImpl,
MojoDuplicateBufferHandleImpl,
MojoMapBufferImpl,
MojoUnmapBufferImpl,
MojoGetBufferInfoImpl,
MojoCreateTrapImpl,
MojoAddTriggerImpl,
MojoRemoveTriggerImpl,
MojoArmTrapImpl,
MojoWrapPlatformHandleImpl,
MojoUnwrapPlatformHandleImpl,
MojoWrapPlatformSharedMemoryRegionImpl,
MojoUnwrapPlatformSharedMemoryRegionImpl,
MojoCreateInvitationImpl,
MojoAttachMessagePipeToInvitationImpl,
MojoExtractMessagePipeFromInvitationImpl,
MojoSendInvitationImpl,
MojoAcceptInvitationImpl,
MojoSetQuotaImpl,
MojoQueryQuotaImpl,
MojoShutdownImpl,
MojoSetDefaultProcessErrorHandlerImpl};
} // namespace
@ -421,7 +421,7 @@ void InitializeCore() {
g_core = core_instance.get();
}
const MojoSystemThunks64& GetSystemThunks() {
const MojoSystemThunks2& GetSystemThunks() {
return g_thunks;
}

@ -14,10 +14,10 @@ namespace core {
// Initializes the global Core object.
MOJO_SYSTEM_IMPL_EXPORT void InitializeCore();
// Returns a MojoSystemThunks64 struct populated with the EDK's implementation
// Returns a MojoSystemThunks2 struct populated with the EDK's implementation
// of each function. This may be used by embedders to populate thunks for
// application loading.
MOJO_SYSTEM_IMPL_EXPORT const MojoSystemThunks64& GetSystemThunks();
MOJO_SYSTEM_IMPL_EXPORT const MojoSystemThunks2& GetSystemThunks();
} // namespace core
} // namespace mojo

@ -137,8 +137,8 @@ bool HandleTable::AddDispatchersFromTransit(
DCHECK_GE(next_available_handle_, 1u);
// If this insertion would cause handle overflow, we're out of handles.
const uint64_t num_handles_available =
std::numeric_limits<uint64_t>::max() - next_available_handle_ + 1;
const uintptr_t num_handles_available =
std::numeric_limits<uintptr_t>::max() - next_available_handle_ + 1;
if (num_handles_available < dispatchers.size()) {
return false;
}

@ -122,7 +122,7 @@ class MOJO_SYSTEM_IMPL_EXPORT HandleTable
base::Lock lock_;
uint64_t next_available_handle_ = 1;
uintptr_t next_available_handle_ = 1;
};
} // namespace core

@ -183,7 +183,7 @@ MojoResult ShutdownImpl(const struct MojoShutdownOptions* options) {
return MOJO_RESULT_OK;
}
MojoSystemThunks64 g_thunks = {0};
MojoSystemThunks2 g_thunks = {0};
} // namespace
@ -193,7 +193,7 @@ MojoSystemThunks64 g_thunks = {0};
#define EXPORT_FROM_MOJO_CORE __attribute__((visibility("default")))
#endif
EXPORT_FROM_MOJO_CORE void MojoGetSystemThunks(MojoSystemThunks64* thunks) {
EXPORT_FROM_MOJO_CORE void MojoGetSystemThunks(MojoSystemThunks2* thunks) {
if (!g_thunks.size) {
g_thunks = mojo::core::GetSystemThunks();
g_thunks.Initialize = InitializeImpl;
@ -202,7 +202,7 @@ EXPORT_FROM_MOJO_CORE void MojoGetSystemThunks(MojoSystemThunks64* thunks) {
// Caller must provide a thunk structure at least large enough to hold Core
// ABI version 0. SetQuota is the first function introduced in ABI version 1.
CHECK_GE(thunks->size, offsetof(MojoSystemThunks64, SetQuota));
CHECK_GE(thunks->size, offsetof(MojoSystemThunks2, SetQuota));
// NOTE: This also overrites |thunks->size| with the actual size of our own
// thunks if smaller than the caller's. This informs the caller that we

@ -34,9 +34,9 @@
namespace {
typedef void (*MojoGetSystemThunksFunction)(MojoSystemThunks64* thunks);
typedef void (*MojoGetSystemThunksFunction)(MojoSystemThunks2* thunks);
MojoSystemThunks64 g_thunks;
MojoSystemThunks2 g_thunks;
MojoResult NotImplemented(const char* name) {
if (g_thunks.size > 0) {
@ -54,9 +54,9 @@ MojoResult NotImplemented(const char* name) {
} // namespace
#define INVOKE_THUNK(name, ...) \
offsetof(MojoSystemThunks64, name) < g_thunks.size \
? g_thunks.name(__VA_ARGS__) \
#define INVOKE_THUNK(name, ...) \
offsetof(MojoSystemThunks2, name) < g_thunks.size \
? g_thunks.name(__VA_ARGS__) \
: NotImplemented(#name)
namespace mojo {
@ -863,7 +863,7 @@ MojoSystemThunks32 g_thunks_32 = {
} // extern "C"
const MojoSystemThunks64* MojoEmbedderGetSystemThunks64() {
const MojoSystemThunks2* MojoEmbedderGetSystemThunks2() {
return &g_thunks;
}
@ -871,7 +871,7 @@ const MojoSystemThunks32* MojoEmbedderGetSystemThunks32() {
return &g_thunks_32;
}
void MojoEmbedderSetSystemThunks(const MojoSystemThunks64* thunks) {
void MojoEmbedderSetSystemThunks(const MojoSystemThunks2* thunks) {
// Assume embedders will always use matching versions of the Mojo Core and
// public APIs.
DCHECK_EQ(thunks->size, sizeof(g_thunks));

@ -29,7 +29,7 @@
// (e.g. an optional "Options" structure as many functions here have).
//
#pragma pack(push, 8)
struct MojoSystemThunks64 {
struct MojoSystemThunks2 {
uint32_t size; // Should be set to sizeof(MojoSystemThunks).
MojoResult (*Initialize)(const struct MojoInitializeOptions* options);
@ -235,7 +235,7 @@ struct MojoSystemThunks64 {
const struct MojoSetDefaultProcessErrorHandlerOptions* options);
};
// Hacks: This is a copy of the ABI from before it was switched to 64-bit
// Hacks: This is a copy of the ABI from before it was switched to pointer-sized
// MojoHandle values. It can be removed once the Chrome OS IME service is
// longer consuming it.
typedef uint32_t MojoHandle32;
@ -441,12 +441,12 @@ struct MojoSystemThunks {
typedef struct MojoSystemThunks MojoSystemThunks32;
MOJO_SYSTEM_EXPORT const struct MojoSystemThunks64*
MojoEmbedderGetSystemThunks64();
MOJO_SYSTEM_EXPORT const struct MojoSystemThunks2*
MojoEmbedderGetSystemThunks2();
MOJO_SYSTEM_EXPORT const MojoSystemThunks32* MojoEmbedderGetSystemThunks32();
MOJO_SYSTEM_EXPORT void MojoEmbedderSetSystemThunks(
const struct MojoSystemThunks64* system_thunks);
const struct MojoSystemThunks2* system_thunks);
#endif // MOJO_PUBLIC_C_SYSTEM_THUNKS_H_

@ -22,7 +22,7 @@ typedef int64_t MojoTimeTicks;
// |MojoHandle|: Handles to Mojo objects.
// |MOJO_HANDLE_INVALID| - A value that is never a valid handle.
typedef uint64_t MojoHandle;
typedef uintptr_t MojoHandle;
#ifdef __cplusplus
const MojoHandle MOJO_HANDLE_INVALID = 0;

@ -120,18 +120,22 @@ static jint JNI_CoreImpl_WriteMessage(
DCHECK(env->GetDirectBufferCapacity(bytes) >= num_bytes);
buffer_size = num_bytes;
}
const MojoHandle* handles = 0;
const jlong* java_handles = nullptr;
uint32_t num_handles = 0;
if (handles_buffer) {
handles =
static_cast<MojoHandle*>(env->GetDirectBufferAddress(handles_buffer));
num_handles =
env->GetDirectBufferCapacity(handles_buffer) / sizeof(MojoHandle);
java_handles =
static_cast<jlong*>(env->GetDirectBufferAddress(handles_buffer));
num_handles = env->GetDirectBufferCapacity(handles_buffer) / sizeof(jlong);
}
// Truncate handle values if necessary.
std::vector<MojoHandle> handles(num_handles);
std::copy(java_handles, java_handles + num_handles, handles.begin());
// Java code will handle invalidating handles if the write succeeded.
return WriteMessageRaw(
MessagePipeHandle(static_cast<MojoHandle>(mojo_handle)), buffer_start,
buffer_size, handles, num_handles, flags);
buffer_size, handles.data(), num_handles, flags);
}
static ScopedJavaLocalRef<jobject> JNI_CoreImpl_ReadMessage(
@ -167,12 +171,14 @@ static ScopedJavaLocalRef<jobject> JNI_CoreImpl_ReadMessage(
if (result != MOJO_RESULT_OK)
return Java_CoreImpl_newReadMessageResult(env, result, nullptr, nullptr);
// Extend handles to 64-bit values if necessary.
std::vector<jlong> java_handles(handles.size());
std::copy(handles.begin(), handles.end(), java_handles.begin());
return Java_CoreImpl_newReadMessageResult(
env, result,
base::android::ToJavaByteArray(env, static_cast<uint8_t*>(buffer),
num_bytes),
base::android::ToJavaLongArray(
env, reinterpret_cast<jlong*>(handles.data()), num_handles));
base::android::ToJavaLongArray(env, java_handles.data(), num_handles));
}
static ScopedJavaLocalRef<jobject> JNI_CoreImpl_ReadData(