Skip to content

Implement 64-Bit System Clock Runtime & Secured Integer Parser#39

Open
LucasChen1108 wants to merge 6 commits intosource-academy:masterfrom
LucasChen1108:master
Open

Implement 64-Bit System Clock Runtime & Secured Integer Parser#39
LucasChen1108 wants to merge 6 commits intosource-academy:masterfrom
LucasChen1108:master

Conversation

@LucasChen1108
Copy link
Copy Markdown

@LucasChen1108 LucasChen1108 commented Apr 7, 2026

Implement 64-Bit System Clock Runtime & Secured Integer Parser

Overview

This pull request addresses two unimplemented stubs within Sinter's default core specification: returning exact VM uptimes via runtime(), and decoding strings securely into integers via parse_int().

The runtime() logic has been adapted to map cleanly with EV3/POSIX <time.h> standards, and a separate web-compatible implementation has been added to lib.c for the Sinter WebAssembly demo.


What's Changed

  • Web Demo Clock Support (lib.c): Added get_time_ms implementation for the WebAssembly target, enabling runtime() to function correctly in the browser-based Sinter demo.
  • Configured EV3 OS Runner Hooks: Sinter runner on Linux target platforms now natively hooks into clock_gettime(CLOCK_MONOTONIC) and bridges hardware execution time accurately into sivmfn_primitives.
  • 64-Bit High Precision Upgrade: Transitioned sinter_get_time_ms to a uint64_t interface to handle long-running programs safely.
  • Dynamic Capacity Fallback: runtime() evaluates whether elapsed uptime exceeds NANBOX_INTMAX limits. If the timestamp overflows, it safely falls back to NANBOX_OFFLOAT, preventing crashes on long-running robots.
  • Registered Implementations: Replaced sivmfn_prim_unimpl stubs for parse_int and runtime in the primitives dispatch table (sivmfn_primitives[]) with the new implementations, activating both functions in the VM for the first time.
  • Spec-Compliant parse_int Parser: primitives.c leverages sistrobj_tocharptr to extract chars from internal C string structs directly.
  • Hardened Validation:
    • Validates radices per JavaScript conventions (rejecting values < 2 or > 36)
    • Catches unsafe float conversions using !isfinite(fv) || truncf(fv) != fv
    • Captures endptr == str from strtol to ensure unresolved strings return NANBOX_CANONICAL_NAN

Type of Patch

  • ✅ New feature (non-breaking change which adds functionality)

Test Plan

Native EV3/Linux:
Compiled via GCC and tested with:

./sinter_runner test_programs/prim_parse_int.svm
./sinter_runner test_programs/prim_get_time.svm

Execution generated 0 faults locally.

Web Demo:
Tested on the browser-based Sinter demo with the following Source program:

const start_time = get_time();
let current_amount = 0;
let binary_test = 0;
let hex_test = 0;
let standard_test = 0;

let i = 0;
while (i < 1000000) {
    binary_test = parse_int("1010", 2);
    hex_test = parse_int("FF", 16);
    standard_test = parse_int("42", 10);
    current_amount = current_amount + binary_test + hex_test + standard_test;
    i = i + 1;
}

const end_time = get_time();
display("Total parsed amount:");
display(current_amount);
display("Time in ms:");
display(end_time - start_time);

Successfully returns correct parsed values and elapsed time in milliseconds.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request implements the parse_int and runtime primitives for the Sinter VM. It introduces a cross-platform mechanism to retrieve the current time in milliseconds and adds a 1MB heap allocation for the native runner. Feedback was provided regarding the missing return value check for malloc in the runner, which could lead to a segmentation fault, and the lack of ERANGE error handling when using strtol in the parse_int implementation.

Comment thread runner/src/runner.c
Comment thread vm/src/primitives.c
@LucasChen1108 LucasChen1108 self-assigned this Apr 7, 2026
@LucasChen1108 LucasChen1108 added the enhancement New feature or request label Apr 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants