원시데이터 수집 시스템
선정목록으로

Debugging WebAssembly? Put Some Whamm on It!

관련 엔지니어링 플라스틱 · (22) POM(폴리아세탈)

기본 정보

저자
  1. Elizabeth Gilbert — Carnegie Mellon University, Pittsburgh, USA
  2. Matthew Schneider — Carnegie Mellon University, Pittsburgh, USA
  3. Zixi An — Carnegie Mellon University, Pittsburgh, USA
  4. Suhas Thalanki — Carnegie Mellon University, Pittsburgh, USA
  5. Wavid Bowman — University of Florida, Gainesville, USA
  6. Anbin Bai — New York University, New York, USA
  7. Ben L. Titzer — Carnegie Mellon University, Pittsburgh, USA
  8. Heather Miller — Carnegie Mellon University, Pittsburgh, USA
DOI
10.1145/3763124
저널
Proceedings of the ACM on Programming Languages
발행년도
2025
발행기관
Association for Computing Machinery
피인용수
1
라이선스
cc-by
논문 유형
–
언어
–
원문 페이지
https://doi.org/10.1145/3763124
토픽
Advancements in Photolithography Techniques
OpenAlex ID
W4414978540

평가 내용

관련도
관련
사업분야
엔지니어링 플라스틱 · (22) POM(폴리아세탈)
부 분야
–
R&D 측면
–
확신도
–
근거 용어
–
판단 근거
임베딩 유사도 기반 평가로 '엔지니어링 플라스틱 > POM(폴리아세탈)' 분야에 선정된 논문입니다.
R&D 활용 포인트
–
평가 입력
–

초록

Debugging and monitoring programs are integral to engineering and deploying software. Dynamic analyses monitor applications through source code or IR injection, machine code or bytecode rewriting, virtual machine APIs, or direct hardware support. While these techniques are viable within their respective domains, common tooling across techniques is rare, leading to fragmentation of skills, duplicated efforts, and inconsistent feature support. We address this problem in the WebAssembly ecosystem with Whamm, an instrumentation framework for Wasm that uses engine-level probing and has a bytecode rewriting fallback to promote portability. Whamm solves three problems: 1) tooling fragmentation, 2) prohibitive instrumentation overhead of general-purpose frameworks, and 3) tedium of tailoring low-level high-performance mechanisms. Whamm provides fully-programmable instrumentation with declarative match rules, static and dynamic predication, automatic state reporting, and user library support, achieving high performance through compiler and engine optimizations. The Whamm engine API allows instrumentation to be provided to a Wasm engine as Wasm code, reusing existing engine optimizations and unlocking new ones, most notably intrinsification , to minimize overhead. A key insight of our work is that explicitly requesting program state in match rules, rather than reflection, enables the engine to efficiently bundle arguments and even inline compiled probe logic. Whamm streamlines the tooling effort, as its bytecode-rewriting target can run instrumented programs everywhere, lowering fragmentation and advancing the state of the art for engine support. We evaluate Whamm with case studies of non-trivial monitors and show it is expressive, powerful, and efficient.

본문 70,958자

