Skip to content

Commit 75cb406

Browse files
authored
Support -uptr mode in xdrpp (#5467)
This adopts the xdrpp changes in https://github.com/xdrpp/xdrpp/tree/cxx20-uptr-hybrid and enables it at 32 byte default threshold for out-of-line allocations; it also adds `--enable-xdrpp-uptr=N` flag to configure this threshold.
2 parents 362dbc5 + 76959ee commit 75cb406

6 files changed

Lines changed: 196 additions & 19 deletions

File tree

‎configure.ac‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
AC_PREREQ([2.68])
99
AC_INIT([stellar-core],[0.1],[],[],[http://www.stellar.org])
1010
# tar-ustar is required for long file names when libsodium is bundled
11-
AM_INIT_AUTOMAKE([-Wall -Wextra -Wconversion subdir-objects tar-ustar silent-rules])
11+
AM_INIT_AUTOMAKE([foreign -Wall -Wno-portability subdir-objects tar-ustar silent-rules])
1212
AC_CONFIG_SRCDIR([configure.ac])
1313
AC_CONFIG_MACRO_DIR([m4])
1414
AC_CANONICAL_HOST
@@ -532,6 +532,25 @@ AS_IF([test "x$enable_thread_jitter" = "xyes"], [
532532
])
533533
AM_CONDITIONAL(BUILD_THREAD_JITTER, [test x$enable_thread_jitter = xyes])
534534

535+
AC_ARG_ENABLE(xdrpp-uptr,
536+
AS_HELP_STRING([--enable-xdrpp-uptr@<:@=BYTES@:>@],
537+
[Indirect xdrpp union arms larger than BYTES (default 16); pass
538+
--enable-xdrpp-uptr to indirect every arm, or --disable-xdrpp-uptr
539+
to store every arm inline]),
540+
[], [enable_xdrpp_uptr=16])
541+
AS_CASE([$enable_xdrpp_uptr],
542+
[yes], [
543+
XDR_UPTR_FLAGS='-uptr-threshold 0'
544+
AC_MSG_NOTICE([indirecting all xdrpp union arms])
545+
],
546+
[no], [XDR_UPTR_FLAGS=],
547+
[""|*@<:@!0-9@:>@*], [AC_MSG_ERROR([--enable-xdrpp-uptr requires a byte count])],
548+
[*], [
549+
XDR_UPTR_FLAGS="-uptr-threshold $enable_xdrpp_uptr"
550+
AC_MSG_NOTICE([indirecting xdrpp union arms larger than $enable_xdrpp_uptr bytes])
551+
])
552+
AC_SUBST([XDR_UPTR_FLAGS])
553+
535554
AC_ARG_ENABLE(tracy,
536555
AS_HELP_STRING([--enable-tracy],
537556
[Enable 'tracy' profiler/tracer client stub]))

‎src/Makefile.am‎

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Process this file with automake to produce Makefile.in
22

3-
AUTOMAKE_OPTIONS = serial-tests
3+
AUTOMAKE_OPTIONS = serial-tests foreign -Wno-portability
44
bin_PROGRAMS = stellar-core
55

66
include $(top_srcdir)/common.mk
@@ -115,11 +115,40 @@ BUILT_SOURCES = $(SRC_X_FILES:.x=.h) $(GENERATED_VERSION_SOURCES) $(TEST_FILES)
115115

116116
$(SRC_X_FILES:.x=.h): $(XDRC) Makefile
117117
XDR_FEATURE_FLAGS =
118+
XDR_ALL_FLAGS = @XDR_UPTR_FLAGS@ $(XDR_FEATURE_FLAGS)
118119

119120
SUFFIXES = .x .h .rs
120-
.x.h:
121-
mkdir -p $(@D)
122-
$(XDRC) -hh -pedantic $(XDR_FEATURE_FLAGS) -o $@ $<
121+
122+
# Unchanged headers keep their old timestamps so dependents aren't rebuilt,
123+
# which leaves them older than xdrc and Makefile. The stamp, not the headers,
124+
# records that generation ran; without it make would re-run xdrc on every
125+
# invocation forever.
126+
xdr-stamp: $(SRC_X_FILES) $(XDRC) Makefile
127+
$(AM_V_GEN)set -e; \
128+
rm -f $@-t; touch $@-t; \
129+
for x in $(SRC_X_FILES); do \
130+
h=$${x%.x}.h; \
131+
mkdir -p `dirname $$h`; \
132+
backup=$$h.tmp; \
133+
had_target=false; \
134+
if test -f $$h; then cp -p $$h $$backup; had_target=true; fi; \
135+
if $(XDRC) -hh -pedantic $(XDR_ALL_FLAGS) -o $$h $(srcdir)/$$x; then \
136+
if $$had_target && cmp -s $$backup $$h; then mv -f $$backup $$h; else rm -f $$backup; fi; \
137+
else \
138+
status=$$?; \
139+
if $$had_target; then mv -f $$backup $$h; else rm -f $$h; fi; \
140+
rm -f $@-t; \
141+
exit $$status; \
142+
fi; \
143+
done; \
144+
mv -f $@-t $@
145+
146+
$(SRC_X_FILES:.x=.h): xdr-stamp
147+
@test -f $@ || { \
148+
mkdir -p $(@D) && \
149+
$(XDRC) -hh -pedantic $(XDR_ALL_FLAGS) -o $@ $(srcdir)/$(@:.h=.x) || \
150+
{ status=$$?; rm -f $@; exit $$status; }; \
151+
}
123152

124153
BISON=bison
125154
FLEX=flex
@@ -708,7 +737,7 @@ endif # ENABLE_FUZZ
708737
clean-local:
709738
rm -rf $(top_builddir)/target $(top_builddir)/src/rust/soroban/*/target $(RUST_DEP_TREE_STAMP) $(SOROBAN_LIBS_STAMP)
710739

711-
CLEANFILES = $(BUILT_SOURCES) *~ */*~ stellar*.log
740+
CLEANFILES = $(BUILT_SOURCES) xdr-stamp *~ */*~ stellar*.log
712741
MAINTAINERCLEANFILES = $(srcdir)/Makefile.in $(srcdir)/*~ $(srcdir)/*/*~
713742

714743
EXTRA_DIST = $(SRC_X_FILES) test/selftest-nopg test/selftest-pg

‎src/main/CommandLine.cpp‎

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,27 @@
5050
#include "test/TestUtils.h"
5151
#include "test/fuzz/FuzzTargetRegistry.h"
5252
#include "test/test.h"
53+
#include "xdr/Stellar-SCP.h"
54+
#include "xdr/Stellar-contract-config-setting.h"
55+
#include "xdr/Stellar-contract-env-meta.h"
56+
#include "xdr/Stellar-contract-meta.h"
57+
#include "xdr/Stellar-contract.h"
58+
#include "xdr/Stellar-exporter.h"
59+
#include "xdr/Stellar-internal.h"
60+
#include "xdr/Stellar-ledger-entries.h"
61+
#include "xdr/Stellar-ledger.h"
62+
#include "xdr/Stellar-overlay.h"
63+
#include "xdr/Stellar-transaction.h"
64+
#include "xdr/Stellar-types.h"
5365
#endif
5466

67+
#include <array>
5568
#include <filesystem>
5669
#include <fmt/format.h>
5770
#include <fstream>
5871
#include <iostream>
5972
#include <lib/clara.hpp>
73+
#include <map>
6074
#include <optional>
6175

6276
namespace stellar
@@ -1715,6 +1729,106 @@ writeVersionInfo(std::ostream& os)
17151729
#ifdef BUILD_TESTS
17161730
namespace
17171731
{
1732+
struct UnionLayoutTotals
1733+
{
1734+
size_t mUnions{};
1735+
size_t mInlineArms{};
1736+
size_t mIndirectArms{};
1737+
size_t mVoidArms{};
1738+
std::map<size_t, std::array<size_t, 2>> mSizeClasses;
1739+
};
1740+
1741+
template <typename Union, size_t... Indices>
1742+
void
1743+
dumpUnionLayout(UnionLayoutTotals& totals, std::index_sequence<Indices...>)
1744+
{
1745+
using Meta = typename Union::_xdr_union_meta;
1746+
std::cout << "union " << Meta::qualified_name << ": size=" << sizeof(Union)
1747+
<< " alignment=" << alignof(Union)
1748+
<< " threshold=" << Meta::inline_threshold
1749+
<< " inline=" << Meta::num_inline_arms
1750+
<< " indirect=" << Meta::num_indirect_arms
1751+
<< " void=" << Meta::num_void_arms << '\n';
1752+
1753+
auto dumpArm = [&]<size_t Index>() {
1754+
if constexpr (Index == 0 && Meta::num_void_arms == 0)
1755+
{
1756+
return;
1757+
}
1758+
constexpr auto layout = Meta::template arm_layout<Index>();
1759+
auto const* storage =
1760+
layout.size == 0 ? "void"
1761+
: (layout.is_indirect ? "indirect" : "inline");
1762+
std::cout << " arm " << Index
1763+
<< " name=" << (layout.name[0] ? layout.name : "<void>")
1764+
<< " size=" << layout.size
1765+
<< " alignment=" << layout.alignment << " storage=" << storage
1766+
<< '\n';
1767+
if (layout.size == 0)
1768+
{
1769+
++totals.mVoidArms;
1770+
}
1771+
else
1772+
{
1773+
auto& sizeClass = totals.mSizeClasses[layout.size];
1774+
++sizeClass[layout.is_indirect ? 1 : 0];
1775+
if (layout.is_indirect)
1776+
{
1777+
++totals.mIndirectArms;
1778+
}
1779+
else
1780+
{
1781+
++totals.mInlineArms;
1782+
}
1783+
}
1784+
};
1785+
(dumpArm.template operator()<Indices>(), ...);
1786+
++totals.mUnions;
1787+
}
1788+
1789+
template <typename... Unions>
1790+
void
1791+
dumpUnionLayouts(xdr::type_list<Unions...>, UnionLayoutTotals& totals)
1792+
{
1793+
(dumpUnionLayout<Unions>(
1794+
totals, std::make_index_sequence<Unions::_xdr_union_meta::num_arms>{}),
1795+
...);
1796+
}
1797+
1798+
int
1799+
runDumpXdrUnions(CommandLineArgs const& args)
1800+
{
1801+
return runWithHelp(args, {}, [&] {
1802+
UnionLayoutTotals totals;
1803+
dumpUnionLayouts(xdr::xdr_union_types_Stellar_contract_config_setting{},
1804+
totals);
1805+
dumpUnionLayouts(xdr::xdr_union_types_Stellar_contract_env_meta{},
1806+
totals);
1807+
dumpUnionLayouts(xdr::xdr_union_types_Stellar_contract_meta{}, totals);
1808+
dumpUnionLayouts(xdr::xdr_union_types_Stellar_contract{}, totals);
1809+
dumpUnionLayouts(xdr::xdr_union_types_Stellar_exporter{}, totals);
1810+
dumpUnionLayouts(xdr::xdr_union_types_Stellar_internal{}, totals);
1811+
dumpUnionLayouts(xdr::xdr_union_types_Stellar_ledger_entries{}, totals);
1812+
dumpUnionLayouts(xdr::xdr_union_types_Stellar_ledger{}, totals);
1813+
dumpUnionLayouts(xdr::xdr_union_types_Stellar_overlay{}, totals);
1814+
dumpUnionLayouts(xdr::xdr_union_types_Stellar_SCP{}, totals);
1815+
dumpUnionLayouts(xdr::xdr_union_types_Stellar_transaction{}, totals);
1816+
dumpUnionLayouts(xdr::xdr_union_types_Stellar_types{}, totals);
1817+
1818+
std::cout << "summary: unions=" << totals.mUnions
1819+
<< " inline=" << totals.mInlineArms
1820+
<< " indirect=" << totals.mIndirectArms
1821+
<< " void=" << totals.mVoidArms << '\n';
1822+
std::cout << "size classes (bytes: inline indirect):\n";
1823+
for (auto const& [size, counts] : totals.mSizeClasses)
1824+
{
1825+
std::cout << " " << size << ": " << counts[0] << ' ' << counts[1]
1826+
<< '\n';
1827+
}
1828+
return 0;
1829+
});
1830+
}
1831+
17181832
int
17191833
runLoadXDR(CommandLineArgs const& args)
17201834
{
@@ -2098,6 +2212,8 @@ handleCommandLine(int argc, char* const* argv)
20982212
{"print-publish-queue", "print all checkpoints scheduled for publish",
20992213
runPrintPublishQueue},
21002214
#ifdef BUILD_TESTS
2215+
{"dump-xdr-unions", "dump calculated XDR union storage layouts",
2216+
runDumpXdrUnions},
21012217
{"load-xdr", "load an XDR bucket file, for testing", runLoadXDR},
21022218
{"rebuild-ledger-from-buckets",
21032219
"rebuild the current database ledger from the bucket list",

‎src/util/xdrquery/XDRFieldResolver.h‎

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -361,26 +361,28 @@ struct XDRFieldResolver
361361

362362
template <typename T>
363363
typename std::enable_if_t<xdr_traits<T>::is_union>
364-
validateUnion(T const& t, char const* fieldName)
364+
validateUnion(T const&, char const*)
365365
{
366366
// The field could have been already matched if it was XDR discriminant.
367367
if (mPathIter == mFieldPath.end())
368368
{
369369
return;
370370
}
371-
for (auto const c : t._xdr_case_values())
372-
{
373-
auto unionFieldName = xdr_traits<T>::union_field_name(c);
374-
if (unionFieldName == nullptr || unionFieldName != *mPathIter)
371+
bool matched = false;
372+
xdr::unionfn::with_each_arm<T>([&](auto arm) {
373+
if constexpr (decltype(arm)::has_field)
375374
{
376-
continue;
375+
if (!matched && mPathIter != mFieldPath.end() &&
376+
std::string_view(arm.name()) == *mPathIter)
377+
{
378+
// Set before recursing: the recursive call advances
379+
// mPathIter, which a sibling arm could otherwise match.
380+
matched = true;
381+
typename decltype(arm)::field_type field{};
382+
(*this)(field, arm.name());
383+
}
377384
}
378-
auto tCopy = t;
379-
tCopy._xdr_discriminant(c, false);
380-
tCopy._xdr_with_mem_ptr(field_archiver, c, *this, tCopy,
381-
unionFieldName);
382-
break;
383-
}
385+
});
384386
}
385387

386388
template <typename T>

‎src/util/xdrquery/test/XDRQueryTests.cpp‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,17 @@ TEST_CASE("XDR field resolver", "[xdrquery]")
326326
getXDRFieldValidated(accountEntry, {"data", "account"}),
327327
XDRQueryError);
328328
}
329+
SECTION("path continuing into a sibling union arm")
330+
{
331+
REQUIRE_THROWS_AS(
332+
getXDRFieldValidated(accountEntry,
333+
{"data", "trustLine", "offer", "offerID"}),
334+
XDRQueryError);
335+
REQUIRE_THROWS_AS(
336+
getXDRFieldValidated(accountEntry,
337+
{"data", "offer", "trustLine", "balance"}),
338+
XDRQueryError);
339+
}
329340
}
330341
}
331342

0 commit comments

Comments
 (0)