Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Jit usage level flag, default None #1164

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ var AppHelpFlagGroups = []flagGroup{
utils.DisableLogFileFlag,
utils.DisableEventLogFlag,
utils.DataDirFlag,
utils.WasmVerifyMethodFlag,
utils.WasmJitLevelFlag,
},
},
{
Expand Down
7 changes: 4 additions & 3 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: "wasmjit-level",
Usage: "Set the wasm jit level to `<level>` (0~3). 0:None 1:Low 2:Mid 3:Heigh",
Value: uint(config.DEFAULT_WASM_JIT_LEVEL),
}
WalletFileFlag = cli.StringFlag{
Name: "wallet,w",
Expand Down
38 changes: 20 additions & 18 deletions common/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename to WasmJitLevelHigh, and document the effects each level enabled.

)

const (
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions core/store/ledgerstore/ledger_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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,
}
Expand Down
3 changes: 2 additions & 1 deletion core/store/ledgerstore/tx_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
}

Expand Down
2 changes: 1 addition & 1 deletion core/validation/transaction_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func setupAPP() *cli.App {
utils.DisableLogFileFlag,
utils.DisableEventLogFlag,
utils.DataDirFlag,
utils.WasmVerifyMethodFlag,
utils.WasmJitLevelFlag,
//account setting
utils.WalletFileFlag,
utils.AccountAddressFlag,
Expand Down
4 changes: 2 additions & 2 deletions smartcontract/service/wasmvm/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
40 changes: 36 additions & 4 deletions smartcontract/service/wasmvm/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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":
Expand All @@ -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
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions smartcontract/service/wasmvm/wasm_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions wasmtest/wasm-test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down