본문 펼치기
Debugging and monitoring programs are integral to engineering and deploying software.Dynamic analyses monitor applications through source code or IR injection, machine code or bytecode rewriting, and virtual machine or direct hardware support.While these techniques are viable within their respective domains, common tooling across techniques is rare, leading to fragmentation of skills, duplicated efforts, and inconsistent feature support.We address this problem in the WebAssembly ecosystem with Whamm, a declarative instrumentation DSL for WebAssembly that abstracts above the instrumentation strategy, leveraging bytecode rewriting and engine support as available.Whamm solves three problems: 1) tooling fragmentation, 2) prohibitive instrumentation overhead of general-purpose frameworks, and 3) tedium of tailoring low-level high-performance mechanisms.Whamm provides fully-programmable instrumentation with declarative match rules, static and dynamic predication, automatic state reporting, and user library support, while achieving high performance through compiler and engine optimizations.At the back end, Whamm provides instrumentation to a Wasm engine as Wasm code, reusing existing engine optimizations and unlocking new ones, most notably intrinsification, to minimize overhead.In particular, explicitly requesting program state in match rules, rather than reflection, enables the engine to efficiently bundle arguments and even inline compiled probe logic.Whamm streamlines the tooling effort, as its bytecode-rewriting target can run instrumented programs everywhere, lowering fragmentation and advancing the state of the art for engine support.We evaluate Whamm with case studies of non-trivial monitors and show it is expressive, powerful, and efficient. Introduction For decades, software tooling has supported developers in debugging and optimizing their programs.While static analysis can provide many definitive insights about programs, only in running and observing programs through a dynamic analysis do we truly understand them.Tools outside of a program, such as the operating system kernel, libraries, and profilers can observe events like page faults, cache misses, branch prediction misses, I/O events, and signals.These tools can report event statistics and memory usage without additional instrumentation.To gather more specific information, programs can be instrumented at the source level (either manually or via the compiler), at the bytecode/IR level, at the emulator/VM level, or with dynamic binary rewriting. Introduction We use the term monitor to refer to a self-contained analysis that observes an application and reports on its behavior, such as execution patterns, internal states, and resource consumption.Monitors may instrument a program by many strategies, modifying the program before or during execution, logging desired events, then generating a report after the program has terminated.Ideally monitors are non-intrusive; that is, they should not alter the semantic behavior of the program. Introduction Authors' Contact Information: Elizabeth Gilbert, evgilber@andrew.cmu.edu,Carnegie Mellon University, Pittsburgh, Pennsylvania, USA; Matthew Schneider, Carnegie Mellon University, Pittsburgh, Pennsylvania, USA; Zixi An, Carnegie Mellon University, Pittsburgh, Pennsylvania, USA; Suhas Thalanki, Carnegie Mellon University, Pittsburgh, Pennsylvania, USA; Wavid Bowman, Florida Institute for National Security, Gainesville, Florida, USA; Alexander Bai, Max Planck Institute for Software Systems, Saabrucken, Saarland, Germany; Ben L. Titzer, Carnegie Mellon University, Pittsburgh, Pennsylvania, USA; Heather Miller, Carnegie Mellon University, Pittsburgh, Pennsylvania, USA.Of course, any instrumentation may perturb performance by increasing execution time or other resource usage.A dynamic analysis tool should minimize overhead. Instrumentation Source-level instrumentation.It is possible to directly write monitor code within or alongside source code, but this has a number of drawbacks.Instrumentation may be unable to capture lowlevel operations such as I/O accesses, system calls, and signals.It creates cross-cutting concerns as source and monitor code are mixed and increases the risk that the monitor is intrusive.Often, the program incurs some performance hits as frequent checks are required. Instrumentation Rewriting.With rewriting, new instructions are inserted directly into the application at points of interest.These new instructions collect, summarize, and report various types of information gathered at runtime.Rewriting is usually performed programmatically, instead of manually, either at the source, bytecode, or machine-code level. Instrumentation Static rewriting involves injecting monitor code into the source code before compilation, or into bytecode or machine code before execution.This rewriting is decoupled from any particular execution platform, increasing portability and expressibility of the monitors.However, the inserted code may inadvertantly alter application behavior, even innocently.On native targets, the presence of additional code can alter program state, since a program may read its own code as data.With bytecode and machine code rewriting, the binary needs to be reorganized to fit the monitoring code, which may not always be possible, and source-level mappings need to be adjusted to handle the new code.Static rewriting tools include Oron [23] (at the source level) and EEL [16] (at the machine code level). Instrumentation In dynamic rewriting, monitoring code is inserted at runtime, which can avoid some of the limitations of static rewriting.For example, when an application dynamically generates new code, static rewriting might not instrument it, but dynamic rewriting can.However, dynamic rewriting requires more sophisticated tools, e.g.dynamic recompilation, which also means that additional instrumentation cost is paid at runtime.Dynamic rewriting tools include Jalangi [31] (at the source level) and DTrace [13] and Pin [19] (at the machine code level). Instrumentation Rewriting can be tedious and is not portable across ISAs.Composing multiple monitors is difficult, and most rewriting frameworks do not automatically handle the necessary parallel composition, as sequential composition would lead to one monitor instrumenting another. Instrumentation Emulation.Emulators and VMs such as QEMU [5] and Valgrind [25] track instrumentation points separately from the application code, requiring no rewriting or re-organization for better developer ergonomics.Instrumentation is invoked outside of the emulated state space, which prevents altering behavior.However, even with purpose-built JITs for emulation, this strategy incurs additional overheads due to unstructured machine code and is generally considered prohibitively expensive for heavy instrumentation. WebAssembly WebAssembly (Wasm) [14], is a portable, low-level bytecode format that serves as an efficient target for many programming languages, such as C, C++, and Rust.While initially designed to run programs on the Web, due to its sandboxed execution environment, it has gained adoption in other contexts, such as cloud computing [40], edge computing [1], and embedded systems [24].Wasm engines offer interpreter and compiler tiers, which together achieve quick startup times and high peak performance.Yet despite its spread and the sophistication of its engines, no standard instrumentation, profiling, or debugging interface for Wasm exists. WebAssembly State of Wasm instrumentation.Today, static rewriting of bytecode is the norm for Wasm instrumentation, but tooling is low-level and clunky.Several static instrumentation tools are available: BREWasm [10], AspectWasm [29], Walrus [3], and Wasabi [17].These tools are less mature than rewriting tools for platforms such as the JVM.Compared to native targets, however, static rewriting on the WebAssembly platform is less intrusive because the execution model features a Harvard architecture, a virtualized execution stack, and multiple memories.It is also portable, as Wasm abstracts above the host ISA.Wasm engines can also minimize the impact of certain overheads; JIT tiers can speed up checks on probes using adaptive optimizations.However, instrumenting bytecode is still tedious because injected code has to account for application semantics and deal with Wasm's stack machine architecture. WebAssembly Alternatively, with direct support in the virtual machine, e.g.Wizard [37], bytecodes can be instrumented without rewriting, which simplifies composability of instrumentation and makes it completely non-intrusive.Both the interpreter and compiler tiers of a Wasm engine can benefit additionally from optimizations specific to instrumentation.One such class of optimizations is called intrinsification in which the engine handles certain cases specially, e.g. by executing special logic in the interpreter and emitting specialized machine code for counter probes in the JIT. Contributions We illustrate the components of the Whamm system in Figure 1 and explain our contributions here.This paper introduces the first expressive, retargetable DSL, codenamed Whamm, designed specifically for Wasm instrumentation, and two complete high-performance realizations of the system based on bytecode rewriting and novel engine support.With bytecode rewriting, the Whamm compiler can inject instrumentation directly into application code, resulting in an instrumented program that can run on any engine.Alternatively, when targeting an engine interface, the Whamm compiler generates a monitor module that can be loaded into a Wasm engine and reused across applications.To support high-performance instrumentation directly in the Wasm engine, we extend the Wizard Research Engine [35] with the ability to load monitors expressed as Wasm modules.Whereas previously, monitors could only be expressed as extensions that require rebuilding the engine, monitor modules are more flexible and extensible.We show a number of Whamm compiler optimizations, including partial evaluation of predicates, as well as engine optimizations such as custom trampolines and inlining instrumentation probes directly into the application code.In particular, we contribute: Contributions • A fully-featured declarative instrumentation DSL for Wasm, codenamed Whamm. Contributions We present Whamm, a domain-specific language for instrumenting Wasm code that is expressive and performant.Our work is open source 1 and will be provided as an artifact. Contributions We demonstrate its language features with a suite of complex monitors.• The first DSL that abstracts above the instrumentation strategy.Whamm can either rewrite bytecode, allowing monitors to run on any Wasm engine, or interface with the engine directly, which can be more composable and efficient.• A suite of compiler optimizations that reduces overhead.We show how the design of Whamm admits optimizations in both the bytecode-rewriting and engine strategies to reduce the overhead of instrumentation both statically and dynamically.• An engine monitoring interface, codenamed whamm, based on monitor modules. Contributions We show how Wasm engines can support extensible dynamic instrumentation by loading ordinary Wasm modules that conform to a monitor interface.• A suite of engine optimizations that increases performance for whamm.We show how Wasm engines can optimize both interpreter and compiler tiers, achieving performance competitive with that only previously available via special-case optimizations.• An empirical evaluation on standard benchmarks and workloads.We demonstrate the effectiveness of Whamm's language features and optimizations through a detailed evaluation of several complex monitors, comparing bytecode rewriting and direct engine support with similar dynamic analysis tools like Pin and Wasabi.Our detailed analysis isolates individual overheads in various tiering configurations to study the effectiveness of each optimization. Instrumentation with Whamm Whamm is a domain-specific language for WebAssembly instrumentation that solves the longstanding problem of either suffering poor performance, poor portability, or poor composability.It enables developer tooling while reducing hassle since it abstracts above the instrumentation technique to best leverage the capabilities available at runtime.Whamm can instrument applications using the bytecode-rewriting technique or by interfacing with an engine that directly supports instrumentation.Together, our language supports a broad domain of applications without requiring a developer to reimplement their monitors to satisfy any particular instrumentation technique or engine. Part Description provider The provider that supports the capability used in the probe.package Part Description provider The package within the provider that supports the capability used in this probe.event Part Description provider The event describing the location(s) for probe insertion.mode Part Description provider The mode used to emit probe actions at target location(s), e.g.before and after. Part Description provider Table 1.A match rule in Whamm consists of four parts. The Whamm Language The syntax of Whamm is inspired by Dtrace [13].Users can express their instrumentation as directives that reference program events and provide predicated actions, as shown in Figure 2.This form can be read as, "When this event occurs during program execution, do these actions if this predicate is true."A directive provides a high-level and intuitive syntax that specifies events at various granularities in the target program.For example, a developer can instrument a function exit either by targeting the wasm:function:exit event provided by the DSL or by explicitly handling each bytecode that may return. The Whamm Language 2.1.1Match Rules.The match rule part of the directive constitutes a syntactic pattern matching one or more program events.It consists of four parts: provider, package, event, and mode, described in Table 1.Each part of the match rule decreases in specificity until reaching the mode of the probe. The Whamm Language As an example, consider the following match rule: The Whamm Language wasm:opcode:br_if:before This rule can be read as, "Insert this probe before each of the br_if Wasm opcodes in the program." Match rules support wildcards to enable matching on a group of events in the same manner.For example, the match rule wasm:opcode:*load*|*store*:before matches all variants of the load and store Wasm opcodes.It is possible to modify program behavior through leveraging the alt probe mode.This mode replaces the original logic at some target bytecode location.Currently, this feature is only fully supported by the Whamm rewriting target. The Whamm Language Type Bounds.Some Wasm opcodes, such as call, local.set,and select are polymorphic, meaning their inputs and outputs can be different types depending on the function or module context.Variables in a probe body that refer to these arguments have types that depend on the match site.One way to address this would be to introduce polymorphism or dynamic typing.Instead, to avoid dynamic typing, we require programs to use type bounds, where the user expresses the expected types of each argument in scope to the probe body when there is more than one static possibility.A type mismatch is then considered a no match for the rule2 .Thus, correct matching requires knowing the types of all arguments at all program points. Predication. A programmer can instrument events under specific conditions with predicates, expressions that must evaluate to true for a probe to be executed.An empty predicate is always true.A probe can be predicated on either static information, such as the match location within a function (pc), or dynamic information, such as the instruction's arguments, or a mix of the two in a compound logical expression.The Whamm compiler implements predication differently depending on the target.For bytecode rewriting, the Whamm compiler performs constant propagation and folding for statically-known variables such as the pc and function id (fid).If any residual predicate remains, the compiler injects instrumentation on the remaining dynamic part.For the engine target, it will automatically split the predicate into static and dynamic parts, with the dynamic part compiled into the probe actions, as explained in Section 4.1.In Figure 2, the matched call opcodes are statically constrained to those located in function 53 (specified by the static predicate fid == 53) with an i32 as its first argument (specified by the type bound on arg0).The body of the probe will be wrapped in code to check the dynamic part of the predicate (arg0 == 3).When injecting an alt probe, dynamic predication guards the consequent actions with the original logic in the else block. Storage Classes. A strikingly useful feature of Whamm is a unique set of storage classes for variables that simplify managing the lifetime and reporting of collected data.For example, variables can be global or local, shared across multiple matches, or shared per execution frame. Storage Classes. Bound functions and globals.Data specific to the event, such as the operands to an instruction, the location of the match, or the name of a target function in a call are directly available in the probe body as bound variables.Each part of the match-rule hierarchy binds appropriate functions and globals.For example, the wasm:opcode:call event provides a target_fn_name variable that can be used in the probe directive to refer to a function's name in the name section.While some variables are statically-known for a match location, other variables are dynamic.In Whamm, all arguments to an instruction (i.e.values on the Wasm operand stack) are available as named, typed variables arg0, arg1, etc.To aid users, we have implemented a nifty CLI tool to query the provided functions and variables in scope for a probe directive. Storage Classes. Derived variables.Having useful, relevant variables in scope automatically, along with match rule wildcards, can greatly simplify writing bytecode-level monitors.Sometimes such variables aren't inherently part of an event, but are easily computable from the other variables in the event.We call these derived variables.Consider a monitor which matches on all load and store Wasm opcodes.It can use the derived addr variable in the probe body to refer to the dynamic memory address.The addr variable is available for memory-access events and hides the fact that for loads, the target address is stored in the first argument, arg0, while for stores, this value is stored in the second argument, arg1.Other examples of derived variables include: taken, which for any branching opcode represents whether the branch will be taken 3 ; effective_addr, which for loads and stores represents the index + offset; and trap, which indicates whether the instruction will trap. Storage Classes. Unshared variables.Many instrumentation tasks requiring retaining information that is specific to a match site and should be maintained across invocations, such as a counter.In Whamm, a programmer may mark a variable as unshared, which means an instance of this variable is allocated for each individual match site of the match rule.The lexical scope of this variable is limited to the probe's body, but the variable will retain its value across multiple invocations of the body.This can be used to collect data at a specific program point over time.For example, the following script collects the number of times that a specific call opcode was reached during application execution: wasm:opcode:call:before { unshared var count: u32; count++; } Shared variables.Some instrumentation tasks are more easily expressed if variables are instantiated once across match sites.In a Whamm probe body, a programmer may mark a variable as shared, which means only one instance of this variable is allocated and is made available at every match site.Like unshared variables, the lexical scope of this variable is limited to the probe's body, and the variable retains its value across multiple invocations of the body, but it is not replicated per site.A small change to the previous script collects the number of times that any call opcode was executed dynamically. Storage Classes. wasm:opcode:call:before { shared var count: u32; count++; } Report variables.Obviously, the purpose of instrumentation is to observe and report.To make this quick and easy, Whamm allows programmers to annotate any variable with the report keyword to request its value be reported on exit.The compiler will automatically generate appropriate printing code, flushing a textual representation of the report variables, along with their match locations.Monitor state is printed to the console or appended to a specified file4 as a commaseparated value listing (CSV) that includes match locations.CSVs allow for easy analysis by other tools.To flush the results of the previous count example, simply add the report keyword to the variable: wasm:opcode:call:before { report shared var count: u32; count++; } Frame variables.Some variables are bound to the lifetime of a function activation.For example, profilers often record the time when a function is entered and the time when it is exited.Handling this properly with recursive functions and exceptions is surprisingly tricky and would otherwise require emulating the callstack.Instead, Whamm makes this easy with the frame keyword, which marks variables that should live within a single activation of the target function.With recursion, multiple copies of the variable co-exist simultaneously, and probe invocations get the appropriate copy for their activation.For the bytecode-rewriting target, the obvious (and efficient) implementation strategy is to simply introduce a new local variable to the function.To support frame variables in the engine target, we modified the engine with the ability to append to a function's local variable declarations dynamically. User Libraries. A common problem in DSLs is feature creep where more and more generalpurpose language features are added, compromising its original focus, becoming a frustratingly underpowered language with limited usefulness.Rather than risk feature creep, we instead see Whamm as a way to weave instrumentation logic into a program, or to generate a monitor that can allow an engine to do that for us, similar to aspect-oriented programming.Thus, we can specialize the language itself towards instrumentation expressiveness rather than general-purpose programming. User Libraries. But how can we scale up to handle big instrumentation tasks?The answer is libraries.Whamm allows monitors to use libraries provided as Wasm code which the compiler automatically links in, relegating the heavy lifting of complex data structures and libraries to a user's preferred language.This extensibility prevents the DSL from becoming unwieldy. User Libraries. For example, we leveraged this capability in the implementation of the map type in Whamm.The map type provides an efficient general-purpose associative array.Its implementation is provided by a Wasm module implemented as a Rust HashMap.Further, the cache-simulator monitor in our evaluation, see Section 5.1, demonstrates how to use a Wasm module as a monitor library.The cache model is implemented in Rust, compiled to Wasm, then linked with Whamm.The Whamm script just focuses on expressive matching and reporting, while the cache model is separate and can be independently swapped.We envision that library support will allow users to add powerful online statistical processing and sampling strategies by writing the complex parts in a general-purpose language and then linking them with Whamm scripts. Implementing Whamm We implemented a compiler for Whamm in approximately 33,000 lines of Rust that parses scripts, performs semantic analyses, implements optimizations, and outputs Wasm supporting two distinct targets: bytecode rewriting and engine support.In the rewriting target, the user supplies scripts and the application module(s) being instrumented together to the Whamm compiler.The compiler then applies all match rules, compiles probe bodies to Wasm, and injects instrumentation with rewriting, creating the final instrumented module.For the engine target, the user need only supply the Whamm scripts, and the compiler produces a standalone Wasm module that implements a monitor according to the whamm monitoring interface.The phases of compilation are what one would expect from a traditional compiler, but with interesting behaviors that stem from: (1) collecting and analyzing bound variables per directive, (2) planning variable-storage allocation, and (3) matching probe events, only for the rewriting target. Bound Variable Collection, Vec〈Whamm-param〉 As explained in Section 2.1.3,bound variables represent static and dynamic data at match locations, like the pc or an operand to an instruction.For each directive, Whamm gathers used variables into a Vec〈Whamm-param〉.By knowing exactly what a probe directive needs and communicating this to the implementation strategy, the data transfer overhead can be minimized. Planning for Storage Allocation Variables in Whamm can have storage classes such as frame, report, shared, and unshared (see Section 2.1.3).Storage allocation for such variables occurs at different times depending on the target. Planning for Storage Allocation With rewriting, the compiler is provided the application bytecode so that it can statically identify all match locations and thus allocate storage ahead of time.In probe bodies, expressions referencing such variables can be reduced to accesses of constant memory addresses.However, when targeting an engine, the Whamm compiler will produce a monitor module that is application-agnostic. Later, when applying a monitor module to an application module at load time, the engine will evaluate match rules against and attach probe callbacks to the appropriate application locations.This latebinding of probes requires the engine and monitor module to cooperate to allocate storage for appropriate variables.As we'll see in more detail later, the engine supports match-time callbacks so the monitor module can invoke logic each time a new match is found in an application.The Whamm compiler emits callbacks that allocate appropriate per-match-site variables and provide their addresses to probe callbacks.The probe body uses this memory address as the dynamic part of the address and uses the static offset value of a Wasm load and store operation to point to the appropriate variable location within that allocated region. The Rewriting Strategy With bytecode rewriting, the compiler directly injects monitoring logic into the application code. 3.3.1 The injection library: Seal.A critical component of the rewriting target is code to load, parse, reflect on, and modify Wasm binary modules.As part of this work, we developed a new, fully-featured open-source Rust library for instrumenting Wasm code, codenamed Seal.Seal allows loading application bytecode into an intermediate representation (IR) that can be decorated.We use decorators on functions and opcodes to attach the bytecode-to-inject with directives on where the logic should be injected while emitting the application bytecode post-instrumentation. 3.3.1 The most basic opcode-level decorators have three behavior options: before, after, and alternate.At module-encoding time, the before and after decorators inject the new bytecode at the original opcode. 3.3.1 The alternate decorator injects the new bytecode instead of the original to support application manipulation.The function-level decorators have two behavior options: entry and exit.At moduleencoding time, the entry decorator injects the new bytecode at the top of the instrumented function.The exit decorator injects the new bytecode at all locations that could return in the function body.Seal's bytecode-injection mechanisms enable library consumers to more easily inject new bytecode into a Wasm module without having to consider the lower-level impacts of injection. Identifying match locations. As shown in Figure 3, we traverse the application bytecode to identify match locations, represented as Seal IR.When the match rule succeeds, the compiler inspects the Vec〈Whamm-param〉 to define appropriate data and partially evaluate the predicate through constant propagation.If the predicate reduces to an expression that depends on dynamic information, the compiler will emit code to evaluate the residual expression as a guard before the probe body. Emitting probes at match locations. Because Wasm defines a stack machine, injecting probe logic that uses the input operands to an instruction needs a minor amount of fiddling.Figure 3 also shows the three parts of an emitted probe: the parameter prologue, the probe itself, and the stack reset epilogue.If Vec〈Whamm-param〉 contains dynamic data requests, these must be bundled as a probe prologue in the emitted bytecode and bound to newly-injected local variables in the function body.Variables representing operand-stack values are initially saved as locals that can be used in the dynamic predicate and probe body, but cleared later to restore the operand stack.Our compiler uses local rewrites to avoid reorganizing the operand-stack usage for the entire function. The Engine Interfacewhamm While Whamm scripts can run on any engine with the rewriting target, the Whamm compiler can also target an engine monitoring interface where the application and monitor are provided separately.The explicit separation between application and monitor provides additional capabilities and performance improvements over rewriting.First, preserving the original application code preserves source mappings, making it easier to trace issues identified during instrumentation back to the underlying source.Second, the engine can enforce separation between monitor state and application state so that a monitor cannot inadvertently alter program behavior.Third, the engine can compose multiple monitors simultaneously.Fourth, monitors can be applied more selectively (e.g.per-function) and dynamically added and removed, which unlocks new use cases such as enabling and disabling heavyweight instrumentation to observe rare program behavior.The engine can completely remove instrumentation when not in use, imposing zero overhead.Fifth, we can instrument events not observable in bytecode, such as loading/verification/compilation of functions, garbage collection, thread scheduling, or other VM-level operations.3.Each step of static instrumentation with Whamm when targeting the bytecode rewriting.In (0) the application module is visited to find event matches, then at a match, (1) the variables referenced by the probe body are saved, (2) the probe body is compiled to WebAssembly and inlined, and (3) the stack is restored to its original state for the original application bytecode.Fig. 4. Example of compiling Whamm to the engine target whamm.The monitor script (0) is compiled to a complete Wasm module that encodes match rules in the names of exported functions.At load time, the engine loads the monitor module and (1) applies the match rules to the application-under-observation.At runtime (2), when a probed location is reached, the engine will (3) bundle the requested data and invoke the probe callback.Monitor modules (whamm-monitor).Recently, [37] demonstrated the first programmable engine instrumentation for Wasm.Monitors are written against engine-specific APIs and compiled-in at VM build time, but are not portable across engines.This has allowed many standard, reusable monitors for Wizard, as well as ad-hoc analyses.Instead, we discovered that Wasm engines are shockingly good at loading Wasm modules and thus the ideal monitor should be provided as a Wasm module.Thus, we designed an API, where the engine accepts monitor modules which provide instrumentation via exported functions.These regular Wasm modules, referred to as whamm-monitors, conform to a naming convention which is a simplified form of the Whamm language, called whamm.Figure 5 shows an example monitor module that targets the engine interface whamm. The Engine Interfacewhamm Declarative matching via export names.While the rewriting target applies match rules and predicates to a specific application module, there is no application module yet when generating a monitor module.We require load-time matching when applications are loaded.We considered several design alternatives, such as (1) the Wasm engine provides the raw bytes of the application module to the monitor module, which could then re-parse it; (2) the monitor module can reflect on the application module through an engine API; or (3) the monitor module communicates matching rules/predicates to the engine declaratively.We chose the last alternative to reduce duplication between engine and monitor and avoid a costly reparse.In this scheme, the information necessary for matching-the match rule, static predicate, and runtime data-is encoded in the export name of the function as shown in Figure 5.The engine parses these export names and performs a simplified form of Whamm-matching to insert engine-level probes.For an engine that already supports dynamic instrumentation via probes, the implementation effort required is low (600 lines of code to parse/match/attach/invoke and 250 lines of interpreter and JIT optimizations). The Engine Interfacewhamm Probe and predicate functions.The Whamm compiler optimizes and translates probe bodies to Wasm functions, called whamm-functions.Predicates must be evaluated by the engine at match time, so the Whamm compiler splits predicates into static and dynamic portions.Static predicates are wrapped in Wasm functions that can be referred to by their export name in the match rule and dynamic predicates are inlined into the probe bodies, see Section 4.1.Both predicates and probes may accept whamm-params that correspond to Whamm variables, and the engine provides their values (whamm-args) at match time or runtime as necessary. The Engine Interfacewhamm Typechecking during matching.Some events have polymorphic operands, such as select or local.get.Type bounds are effectively a type predicate for variables, as described in Section 2.1.1.In Wasm bytecode, the types on the operand stack may change from instruction to instruction.Checking the types of argN variables would therefore seem to be similar to Wasm code validation, a surprisingly tricky endeavor.Yet, hallelujah, an engine already has a code validator!We extend the code validator with a callback so that the types of argN and localN variables are known at potential probe match sites.For most rules, a type mismatch is an error, but for type bounds, a mismatch means the probe is simply not inserted.This is the most complicated part of our engine changes, at approximately 200 lines of code.Match time callbacks.Many monitors store per-match-site data, such as unshared variables in Whamm.To implement this functionality and more, the engine interface allows match-time callbacks that are invoked by the engine when a match rule and its static predicate are true.A whamm-monitor encodes match-time callbacks into the export name per the grammar shown in Figure 6.Statically-known whamm-args, like the function id (fid) and program counter (pc), are allowed as arguments to match-time callbacks.The whamm-monitor can, for example, allocate a new entry in its internal monitoring data structure and return the entry's address in the whamm-monitor's memory.The engine passes the result of the static callback-a constant-to the probe function.Thus one export can define a match rule with a single probe function that accepts custom data for each match site.The Whamm compiler uses this functionality to implement storage for unshared variables. The Engine Interfacewhamm Start function and exit callback.In addition to matching on instruction patterns, the engine interface also allows the normal Wasm start function.Similarly, to report the result of the analysis, the engine will invoke an exported function named "wasm:exit" upon application termination. The Engine Interfacewhamm Summary: an adoptable engine interface.The engine interface retains a simplified form of declarative matching expressed in export names but uses Wasm functions for almost everything else: probes, predicates, and reporting logic.This is to minimize engine burden in hopes of future adoption by more engines.We chose to prototype the engine interface in the Wizard Research Engine, which already supports a set of bytecode-level dynamic probing mechanisms.Using these existing mechanisms, we implemented the engine interface in just 600 lines of new code.With an already well-encapsulated Monitor extension point, extending the Wizard engine to support Whamm was as simple as writing a new subclass that loads a whamm-monitor, parses its export names, applies match rules, and inserts custom probes that invoke Wasm functions and reporting at exit, all without changing any existing code.This provided an unoptimized baseline to which we later added interpreter, JIT, and runtime optimizations that we detail in the next section.In particular, implementing whamm-args naively uses a relatively expensive reflective access to the caller's frame in the probe code.Nevertheless, given the low implementation burden, we believe that other engines could adopt the same engine interface and thus whamm-monitors could one day work on any engine. Optimizing Whamm In this section, we detail optimizations designed to reduce the overhead of instrumentation with Whamm.These optimizations fall into two main categories: static optimizations applied by the Whamm compiler, and dynamic optimizations implemented in the engine. Whamm Compiler Optimizations Optimizations can be applied by the Whamm compiler for both the rewriting and engine targets. Whamm Compiler Optimizations Constant propagation.Constants appear surprisingly often in Whamm expressions, since match rules, predicates, and probe bodies often mention bound variables such as pc that become constants for a given match site.The Whamm compiler employs constant propagation to emit better instrumentation code and to optimize predicates.Deciding more predicates statically means fewer probes will be inserted, and partially evaluating them minimizes the complexity of dynamic predicates. Whamm Compiler Optimizations Minimizing operand stack shuffling.The Whamm compiler instruments Wasm bytecode by rewriting in a mostly linear pass.When probes use instruction operands (i.e.argN), some stack shuffling is required.To avoid a global rewrite of an entire function (e.g.reallocating local variables or reshuffling the operand stack), the compiler instead emits the minimal operand-stack save-andrestore code before emitting instrumentation inline.With argN meaning the -th operand, and arg0 referring to the top of stack, the number of operands that must be saved is equal to + 1, being the largest used in the directive. Whamm Compiler Optimizations Static/dynamic splitting of predicates.Match predicates in Whamm can involve both static and dynamic variables, while in the engine interface, static predicates may be expressed as part of export names.To map Whamm predicates onto the engine monitoring interface, the simplest implementation strategy is to classify all predicates as dynamic and emit code within probe bodies to evaluate them.While correct, this results in instrumenting more match sites and imposing more overhead than necessary on each probe invocation.A somewhat smarter strategy is to conservatively classify predicates as either fully static or fully dynamic.This is the strategy the current compiler uses. Whamm Compiler Optimizations However, in the future we can do better by classifying subexpressions as static, which are known at match time, and dynamic, which are only known at runtime, and applying partial evaluation.For example, consider a static variable A and a dynamic variable B in the predicate expression = A || B. By definition, static variables are known before dynamic variables, so we can split the evaluation of an expression into two stages.In the first stage, static expressions are evaluated, and to derive expressions for the second stage, we substitute into the original expression all the possible outcomes and then apply constant propagation, folding, and strength reduction to obtain a residual dynamic predicate.To see this, let's assume A evaluates to either true or false.We can then generate two different residual dynamic predicates, one where A↦ →true, and one for A↦ →false.These are, respectively, true || B and false || B. After constant folding and strength reduction, we have true and B. Whamm Compiler Optimizations While symbolic approaches might apply, an effective brute-force strategy is: Whamm Compiler Optimizations (1) Categorize each subexpression as static or dynamic. Whamm Compiler Optimizations (2) Generate a truth table for the outcome of each static subexpression. Whamm Compiler Optimizations (3) Substitute constants, fold, and strength-reduce to produce residual dynamic predicates. Whamm Compiler Optimizations (4) Collect results, combine equivalences, and simplify.Figure 7 shows an in-depth example of this static analysis.After optimization, the whamm-monitor contains two specialized probes for the single DSL probe expression.One probe is purely statically predicated, while the second probe handles the case where the static predicate is false, requiring the dynamic residual to be evaluated. Engine Optimizations The Wizard engine supports dynamic instrumentation by allowing user-defined probe objects to be attached as specific Wasm instructions.These probes are implemented in the engine (i.e. as host code) and as we've seen, form the basis of our whamm-probe implementation.This section details three key optimizations to reduce overheads associated with this mechanism. Engine Optimizations To understand the runtime overhead of the engine-probe mechanism, consider the definition of a Wizard probe.Such probes are defined in terms of Virgil classes with a fire() method that is called at runtime.The method receives a (function, pc) pair and a handle to a lazily-allocated reflective FrameAccessor object for accessing the operand stack, locals, etc. Flexible instrumentation is seamlessly supported in both the interpreter and JIT tiers of the engine but requires a relatively complicated transfer sequence from application code to probe code, consisting of these steps: Engine Optimizations (1) commit program state to the machine frame (2) save interpreter or JIT VM state, such as a value stack pointer (3) look up the probe object through indirections from the instance pointer (4) invoke the (host) probe function Depending on the complexity of the probe's logic, it may incur additional overheads as it executes: Engine Optimizations (1) potentially redundant computation in the probe function (2) materializing the FrameAccessor object if requested (3) expensive frame access via indirection through the FrameAccessor In practice, most probes don't require the full generality of the probing system, allowing for engine optimizations that remove some of the above steps at probe callsites.Removing unneeded work in the transfer sequence and making accessing of program variables cheaper are key to reducing instrumentation overhead.This is especially true for the whamm-probe, which implements an adapter that transfers data from application Wasm context, through the runtime, then back to another Wasm context.This is relatively expensive to do via the engine APIs.In the general case, each Whamm function may have both constants and variables as whamm-args; the probe loads their values from the FrameAccessor and calls the target function with the whamm-args using the engine's host-to-Wasm call mechanism.A host-to-Wasm call has relatively high overhead, as it may allocate a fresh machine stack 5 for the call and the whamm-args for the call are copied to and 5 as part of Wizard's support for the stack-switching proposal from the heap at least once.To reduce overhead of whamm-probe instrumentation, we identified three optimizations: Engine Optimizations 1. (Interpreter) Use trampolines for customized transfer and a Wasm-to-Wasm call.Since the whamm-probe object is an engine-level wrapper around a Wasm function, the obvious optimization is to avoid the expensive transfer sequence detailed above and simply make the application-to-instrumentation transfer a regular Wasm call.The only wrinkle is that whamm-args must be passed somehow.Since these are known at binding time and consist of a mix of constants and frame accesses, we wrote a trampoline generator which produces a small machine code stub for each whamm-probe signature, at match time, before program execution.The trampoline contains the minimum machine instructions specialized to each unique signature that performs the necessary moves to/from the operand stack slots to properly load whamm-args.It then performs a Wasm-to-Wasm call to the whamm-probe function, landing either in JIT-compiled code for the function or back in the interpreter, with a new frame for the probe function.After the call returns, the trampoline dispatches on the original application opcode to continue execution.One trampoline is generated for each unique whamm-probe signature, so many probe objects sharing the same signature also share trampolines, incurring minimum space and time overhead.This optimization completely removes the need to materialize a FrameAccessor or setup an entirely new execution context with the Execute API. Engine Optimizations 2. (JIT) Replace the call to the whamm-probe with a direct Wasm call in the compiler.Wizard employs a single-pass compiler [36] (SPC) as its JIT tier.This compiler already contains support for intrinsifying counter and top-of-stack probes [37].We extend the compiler with an additional case for when a probe at a given site is attached to a whamm-probe function.Like interpretation, we can benefit from specializing the transfer sequence, but further, the compiler can inline this transfer sequence.As the SPC allocates registers and tracks constants via abstract interpretation, the inlined transfer sequence is more efficient.Moreover, the JIT can emit a direct call to the whamm-probe function. Engine Optimizations 3. (JIT) Inline compiled whamm-monitor functions directly into application code.We can further reduce overhead for probes by inlining the probe's Wasm code into application code at probe sites.This avoids the cost of a Wasm function call-a disproportionate overhead when considering many probe functions are short sequences that should be no more than a couple machine instructions.Inlining short probe functions is a big win that brings fully programmable instrumentation up to speed with handwritten intrinsification.For comparison, Wizard's prior CountProbe intrinsification allowed using simple counter probes at the cost of just two inline machine instructions per probe site.Expressing a counter as a whamm-probe with six Wasm instructions to load, increment, and store an internal memory address results in the JIT compiler inlining just five machine instructions 6 . Engine Optimizations We chose to implement very simple 7 inlining in the SPC as a proof of concept.As a state-of-theart optimizing baseline compiler, the SPC performs abstract interpretation and models the control stack during a single forward compilation pass.When reaching a whamm-probe, we modify the SPC to simply push a new control stack entry modeling the entry of a new function, juggle a bit of abstract state, and then continue to generate code from the bytecode of the inlined function.Since the SPC already models constants and allocates registers, the new bytecode is inlined and optimized in the context of the application code.In particular, locations of whamm-args are statically known at the site and any results from match-time callbacks become constants.This means that match-time Table 2.The suite of monitors implemented for this evaluation. Programmability and Usability For our evaluation, we implement a diverse set of monitors (Table 2) to demonstrate that Whamm provides concise programmable instrumentation with high performance.Through specializing Whamm syntax to commonly-needed operations and storage primitives, fewer lines of code are needed.For example, it only requires 4 lines of code for a simple hotness monitor and 13 lines of code for the relatively complicated cache_sim monitor.In contrast, manual bytecode rewriting with Walrus/Seal require tedious handwritten bytecode and careful management of instrumentation metadata.For more general programmability, Whamm scripts can rely on libraries.This greatly simplifies complex monitoring problems.For example, a succinct implementation of the imix monitor can be written in 4 lines of code, shown below.report var dyn_categories: map<u32, i32>; wasm:opcode:*:before { dyn_categories[category_id]++; // category_id is a bound variable } Programmability and Usability The above is short, but it has poor performance due to the use of a library-provided map data structure.Whamm is flexible enough to support multiple implementations of a monitor, optimized to the desired criteria (conciseness, performance, maintainability) of the user.A faster version of the imix monitor could use one directive per opcode: report var control: u32; report var arith: u32; ... wasm:opcode:br_if:before { control++; } wasm:opcode:i32.add:before{ arith++; } ... Performance To evaluate the performance of monitoring with Whamm, we evaluate overhead costs on the Polybench/C benchmark suite [27] with the medium dataset.We evaluate our system against the state of the art Pin [19] machine-code-rewriting instrumentation framework, by compiling the Wasm binaries back to C using wasm2c, then to x86-64 using gcc.To obtain baseline (uninstrumented) execution times (see Table 3 in the appendix), we ran each benchmark on Wizard interpreter/JIT tiers, natively, and on V8 using the --liftoff-only option, and averaged execution times from five runs.We used (patched) Wizard version 25.2717,V8 version 13.6.0,wasm2c version 1.0.34,Pin version 3.31, and gcc version 13.2.0 with optimization level -O2.All experiments were performed on a machine with two Xeon Platinum 8168 CPUs @ 2.70GHz with 384GiB of DDR4 RAM.Given uninstrumented execution time in seconds, , and instrumented execution time, , we use the term relative overhead to refer to the ratio / , while absolute overhead refers to − . Whamm vs. State-of-the-Art Wasm Frameworks.We compare the relative overhead imposed by Whamm rewriting and whamm-monitors with state-of-the-art WebAssembly instrumentation frameworks by implementing reasonably-equivalent monitors in Native Wizard instrumentation, Wasabi [17], IR injection with the Seal and Walrus libraries [3], and handwritten whamm-monitors, see Figure 8.We ran each of these implementations in their most-optimized configuration on the Wizard JIT.As Wasabi requires an engine that runs JavaScript, we ran the Wasabi experiments on the V8 engine and limited execution to its baseline compiler, Liftoff, for a fair comparison 9 .The uninstrumented time for each benchmark can be found in Table 3. Whamm vs. Whamm vs. Wasabi.Wasabi rewrites Wasm bytecode to inject runtime calls to JavaScript instrumentation.While this is known to be expensive for Wasabi, our results show that whamm instrumentation is similarly expensive when implemented as runtime calls.Both impose orders of magnitude overhead, shown in results for Wasabi and our unoptimized int-rt-*/jit-rt-* configurations in Figures 9 and 10. Whamm vs. Whamm vs. Walrus/Seal.Directly injecting into Wasm IR with library support allows one to inject custom, nearly optimal, bytecode via rewriting.For short-running programs, our data shows that the binding and setup time dominate, giving the advantage to Walrus/Seal and native.However, for long-running programs, all monitors show both Whamm targets to be competitive with manual rewriting, usually within 1.5 − 2×.Another difference is that the Walrus/Seal monitors use more optimized handwritten bytecode injection that has denser data representations, and do not actually report results.Improvements to the Whamm compiler could lessen this gap. Whamm vs. Whamm vs. Native Wizard Instrumentation.Native Wizard instrumentation is highly optimized for special cases via JIT intrinsics that recognize special Probe definitions in the engine API.For example, the CountProbe optimizes the emitted machine code to just two instructions, and the top-of-stack probe directly passes the top-of-stack to probe callbacks.The impact of such optimizations is seen in the efficiency of the hotness and branches monitors.Native engine instrumentation is faster than Whamm on small benchmarks for the cache_sim monitor, partly due to the overhead of JIT inlining for whamm-monitors and stack manipulations for the Whamm rewriting target.Note that the native top-of-stack specialization duplicates the semantics of a whamm-monitor requesting the arg0 variable.While our engine interface does not yet beat the performance of these highly specialized probes, it is still competitive, and as we discuss in future work, more compiler optimization can narrow this gap. Whamm vs. Whamm vs. Handwritten Monitor Modules.We demonstrate the effectiveness of the Whamm compiler by implementing logically-equivalent handwritten whamm-monitors designed for maximum performance.While the handwritten branches monitor is more performant than that of Whamm for short-running programs (mostly load time effects), for long-running programs Whamm monitors have nearly-equivalent performance as the handwritten bytecode. whamm Interpreter Optimization.We evaluate the effect of the trampoline interpreter optimization in Figure 9.In the base case, int-rt-int, the application code is interpreted; when an instrumented point is reached, a runtime call is made to invoke the whamm-function.The instrumentation code itself is then interpreted.We show the impact of simply JITing the instrumentation code, leaving the runtime call as-is in the configuration int-rt-jit.The optimized case, int-tramp-int, leverages the trampoline that enables a direct call to the whamm-function, skipping the runtime call.Further optimization is then shown with the int-tramp-jit configuration, in which the trampoline calls a JITed whamm-function.We further show the performance of simply interpreting an application rewritten with Whamm. whamm For the branches and hotness monitors, trampoline generation imposes more overhead than runtime calls into JITed probes for short-running programs.However, as base program runtime increases, the runtime call dominates the overhead (consistent with the Wasabi framework).For the imix monitor, the trampoline saves the expensive runtime call, but lags behind rewriting performance.This is because the trampoline still has an indirection overhead, whereas rewriting is inlined in the application code.As the cache_sim probe bodies are more complex than other monitors, JIT-compiling instrumentation provides a large performance boost as the overhead is dominated by actual instrumentation logic rather than the injection and invocation strategies. whamm 5.2.3 whamm JIT Optimization.We evaluate the whamm JIT optimizations in Figure 10.In the base case, jit-rt-int, the application code is JIT-compiled; when an instrumented point is reached, a runtime call is made to invoke the probe callback, the instrumentation code is then interpreted.We show the impact of simply JIT-compiling the instrumentation code, leaving the runtime call as-is in the configuration, jit-rt-jit.In the first optimized case, jit-wasm-int, the application code performs a direct Wasm call at the probe site in the compiled machine code, skipping the runtime call.The inlined case further optimizes probe invocation by directly inlining the whamm-function at the probe site.We further show the performance of simply JIT-compiling an application rewritten with Whamm. whamm In general, instrumentation overhead is dominated by the runtime call, if present.Removing this call overhead results in a large performance boost, as shown by jit-wasm-int.Further, inlining reduces the overhead costs to be even lower than rewriting!The branches monitor requires the use of arg0 to check the dynamic target of a branching opcode.As Wasm is a stack machine, accessing the top-of-stack when rewriting requires stack manipulations (see Figure 3), while the engine knows the value's register.This observation optimizes transfer time for inlined probe bodies.Inlining the imix monitors has similar performance to Whamm rewriting, regardless of base program run time.For the cache_sim monitor, we observe that, after removing the runtime call, the overhead is dominated by the time spent in the cache library, making optimization techniques for transfer cost negligible. whamm 5.2.4 Whamm vs. Pin, a Traditional Highly Performant Framework.We evaluate the performance of the imix, icount, and cache_sim monitors in Whamm against logically-equivalent implementations in Pin [19], a highly-performant machine-code rewriting instrumentation framework.We compare the absolute overhead, − , imposed by instrumenting machine code with Pin and JITed Wasm bytecode with Whamm rewriting and whamm.As shown in Table 3, the base performance of machine code is, on average, 5× faster than Wizard's baseline JIT compiler.As we are comparing absolute overhead, the difference in base runtime is factored out, but instrumentation code compiled in Pin has an advantage. whamm Figure 11 explains the various types of overheads imposed by each framework and compares the time required for each in Figure 12.We calculate the bind overhead with engine metrics for whamm (done statically in rewriting), the transfer overhead by instrumenting a program with empty probes, the report overhead through engine metrics for whamm and measuring the difference between instrumentation overhead with and without flushing for Whamm rewriting, and instr as the rest of the time.We calculate base bind for Pin by starting up the framework for each benchmark with no monitor activated and bind through taking the difference between the overhead of instrumenting with empty probes and base bind.We were unable to compute the transfer and report overheads for Pin, so these are not separated from the instr overhead in the charts. whamm For shorter running programs, especially for simple probes like those in imix and icount, the Whamm rewriting and engine strategies are more performant than Pin due to their small, Fig. 11.Visualization of the various overheads imposed by Pin, Whamm rewriting, and whamm-monitors.The base rows represent the runtime for uninstrumented machine code or bytecode.Rewriting imposes some static overhead to inject instrumentation into the program, bind cost.This cost is paid at runtime for both Pin and whamm-monitors.Pin requires some base bind overhead to start up the framework itself plus some cost relative to the size of the program.All three frameworks have overhead for transfer-ing into monitor code, executing it, (instr) and flushing monitor state (report). whamm or non-existent, bind overheads.The cache_sim monitor overhead becomes large for all longrunning benchmarks because these programs, except seidel-2d, all have a high number of memory read/write operations resulting in heavy instrumentation.While Pin is fastest for long-running programs, it clearly has large, fixed startup overhead and surprisingly large binding time.A significant portion of the gap for long-running programs is the code quality of Wizard's SPC JIT when compiling complex monitor code.We expect that in another engine with a more optimizing compiler, monitor code speed will approach optimal.Of note is that our performance was achievable with very few lines of code for all monitors (see Table 2) that is both intuitive and maintainable rather than having to write the low-level C code necessary for Pin instrumentation. Related work Dynamic analysis frameworks have been the subject of a vast amount of research.The main choices include whether to inject code into the source code or application binary, recompile programs with new instrumentation, or emulate the process space of a program.These approaches have various tradeoffs including performance overheads, intrusiveness concerns, usability, and composibility. Related work Code injection A common approach is to inject monitoring code directly into a program binary [43], either by injecting the entire monitoring code inline or with a trampoline that jumps to out-of-line instrumentation code. Related work Code injection can be performed statically, that is, separately from execution.For Java bytecode, early static tools include Soot [39] and Bloat [26].As Aspect-Oriented Programming (AOP) emerged, so did tools DiSL [20], AspectJ [15] and BISM [32,33] that targeted joinpoints.While AOP instrumentation was readable, it was unable to manipulate bytecode at a low-level.The ASM [9] framework is capable of low-level manipulation, but was considered user unfriendly.The later Clojure [38] bytecode manipulation library enables low-level manipulations in a readable, expressive, and concise notation. Related work Alternatively, code can be injected at runtime.DTrace [12,13], inspired by Paradyn [21], enables tracing at both the user and kernel layer of the OS with dynamically-injected trampolines.Dyninst [6] interfaces with a program's CFG and maps modifications to concrete binary rewrites.A user can tie instrumentation to instructions or CFG abstractions (e.g.function entry/exit).Recent research in this direction [11,28,41] focuses on reducing instrumentation overhead with low-level optimizations. Related work Fig. 12.The absolute overhead of the imix, icount, and cache_sim monitors when using the Whamm rewriting (left column), the Whamm engine interface (middle column), and Pin framework (right column) broken down by type of overhead (see Figure 11).Whamm Rewriting performance is computed for a JITed instrumented application and Whamm engine interface is run is its most-optimized configuration, JIT inlining of probes.Numbers are relative to uninstrumented execution time.Lower is better. Related work For Wasm, many solutions exist like [42], Walrus [3], BREWasm [10], and Wasabi [17], but none are as user-friendly as the bytecode rewriting framework presented in this work. Related work Recompilation.Compiled programs can be recompiled to inject code using several techniques.For example, compiled code can be lifted to a higher-level IR, instrumented, then recompiled.Early examples of static lifting for instrumentation include ATOM [34] and EEL [16].Etch [30], through observing an initial program execution, discovered dynamic program properties to inform static instrumentation.DynamoRIO [7] and Pin [19] use dynamic recompilation of native binaries for instrumentation.Their JIT compilers are purpose-built for instrumentation but since it runs code as part of the initial process, it is susceptible to being intrusive. Related work Emulation.QEMU [5] is a widely-used CPU emulator that virtualizes a user-space process while supporting non-intrusive instrumentation.Valgrind [25], primarily used as a memory debugger, is similar.They include JIT compilers that are not necessarily "purpose-built" for instrumentation, but for cross-compilation, leading to an inherent overhead over virtual machines. Related work Direct engine support.Virtual machines and their runtime systems can be designed with specific support for instrumentation.In .NET [4], users build profiler DLLs that are loaded by the CLR into the same process as a target application and interface through callbacks.The JVM Tool Interface [2] instruments Java bytecode by attaching callbacks to agents written against the lowerlevel engine API.However, evidence suggests that event handling in agents can be prohibitively slow and the engine API cannot handle events at the level of individual bytecodes.For Wasm, the Wizard Research Engine [35] provides support for monitoring by writing engine-specific probes in the Virgil language [37].Our work leverages the existing support in the Wizard Engine to deliver easy-to-implement, yet portable engine-level instrumentation. Related work Composability of analyses.Composability of instrumentation allows programmers to use multiple analyses simultaneously on the same application.Some instrumentation frameworks have attempted to promote interoperability of monitors through meta-modeling [8] and interface standardization [18].Other frameworks have emphasized the need for composable monitors in works such as the built-in Wizard VM instrumentation support [37] and a technique called polymorphic instrumentation [22].Virtual-machine based solutions like the JVM, .NET, and the Wizard engine allow general composability.Our engine monitoring interface goes further and enforces separation and composability of monitors by sandboxing them in Wasm modules. Conclusion and Future Work In this work, we presented Whamm, a declarative instrumentation language.The syntax of this language provides a semantically rich environment for natural instrumentation expression.We showed that Whamm's performance is competitive with performant frameworks due to novel optimizations, freeing users from writing low-level, tedious code. Conclusion and Future Work In future work, we plan to expand the instrumentable events in Whamm to support use cases such as security policy enforcement, modeling data flow between Wasm components, and resilience engineering tooling.We also hope to aid further in the debuggability of instrumentation through creating support frameworks such as an IDE plugin for live programming with Whamm and a test suite framework.Another area of interest is designing a Wasm reflection and instrumentation API to enable the monitors themselves to perform the static bytecode matching logic for dynamic instrumentation.We also have plans to further optimize Whamm performance through implementing predicate optimizations as outlined in Section 4.1.In this work we focused effort on prototyping in Wizard, which has advanced instrumentation, but no optimizing compiler.Since such a compiler would presumably be good at optimizing Wasm code, it would excel at optimizing application and monitor code together, particularly as probe bodies are usually short but may have many common subexpressions across match sites.Some instrumentation tasks might involve predicates that change infrequently, such as guarding detailed instrumentation for a rare program event.While the underlying engine supports dynamically removing and adding new probes one-by-one, each individual insertion/removal can deoptimize a function.Rather than insert many probes at once and then remove them all at once, instrumentation might be better expressed with predicates on global variables with the engine employing optimization guards [44].Our intention with whamm was to entice production engines into supporting a standardized monitoring API by limiting the additional mechanisms needed, e.g. by reusing Wasm modules deftly. Data-Availability Statement We plan to submit an artifact for evaluation.This artifact will include the benchmark suites used, scripts to run the experiments, usage instructions, and our experiment results as CSVs. Fig. 1 . Fig. 1.Contribution map of this paper.We have designed and implemented a new expressive DSL (codenamed Whamm) for concise instrumentation of Wasm applications with support for extensible libraries.It features an optimizing compiler that targets either rewriting (which injects bytecode into the application and can run anywhere) or a new engine monitoring interface (codenamed whamm) with high performance. Fig. 2 . Fig. 2. Directives in Whamm consist of a triple of a match rule that includes bounds on typed arguments, an optional predicate, and consequent actions. Fig. Fig.3.Each step of static instrumentation with Whamm when targeting the bytecode rewriting.In (0) the application module is visited to find event matches, then at a match, (1) the variables referenced by the probe body are saved, (2) the probe body is compiled to WebAssembly and inlined, and (3) the stack is restored to its original state for the original application bytecode. Fig. 5 . Fig.5.Illustrates the whamm engine interface.Wasm monitor modules encode directive match rules, type bounds and predicates into exported functions, expressing where to insert probes declaratively.The match time callback readies instrumentation state for each match location in the program.The variable request(s) or whamm-params, instruct the engine on the program state to pass during callback invocation. Fig. 6 . Fig.6.The grammar for whamm export names in a monitor module. Fig. 7 . Fig. 7. Illustration of splitting a mixed predicate (upper left) into subexpressions to evaluate statically and dynamically (right-side output), reducing predication overhead in the engine. Fig. 8 . Fig. 8.The relative execution times, log scale, of the branches, hotness, imix, and cache_sim monitors against state-of-the-art Wasm instrumentation frameworks across the Polybench/C benchmark suite sorted by absolute execution time.Ratios are relative to uninstrumented execution time.Lower is better. Fig. 9 . Fig.9.The relative execution times, log scale, of the branches, hotness, imix, and cache_sim monitors when instrumenting an interpreted program.We compare the base unoptimized case, int-rt-int, JITing the instrumentation of the unoptimized case, int-rt-jit, the interpreter trampoline optimization, int-tramp-int, and interpreting simple Whamm rewriting.Ratios are relative to uninstrumented execution time.Lower is better. Fig. 10 . Fig.10.The relative execution times, log scale, of the branches, hotness, imix, and cache_sim monitors when instrumenting a JITed program.We compare the base unoptimized case, jit-rt-int, JITing the instrumentation of the unoptimized case, jit-rt-jit, enabling the JIT wasm-to-wasm call optimization, jit-wasm-int, JITing the instrumentation of that case, jit-wasm-jit, inlining the instrumentation by the JIT, and JITing simple Whamm rewriting.Ratios are relative to uninstrumented execution time.Lower is better. Fig. 13 . Fig.13.Syntax of Whamm. Table 3 . The baseline execution time for the benchmark suites, uninstrumented.

