Add js heap size limit & tests (#2177)

This commit is contained in:
Maik Riechert 2021-02-16 20:07:54 +00:00 коммит произвёл GitHub
Родитель 951a7a9380
Коммит e9af59eb3c
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
6 изменённых файлов: 108 добавлений и 1 удалений

Просмотреть файл

@ -620,6 +620,13 @@ if(BUILD_TESTS)
${CMAKE_SOURCE_DIR}/tests/js-custom-authorization
)
add_e2e_test(
NAME js_limits_test
PYTHON_SCRIPT ${CMAKE_SOURCE_DIR}/tests/js-limits/limits.py
CONSENSUS cft
ADDITIONAL_ARGS --js-app-bundle ${CMAKE_SOURCE_DIR}/tests/js-limits
)
add_e2e_test(
NAME authentication_test
PYTHON_SCRIPT ${CMAKE_SOURCE_DIR}/tests/js-authentication/authentication.py

Просмотреть файл

@ -29,7 +29,7 @@ if("sgx" IN_LIST COMPILE_TARGETS)
)
target_compile_options(
quickjs.enclave PUBLIC -nostdinc -DCONFIG_VERSION="${QUICKJS_VERSION}"
-DEMSCRIPTEN
-DEMSCRIPTEN -DCONFIG_STACK_CHECK
)
target_link_libraries(quickjs.enclave PUBLIC ${OE_TARGET_LIBC})
set_property(TARGET quickjs.enclave PROPERTY POSITION_INDEPENDENT_CODE ON)

Просмотреть файл

@ -969,6 +969,7 @@ namespace ccfapp
JSAutoFreeRuntime auto_free_rt(rt);
JS_SetMaxStackSize(rt, 1024 * 1024);
JS_SetMemoryLimit(rt, 100 * 1024 * 1024);
JSModuleLoaderArg js_module_loader_arg{&this->network, &args.tx};
JS_SetModuleLoaderFunc(

26
tests/js-limits/app.json Normal file
Просмотреть файл

@ -0,0 +1,26 @@
{
"endpoints": {
"/recursive": {
"post": {
"js_module": "limits.js",
"js_function": "recursive",
"forwarding_required": "never",
"execute_outside_consensus": "never",
"authn_policies": ["user_cert"],
"readonly": true,
"openapi": {}
}
},
"/alloc": {
"post": {
"js_module": "limits.js",
"js_function": "alloc",
"forwarding_required": "never",
"execute_outside_consensus": "never",
"authn_policies": ["user_cert"],
"readonly": true,
"openapi": {}
}
}
}
}

56
tests/js-limits/limits.py Normal file
Просмотреть файл

@ -0,0 +1,56 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the Apache 2.0 License.
import http
import infra.network
import infra.path
import infra.proc
import infra.net
import infra.e2e_args
import suite.test_requirements as reqs
@reqs.description("Test stack size limit")
def test_stack_size_limit(network, args):
primary, _ = network.find_nodes()
with primary.client("user0") as c:
r = c.post("/app/recursive", body={"depth": 50})
assert r.status_code == http.HTTPStatus.OK, r.status_code
with primary.client("user0") as c:
r = c.post("/app/recursive", body={"depth": 1000})
assert r.status_code == http.HTTPStatus.INTERNAL_SERVER_ERROR, r.status_code
return network
@reqs.description("Test heap size limit")
def test_heap_size_limit(network, args):
primary, _ = network.find_nodes()
with primary.client("user0") as c:
r = c.post("/app/alloc", body={"size": 5 * 1024 * 1024})
assert r.status_code == http.HTTPStatus.OK, r.status_code
with primary.client("user0") as c:
r = c.post("/app/alloc", body={"size": 500 * 1024 * 1024})
assert r.status_code == http.HTTPStatus.INTERNAL_SERVER_ERROR, r.status_code
return network
def run(args):
with infra.network.network(
args.nodes, args.binary_dir, args.debug_nodes, args.perf_nodes, pdb=args.pdb
) as network:
network.start_and_join(args)
network = test_stack_size_limit(network, args)
network = test_heap_size_limit(network, args)
if __name__ == "__main__":
args = infra.e2e_args.cli_args()
args.package = "libjs_generic"
args.nodes = infra.e2e_args.max_nodes(args, f=0)
run(args)

Просмотреть файл

@ -0,0 +1,17 @@
export function recursive(request) {
const depth = request.body.json()["depth"];
_recursive(depth);
return {};
}
function _recursive(depth) {
if (depth > 0) {
_recursive(--depth);
}
}
export function alloc(request) {
const size = request.body.json()["size"];
new Uint8Array(size);
return {};
}