From 6473b3ca487caa76cbf0b5b9ed90bbcf0ffc4cd0 Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Tue, 8 Apr 2025 15:49:28 +0300 Subject: [PATCH] feat(vendor): update MicroPython to support sorted qstr pools Requires https://github.com/trezor/micropython/pull/20. [no changelog] --- common/protob/pb2py | 26 ++++++++++++++------------ core/embed/rust/qstr.h | 20 ++++++++++++++++++-- vendor/micropython | 2 +- 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/common/protob/pb2py b/common/protob/pb2py index 226f983aac..336f2da067 100755 --- a/common/protob/pb2py +++ b/common/protob/pb2py @@ -132,8 +132,9 @@ WIRETYPE_ENTRY = c.Sequence( "msg_offset" / c.Int16ul, ) -# QDEF(MP_QSTR_copysign, 5171, 8, "copysign") -QDEF_RE = re.compile(r'^QDEF\(MP_QSTR(\S+), ([0-9]+), ([0-9])+, "(.*)"\)$') +# QDEF1(MP_QSTR_copysign, 5171, 8, "copysign") +QDEF_RE = re.compile(r'^QDEF(\d)\(MP_QSTR\S+, [0-9]+, [0-9]+, "(.*)"\)$') +QDEF_RE_SKIP = re.compile("^$|^//") # empty line or a comment @dataclass @@ -540,17 +541,18 @@ class RustBlobRenderer: def build_qstr_map(self, qstr_defs): # QSTR defs are rolled out into an enum in py/qstr.h, the numeric # value is simply an incremented integer. - qstr_counter = 0 with open(qstr_defs, "r") as f: - for line in f: - match = QDEF_RE.match(line) - if not match: - continue - line = match.group(0) - string = match.group(4) - self.qstr_map[string] = qstr_counter - qstr_counter += 1 - logging.debug(f"Found {qstr_counter} Qstr defs") + matches = [ + QDEF_RE.match(line).groups() + for line in f + if not QDEF_RE_SKIP.match(line) + ] + # QDEF0 are processed before QDEF1 (see `vendor/micropython/py/qstr.h`) + # Keep the original QSTR order (since Python's sort is stable) + matches.sort(key=lambda m: int(m[0])) + for i, (_qdef, string) in enumerate(matches): + self.qstr_map[string] = i + logging.debug(f"Found {len(matches)} Qstr defs") def build_enums_with_offsets(self): enums = [] diff --git a/core/embed/rust/qstr.h b/core/embed/rust/qstr.h index 4678dc5965..7e03ae5d08 100644 --- a/core/embed/rust/qstr.h +++ b/core/embed/rust/qstr.h @@ -1,5 +1,21 @@ enum Qstr { -#define QDEF(id, hash, len, str) id, + +// Copied from `vendor/micropython/py/qstr.h`: + +#define QDEF0(id, hash, len, str) id, +#define QDEF1(id, hash, len, str) #include "genhdr/qstrdefs.generated.h" -#undef QDEF +#undef QDEF0 +#undef QDEF1 + MP_QSTRnumber_of_static, + // unused but shifts the enum counter back one + MP_QSTRstart_of_main = MP_QSTRnumber_of_static - 1, + +#define QDEF0(id, hash, len, str) +#define QDEF1(id, hash, len, str) id, +#include "genhdr/qstrdefs.generated.h" +#undef QDEF0 +#undef QDEF1 + // no underscore so it can't clash with any of the above + MP_QSTRnumber_of, }; diff --git a/vendor/micropython b/vendor/micropython index 52747e831f..2d09fb5f20 160000 --- a/vendor/micropython +++ b/vendor/micropython @@ -1 +1 @@ -Subproject commit 52747e831f4f81ae0bff3d8fee1828c6880545ba +Subproject commit 2d09fb5f206cd76220eb9ca19cee9da1f7886dfa