섹션 본문 113문단 · 68,862자

(제목 없음)

1Debugging and monitoring programs are integral to engineering and deploying software.Dynamic analyses monitor applications through source code or IR injection, machine code or bytecode rewriting, and virtual machine or direct hardware support.While these techniques are viable within their respective domains, common tooling across techniques is rare, leading to fragmentation of skills, duplicated efforts, and inconsistent feature support.We address this problem in the WebAssembly ecosystem with Whamm, a declarative instrumentation DSL for WebAssembly that abstracts above the instrumentation strategy, leveraging bytecode rewriting and engine support as available.Whamm solves three problems: 1) tooling fragmentation, 2) prohibitive instrumentation overhead of general-purpose frameworks, and 3) tedium of tailoring low-level high-performance mechanisms.Whamm provides fully-programmable instrumentation with declarative match rules, static and dynamic predication, automatic state reporting, and user library support, while achieving high performance through compiler and engine optimizations.At the back end, Whamm provides instrumentation to a Wasm engine as Wasm code, reusing existing engine optimizations and unlocking new ones, most notably intrinsification, to minimize overhead.In particular, explicitly requesting program state in match rules, rather than reflection, enables the engine to efficiently bundle arguments and even inline compiled probe logic.Whamm streamlines the tooling effort, as its bytecode-rewriting target can run instrumented programs everywhere, lowering fragmentation and advancing the state of the art for engine support.We evaluate Whamm with case studies of non-trivial monitors and show it is expressive, powerful, and efficient.

