Files
jerryscript/targets/mbedos5/jerryscript-mbed/jerryscript-mbed-launcher/source/launcher.cpp
T
Akos Kiss f29e6f9020 Fix undefined overflow behavior when converting double to integer (#3629)
Overflows in conversions from floating-point to integer are
undefined behavior in the C99 standard. (Clause 6.3.1.4: "If the
value of the integral part cannot be represented by the integer
type, the behavior is undefined.")

When UBSAN is enabled, this gets reported at `srand()` calls. (The
random seed is usually initialized using the date port API, which
represents dates as `double`s. But `srand` takes an `unsigned int`.
A simple cast from `double` to `unsigned` becomes undefined
behavior if the value is too large. And "now" is too large
nowadays. So, effectively, all executions start with an undefined
behavior.)

This patch fixes this by casting the floating-point value of the
date to an integer through a union.

JerryScript-DCO-1.0-Signed-off-by: Akos Kiss akiss@inf.u-szeged.hu
2020-03-27 11:03:28 +01:00

96 lines
2.7 KiB
C++

/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mbed.h"
#include "rtos.h"
#include "jerry-core/include/jerryscript.h"
#include "jerry-core/include/jerryscript-port.h"
#include "jerryscript-mbed-event-loop/EventLoop.h"
#include "jerryscript-mbed-util/js_source.h"
#include "jerryscript-mbed-library-registry/registry.h"
#include "jerryscript-mbed-launcher/launcher.h"
#include "jerryscript-mbed-launcher/setup.h"
#include "jerry-targetjs.h"
DECLARE_JS_CODES;
/**
* load_javascript
*
* Parse and run javascript files specified in jerry-targetjs.h
*/
static int load_javascript() {
for (int src = 0; js_codes[src].source; src++) {
LOG_PRINT("running js file %s\r\n", js_codes[src].name);
const jerry_char_t* code = reinterpret_cast<const jerry_char_t*>(js_codes[src].source);
const size_t length = js_codes[src].length;
jerry_value_t parsed_code = jerry_parse(NULL, 0, code, length, JERRY_PARSE_NO_OPTS);
if (jerry_value_is_error(parsed_code)) {
LOG_PRINT_ALWAYS("jerry_parse failed [%s]\r\n", js_codes[src].name);
jerry_release_value(parsed_code);
jsmbed_js_exit();
return -1;
}
jerry_value_t returned_value = jerry_run(parsed_code);
jerry_release_value(parsed_code);
if (jerry_value_is_error(returned_value)) {
LOG_PRINT_ALWAYS("jerry_run failed [%s]\r\n", js_codes[src].name);
jerry_release_value(returned_value);
jsmbed_js_exit();
return -1;
}
jerry_release_value(returned_value);
}
return 0;
}
int jsmbed_js_init() {
union { double d; unsigned u; } now = { .d = jerry_port_get_current_time () };
srand (now.u);
jerry_init_flag_t flags = JERRY_INIT_EMPTY;
jerry_init(flags);
jsmbed_js_load_magic_strings();
mbed::js::LibraryRegistry::getInstance().register_all();
return 0;
}
void jsmbed_js_exit() {
jerry_cleanup();
}
void jsmbed_js_launch() {
jsmbed_js_init();
puts(" JerryScript in mbed\r\n");
puts(" build date: " __DATE__ " \r\n");
if (load_javascript() == 0) {
mbed::js::event_loop();
}
}