Skip to content

Commit ca5b0a7

Browse files
Check for out of bounds memory access
1 parent a7225ce commit ca5b0a7

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

Source/buildbindingccpp.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1883,6 +1883,9 @@ func writeWasmtimeLambda(w LanguageWriter, NameSpace string, method ComponentDef
18831883
if !cParamIsPointer(cParam) {
18841884
pointerString = "*"
18851885
}
1886+
// ensure memory access does not go outside of WASM linear memory
1887+
lines = append(lines, fmt.Sprintf("if (w%s + sizeof(%s) >= nMemorySize)", cParam.ParamName, cParam.ParamType))
1888+
lines = append(lines, fmt.Sprintf(" return %s_ERROR_INVALIDPARAM;", strings.ToUpper(NameSpace)))
18861889
lines = append(lines, fmt.Sprintf("%s %s = %s( (%s %s)(_pData + w%s) );" , cParam.ParamType, cParam.ParamName, pointerString, cParam.ParamType, pointerString, cParam.ParamName))
18871890
if cParamIsPointer(cParam) {
18881891
// a nullptr in the WASM module is conveyed to the host as a 0 offset in WASM linear memory
@@ -1895,9 +1898,9 @@ func writeWasmtimeLambda(w LanguageWriter, NameSpace string, method ComponentDef
18951898

18961899
w.Writeln(" auto %s = [this, &store](%s)", methodName, wasmtimeParams)
18971900
w.Writeln(" {")
1898-
w.Writeln(" ")
18991901
w.Writeln(" auto data = this->m_pMemory->data(store);")
19001902
w.Writeln(" uint8_t* _pData = data.data();")
1903+
w.Writeln(" const uint64_t nMemorySize = this->m_pMemory->size(store);")
19011904
w.Writeln(" ")
19021905

19031906
w.Writelns(" ", lines)
@@ -1943,7 +1946,7 @@ func buildCppwasmHostHeader(component ComponentDefinition, w LanguageWriter, Nam
19431946
}
19441947
w.Writeln("")
19451948

1946-
w.Writeln("namespace %s {", NameSpace)
1949+
w.Writeln("namespace %sWASM {", NameSpace)
19471950
w.Writeln("")
19481951

19491952
w.Writeln("/*************************************************************************************************************************")

0 commit comments

Comments
 (0)