Introduction

2For decades, software tooling has supported developers in debugging and optimizing their programs.While static analysis can provide many definitive insights about programs, only in running and observing programs through a dynamic analysis do we truly understand them.Tools outside of a program, such as the operating system kernel, libraries, and profilers can observe events like page faults, cache misses, branch prediction misses, I/O events, and signals.These tools can report event statistics and memory usage without additional instrumentation.To gather more specific information, programs can be instrumented at the source level (either manually or via the compiler), at the bytecode/IR level, at the emulator/VM level, or with dynamic binary rewriting.

3We use the term monitor to refer to a self-contained analysis that observes an application and reports on its behavior, such as execution patterns, internal states, and resource consumption.Monitors may instrument a program by many strategies, modifying the program before or during execution, logging desired events, then generating a report after the program has terminated.Ideally monitors are non-intrusive; that is, they should not alter the semantic behavior of the program.

4Authors' Contact Information: Elizabeth Gilbert, evgilber@andrew.cmu.edu,Carnegie Mellon University, Pittsburgh, Pennsylvania, USA; Matthew Schneider, Carnegie Mellon University, Pittsburgh, Pennsylvania, USA; Zixi An, Carnegie Mellon University, Pittsburgh, Pennsylvania, USA; Suhas Thalanki, Carnegie Mellon University, Pittsburgh, Pennsylvania, USA; Wavid Bowman, Florida Institute for National Security, Gainesville, Florida, USA; Alexander Bai, Max Planck Institute for Software Systems, Saabrucken, Saarland, Germany; Ben L. Titzer, Carnegie Mellon University, Pittsburgh, Pennsylvania, USA; Heather Miller, Carnegie Mellon University, Pittsburgh, Pennsylvania, USA.Of course, any instrumentation may perturb performance by increasing execution time or other resource usage.A dynamic analysis tool should minimize overhead.

