Skip to content

Commit

Permalink
[trace-view] Added support for native functions and global locations (#…
Browse files Browse the repository at this point in the history
…20080)

## Description 

Adds support for native functions where the `CloseFrame` event happens
right after `OpenFrame` event and both need to be skipped to skip over
the native function. Also added support for handling global locations -
they should be parsed properly but largely ignored in the trace as they
cannot directly affect values of local variables

## Test plan 

All old and new tests must pass
  • Loading branch information
awelc authored Oct 30, 2024
1 parent 21639a5 commit 7e6319f
Show file tree
Hide file tree
Showing 30 changed files with 1,269 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,17 @@ export class Runtime extends EventEmitter {
this.sendEvent(RuntimeEvents.stopOnStep);
return false;
} else if (currentEvent.type === TraceEventKind.OpenFrame) {
// if function is native then the next event will be CloseFrame
if (currentEvent.isNative) {
if (this.trace.events.length <= this.eventIndex + 1 ||
this.trace.events[this.eventIndex + 1].type !== TraceEventKind.CloseFrame) {
throw new Error('Expected an CloseFrame event after native OpenFrame event');
}
// skip over CloseFrame as there is no frame to pop
this.eventIndex++;
return this.step(next, stopAtCloseFrame);
}

// create a new frame and push it onto the stack
const newFrame =
this.newStackFrame(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@

import * as fs from 'fs';
import { FRAME_LIFETIME, ModuleInfo } from './utils';
import { IRuntimeCompundValue, RuntimeValueType, IRuntimeVariableLoc } from './runtime';
import {
IRuntimeCompundValue,
RuntimeValueType,
IRuntimeVariableLoc,
IRuntimeRefValue
} from './runtime';
import { ISourceMap, ILoc, IFileInfo } from './source_map_utils';


Expand Down Expand Up @@ -203,6 +208,7 @@ export type TraceEvent =
id: number,
name: string,
fileHash: string
isNative: boolean,
localsTypes: string[],
localsNames: string[],
paramValues: RuntimeValueType[]
Expand Down Expand Up @@ -346,6 +352,7 @@ export function readTrace(
id: frame.frame_id,
name: frame.function_name,
fileHash: sourceMap.fileHash,
isNative: frame.is_native,
localsTypes,
localsNames: funEntry.localsNames,
paramValues,
Expand Down Expand Up @@ -424,6 +431,10 @@ export function readTrace(
const location = effect.Write ? effect.Write.location : effect.Read!.location;
const loc = processJSONLocalLocation(location, localLifetimeEnds);
if (effect.Write) {
if (!loc) {
throw new Error('Unsupported location type in Write effect');
}
// process a write only if the location is supported
const value = 'RuntimeValue' in effect.Write.root_value_after_write
? traceRuntimeValueFromJSON(effect.Write.root_value_after_write.RuntimeValue.value)
: traceRefValueFromJSON(effect.Write.root_value_after_write);
Expand Down Expand Up @@ -488,12 +499,11 @@ function JSONTraceAddressToHexString(address: string): string {
* @param localLifetimeEnds map of local variable lifetimes (defined if local variable
* lifetime should happen).
* @returns variable location.
* @throws error if the location type is not supported.
*/
function processJSONLocalLocation(
traceLocation: JSONTraceLocation,
localLifetimeEnds?: Map<number, number[]>,
): IRuntimeVariableLoc {
): IRuntimeVariableLoc | undefined {
if ('Local' in traceLocation) {
const frameID = traceLocation.Local[0];
const localIndex = traceLocation.Local[1];
Expand All @@ -506,7 +516,15 @@ function processJSONLocalLocation(
} else if ('Indexed' in traceLocation) {
return processJSONLocalLocation(traceLocation.Indexed[0], localLifetimeEnds);
} else {
throw new Error(`Unsupported location type: ${JSON.stringify(traceLocation)}`);
// Currently, there is nothing that needs to be done for 'Global' locations,
// neither with respect to lifetime nor with respect to location itself.
// This is because `Global` locations currently only represent read-only
// refererence values returned from native functions. If there ever was
// a native functino that would return a mutable reference, we should
// consider how to handle value changes via such reference, but it's unlikely
// that such a function would ever be added to either Move stdlib or
// the Sui framework.
return undefined;
}
}

Expand All @@ -515,18 +533,23 @@ function processJSONLocalLocation(
*
* @param value JSON trace reference value.
* @returns runtime value.
* @throws Error with a descriptive error message if conversion has failed.
*/
function traceRefValueFromJSON(value: JSONTraceRefValue): RuntimeValueType {
if ('MutRef' in value) {
return {
mutable: true,
loc: processJSONLocalLocation(value.MutRef.location)
};
const loc = processJSONLocalLocation(value.MutRef.location);
if (!loc) {
throw new Error('Unsupported location type in MutRef');
}
const ret: IRuntimeRefValue = { mutable: true, loc };
return ret;
} else {
return {
mutable: false,
loc: processJSONLocalLocation(value.ImmRef.location)
};
const loc = processJSONLocalLocation(value.ImmRef.location);
if (!loc) {
throw new Error('Unsupported location type in ImmRef');
}
const ret: IRuntimeRefValue = { mutable: false, loc };
return ret;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "global_loc"
edition = "2024.beta"

[dependencies]
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "framework/mainnet" }

[addresses]
global_loc = "0x0"
Sui = "0x2"
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"definition_location":{"file_hash":[34,201,103,208,120,108,208,171,127,162,154,113,96,186,51,169,173,216,199,217,88,54,128,150,101,140,27,7,37,201,47,24],"start":395,"end":398},"module_name":["0000000000000000000000000000000000000000000000000000000000000001","bcs"],"struct_map":{},"enum_map":{},"function_map":{"0":{"definition_location":{"file_hash":[34,201,103,208,120,108,208,171,127,162,154,113,96,186,51,169,173,216,199,217,88,54,128,150,101,140,27,7,37,201,47,24],"start":510,"end":518},"type_parameters":[["MoveValue",{"file_hash":[34,201,103,208,120,108,208,171,127,162,154,113,96,186,51,169,173,216,199,217,88,54,128,150,101,140,27,7,37,201,47,24],"start":519,"end":528}]],"parameters":[["v#0#0",{"file_hash":[34,201,103,208,120,108,208,171,127,162,154,113,96,186,51,169,173,216,199,217,88,54,128,150,101,140,27,7,37,201,47,24],"start":530,"end":531}]],"returns":[{"file_hash":[34,201,103,208,120,108,208,171,127,162,154,113,96,186,51,169,173,216,199,217,88,54,128,150,101,140,27,7,37,201,47,24],"start":546,"end":556}],"locals":[],"nops":{},"code_map":{},"is_native":true},"1":{"definition_location":{"file_hash":[34,201,103,208,120,108,208,171,127,162,154,113,96,186,51,169,173,216,199,217,88,54,128,150,101,140,27,7,37,201,47,24],"start":383,"end":557},"type_parameters":[],"parameters":[],"returns":[],"locals":[],"nops":{},"code_map":{"0":{"file_hash":[34,201,103,208,120,108,208,171,127,162,154,113,96,186,51,169,173,216,199,217,88,54,128,150,101,140,27,7,37,201,47,24],"start":383,"end":557}},"is_native":false}},"constant_map":{}}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"definition_location":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":70,"end":71},"module_name":["0000000000000000000000000000000000000000000000000000000000000000","m"],"struct_map":{"0":{"definition_location":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":88,"end":98},"type_parameters":[],"fields":[{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":113,"end":115},{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":126,"end":129}]}},"enum_map":{},"function_map":{"0":{"definition_location":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":142,"end":145},"type_parameters":[],"parameters":[["o#0#0",{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":146,"end":147}],["p#0#0",{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":161,"end":162}]],"returns":[{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":169,"end":172}],"locals":[["%#1",{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":187,"end":201}],["%#2",{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":187,"end":212}],["n#1#0",{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":183,"end":184}],["num#1#0",{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":242,"end":245}]],"nops":{},"code_map":{"0":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":198,"end":200},"1":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":187,"end":201},"4":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":187,"end":212},"6":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":187,"end":215},"7":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":213,"end":214},"8":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":187,"end":215},"10":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":183,"end":184},"11":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":250,"end":251},"12":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":225,"end":247},"13":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":242,"end":245},"14":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":257,"end":275},"15":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":282,"end":283},"16":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":282,"end":290},"17":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":295,"end":298},"18":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":295,"end":305},"19":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":292,"end":293},"20":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":310,"end":311},"21":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":310,"end":318},"22":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":307,"end":308},"23":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":281,"end":319}},"is_native":false},"1":{"definition_location":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":335,"end":339},"type_parameters":[],"parameters":[],"returns":[],"locals":[["ctx#1#0",{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":356,"end":359}]],"nops":{},"code_map":{"0":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":362,"end":381},"1":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":352,"end":359},"2":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":435,"end":443},"3":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":423,"end":444},"4":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":451,"end":453},"5":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":406,"end":455},"6":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":457,"end":459},"7":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":402,"end":460},"8":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":590,"end":598},"9":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":578,"end":599},"10":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":606,"end":608},"11":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":561,"end":610},"12":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":612,"end":614},"13":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":557,"end":615},"14":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":555,"end":556},"15":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":543,"end":547},"16":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":615,"end":616}},"is_native":false},"2":{"definition_location":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":51,"end":618},"type_parameters":[],"parameters":[],"returns":[],"locals":[],"nops":{},"code_map":{"0":{"file_hash":[9,0,245,148,110,8,77,217,5,163,139,61,20,43,54,140,85,157,1,225,181,146,97,220,49,193,128,189,216,45,155,5],"start":51,"end":618}},"is_native":false}},"constant_map":{}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

/// Utility for converting a Move value to its binary representation in BCS (Binary Canonical
/// Serialization). BCS is the binary encoding for Move resources and other non-module values
/// published on-chain. See https://github.com/diem/bcs#binary-canonical-serialization-bcs for more
/// details on BCS.
module std::bcs;

/// Return the binary representation of `v` in BCS (Binary Canonical Serialization) format
public native fun to_bytes<MoveValue>(v: &MoveValue): vector<u8>;
Loading

0 comments on commit 7e6319f

Please sign in to comment.