From ac96830bc1df45db30b1089d5fe7b0e4c78ed7c7 Mon Sep 17 00:00:00 2001 From: steven Date: Thu, 27 Feb 2020 15:21:11 +0800 Subject: [PATCH 1/2] add Jit usage level flag, default None --- cmd/config.go | 13 +++++-- cmd/usage.go | 2 +- cmd/utils/flags.go | 7 ++-- common/config/config.go | 38 ++++++++++--------- core/store/ledgerstore/ledger_store.go | 5 +-- core/store/ledgerstore/tx_handler.go | 3 +- core/validation/transaction_validator.go | 2 +- main.go | 2 +- smartcontract/service/wasmvm/contract.go | 4 +- smartcontract/service/wasmvm/utils.go | 40 ++++++++++++++++++-- smartcontract/service/wasmvm/wasm_service.go | 3 +- wasmtest/wasm-test.go | 1 + 12 files changed, 80 insertions(+), 40 deletions(-) diff --git a/cmd/config.go b/cmd/config.go index 12f2d39566..5c1fb12540 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -19,7 +19,9 @@ package cmd import ( + "errors" "fmt" + "github.com/ontio/ontology/cmd/utils" "github.com/ontio/ontology/common" "github.com/ontio/ontology/common/config" @@ -62,12 +64,15 @@ func SetOntologyConfig(ctx *cli.Context) (*config.OntologyConfig, error) { } } - enableWasmJitVerify := ctx.GlobalBool(utils.GetFlagName(utils.WasmVerifyMethodFlag)) - if enableWasmJitVerify { - log.Infof("Enable wasm jit verifier") - cfg.Common.WasmVerifyMethod = config.JitVerifyMethod + wasmJitLevel := ctx.GlobalInt(utils.GetFlagName(utils.WasmJitLevelFlag)) + + if wasmJitLevel > 3 { + return nil, errors.New("Error wasmJitLevel, should be (0~3). 0:None 1:Low 2:Mid 3:Heigh") } + cfg.Common.WasmJitLevel = config.WasmJitLevelType(wasmJitLevel) + log.Infof("Wasm Jit Level:%d", cfg.Common.WasmJitLevel) + return cfg, nil } diff --git a/cmd/usage.go b/cmd/usage.go index 5d05a7a29c..8fd898cad2 100644 --- a/cmd/usage.go +++ b/cmd/usage.go @@ -102,7 +102,7 @@ var AppHelpFlagGroups = []flagGroup{ utils.DisableLogFileFlag, utils.DisableEventLogFlag, utils.DataDirFlag, - utils.WasmVerifyMethodFlag, + utils.WasmJitLevelFlag, }, }, { diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 77156a5401..2d18f344c2 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -52,9 +52,10 @@ var ( Name: "disable-event-log", Usage: "Discard event log output by smart contract execution", } - WasmVerifyMethodFlag = cli.BoolFlag{ - Name: "enable-wasmjit-verifier", - Usage: "Enable wasmjit verifier to verify wasm contract", + WasmJitLevelFlag = cli.UintFlag{ + Name: "jitlevel", + Usage: "Set the wasm jit level to `` (0~3). 0:None 1:Low 2:Mid 3:Heigh", + Value: uint(config.DEFAULT_WASM_JIT_LEVEL), } WalletFileFlag = cli.StringFlag{ Name: "wallet,w", diff --git a/common/config/config.go b/common/config/config.go index 020e9936c7..8c45a734c9 100644 --- a/common/config/config.go +++ b/common/config/config.go @@ -35,12 +35,13 @@ import ( var Version = "" //Set value when build project -type VerifyMethod int +type WasmJitLevelType int const ( - InterpVerifyMethod VerifyMethod = iota - JitVerifyMethod - NoneVerifyMethod + WasmJitLevelNone WasmJitLevelType = iota + WasmJitLevelLow + WasmJitLevelMid + WasmJitLevelHeigh ) const ( @@ -57,6 +58,7 @@ const ( CONSENSUS_TYPE_VBFT = "vbft" DEFAULT_LOG_LEVEL = log.InfoLog + DEFAULT_WASM_JIT_LEVEL = WasmJitLevelNone DEFAULT_MAX_LOG_SIZE = 100 //MByte DEFAULT_NODE_PORT = uint(20338) DEFAULT_CONSENSUS_PORT = uint(20339) @@ -498,14 +500,14 @@ type SOLOConfig struct { } type CommonConfig struct { - LogLevel uint - NodeType string - EnableEventLog bool - SystemFee map[string]int64 - GasLimit uint64 - GasPrice uint64 - DataDir string - WasmVerifyMethod VerifyMethod + LogLevel uint + NodeType string + EnableEventLog bool + SystemFee map[string]int64 + GasLimit uint64 + GasPrice uint64 + DataDir string + WasmJitLevel WasmJitLevelType } type ConsensusConfig struct { @@ -571,12 +573,12 @@ func NewOntologyConfig() *OntologyConfig { return &OntologyConfig{ Genesis: MainNetConfig, Common: &CommonConfig{ - LogLevel: DEFAULT_LOG_LEVEL, - EnableEventLog: DEFAULT_ENABLE_EVENT_LOG, - SystemFee: make(map[string]int64), - GasLimit: DEFAULT_GAS_LIMIT, - DataDir: DEFAULT_DATA_DIR, - WasmVerifyMethod: InterpVerifyMethod, + LogLevel: DEFAULT_LOG_LEVEL, + EnableEventLog: DEFAULT_ENABLE_EVENT_LOG, + SystemFee: make(map[string]int64), + GasLimit: DEFAULT_GAS_LIMIT, + DataDir: DEFAULT_DATA_DIR, + WasmJitLevel: DEFAULT_WASM_JIT_LEVEL, }, Consensus: &ConsensusConfig{ EnableConsensus: true, diff --git a/core/store/ledgerstore/ledger_store.go b/core/store/ledgerstore/ledger_store.go index 03f3822a0f..016684dd31 100644 --- a/core/store/ledgerstore/ledger_store.go +++ b/core/store/ledgerstore/ledger_store.go @@ -1220,8 +1220,7 @@ func (this *LedgerStoreImp) PreExecuteContractWithParam(tx *types.Transaction, p deploy := tx.Payload.(*payload.DeployCode) if deploy.VmType() == payload.WASMVM_TYPE { - wasmCode := deploy.GetRawCode() - err := wasmvm.WasmjitValidate(wasmCode) + _, err := wasmvm.ReadWasmModule(deploy.GetRawCode(), wasmvm.GetVerifyMethodByJitLevel(config.DefConfig.Common.WasmJitLevel)) if err != nil { return stf, err } @@ -1244,7 +1243,7 @@ func (this *LedgerStoreImp) PreExecuteContractWithParam(tx *types.Transaction, p //PreExecuteContract return the result of smart contract execution without commit to store func (this *LedgerStoreImp) PreExecuteContract(tx *types.Transaction) (*sstate.PreExecResult, error) { param := PrexecuteParam{ - JitMode: false, + JitMode: wasmvm.GetJitModeByJitLevel(config.DefConfig.Common.WasmJitLevel, true), WasmFactor: 0, MinGas: true, } diff --git a/core/store/ledgerstore/tx_handler.go b/core/store/ledgerstore/tx_handler.go index c92a657797..03cf1b08fc 100644 --- a/core/store/ledgerstore/tx_handler.go +++ b/core/store/ledgerstore/tx_handler.go @@ -75,7 +75,7 @@ func (self *StateStore) HandleDeployTransaction(store store.LedgerStore, overlay ) if deploy.VmType() == payload.WASMVM_TYPE { - _, err = wasmvm.ReadWasmModule(deploy.GetRawCode(), sysconfig.DefConfig.Common.WasmVerifyMethod) + _, err = wasmvm.ReadWasmModule(deploy.GetRawCode(), wasmvm.GetVerifyMethodByJitLevel(sysconfig.DefConfig.Common.WasmJitLevel)) if err != nil { return err } @@ -223,6 +223,7 @@ func (self *StateStore) HandleInvokeTransaction(store store.LedgerStore, overlay GasTable: gasTable, Gas: availableGasLimit - codeLenGasLimit, WasmExecStep: sysconfig.DEFAULT_WASM_MAX_STEPCOUNT, + JitMode: wasmvm.GetJitModeByJitLevel(sysconfig.DefConfig.Common.WasmJitLevel, false), PreExec: false, } diff --git a/core/validation/transaction_validator.go b/core/validation/transaction_validator.go index e7b3b0ceee..779a0127e5 100644 --- a/core/validation/transaction_validator.go +++ b/core/validation/transaction_validator.go @@ -118,7 +118,7 @@ func checkTransactionPayload(tx *types.Transaction) error { case *payload.DeployCode: deploy := tx.Payload.(*payload.DeployCode) if deploy.VmType() == payload.WASMVM_TYPE { - _, err := wasmvm.ReadWasmModule(deploy.GetRawCode(), config.DefConfig.Common.WasmVerifyMethod) + _, err := wasmvm.ReadWasmModule(deploy.GetRawCode(), wasmvm.GetVerifyMethodByJitLevel(config.DefConfig.Common.WasmJitLevel)) if err != nil { return err } diff --git a/main.go b/main.go index 0b0786d459..ae4e703db5 100644 --- a/main.go +++ b/main.go @@ -88,7 +88,7 @@ func setupAPP() *cli.App { utils.DisableLogFileFlag, utils.DisableEventLogFlag, utils.DataDirFlag, - utils.WasmVerifyMethodFlag, + utils.WasmJitLevelFlag, //account setting utils.WalletFileFlag, utils.AccountAddressFlag, diff --git a/smartcontract/service/wasmvm/contract.go b/smartcontract/service/wasmvm/contract.go index b9aa6403fc..253d96401e 100644 --- a/smartcontract/service/wasmvm/contract.go +++ b/smartcontract/service/wasmvm/contract.go @@ -123,7 +123,7 @@ func ContractCreate(proc *exec.Process, if err != nil { panic(err) } - _, err = ReadWasmModule(wasmCode, config.DefConfig.Common.WasmVerifyMethod) + _, err = ReadWasmModule(wasmCode, GetVerifyMethodByJitLevel(config.DefConfig.Common.WasmJitLevel)) if err != nil { panic(err) } @@ -200,7 +200,7 @@ func ContractMigrate(proc *exec.Process, if err != nil { panic(err) } - _, err = ReadWasmModule(wasmCode, config.DefConfig.Common.WasmVerifyMethod) + _, err = ReadWasmModule(wasmCode, GetVerifyMethodByJitLevel(config.DefConfig.Common.WasmJitLevel)) if err != nil { panic(err) } diff --git a/smartcontract/service/wasmvm/utils.go b/smartcontract/service/wasmvm/utils.go index ebc261f70e..23256c3c5c 100644 --- a/smartcontract/service/wasmvm/utils.go +++ b/smartcontract/service/wasmvm/utils.go @@ -28,6 +28,38 @@ import ( "github.com/ontio/wagon/wasm" ) +type VerifyMethod int + +const ( + InterpVerifyMethod VerifyMethod = iota + JitVerifyMethod + NoneVerifyMethod +) + +func GetJitModeByJitLevel(l config.WasmJitLevelType, isPre bool) bool { + switch l { + case config.WasmJitLevelNone, config.WasmJitLevelLow: + return false + case config.WasmJitLevelMid: + if isPre { + return true + } + case config.WasmJitLevelHeigh: + return true + } + + return false +} + +func GetVerifyMethodByJitLevel(l config.WasmJitLevelType) VerifyMethod { + switch l { + case config.WasmJitLevelLow, config.WasmJitLevelMid, config.WasmJitLevelHeigh: + return JitVerifyMethod + default: + return InterpVerifyMethod + } +} + func ReadWasmMemory(proc *exec.Process, ptr uint32, len uint32) ([]byte, error) { if uint64(proc.MemSize()) < uint64(ptr)+uint64(len) { return nil, errors.New("contract create len is greater than memory size") @@ -80,7 +112,7 @@ func checkOntoWasm(m *wasm.Module) error { return nil } -func ReadWasmModule(code []byte, verify config.VerifyMethod) (*exec.CompiledModule, error) { +func ReadWasmModule(code []byte, verify VerifyMethod) (*exec.CompiledModule, error) { m, err := wasm.ReadModule(bytes.NewReader(code), func(name string) (*wasm.Module, error) { switch name { case "env": @@ -92,7 +124,7 @@ func ReadWasmModule(code []byte, verify config.VerifyMethod) (*exec.CompiledModu return nil, err } - if verify != config.NoneVerifyMethod { + if verify != NoneVerifyMethod { err = checkOntoWasm(m) if err != nil { return nil, err @@ -104,12 +136,12 @@ func ReadWasmModule(code []byte, verify config.VerifyMethod) (*exec.CompiledModu } switch verify { - case config.InterpVerifyMethod: + case InterpVerifyMethod: err = validate.VerifyWasmCodeFromRust(code) if err != nil { return nil, err } - case config.JitVerifyMethod: + case JitVerifyMethod: err := WasmjitValidate(code) if err != nil { return nil, err diff --git a/smartcontract/service/wasmvm/wasm_service.go b/smartcontract/service/wasmvm/wasm_service.go index 2ff9c45cab..577d4d270b 100644 --- a/smartcontract/service/wasmvm/wasm_service.go +++ b/smartcontract/service/wasmvm/wasm_service.go @@ -22,7 +22,6 @@ import ( "github.com/hashicorp/golang-lru" "github.com/ontio/ontology/common" - "github.com/ontio/ontology/common/config" "github.com/ontio/ontology/core/store" "github.com/ontio/ontology/core/types" "github.com/ontio/ontology/errors" @@ -177,7 +176,7 @@ func invokeInterpreter(this *WasmVmService, contract *states.WasmContractParam, } if compiled == nil { - module, err := ReadWasmModule(wasmCode, config.NoneVerifyMethod) + module, err := ReadWasmModule(wasmCode, NoneVerifyMethod) if err != nil { return nil, err } diff --git a/wasmtest/wasm-test.go b/wasmtest/wasm-test.go index 8f0128eb94..61386fbeb5 100644 --- a/wasmtest/wasm-test.go +++ b/wasmtest/wasm-test.go @@ -280,6 +280,7 @@ func main() { config.DefConfig.Genesis.SOLO.GenBlockTime = 3 config.DefConfig.Genesis.SOLO.Bookkeepers = []string{hex.EncodeToString(buf)} config.DefConfig.P2PNode.NetworkId = 0 + config.DefConfig.Common.WasmJitLevel = config.WasmJitLevelMid bookkeepers := []keypair.PublicKey{acct.PublicKey} //Init event hub From c318949f1c0f92f9ea1ae93c2e02fe55857ad79b Mon Sep 17 00:00:00 2001 From: laizy Date: Thu, 27 Feb 2020 16:03:06 +0800 Subject: [PATCH 2/2] Update cmd/utils/flags.go --- cmd/utils/flags.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 2d18f344c2..2b43d203e6 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -53,7 +53,7 @@ var ( Usage: "Discard event log output by smart contract execution", } WasmJitLevelFlag = cli.UintFlag{ - Name: "jitlevel", + Name: "wasmjit-level", Usage: "Set the wasm jit level to `` (0~3). 0:None 1:Low 2:Mid 3:Heigh", Value: uint(config.DEFAULT_WASM_JIT_LEVEL), }