Instrumentation

5Source-level instrumentation.It is possible to directly write monitor code within or alongside source code, but this has a number of drawbacks.Instrumentation may be unable to capture lowlevel operations such as I/O accesses, system calls, and signals.It creates cross-cutting concerns as source and monitor code are mixed and increases the risk that the monitor is intrusive.Often, the program incurs some performance hits as frequent checks are required.

6Rewriting.With rewriting, new instructions are inserted directly into the application at points of interest.These new instructions collect, summarize, and report various types of information gathered at runtime.Rewriting is usually performed programmatically, instead of manually, either at the source, bytecode, or machine-code level.

7Static rewriting involves injecting monitor code into the source code before compilation, or into bytecode or machine code before execution.This rewriting is decoupled from any particular execution platform, increasing portability and expressibility of the monitors.However, the inserted code may inadvertantly alter application behavior, even innocently.On native targets, the presence of additional code can alter program state, since a program may read its own code as data.With bytecode and machine code rewriting, the binary needs to be reorganized to fit the monitoring code, which may not always be possible, and source-level mappings need to be adjusted to handle the new code.Static rewriting tools include Oron [23] (at the source level) and EEL [16] (at the machine code level).

8In dynamic rewriting, monitoring code is inserted at runtime, which can avoid some of the limitations of static rewriting.For example, when an application dynamically generates new code, static rewriting might not instrument it, but dynamic rewriting can.However, dynamic rewriting requires more sophisticated tools, e.g.dynamic recompilation, which also means that additional instrumentation cost is paid at runtime.Dynamic rewriting tools include Jalangi [31] (at the source level) and DTrace [13] and Pin [19] (at the machine code level).

9Rewriting can be tedious and is not portable across ISAs.Composing multiple monitors is difficult, and most rewriting frameworks do not automatically handle the necessary parallel composition, as sequential composition would lead to one monitor instrumenting another.

10Emulation.Emulators and VMs such as QEMU [5] and Valgrind [25] track instrumentation points separately from the application code, requiring no rewriting or re-organization for better developer ergonomics.Instrumentation is invoked outside of the emulated state space, which prevents altering behavior.However, even with purpose-built JITs for emulation, this strategy incurs additional overheads due to unstructured machine code and is generally considered prohibitively expensive for heavy instrumentation.

WebAssembly

11WebAssembly (Wasm) [14], is a portable, low-level bytecode format that serves as an efficient target for many programming languages, such as C, C++, and Rust.While initially designed to run programs on the Web, due to its sandboxed execution environment, it has gained adoption in other contexts, such as cloud computing [40], edge computing [1], and embedded systems [24].Wasm engines offer interpreter and compiler tiers, which together achieve quick startup times and high peak performance.Yet despite its spread and the sophistication of its engines, no standard instrumentation, profiling, or debugging interface for Wasm exists.

12State of Wasm instrumentation.Today, static rewriting of bytecode is the norm for Wasm instrumentation, but tooling is low-level and clunky.Several static instrumentation tools are available: BREWasm [10], AspectWasm [29], Walrus [3], and Wasabi [17].These tools are less mature than rewriting tools for platforms such as the JVM.Compared to native targets, however, static rewriting on the WebAssembly platform is less intrusive because the execution model features a Harvard architecture, a virtualized execution stack, and multiple memories.It is also portable, as Wasm abstracts above the host ISA.Wasm engines can also minimize the impact of certain overheads; JIT tiers can speed up checks on probes using adaptive optimizations.However, instrumenting bytecode is still tedious because injected code has to account for application semantics and deal with Wasm's stack machine architecture.

13Alternatively, with direct support in the virtual machine, e.g.Wizard [37], bytecodes can be instrumented without rewriting, which simplifies composability of instrumentation and makes it completely non-intrusive.Both the interpreter and compiler tiers of a Wasm engine can benefit additionally from optimizations specific to instrumentation.One such class of optimizations is called intrinsification in which the engine handles certain cases specially, e.g. by executing special logic in the interpreter and emitting specialized machine code for counter probes in the JIT.

Contributions

14We illustrate the components of the Whamm system in Figure 1 and explain our contributions here.This paper introduces the first expressive, retargetable DSL, codenamed Whamm, designed specifically for Wasm instrumentation, and two complete high-performance realizations of the system based on bytecode rewriting and novel engine support.With bytecode rewriting, the Whamm compiler can inject instrumentation directly into application code, resulting in an instrumented program that can run on any engine.Alternatively, when targeting an engine interface, the Whamm compiler generates a monitor module that can be loaded into a Wasm engine and reused across applications.To support high-performance instrumentation directly in the Wasm engine, we extend the Wizard Research Engine [35] with the ability to load monitors expressed as Wasm modules.Whereas previously, monitors could only be expressed as extensions that require rebuilding the engine, monitor modules are more flexible and extensible.We show a number of Whamm compiler optimizations, including partial evaluation of predicates, as well as engine optimizations such as custom trampolines and inlining instrumentation probes directly into the application code.In particular, we contribute:

15• A fully-featured declarative instrumentation DSL for Wasm, codenamed Whamm.

16We present Whamm, a domain-specific language for instrumenting Wasm code that is expressive and performant.Our work is open source 1 and will be provided as an artifact.

17We demonstrate its language features with a suite of complex monitors.• The first DSL that abstracts above the instrumentation strategy.Whamm can either rewrite bytecode, allowing monitors to run on any Wasm engine, or interface with the engine directly, which can be more composable and efficient.• A suite of compiler optimizations that reduces overhead.We show how the design of Whamm admits optimizations in both the bytecode-rewriting and engine strategies to reduce the overhead of instrumentation both statically and dynamically.• An engine monitoring interface, codenamed whamm, based on monitor modules.

18We show how Wasm engines can support extensible dynamic instrumentation by loading ordinary Wasm modules that conform to a monitor interface.• A suite of engine optimizations that increases performance for whamm.We show how Wasm engines can optimize both interpreter and compiler tiers, achieving performance competitive with that only previously available via special-case optimizations.• An empirical evaluation on standard benchmarks and workloads.We demonstrate the effectiveness of Whamm's language features and optimizations through a detailed evaluation of several complex monitors, comparing bytecode rewriting and direct engine support with similar dynamic analysis tools like Pin and Wasabi.Our detailed analysis isolates individual overheads in various tiering configurations to study the effectiveness of each optimization.

Instrumentation with Whamm

19Whamm is a domain-specific language for WebAssembly instrumentation that solves the longstanding problem of either suffering poor performance, poor portability, or poor composability.It enables developer tooling while reducing hassle since it abstracts above the instrumentation technique to best leverage the capabilities available at runtime.Whamm can instrument applications using the bytecode-rewriting technique or by interfacing with an engine that directly supports instrumentation.Together, our language supports a broad domain of applications without requiring a developer to reimplement their monitors to satisfy any particular instrumentation technique or engine.

Part Description provider

20The provider that supports the capability used in the probe.package

21The package within the provider that supports the capability used in this probe.event

22The event describing the location(s) for probe insertion.mode

23The mode used to emit probe actions at target location(s), e.g.before and after.

24Table 1.A match rule in Whamm consists of four parts.

The Whamm Language

25The syntax of Whamm is inspired by Dtrace [13].Users can express their instrumentation as directives that reference program events and provide predicated actions, as shown in Figure 2.This form can be read as, "When this event occurs during program execution, do these actions if this predicate is true."A directive provides a high-level and intuitive syntax that specifies events at various granularities in the target program.For example, a developer can instrument a function exit either by targeting the wasm:function:exit event provided by the DSL or by explicitly handling each bytecode that may return.

262.1.1Match Rules.The match rule part of the directive constitutes a syntactic pattern matching one or more program events.It consists of four parts: provider, package, event, and mode, described in Table 1.Each part of the match rule decreases in specificity until reaching the mode of the probe.

27As an example, consider the following match rule:

28wasm:opcode:br_if:before This rule can be read as, "Insert this probe before each of the br_if Wasm opcodes in the program." Match rules support wildcards to enable matching on a group of events in the same manner.For example, the match rule wasm:opcode:*load*|*store*:before matches all variants of the load and store Wasm opcodes.It is possible to modify program behavior through leveraging the alt probe mode.This mode replaces the original logic at some target bytecode location.Currently, this feature is only fully supported by the Whamm rewriting target.

29Type Bounds.Some Wasm opcodes, such as call, local.set,and select are polymorphic, meaning their inputs and outputs can be different types depending on the function or module context.Variables in a probe body that refer to these arguments have types that depend on the match site.One way to address this would be to introduce polymorphism or dynamic typing.Instead, to avoid dynamic typing, we require programs to use type bounds, where the user expresses the expected types of each argument in scope to the probe body when there is more than one static possibility.A type mismatch is then considered a no match for the rule2 .Thus, correct matching requires knowing the types of all arguments at all program points.

Predication.

30A programmer can instrument events under specific conditions with predicates, expressions that must evaluate to true for a probe to be executed.An empty predicate is always true.A probe can be predicated on either static information, such as the match location within a function (pc), or dynamic information, such as the instruction's arguments, or a mix of the two in a compound logical expression.The Whamm compiler implements predication differently depending on the target.For bytecode rewriting, the Whamm compiler performs constant propagation and folding for statically-known variables such as the pc and function id (fid).If any residual predicate remains, the compiler injects instrumentation on the remaining dynamic part.For the engine target, it will automatically split the predicate into static and dynamic parts, with the dynamic part compiled into the probe actions, as explained in Section 4.1.In Figure 2, the matched call opcodes are statically constrained to those located in function 53 (specified by the static predicate fid == 53) with an i32 as its first argument (specified by the type bound on arg0).The body of the probe will be wrapped in code to check the dynamic part of the predicate (arg0 == 3).When injecting an alt probe, dynamic predication guards the consequent actions with the original logic in the else block.

Storage Classes.

31A strikingly useful feature of Whamm is a unique set of storage classes for variables that simplify managing the lifetime and reporting of collected data.For example, variables can be global or local, shared across multiple matches, or shared per execution frame.

32Bound functions and globals.Data specific to the event, such as the operands to an instruction, the location of the match, or the name of a target function in a call are directly available in the probe body as bound variables.Each part of the match-rule hierarchy binds appropriate functions and globals.For example, the wasm:opcode:call event provides a target_fn_name variable that can be used in the probe directive to refer to a function's name in the name section.While some variables are statically-known for a match location, other variables are dynamic.In Whamm, all arguments to an instruction (i.e.values on the Wasm operand stack) are available as named, typed variables arg0, arg1, etc.To aid users, we have implemented a nifty CLI tool to query the provided functions and variables in scope for a probe directive.

33Derived variables.Having useful, relevant variables in scope automatically, along with match rule wildcards, can greatly simplify writing bytecode-level monitors.Sometimes such variables aren't inherently part of an event, but are easily computable from the other variables in the event.We call these derived variables.Consider a monitor which matches on all load and store Wasm opcodes.It can use the derived addr variable in the probe body to refer to the dynamic memory address.The addr variable is available for memory-access events and hides the fact that for loads, the target address is stored in the first argument, arg0, while for stores, this value is stored in the second argument, arg1.Other examples of derived variables include: taken, which for any branching opcode represents whether the branch will be taken 3 ; effective_addr, which for loads and stores represents the index + offset; and trap, which indicates whether the instruction will trap.

