Skip to content

Commit 6331710

Browse files
sipahebasto
andcommitted
miniscript: make operator_mst consteval
It seems modern compilers don't realize that all invocations of operator""_mst can be evaluated at compile time, despite the constexpr keyword. Since C++20, we can force them to evaluate at compile time, turning all the miniscript type constants into actual compile-time constants. It appears that MSVC does not support consteval operator"" when used inside certain expressions. For the few places where this happens, define a constant outside the operator call. Co-Authored-By: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com>
1 parent 70e4d6f commit 6331710

File tree

3 files changed

+38
-31
lines changed

3 files changed

+38
-31
lines changed

src/script/miniscript.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ Type ComputeType(Fragment fragment, Type x, Type y, Type z, const std::vector<Ty
231231
Type acc_tl = "k"_mst;
232232
for (size_t i = 0; i < sub_types.size(); ++i) {
233233
Type t = sub_types[i];
234-
if (!(t << (i ? "Wdu"_mst : "Bdu"_mst))) return ""_mst; // Require Bdu, Wdu, Wdu, ...
234+
static constexpr auto WDU{"Wdu"_mst}, BDU{"Bdu"_mst};
235+
if (!(t << (i ? WDU : BDU))) return ""_mst; // Require Bdu, Wdu, Wdu, ...
235236
if (!(t << "e"_mst)) all_e = false;
236237
if (!(t << "m"_mst)) all_m = false;
237238
if (t << "s"_mst) num_s += 1;

src/script/miniscript.h

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,12 @@ class Type {
123123
//! Internal bitmap of properties (see ""_mst operator for details).
124124
uint32_t m_flags;
125125

126-
//! Internal constructor used by the ""_mst operator.
127-
explicit constexpr Type(uint32_t flags) : m_flags(flags) {}
126+
//! Internal constructor.
127+
explicit constexpr Type(uint32_t flags) noexcept : m_flags(flags) {}
128128

129129
public:
130-
//! The only way to publicly construct a Type is using this literal operator.
131-
friend constexpr Type operator"" _mst(const char* c, size_t l);
130+
//! Construction function used by the ""_mst operator.
131+
static consteval Type Make(uint32_t flags) noexcept { return Type(flags); }
132132

133133
//! Compute the type with the union of properties.
134134
constexpr Type operator|(Type x) const { return Type(m_flags | x.m_flags); }
@@ -150,11 +150,11 @@ class Type {
150150
};
151151

152152
//! Literal operator to construct Type objects.
153-
inline constexpr Type operator"" _mst(const char* c, size_t l) {
154-
Type typ{0};
153+
inline consteval Type operator"" _mst(const char* c, size_t l) {
154+
Type typ{Type::Make(0)};
155155

156156
for (const char *p = c; p < c + l; p++) {
157-
typ = typ | Type(
157+
typ = typ | Type::Make(
158158
*p == 'B' ? 1 << 0 : // Base type
159159
*p == 'V' ? 1 << 1 : // Verify type
160160
*p == 'K' ? 1 << 2 : // Key type
@@ -548,7 +548,8 @@ struct Node {
548548
for (const auto& sub : subs) {
549549
subsize += sub->ScriptSize();
550550
}
551-
Type sub0type = subs.size() > 0 ? subs[0]->GetType() : ""_mst;
551+
static constexpr auto NONE_MST{""_mst};
552+
Type sub0type = subs.size() > 0 ? subs[0]->GetType() : NONE_MST;
552553
return internal::ComputeScriptLen(fragment, sub0type, subsize, k, subs.size(), keys.size(), m_script_ctx);
553554
}
554555

@@ -712,9 +713,10 @@ struct Node {
712713
for (const auto& sub : subs) sub_types.push_back(sub->GetType());
713714
}
714715
// All other nodes than THRESH can be computed just from the types of the 0-3 subexpressions.
715-
Type x = subs.size() > 0 ? subs[0]->GetType() : ""_mst;
716-
Type y = subs.size() > 1 ? subs[1]->GetType() : ""_mst;
717-
Type z = subs.size() > 2 ? subs[2]->GetType() : ""_mst;
716+
static constexpr auto NONE_MST{""_mst};
717+
Type x = subs.size() > 0 ? subs[0]->GetType() : NONE_MST;
718+
Type y = subs.size() > 1 ? subs[1]->GetType() : NONE_MST;
719+
Type z = subs.size() > 2 ? subs[2]->GetType() : NONE_MST;
718720

719721
return SanitizeType(ComputeType(fragment, x, y, z, sub_types, k, data.size(), subs.size(), keys.size(), m_script_ctx));
720722
}

src/test/fuzz/miniscript.cpp

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ std::optional<NodeInfo> ConsumeNodeStable(MsCtx script_ctx, FuzzedDataProvider&
391391
bool allow_K = (type_needed == ""_mst) || (type_needed << "K"_mst);
392392
bool allow_V = (type_needed == ""_mst) || (type_needed << "V"_mst);
393393
bool allow_W = (type_needed == ""_mst) || (type_needed << "W"_mst);
394+
static constexpr auto B{"B"_mst}, K{"K"_mst}, V{"V"_mst}, W{"W"_mst};
394395

395396
switch (provider.ConsumeIntegral<uint8_t>()) {
396397
case 0:
@@ -440,22 +441,22 @@ std::optional<NodeInfo> ConsumeNodeStable(MsCtx script_ctx, FuzzedDataProvider&
440441
}
441442
case 11:
442443
if (!(allow_B || allow_K || allow_V)) return {};
443-
return {{{"B"_mst, type_needed, type_needed}, Fragment::ANDOR}};
444+
return {{{B, type_needed, type_needed}, Fragment::ANDOR}};
444445
case 12:
445446
if (!(allow_B || allow_K || allow_V)) return {};
446-
return {{{"V"_mst, type_needed}, Fragment::AND_V}};
447+
return {{{V, type_needed}, Fragment::AND_V}};
447448
case 13:
448449
if (!allow_B) return {};
449-
return {{{"B"_mst, "W"_mst}, Fragment::AND_B}};
450+
return {{{B, W}, Fragment::AND_B}};
450451
case 15:
451452
if (!allow_B) return {};
452-
return {{{"B"_mst, "W"_mst}, Fragment::OR_B}};
453+
return {{{B, W}, Fragment::OR_B}};
453454
case 16:
454455
if (!allow_V) return {};
455-
return {{{"B"_mst, "V"_mst}, Fragment::OR_C}};
456+
return {{{B, V}, Fragment::OR_C}};
456457
case 17:
457458
if (!allow_B) return {};
458-
return {{{"B"_mst, "B"_mst}, Fragment::OR_D}};
459+
return {{{B, B}, Fragment::OR_D}};
459460
case 18:
460461
if (!(allow_B || allow_K || allow_V)) return {};
461462
return {{{type_needed, type_needed}, Fragment::OR_I}};
@@ -472,25 +473,25 @@ std::optional<NodeInfo> ConsumeNodeStable(MsCtx script_ctx, FuzzedDataProvider&
472473
}
473474
case 20:
474475
if (!allow_W) return {};
475-
return {{{"B"_mst}, Fragment::WRAP_A}};
476+
return {{{B}, Fragment::WRAP_A}};
476477
case 21:
477478
if (!allow_W) return {};
478-
return {{{"B"_mst}, Fragment::WRAP_S}};
479+
return {{{B}, Fragment::WRAP_S}};
479480
case 22:
480481
if (!allow_B) return {};
481-
return {{{"K"_mst}, Fragment::WRAP_C}};
482+
return {{{K}, Fragment::WRAP_C}};
482483
case 23:
483484
if (!allow_B) return {};
484-
return {{{"V"_mst}, Fragment::WRAP_D}};
485+
return {{{V}, Fragment::WRAP_D}};
485486
case 24:
486487
if (!allow_V) return {};
487-
return {{{"B"_mst}, Fragment::WRAP_V}};
488+
return {{{B}, Fragment::WRAP_V}};
488489
case 25:
489490
if (!allow_B) return {};
490-
return {{{"B"_mst}, Fragment::WRAP_J}};
491+
return {{{B}, Fragment::WRAP_J}};
491492
case 26:
492493
if (!allow_B) return {};
493-
return {{{"B"_mst}, Fragment::WRAP_N}};
494+
return {{{B}, Fragment::WRAP_N}};
494495
case 27: {
495496
if (!allow_B || !IsTapscript(script_ctx)) return {};
496497
const auto k = provider.ConsumeIntegral<uint16_t>();
@@ -528,20 +529,23 @@ struct SmartInfo
528529
{
529530
/* Construct a set of interesting type requirements to reason with (sections of BKVWzondu). */
530531
std::vector<Type> types;
532+
static constexpr auto B_mst{"B"_mst}, K_mst{"K"_mst}, V_mst{"V"_mst}, W_mst{"W"_mst};
533+
static constexpr auto d_mst{"d"_mst}, n_mst{"n"_mst}, o_mst{"o"_mst}, u_mst{"u"_mst}, z_mst{"z"_mst};
534+
static constexpr auto NONE_mst{""_mst};
531535
for (int base = 0; base < 4; ++base) { /* select from B,K,V,W */
532-
Type type_base = base == 0 ? "B"_mst : base == 1 ? "K"_mst : base == 2 ? "V"_mst : "W"_mst;
536+
Type type_base = base == 0 ? B_mst : base == 1 ? K_mst : base == 2 ? V_mst : W_mst;
533537
for (int zo = 0; zo < 3; ++zo) { /* select from z,o,(none) */
534-
Type type_zo = zo == 0 ? "z"_mst : zo == 1 ? "o"_mst : ""_mst;
538+
Type type_zo = zo == 0 ? z_mst : zo == 1 ? o_mst : NONE_mst;
535539
for (int n = 0; n < 2; ++n) { /* select from (none),n */
536540
if (zo == 0 && n == 1) continue; /* z conflicts with n */
537541
if (base == 3 && n == 1) continue; /* W conflicts with n */
538-
Type type_n = n == 0 ? ""_mst : "n"_mst;
542+
Type type_n = n == 0 ? NONE_mst : n_mst;
539543
for (int d = 0; d < 2; ++d) { /* select from (none),d */
540544
if (base == 2 && d == 1) continue; /* V conflicts with d */
541-
Type type_d = d == 0 ? ""_mst : "d"_mst;
545+
Type type_d = d == 0 ? NONE_mst : d_mst;
542546
for (int u = 0; u < 2; ++u) { /* select from (none),u */
543547
if (base == 2 && u == 1) continue; /* V conflicts with u */
544-
Type type_u = u == 0 ? ""_mst : "u"_mst;
548+
Type type_u = u == 0 ? NONE_mst : u_mst;
545549
Type type = type_base | type_zo | type_n | type_d | type_u;
546550
types.push_back(type);
547551
}
@@ -683,7 +687,7 @@ struct SmartInfo
683687
/* Find which types are useful. The fuzzer logic only cares about constructing
684688
* B,V,K,W nodes, so any type that isn't needed in any recipe (directly or
685689
* indirectly) for the construction of those is uninteresting. */
686-
std::set<Type> useful_types{"B"_mst, "V"_mst, "K"_mst, "W"_mst};
690+
std::set<Type> useful_types{B_mst, V_mst, K_mst, W_mst};
687691
// Find the transitive closure by adding types until the set of types does not change.
688692
while (true) {
689693
size_t set_size = useful_types.size();

0 commit comments

Comments
 (0)