0

Don't use constexpr MD5 for Mojo's IPC hashes

As these files are generated, there's no need to make the compiler
constexpr evaluate these. Also switch these to a more modern hash, as
MD5 has been broken for quite some time.

Bug: 406829393
Change-Id: Ie5f258560339bc4238efe90e38c32bfa98f9983f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6405582
Reviewed-by: Alex Gough <ajgo@chromium.org>
Commit-Queue: Daniel Cheng <dcheng@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1439852}
This commit is contained in:
Daniel Cheng 2025-03-28 22:51:39 -07:00 committed by Chromium LUCI CQ
parent 4ab80fe3c3
commit 4af1930f2b
2 changed files with 12 additions and 4 deletions
mojo/public/tools/bindings/generators

@ -149,14 +149,14 @@ const char* {{interface.name}}::MessageToMethodName_(mojo::Message& message) {
#if !BUILDFLAG(IS_FUCHSIA)
{%- for method in interface.methods %}
uint32_t {{interface.name}}::{{method.name}}_Sym::IPCStableHash() {
// This method's address is used for indetifiying the mojo method name after
// This method's address is used for identifiying the mojo method name after
// symbolization. So each IPCStableHash should have a unique address.
// We cannot use NO_CODE_FOLDING() here - it relies on the uniqueness of
// __LINE__ value, which is not unique accross different mojo modules.
// The code below is very similar to NO_CODE_FOLDING, but it uses a unique
// hash instead of __LINE__.
constexpr uint32_t kHash = base::MD5Hash32Constexpr(
"(Impl){{namespace_as_string}}::{{interface.name}}::{{method.name}}");
{%- set message_name = "%s::%s::%s"|format(namespace_as_string, interface.name, method.name) %}
static constexpr uint32_t kHash = {{message_name|ipc_hash}}; // IPCStableHash for {{message_name}}
const uint32_t hash = kHash;
base::debug::Alias(&hash);
return hash;

@ -3,6 +3,7 @@
# found in the LICENSE file.
"""Generates C++ source files from a mojom.Module."""
import hashlib
import os
import mojom.generate.generator as generator
import mojom.generate.module as mojom
@ -178,6 +179,11 @@ def ShouldInlineUnion(union):
return not any(mojom.IsReferenceKind(field.kind) for field in union.fields)
def _IpcHash(message_name):
sha256_hash = hashlib.sha256(message_name.encode('utf-8'))
return f'0x{sha256_hash.hexdigest()[:8]}'
def HasPackedMethodOrdinals(interface):
"""Returns whether all method ordinals are packed such that indexing into a
table would be efficient."""
@ -409,6 +415,7 @@ class Generator(generator.Generator):
"requires_context_for_data_view": RequiresContextForDataView,
"should_inline": ShouldInlineStruct,
"should_inline_union": ShouldInlineUnion,
"ipc_hash": _IpcHash,
"is_array_kind": mojom.IsArrayKind,
"is_bool_kind": mojom.IsBoolKind,
"is_default_constructible": self._IsDefaultConstructible,
@ -550,7 +557,8 @@ class Generator(generator.Generator):
return self._ExpressionToText(constant.value, kind=constant.kind)
def _ConstantLength(self, constant):
# The length of the string value, removing the quotes, but preserving the null-terminator.
# The length of the string value, removing the quotes, but preserving the
# null-terminator.
return f"{len(constant.value) - 1}"
def _UnderToCamel(self, value, digits_split=False):