34Unshared variables.Many instrumentation tasks requiring retaining information that is specific to a match site and should be maintained across invocations, such as a counter.In Whamm, a programmer may mark a variable as unshared, which means an instance of this variable is allocated for each individual match site of the match rule.The lexical scope of this variable is limited to the probe's body, but the variable will retain its value across multiple invocations of the body.This can be used to collect data at a specific program point over time.For example, the following script collects the number of times that a specific call opcode was reached during application execution: wasm:opcode:call:before { unshared var count: u32; count++; } Shared variables.Some instrumentation tasks are more easily expressed if variables are instantiated once across match sites.In a Whamm probe body, a programmer may mark a variable as shared, which means only one instance of this variable is allocated and is made available at every match site.Like unshared variables, the lexical scope of this variable is limited to the probe's body, and the variable retains its value across multiple invocations of the body, but it is not replicated per site.A small change to the previous script collects the number of times that any call opcode was executed dynamically.

35wasm:opcode:call:before { shared var count: u32; count++; } Report variables.Obviously, the purpose of instrumentation is to observe and report.To make this quick and easy, Whamm allows programmers to annotate any variable with the report keyword to request its value be reported on exit.The compiler will automatically generate appropriate printing code, flushing a textual representation of the report variables, along with their match locations.Monitor state is printed to the console or appended to a specified file4 as a commaseparated value listing (CSV) that includes match locations.CSVs allow for easy analysis by other tools.To flush the results of the previous count example, simply add the report keyword to the variable: wasm:opcode:call:before { report shared var count: u32; count++; } Frame variables.Some variables are bound to the lifetime of a function activation.For example, profilers often record the time when a function is entered and the time when it is exited.Handling this properly with recursive functions and exceptions is surprisingly tricky and would otherwise require emulating the callstack.Instead, Whamm makes this easy with the frame keyword, which marks variables that should live within a single activation of the target function.With recursion, multiple copies of the variable co-exist simultaneously, and probe invocations get the appropriate copy for their activation.For the bytecode-rewriting target, the obvious (and efficient) implementation strategy is to simply introduce a new local variable to the function.To support frame variables in the engine target, we modified the engine with the ability to append to a function's local variable declarations dynamically.

User Libraries.

36A common problem in DSLs is feature creep where more and more generalpurpose language features are added, compromising its original focus, becoming a frustratingly underpowered language with limited usefulness.Rather than risk feature creep, we instead see Whamm as a way to weave instrumentation logic into a program, or to generate a monitor that can allow an engine to do that for us, similar to aspect-oriented programming.Thus, we can specialize the language itself towards instrumentation expressiveness rather than general-purpose programming.

37But how can we scale up to handle big instrumentation tasks?The answer is libraries.Whamm allows monitors to use libraries provided as Wasm code which the compiler automatically links in, relegating the heavy lifting of complex data structures and libraries to a user's preferred language.This extensibility prevents the DSL from becoming unwieldy.

38For example, we leveraged this capability in the implementation of the map type in Whamm.The map type provides an efficient general-purpose associative array.Its implementation is provided by a Wasm module implemented as a Rust HashMap.Further, the cache-simulator monitor in our evaluation, see Section 5.1, demonstrates how to use a Wasm module as a monitor library.The cache model is implemented in Rust, compiled to Wasm, then linked with Whamm.The Whamm script just focuses on expressive matching and reporting, while the cache model is separate and can be independently swapped.We envision that library support will allow users to add powerful online statistical processing and sampling strategies by writing the complex parts in a general-purpose language and then linking them with Whamm scripts.

Implementing Whamm

39We implemented a compiler for Whamm in approximately 33,000 lines of Rust that parses scripts, performs semantic analyses, implements optimizations, and outputs Wasm supporting two distinct targets: bytecode rewriting and engine support.In the rewriting target, the user supplies scripts and the application module(s) being instrumented together to the Whamm compiler.The compiler then applies all match rules, compiles probe bodies to Wasm, and injects instrumentation with rewriting, creating the final instrumented module.For the engine target, the user need only supply the Whamm scripts, and the compiler produces a standalone Wasm module that implements a monitor according to the whamm monitoring interface.The phases of compilation are what one would expect from a traditional compiler, but with interesting behaviors that stem from: (1) collecting and analyzing bound variables per directive, (2) planning variable-storage allocation, and (3) matching probe events, only for the rewriting target.

Bound Variable Collection, Vec〈Whamm-param〉

40As explained in Section 2.1.3,bound variables represent static and dynamic data at match locations, like the pc or an operand to an instruction.For each directive, Whamm gathers used variables into a Vec〈Whamm-param〉.By knowing exactly what a probe directive needs and communicating this to the implementation strategy, the data transfer overhead can be minimized.

Planning for Storage Allocation

41Variables in Whamm can have storage classes such as frame, report, shared, and unshared (see Section 2.1.3).Storage allocation for such variables occurs at different times depending on the target.

42With rewriting, the compiler is provided the application bytecode so that it can statically identify all match locations and thus allocate storage ahead of time.In probe bodies, expressions referencing such variables can be reduced to accesses of constant memory addresses.However, when targeting an engine, the Whamm compiler will produce a monitor module that is application-agnostic. Later, when applying a monitor module to an application module at load time, the engine will evaluate match rules against and attach probe callbacks to the appropriate application locations.This latebinding of probes requires the engine and monitor module to cooperate to allocate storage for appropriate variables.As we'll see in more detail later, the engine supports match-time callbacks so the monitor module can invoke logic each time a new match is found in an application.The Whamm compiler emits callbacks that allocate appropriate per-match-site variables and provide their addresses to probe callbacks.The probe body uses this memory address as the dynamic part of the address and uses the static offset value of a Wasm load and store operation to point to the appropriate variable location within that allocated region.

The Rewriting Strategy

43With bytecode rewriting, the compiler directly injects monitoring logic into the application code.

3.3.1

44The injection library: Seal.A critical component of the rewriting target is code to load, parse, reflect on, and modify Wasm binary modules.As part of this work, we developed a new, fully-featured open-source Rust library for instrumenting Wasm code, codenamed Seal.Seal allows loading application bytecode into an intermediate representation (IR) that can be decorated.We use decorators on functions and opcodes to attach the bytecode-to-inject with directives on where the logic should be injected while emitting the application bytecode post-instrumentation.

45The most basic opcode-level decorators have three behavior options: before, after, and alternate.At module-encoding time, the before and after decorators inject the new bytecode at the original opcode.

46The alternate decorator injects the new bytecode instead of the original to support application manipulation.The function-level decorators have two behavior options: entry and exit.At moduleencoding time, the entry decorator injects the new bytecode at the top of the instrumented function.The exit decorator injects the new bytecode at all locations that could return in the function body.Seal's bytecode-injection mechanisms enable library consumers to more easily inject new bytecode into a Wasm module without having to consider the lower-level impacts of injection.

Identifying match locations.

47As shown in Figure 3, we traverse the application bytecode to identify match locations, represented as Seal IR.When the match rule succeeds, the compiler inspects the Vec〈Whamm-param〉 to define appropriate data and partially evaluate the predicate through constant propagation.If the predicate reduces to an expression that depends on dynamic information, the compiler will emit code to evaluate the residual expression as a guard before the probe body.

Emitting probes at match locations.

48Because Wasm defines a stack machine, injecting probe logic that uses the input operands to an instruction needs a minor amount of fiddling.Figure 3 also shows the three parts of an emitted probe: the parameter prologue, the probe itself, and the stack reset epilogue.If Vec〈Whamm-param〉 contains dynamic data requests, these must be bundled as a probe prologue in the emitted bytecode and bound to newly-injected local variables in the function body.Variables representing operand-stack values are initially saved as locals that can be used in the dynamic predicate and probe body, but cleared later to restore the operand stack.Our compiler uses local rewrites to avoid reorganizing the operand-stack usage for the entire function.

The Engine Interfacewhamm

49While Whamm scripts can run on any engine with the rewriting target, the Whamm compiler can also target an engine monitoring interface where the application and monitor are provided separately.The explicit separation between application and monitor provides additional capabilities and performance improvements over rewriting.First, preserving the original application code preserves source mappings, making it easier to trace issues identified during instrumentation back to the underlying source.Second, the engine can enforce separation between monitor state and application state so that a monitor cannot inadvertently alter program behavior.Third, the engine can compose multiple monitors simultaneously.Fourth, monitors can be applied more selectively (e.g.per-function) and dynamically added and removed, which unlocks new use cases such as enabling and disabling heavyweight instrumentation to observe rare program behavior.The engine can completely remove instrumentation when not in use, imposing zero overhead.Fifth, we can instrument events not observable in bytecode, such as loading/verification/compilation of functions, garbage collection, thread scheduling, or other VM-level operations.3.Each step of static instrumentation with Whamm when targeting the bytecode rewriting.In (0) the application module is visited to find event matches, then at a match, (1) the variables referenced by the probe body are saved, (2) the probe body is compiled to WebAssembly and inlined, and (3) the stack is restored to its original state for the original application bytecode.Fig. 4. Example of compiling Whamm to the engine target whamm.The monitor script (0) is compiled to a complete Wasm module that encodes match rules in the names of exported functions.At load time, the engine loads the monitor module and (1) applies the match rules to the application-under-observation.At runtime (2), when a probed location is reached, the engine will (3) bundle the requested data and invoke the probe callback.Monitor modules (whamm-monitor).Recently, [37] demonstrated the first programmable engine instrumentation for Wasm.Monitors are written against engine-specific APIs and compiled-in at VM build time, but are not portable across engines.This has allowed many standard, reusable monitors for Wizard, as well as ad-hoc analyses.Instead, we discovered that Wasm engines are shockingly good at loading Wasm modules and thus the ideal monitor should be provided as a Wasm module.Thus, we designed an API, where the engine accepts monitor modules which provide instrumentation via exported functions.These regular Wasm modules, referred to as whamm-monitors, conform to a naming convention which is a simplified form of the Whamm language, called whamm.Figure 5 shows an example monitor module that targets the engine interface whamm.

50Declarative matching via export names.While the rewriting target applies match rules and predicates to a specific application module, there is no application module yet when generating a monitor module.We require load-time matching when applications are loaded.We considered several design alternatives, such as (1) the Wasm engine provides the raw bytes of the application module to the monitor module, which could then re-parse it; (2) the monitor module can reflect on the application module through an engine API; or (3) the monitor module communicates matching rules/predicates to the engine declaratively.We chose the last alternative to reduce duplication between engine and monitor and avoid a costly reparse.In this scheme, the information necessary for matching-the match rule, static predicate, and runtime data-is encoded in the export name of the function as shown in Figure 5.The engine parses these export names and performs a simplified form of Whamm-matching to insert engine-level probes.For an engine that already supports dynamic instrumentation via probes, the implementation effort required is low (600 lines of code to parse/match/attach/invoke and 250 lines of interpreter and JIT optimizations).

51Probe and predicate functions.The Whamm compiler optimizes and translates probe bodies to Wasm functions, called whamm-functions.Predicates must be evaluated by the engine at match time, so the Whamm compiler splits predicates into static and dynamic portions.Static predicates are wrapped in Wasm functions that can be referred to by their export name in the match rule and dynamic predicates are inlined into the probe bodies, see Section 4.1.Both predicates and probes may accept whamm-params that correspond to Whamm variables, and the engine provides their values (whamm-args) at match time or runtime as necessary.

52Typechecking during matching.Some events have polymorphic operands, such as select or local.get.Type bounds are effectively a type predicate for variables, as described in Section 2.1.1.In Wasm bytecode, the types on the operand stack may change from instruction to instruction.Checking the types of argN variables would therefore seem to be similar to Wasm code validation, a surprisingly tricky endeavor.Yet, hallelujah, an engine already has a code validator!We extend the code validator with a callback so that the types of argN and localN variables are known at potential probe match sites.For most rules, a type mismatch is an error, but for type bounds, a mismatch means the probe is simply not inserted.This is the most complicated part of our engine changes, at approximately 200 lines of code.Match time callbacks.Many monitors store per-match-site data, such as unshared variables in Whamm.To implement this functionality and more, the engine interface allows match-time callbacks that are invoked by the engine when a match rule and its static predicate are true.A whamm-monitor encodes match-time callbacks into the export name per the grammar shown in Figure 6.Statically-known whamm-args, like the function id (fid) and program counter (pc), are allowed as arguments to match-time callbacks.The whamm-monitor can, for example, allocate a new entry in its internal monitoring data structure and return the entry's address in the whamm-monitor's memory.The engine passes the result of the static callback-a constant-to the probe function.Thus one export can define a match rule with a single probe function that accepts custom data for each match site.The Whamm compiler uses this functionality to implement storage for unshared variables.

53Start function and exit callback.In addition to matching on instruction patterns, the engine interface also allows the normal Wasm start function.Similarly, to report the result of the analysis, the engine will invoke an exported function named "wasm:exit" upon application termination.

54Summary: an adoptable engine interface.The engine interface retains a simplified form of declarative matching expressed in export names but uses Wasm functions for almost everything else: probes, predicates, and reporting logic.This is to minimize engine burden in hopes of future adoption by more engines.We chose to prototype the engine interface in the Wizard Research Engine, which already supports a set of bytecode-level dynamic probing mechanisms.Using these existing mechanisms, we implemented the engine interface in just 600 lines of new code.With an already well-encapsulated Monitor extension point, extending the Wizard engine to support Whamm was as simple as writing a new subclass that loads a whamm-monitor, parses its export names, applies match rules, and inserts custom probes that invoke Wasm functions and reporting at exit, all without changing any existing code.This provided an unoptimized baseline to which we later added interpreter, JIT, and runtime optimizations that we detail in the next section.In particular, implementing whamm-args naively uses a relatively expensive reflective access to the caller's frame in the probe code.Nevertheless, given the low implementation burden, we believe that other engines could adopt the same engine interface and thus whamm-monitors could one day work on any engine.

Optimizing Whamm

55In this section, we detail optimizations designed to reduce the overhead of instrumentation with Whamm.These optimizations fall into two main categories: static optimizations applied by the Whamm compiler, and dynamic optimizations implemented in the engine.

Whamm Compiler Optimizations

56Optimizations can be applied by the Whamm compiler for both the rewriting and engine targets.

57Constant propagation.Constants appear surprisingly often in Whamm expressions, since match rules, predicates, and probe bodies often mention bound variables such as pc that become constants for a given match site.The Whamm compiler employs constant propagation to emit better instrumentation code and to optimize predicates.Deciding more predicates statically means fewer probes will be inserted, and partially evaluating them minimizes the complexity of dynamic predicates.

58Minimizing operand stack shuffling.The Whamm compiler instruments Wasm bytecode by rewriting in a mostly linear pass.When probes use instruction operands (i.e.argN), some stack shuffling is required.To avoid a global rewrite of an entire function (e.g.reallocating local variables or reshuffling the operand stack), the compiler instead emits the minimal operand-stack save-andrestore code before emitting instrumentation inline.With argN meaning the -th operand, and arg0 referring to the top of stack, the number of operands that must be saved is equal to + 1, being the largest used in the directive.

59Static/dynamic splitting of predicates.Match predicates in Whamm can involve both static and dynamic variables, while in the engine interface, static predicates may be expressed as part of export names.To map Whamm predicates onto the engine monitoring interface, the simplest implementation strategy is to classify all predicates as dynamic and emit code within probe bodies to evaluate them.While correct, this results in instrumenting more match sites and imposing more overhead than necessary on each probe invocation.A somewhat smarter strategy is to conservatively classify predicates as either fully static or fully dynamic.This is the strategy the current compiler uses.

60However, in the future we can do better by classifying subexpressions as static, which are known at match time, and dynamic, which are only known at runtime, and applying partial evaluation.For example, consider a static variable A and a dynamic variable B in the predicate expression = A || B. By definition, static variables are known before dynamic variables, so we can split the evaluation of an expression into two stages.In the first stage, static expressions are evaluated, and to derive expressions for the second stage, we substitute into the original expression all the possible outcomes and then apply constant propagation, folding, and strength reduction to obtain a residual dynamic predicate.To see this, let's assume A evaluates to either true or false.We can then generate two different residual dynamic predicates, one where A↦ →true, and one for A↦ →false.These are, respectively, true || B and false || B. After constant folding and strength reduction, we have true and B.

61While symbolic approaches might apply, an effective brute-force strategy is:

62(1) Categorize each subexpression as static or dynamic.

63(2) Generate a truth table for the outcome of each static subexpression.

64(3) Substitute constants, fold, and strength-reduce to produce residual dynamic predicates.

65(4) Collect results, combine equivalences, and simplify.Figure 7 shows an in-depth example of this static analysis.After optimization, the whamm-monitor contains two specialized probes for the single DSL probe expression.One probe is purely statically predicated, while the second probe handles the case where the static predicate is false, requiring the dynamic residual to be evaluated.

Engine Optimizations

66The Wizard engine supports dynamic instrumentation by allowing user-defined probe objects to be attached as specific Wasm instructions.These probes are implemented in the engine (i.e. as host code) and as we've seen, form the basis of our whamm-probe implementation.This section details three key optimizations to reduce overheads associated with this mechanism.

67To understand the runtime overhead of the engine-probe mechanism, consider the definition of a Wizard probe.Such probes are defined in terms of Virgil classes with a fire() method that is called at runtime.The method receives a (function, pc) pair and a handle to a lazily-allocated reflective FrameAccessor object for accessing the operand stack, locals, etc. Flexible instrumentation is seamlessly supported in both the interpreter and JIT tiers of the engine but requires a relatively complicated transfer sequence from application code to probe code, consisting of these steps:

68(1) commit program state to the machine frame (2) save interpreter or JIT VM state, such as a value stack pointer (3) look up the probe object through indirections from the instance pointer (4) invoke the (host) probe function Depending on the complexity of the probe's logic, it may incur additional overheads as it executes:

69(1) potentially redundant computation in the probe function (2) materializing the FrameAccessor object if requested (3) expensive frame access via indirection through the FrameAccessor In practice, most probes don't require the full generality of the probing system, allowing for engine optimizations that remove some of the above steps at probe callsites.Removing unneeded work in the transfer sequence and making accessing of program variables cheaper are key to reducing instrumentation overhead.This is especially true for the whamm-probe, which implements an adapter that transfers data from application Wasm context, through the runtime, then back to another Wasm context.This is relatively expensive to do via the engine APIs.In the general case, each Whamm function may have both constants and variables as whamm-args; the probe loads their values from the FrameAccessor and calls the target function with the whamm-args using the engine's host-to-Wasm call mechanism.A host-to-Wasm call has relatively high overhead, as it may allocate a fresh machine stack 5 for the call and the whamm-args for the call are copied to and 5 as part of Wizard's support for the stack-switching proposal from the heap at least once.To reduce overhead of whamm-probe instrumentation, we identified three optimizations:

701. (Interpreter) Use trampolines for customized transfer and a Wasm-to-Wasm call.Since the whamm-probe object is an engine-level wrapper around a Wasm function, the obvious optimization is to avoid the expensive transfer sequence detailed above and simply make the application-to-instrumentation transfer a regular Wasm call.The only wrinkle is that whamm-args must be passed somehow.Since these are known at binding time and consist of a mix of constants and frame accesses, we wrote a trampoline generator which produces a small machine code stub for each whamm-probe signature, at match time, before program execution.The trampoline contains the minimum machine instructions specialized to each unique signature that performs the necessary moves to/from the operand stack slots to properly load whamm-args.It then performs a Wasm-to-Wasm call to the whamm-probe function, landing either in JIT-compiled code for the function or back in the interpreter, with a new frame for the probe function.After the call returns, the trampoline dispatches on the original application opcode to continue execution.One trampoline is generated for each unique whamm-probe signature, so many probe objects sharing the same signature also share trampolines, incurring minimum space and time overhead.This optimization completely removes the need to materialize a FrameAccessor or setup an entirely new execution context with the Execute API.

712. (JIT) Replace the call to the whamm-probe with a direct Wasm call in the compiler.Wizard employs a single-pass compiler [36] (SPC) as its JIT tier.This compiler already contains support for intrinsifying counter and top-of-stack probes [37].We extend the compiler with an additional case for when a probe at a given site is attached to a whamm-probe function.Like interpretation, we can benefit from specializing the transfer sequence, but further, the compiler can inline this transfer sequence.As the SPC allocates registers and tracks constants via abstract interpretation, the inlined transfer sequence is more efficient.Moreover, the JIT can emit a direct call to the whamm-probe function.

723. (JIT) Inline compiled whamm-monitor functions directly into application code.We can further reduce overhead for probes by inlining the probe's Wasm code into application code at probe sites.This avoids the cost of a Wasm function call-a disproportionate overhead when considering many probe functions are short sequences that should be no more than a couple machine instructions.Inlining short probe functions is a big win that brings fully programmable instrumentation up to speed with handwritten intrinsification.For comparison, Wizard's prior CountProbe intrinsification allowed using simple counter probes at the cost of just two inline machine instructions per probe site.Expressing a counter as a whamm-probe with six Wasm instructions to load, increment, and store an internal memory address results in the JIT compiler inlining just five machine instructions 6 .

73We chose to implement very simple 7 inlining in the SPC as a proof of concept.As a state-of-theart optimizing baseline compiler, the SPC performs abstract interpretation and models the control stack during a single forward compilation pass.When reaching a whamm-probe, we modify the SPC to simply push a new control stack entry modeling the entry of a new function, juggle a bit of abstract state, and then continue to generate code from the bytecode of the inlined function.Since the SPC already models constants and allocates registers, the new bytecode is inlined and optimized in the context of the application code.In particular, locations of whamm-args are statically known at the site and any results from match-time callbacks become constants.This means that match-time Table 2.The suite of monitors implemented for this evaluation.

Programmability and Usability

74For our evaluation, we implement a diverse set of monitors (Table 2) to demonstrate that Whamm provides concise programmable instrumentation with high performance.Through specializing Whamm syntax to commonly-needed operations and storage primitives, fewer lines of code are needed.For example, it only requires 4 lines of code for a simple hotness monitor and 13 lines of code for the relatively complicated cache_sim monitor.In contrast, manual bytecode rewriting with Walrus/Seal require tedious handwritten bytecode and careful management of instrumentation metadata.For more general programmability, Whamm scripts can rely on libraries.This greatly simplifies complex monitoring problems.For example, a succinct implementation of the imix monitor can be written in 4 lines of code, shown below.report var dyn_categories: map<u32, i32>; wasm:opcode:*:before { dyn_categories[category_id]++; // category_id is a bound variable }

75The above is short, but it has poor performance due to the use of a library-provided map data structure.Whamm is flexible enough to support multiple implementations of a monitor, optimized to the desired criteria (conciseness, performance, maintainability) of the user.A faster version of the imix monitor could use one directive per opcode: report var control: u32; report var arith: u32; ... wasm:opcode:br_if:before { control++; } wasm:opcode:i32.add:before{ arith++; } ...

Performance

76To evaluate the performance of monitoring with Whamm, we evaluate overhead costs on the Polybench/C benchmark suite [27] with the medium dataset.We evaluate our system against the state of the art Pin [19] machine-code-rewriting instrumentation framework, by compiling the Wasm binaries back to C using wasm2c, then to x86-64 using gcc.To obtain baseline (uninstrumented) execution times (see Table 3 in the appendix), we ran each benchmark on Wizard interpreter/JIT tiers, natively, and on V8 using the --liftoff-only option, and averaged execution times from five runs.We used (patched) Wizard version 25.2717,V8 version 13.6.0,wasm2c version 1.0.34,Pin version 3.31, and gcc version 13.2.0 with optimization level -O2.All experiments were performed on a machine with two Xeon Platinum 8168 CPUs @ 2.70GHz with 384GiB of DDR4 RAM.Given uninstrumented execution time in seconds, , and instrumented execution time, , we use the term relative overhead to refer to the ratio / , while absolute overhead refers to − .

Whamm vs.

77State-of-the-Art Wasm Frameworks.We compare the relative overhead imposed by Whamm rewriting and whamm-monitors with state-of-the-art WebAssembly instrumentation frameworks by implementing reasonably-equivalent monitors in Native Wizard instrumentation, Wasabi [17], IR injection with the Seal and Walrus libraries [3], and handwritten whamm-monitors, see Figure 8.We ran each of these implementations in their most-optimized configuration on the Wizard JIT.As Wasabi requires an engine that runs JavaScript, we ran the Wasabi experiments on the V8 engine and limited execution to its baseline compiler, Liftoff, for a fair comparison 9 .The uninstrumented time for each benchmark can be found in Table 3.

78Whamm vs. Wasabi.Wasabi rewrites Wasm bytecode to inject runtime calls to JavaScript instrumentation.While this is known to be expensive for Wasabi, our results show that whamm instrumentation is similarly expensive when implemented as runtime calls.Both impose orders of magnitude overhead, shown in results for Wasabi and our unoptimized int-rt-*/jit-rt-* configurations in Figures 9 and 10.

79Whamm vs. Walrus/Seal.Directly injecting into Wasm IR with library support allows one to inject custom, nearly optimal, bytecode via rewriting.For short-running programs, our data shows that the binding and setup time dominate, giving the advantage to Walrus/Seal and native.However, for long-running programs, all monitors show both Whamm targets to be competitive with manual rewriting, usually within 1.5 − 2×.Another difference is that the Walrus/Seal monitors use more optimized handwritten bytecode injection that has denser data representations, and do not actually report results.Improvements to the Whamm compiler could lessen this gap.

80Whamm vs. Native Wizard Instrumentation.Native Wizard instrumentation is highly optimized for special cases via JIT intrinsics that recognize special Probe definitions in the engine API.For example, the CountProbe optimizes the emitted machine code to just two instructions, and the top-of-stack probe directly passes the top-of-stack to probe callbacks.The impact of such optimizations is seen in the efficiency of the hotness and branches monitors.Native engine instrumentation is faster than Whamm on small benchmarks for the cache_sim monitor, partly due to the overhead of JIT inlining for whamm-monitors and stack manipulations for the Whamm rewriting target.Note that the native top-of-stack specialization duplicates the semantics of a whamm-monitor requesting the arg0 variable.While our engine interface does not yet beat the performance of these highly specialized probes, it is still competitive, and as we discuss in future work, more compiler optimization can narrow this gap.

81Whamm vs. Handwritten Monitor Modules.We demonstrate the effectiveness of the Whamm compiler by implementing logically-equivalent handwritten whamm-monitors designed for maximum performance.While the handwritten branches monitor is more performant than that of Whamm for short-running programs (mostly load time effects), for long-running programs Whamm monitors have nearly-equivalent performance as the handwritten bytecode.

whamm

82Interpreter Optimization.We evaluate the effect of the trampoline interpreter optimization in Figure 9.In the base case, int-rt-int, the application code is interpreted; when an instrumented point is reached, a runtime call is made to invoke the whamm-function.The instrumentation code itself is then interpreted.We show the impact of simply JITing the instrumentation code, leaving the runtime call as-is in the configuration int-rt-jit.The optimized case, int-tramp-int, leverages the trampoline that enables a direct call to the whamm-function, skipping the runtime call.Further optimization is then shown with the int-tramp-jit configuration, in which the trampoline calls a JITed whamm-function.We further show the performance of simply interpreting an application rewritten with Whamm.

83For the branches and hotness monitors, trampoline generation imposes more overhead than runtime calls into JITed probes for short-running programs.However, as base program runtime increases, the runtime call dominates the overhead (consistent with the Wasabi framework).For the imix monitor, the trampoline saves the expensive runtime call, but lags behind rewriting performance.This is because the trampoline still has an indirection overhead, whereas rewriting is inlined in the application code.As the cache_sim probe bodies are more complex than other monitors, JIT-compiling instrumentation provides a large performance boost as the overhead is dominated by actual instrumentation logic rather than the injection and invocation strategies.

845.2.3 whamm JIT Optimization.We evaluate the whamm JIT optimizations in Figure 10.In the base case, jit-rt-int, the application code is JIT-compiled; when an instrumented point is reached, a runtime call is made to invoke the probe callback, the instrumentation code is then interpreted.We show the impact of simply JIT-compiling the instrumentation code, leaving the runtime call as-is in the configuration, jit-rt-jit.In the first optimized case, jit-wasm-int, the application code performs a direct Wasm call at the probe site in the compiled machine code, skipping the runtime call.The inlined case further optimizes probe invocation by directly inlining the whamm-function at the probe site.We further show the performance of simply JIT-compiling an application rewritten with Whamm.

85In general, instrumentation overhead is dominated by the runtime call, if present.Removing this call overhead results in a large performance boost, as shown by jit-wasm-int.Further, inlining reduces the overhead costs to be even lower than rewriting!The branches monitor requires the use of arg0 to check the dynamic target of a branching opcode.As Wasm is a stack machine, accessing the top-of-stack when rewriting requires stack manipulations (see Figure 3), while the engine knows the value's register.This observation optimizes transfer time for inlined probe bodies.Inlining the imix monitors has similar performance to Whamm rewriting, regardless of base program run time.For the cache_sim monitor, we observe that, after removing the runtime call, the overhead is dominated by the time spent in the cache library, making optimization techniques for transfer cost negligible.

865.2.4 Whamm vs. Pin, a Traditional Highly Performant Framework.We evaluate the performance of the imix, icount, and cache_sim monitors in Whamm against logically-equivalent implementations in Pin [19], a highly-performant machine-code rewriting instrumentation framework.We compare the absolute overhead, − , imposed by instrumenting machine code with Pin and JITed Wasm bytecode with Whamm rewriting and whamm.As shown in Table 3, the base performance of machine code is, on average, 5× faster than Wizard's baseline JIT compiler.As we are comparing absolute overhead, the difference in base runtime is factored out, but instrumentation code compiled in Pin has an advantage.

87Figure 11 explains the various types of overheads imposed by each framework and compares the time required for each in Figure 12.We calculate the bind overhead with engine metrics for whamm (done statically in rewriting), the transfer overhead by instrumenting a program with empty probes, the report overhead through engine metrics for whamm and measuring the difference between instrumentation overhead with and without flushing for Whamm rewriting, and instr as the rest of the time.We calculate base bind for Pin by starting up the framework for each benchmark with no monitor activated and bind through taking the difference between the overhead of instrumenting with empty probes and base bind.We were unable to compute the transfer and report overheads for Pin, so these are not separated from the instr overhead in the charts.

88For shorter running programs, especially for simple probes like those in imix and icount, the Whamm rewriting and engine strategies are more performant than Pin due to their small, Fig. 11.Visualization of the various overheads imposed by Pin, Whamm rewriting, and whamm-monitors.The base rows represent the runtime for uninstrumented machine code or bytecode.Rewriting imposes some static overhead to inject instrumentation into the program, bind cost.This cost is paid at runtime for both Pin and whamm-monitors.Pin requires some base bind overhead to start up the framework itself plus some cost relative to the size of the program.All three frameworks have overhead for transfer-ing into monitor code, executing it, (instr) and flushing monitor state (report).

89or non-existent, bind overheads.The cache_sim monitor overhead becomes large for all longrunning benchmarks because these programs, except seidel-2d, all have a high number of memory read/write operations resulting in heavy instrumentation.While Pin is fastest for long-running programs, it clearly has large, fixed startup overhead and surprisingly large binding time.A significant portion of the gap for long-running programs is the code quality of Wizard's SPC JIT when compiling complex monitor code.We expect that in another engine with a more optimizing compiler, monitor code speed will approach optimal.Of note is that our performance was achievable with very few lines of code for all monitors (see Table 2) that is both intuitive and maintainable rather than having to write the low-level C code necessary for Pin instrumentation.

Related work

90Dynamic analysis frameworks have been the subject of a vast amount of research.The main choices include whether to inject code into the source code or application binary, recompile programs with new instrumentation, or emulate the process space of a program.These approaches have various tradeoffs including performance overheads, intrusiveness concerns, usability, and composibility.

91Code injection A common approach is to inject monitoring code directly into a program binary [43], either by injecting the entire monitoring code inline or with a trampoline that jumps to out-of-line instrumentation code.

92Code injection can be performed statically, that is, separately from execution.For Java bytecode, early static tools include Soot [39] and Bloat [26].As Aspect-Oriented Programming (AOP) emerged, so did tools DiSL [20], AspectJ [15] and BISM [32,33] that targeted joinpoints.While AOP instrumentation was readable, it was unable to manipulate bytecode at a low-level.The ASM [9] framework is capable of low-level manipulation, but was considered user unfriendly.The later Clojure [38] bytecode manipulation library enables low-level manipulations in a readable, expressive, and concise notation.

93Alternatively, code can be injected at runtime.DTrace [12,13], inspired by Paradyn [21], enables tracing at both the user and kernel layer of the OS with dynamically-injected trampolines.Dyninst [6] interfaces with a program's CFG and maps modifications to concrete binary rewrites.A user can tie instrumentation to instructions or CFG abstractions (e.g.function entry/exit).Recent research in this direction [11,28,41] focuses on reducing instrumentation overhead with low-level optimizations.

94Fig. 12.The absolute overhead of the imix, icount, and cache_sim monitors when using the Whamm rewriting (left column), the Whamm engine interface (middle column), and Pin framework (right column) broken down by type of overhead (see Figure 11).Whamm Rewriting performance is computed for a JITed instrumented application and Whamm engine interface is run is its most-optimized configuration, JIT inlining of probes.Numbers are relative to uninstrumented execution time.Lower is better.

95For Wasm, many solutions exist like [42], Walrus [3], BREWasm [10], and Wasabi [17], but none are as user-friendly as the bytecode rewriting framework presented in this work.

96Recompilation.Compiled programs can be recompiled to inject code using several techniques.For example, compiled code can be lifted to a higher-level IR, instrumented, then recompiled.Early examples of static lifting for instrumentation include ATOM [34] and EEL [16].Etch [30], through observing an initial program execution, discovered dynamic program properties to inform static instrumentation.DynamoRIO [7] and Pin [19] use dynamic recompilation of native binaries for instrumentation.Their JIT compilers are purpose-built for instrumentation but since it runs code as part of the initial process, it is susceptible to being intrusive.

97Emulation.QEMU [5] is a widely-used CPU emulator that virtualizes a user-space process while supporting non-intrusive instrumentation.Valgrind [25], primarily used as a memory debugger, is similar.They include JIT compilers that are not necessarily "purpose-built" for instrumentation, but for cross-compilation, leading to an inherent overhead over virtual machines.

98Direct engine support.Virtual machines and their runtime systems can be designed with specific support for instrumentation.In .NET [4], users build profiler DLLs that are loaded by the CLR into the same process as a target application and interface through callbacks.The JVM Tool Interface [2] instruments Java bytecode by attaching callbacks to agents written against the lowerlevel engine API.However, evidence suggests that event handling in agents can be prohibitively slow and the engine API cannot handle events at the level of individual bytecodes.For Wasm, the Wizard Research Engine [35] provides support for monitoring by writing engine-specific probes in the Virgil language [37].Our work leverages the existing support in the Wizard Engine to deliver easy-to-implement, yet portable engine-level instrumentation.

99Composability of analyses.Composability of instrumentation allows programmers to use multiple analyses simultaneously on the same application.Some instrumentation frameworks have attempted to promote interoperability of monitors through meta-modeling [8] and interface standardization [18].Other frameworks have emphasized the need for composable monitors in works such as the built-in Wizard VM instrumentation support [37] and a technique called polymorphic instrumentation [22].Virtual-machine based solutions like the JVM, .NET, and the Wizard engine allow general composability.Our engine monitoring interface goes further and enforces separation and composability of monitors by sandboxing them in Wasm modules.

Conclusion and Future Work

100In this work, we presented Whamm, a declarative instrumentation language.The syntax of this language provides a semantically rich environment for natural instrumentation expression.We showed that Whamm's performance is competitive with performant frameworks due to novel optimizations, freeing users from writing low-level, tedious code.

101In future work, we plan to expand the instrumentable events in Whamm to support use cases such as security policy enforcement, modeling data flow between Wasm components, and resilience engineering tooling.We also hope to aid further in the debuggability of instrumentation through creating support frameworks such as an IDE plugin for live programming with Whamm and a test suite framework.Another area of interest is designing a Wasm reflection and instrumentation API to enable the monitors themselves to perform the static bytecode matching logic for dynamic instrumentation.We also have plans to further optimize Whamm performance through implementing predicate optimizations as outlined in Section 4.1.In this work we focused effort on prototyping in Wizard, which has advanced instrumentation, but no optimizing compiler.Since such a compiler would presumably be good at optimizing Wasm code, it would excel at optimizing application and monitor code together, particularly as probe bodies are usually short but may have many common subexpressions across match sites.Some instrumentation tasks might involve predicates that change infrequently, such as guarding detailed instrumentation for a rare program event.While the underlying engine supports dynamically removing and adding new probes one-by-one, each individual insertion/removal can deoptimize a function.Rather than insert many probes at once and then remove them all at once, instrumentation might be better expressed with predicates on global variables with the engine employing optimization guards [44].Our intention with whamm was to entice production engines into supporting a standardized monitoring API by limiting the additional mechanisms needed, e.g. by reusing Wasm modules deftly.

Data-Availability Statement

102We plan to submit an artifact for evaluation.This artifact will include the benchmark suites used, scripts to run the experiments, usage instructions, and our experiment results as CSVs.

Fig. 1 .

103Fig. 1.Contribution map of this paper.We have designed and implemented a new expressive DSL (codenamed Whamm) for concise instrumentation of Wasm applications with support for extensible libraries.It features an optimizing compiler that targets either rewriting (which injects bytecode into the application and can run anywhere) or a new engine monitoring interface (codenamed whamm) with high performance.

Fig. 2 .

104Fig. 2. Directives in Whamm consist of a triple of a match rule that includes bounds on typed arguments, an optional predicate, and consequent actions.

Fig.

105Fig.3.Each step of static instrumentation with Whamm when targeting the bytecode rewriting.In (0) the application module is visited to find event matches, then at a match, (1) the variables referenced by the probe body are saved, (2) the probe body is compiled to WebAssembly and inlined, and (3) the stack is restored to its original state for the original application bytecode.

Fig. 5 .

106Fig.5.Illustrates the whamm engine interface.Wasm monitor modules encode directive match rules, type bounds and predicates into exported functions, expressing where to insert probes declaratively.The match time callback readies instrumentation state for each match location in the program.The variable request(s) or whamm-params, instruct the engine on the program state to pass during callback invocation.

Fig. 6 .

107Fig.6.The grammar for whamm export names in a monitor module.

Fig. 7 .

108Fig. 7. Illustration of splitting a mixed predicate (upper left) into subexpressions to evaluate statically and dynamically (right-side output), reducing predication overhead in the engine.

Fig. 8 .

109Fig. 8.The relative execution times, log scale, of the branches, hotness, imix, and cache_sim monitors against state-of-the-art Wasm instrumentation frameworks across the Polybench/C benchmark suite sorted by absolute execution time.Ratios are relative to uninstrumented execution time.Lower is better.

Fig. 9 .

110Fig.9.The relative execution times, log scale, of the branches, hotness, imix, and cache_sim monitors when instrumenting an interpreted program.We compare the base unoptimized case, int-rt-int, JITing the instrumentation of the unoptimized case, int-rt-jit, the interpreter trampoline optimization, int-tramp-int, and interpreting simple Whamm rewriting.Ratios are relative to uninstrumented execution time.Lower is better.

Fig. 10 .

111Fig.10.The relative execution times, log scale, of the branches, hotness, imix, and cache_sim monitors when instrumenting a JITed program.We compare the base unoptimized case, jit-rt-int, JITing the instrumentation of the unoptimized case, jit-rt-jit, enabling the JIT wasm-to-wasm call optimization, jit-wasm-int, JITing the instrumentation of that case, jit-wasm-jit, inlining the instrumentation by the JIT, and JITing simple Whamm rewriting.Ratios are relative to uninstrumented execution time.Lower is better.

Fig. 13 .

112Fig.13.Syntax of Whamm.

Table 3 .

113The baseline execution time for the benchmark suites, uninstrumented.

참고문헌 21건

  1. DTrace ACM SIGSOFT Software Engineering Notes · Association for Computing Machinery · 2012 10.1145/2088883.2088902
  2. ATOM Proceedings of the ACM SIGPLAN Conference on Programming Language Design and Implementation · 1994 10.1145/178243.178260
  3. Soot 2010 10.1145/1925805.1925818
  4. The Paradyn parallel performance measurement tool Computer · IEEE Computer Society · 1995 10.1109/2.471178
  5. Proceedings of the 5th international symposium on Principles and practice of programming in Java - PPPJ '07 2007 10.1145/1294325
  6. Design and Implementation of a Java Bytecode Manipulation Library for Clojure Journal of Information Processing · Information Processing Society of Japan · 2015 10.2197/ipsjjip.23.716
  7. Pin ACM SIGPLAN Notices · Association for Computing Machinery · 2005 10.1145/1064978.1065034
  8. Fast, Flexible, Polyglot Instrumentation Support for Debuggers and other Tools The Art Science and Engineering of Programming · Aspect-Oriented Software Association (AOSA) · 2018 10.22152/programming-journal.org/2018/2/14
  9. SEISMIC: SEcure In-lined Script Monitors for Interrupting Cryptojacks Lecture notes in computer science · Springer Science+Business Media · 2018 10.1007/978-3-319-98989-1_7
  10. From Hack to Elaborate Technique—A Survey on Binary Rewriting ACM Computing Surveys · Association for Computing Machinery · 2019 10.1145/3316415
  11. Concepts and experiments in computational reflection ACM SIGPLAN Notices · Association for Computing Machinery · 1987 10.1145/38807.38821
  12. Efficient and expressive bytecode-level instrumentation for Java programs International Journal on Software Tools for Technology Transfer · Springer Science+Business Media · 2023 10.1007/s10009-023-00708-z
  13. Proceedings of the 26th ACM International Conference on Architectural Support for Programming Languages and Operating Systems 2021 10.1145/3445814
  14. Evaluating the accuracy of Java profilers ACM SIGPLAN Notices · Association for Computing Machinery · 2010 10.1145/1809028.1806618
  15. Valgrind ACM SIGPLAN Notices · Association for Computing Machinery · 2007 10.1145/1273442.1250746
  16. Proceedings of the ACM SIGPLAN 1990 conference on Programming language design and implementation - PLDI '90 1990 10.1145/93542
  17. Web Information Systems Engineering – WISE 2022 Lecture notes in computer science · Springer Science+Business Media · 2022 10.1007/978-3-031-20891-1
  18. Towards Solving the Challenge of Minimal Overhead Monitoring 2023 10.1145/3578245.3584851
  19. Optimization-Aware Compiler-Level Event Profiling ACM Transactions on Programming Languages and Systems · Association for Computing Machinery · 2023 10.1145/3591473
  20. Debugging WebAssembly? Put Some Whamm on It! Proceedings of the ACM on Programming Languages · Association for Computing Machinery · 2025 10.1145/3763124
  21. Debugging WebAssembly? Put some Whamm on it! Zenodo (CERN European Organization for Nuclear Research) · European Organization for Nuclear Research · 2025 105281zenodo16929456