schemurai 3.0.2 → 3.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +22 -3
- data/ext/schemurai_native/Cargo.lock +14 -3
- data/ext/schemurai_native/Cargo.toml +4 -3
- data/ext/schemurai_native/build.rs +7 -6
- data/ext/schemurai_native/extconf.rb +4 -0
- data/ext/schemurai_native/glue.c +33 -10
- data/ext/schemurai_native/protocol.def +52 -0
- data/ext/schemurai_native/protocol.h +2 -0
- data/ext/schemurai_native/src/container.rs +6 -6
- data/ext/schemurai_native/src/content.rs +63 -31
- data/ext/schemurai_native/src/diagnostics.rs +75 -116
- data/ext/schemurai_native/src/lib.rs +56 -54
- data/ext/schemurai_native/src/memo.rs +68 -28
- data/ext/schemurai_native/src/memo_scope.rs +30 -0
- data/ext/schemurai_native/src/number/arbitrary.rs +57 -1
- data/ext/schemurai_native/src/number.rs +10 -10
- data/ext/schemurai_native/src/pointer.rs +19 -6
- data/ext/schemurai_native/src/runtime.rs +27 -0
- data/ext/schemurai_native/src/scope.rs +52 -5
- data/lib/schemurai/evaluation_limits.rb +65 -0
- data/lib/schemurai/limited_evaluator.rb +2 -59
- data/lib/schemurai/version.rb +1 -1
- data/lib/schemurai/vm/limited_evaluator.rb +2 -57
- metadata +3 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 967138a71d8f45234a75282fb65c665f58ca6611ee9a859ab4b69631e8342daa
|
|
4
|
+
data.tar.gz: eb53632ae8b019e44fe588ecf30e258879574810c2172edba60e34a6f56a106c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 941e91894ff671660b76e3800da82b58509994e594df8ab7f15f494ff321a7b45c001a5d35fbfc4611d2e8c844ebd5684b234e965fbfbf861c8d330ef1ccb048
|
|
7
|
+
data.tar.gz: 5657384c954dd8c665627a00fa4de17e0f691f7199a605de10984ec3318563249f986cc790020cc5849b99fbbe309d408525f121b3d70aa514dfc0e03201e208
|
data/README.md
CHANGED
|
@@ -288,9 +288,28 @@ scans use per-item evaluation so successful scans can poll.
|
|
|
288
288
|
`Schemurai.backend`, `SchemaRegistry#backend`, and `Validator#backend` expose
|
|
289
289
|
the selected backend. Pass `backend: :ruby` to force the Ruby oracle or
|
|
290
290
|
`backend: :vm` to ahead-of-time compile schemas for the validator virtual machine.
|
|
291
|
-
Pass `backend: :native` to use the experimental Rust VM extension.
|
|
292
|
-
|
|
293
|
-
|
|
291
|
+
Pass `backend: :native` to use the experimental Rust VM extension. Releases
|
|
292
|
+
include precompiled gems on RubyGems.org for CRuby 4.0 on these platforms:
|
|
293
|
+
|
|
294
|
+
| OS | CPU | libc | Gem platform |
|
|
295
|
+
| --- | --- | --- | --- |
|
|
296
|
+
| Linux | x64 | glibc 2.36+ | `x86_64-linux-gnu` |
|
|
297
|
+
| Linux | arm64 | glibc 2.36+ | `aarch64-linux-gnu` |
|
|
298
|
+
| Linux | x64 | musl (Alpine 3.23+) | `x86_64-linux-musl` |
|
|
299
|
+
| Linux | arm64 | musl (Alpine 3.23+) | `aarch64-linux-musl` |
|
|
300
|
+
| macOS 14+ | x64 | — | `x86_64-darwin` |
|
|
301
|
+
| macOS 14+ | arm64 | — | `arm64-darwin` |
|
|
302
|
+
|
|
303
|
+
RubyGems/Bundler selects the matching platform gem during normal installation;
|
|
304
|
+
these gems need no C compiler, Rust, or Ruby development headers. Each platform
|
|
305
|
+
gem bundles its supported Ruby minor ABIs and restricts `required_ruby_version`
|
|
306
|
+
to that range. Ruby patch releases share the same binary. To build from source,
|
|
307
|
+
use `gem install schemurai --platform ruby` or Bundler's `force_ruby_platform`.
|
|
308
|
+
See [native gem releases](docs/native-gem-releases.md) for the build matrix.
|
|
309
|
+
|
|
310
|
+
Building the native extension from source requires a C toolchain, Ruby
|
|
311
|
+
development headers, and Rust 1.93 or newer. If cargo is absent, installation of
|
|
312
|
+
the source gem skips the optional native
|
|
294
313
|
extension and Ruby/VM remain available; explicitly selecting `:native` then
|
|
295
314
|
raises `LoadError`. Native execution borrows Ruby values directly; its current coverage and
|
|
296
315
|
remaining VM delegation are documented below.
|
|
@@ -67,6 +67,16 @@ dependencies = [
|
|
|
67
67
|
"num-traits",
|
|
68
68
|
]
|
|
69
69
|
|
|
70
|
+
[[package]]
|
|
71
|
+
name = "num-bigint"
|
|
72
|
+
version = "0.5.1"
|
|
73
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
74
|
+
checksum = "93e7820bc0a80a0238e650327316f929ba18d5be054b647490a3a6a339f3e7c0"
|
|
75
|
+
dependencies = [
|
|
76
|
+
"num-integer",
|
|
77
|
+
"num-traits",
|
|
78
|
+
]
|
|
79
|
+
|
|
70
80
|
[[package]]
|
|
71
81
|
name = "num-integer"
|
|
72
82
|
version = "0.1.47"
|
|
@@ -82,7 +92,7 @@ version = "0.4.2"
|
|
|
82
92
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
83
93
|
checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824"
|
|
84
94
|
dependencies = [
|
|
85
|
-
"num-bigint",
|
|
95
|
+
"num-bigint 0.4.8",
|
|
86
96
|
"num-integer",
|
|
87
97
|
"num-traits",
|
|
88
98
|
]
|
|
@@ -127,13 +137,14 @@ checksum = "9774ba4a74de5f7b1c1451ed6cd5285a32eddb5cccb8cc655a4e50009e06477f"
|
|
|
127
137
|
|
|
128
138
|
[[package]]
|
|
129
139
|
name = "schemurai-native"
|
|
130
|
-
version = "3.
|
|
140
|
+
version = "3.1.1"
|
|
131
141
|
dependencies = [
|
|
132
142
|
"base64-simd",
|
|
133
143
|
"base64-turbo",
|
|
134
144
|
"memchr",
|
|
135
|
-
"num-bigint",
|
|
145
|
+
"num-bigint 0.5.1",
|
|
136
146
|
"num-rational",
|
|
147
|
+
"num-traits",
|
|
137
148
|
"regex-automata",
|
|
138
149
|
"regex-syntax",
|
|
139
150
|
"ryu",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[package]
|
|
2
2
|
name = "schemurai-native"
|
|
3
|
-
version = "3.
|
|
3
|
+
version = "3.1.1"
|
|
4
4
|
edition = "2024"
|
|
5
5
|
rust-version = "1.93"
|
|
6
6
|
publish = false
|
|
@@ -9,8 +9,9 @@ publish = false
|
|
|
9
9
|
base64-simd = "0.8.0"
|
|
10
10
|
base64-turbo = "0.5.0"
|
|
11
11
|
memchr = "2.8.3"
|
|
12
|
-
num-bigint = "0.
|
|
13
|
-
num-rational = "0.4.2"
|
|
12
|
+
num-bigint = "0.5.1"
|
|
13
|
+
num-rational = { version = "0.4.2", default-features = false, features = ["std"] }
|
|
14
|
+
num-traits = "0.2.19"
|
|
14
15
|
regex-automata = "0.4.18"
|
|
15
16
|
regex-syntax = "0.8.11"
|
|
16
17
|
ryu = "1.0.23"
|
|
@@ -5,10 +5,12 @@ fn main() {
|
|
|
5
5
|
let definitions = fs::read_to_string("protocol.def").expect("read native protocol");
|
|
6
6
|
let mut output = String::new();
|
|
7
7
|
for line in definitions.lines() {
|
|
8
|
-
let (fields,
|
|
9
|
-
(fields,
|
|
8
|
+
let (fields, prefix) = if let Some(fields) = line.strip_prefix("SN_OPCODE(") {
|
|
9
|
+
(fields, "OP_")
|
|
10
|
+
} else if let Some(fields) = line.strip_prefix("SN_FIELD(") {
|
|
11
|
+
(fields, "FIELD_")
|
|
10
12
|
} else if let Some(fields) = line.strip_prefix("SN_CONSTANT(") {
|
|
11
|
-
(fields,
|
|
13
|
+
(fields, "")
|
|
12
14
|
} else {
|
|
13
15
|
continue;
|
|
14
16
|
};
|
|
@@ -18,16 +20,15 @@ fn main() {
|
|
|
18
20
|
.split(',')
|
|
19
21
|
.map(str::trim)
|
|
20
22
|
.collect();
|
|
21
|
-
assert_eq!(fields.len(), if
|
|
23
|
+
assert_eq!(fields.len(), if prefix.is_empty() { 2 } else { 3 });
|
|
22
24
|
let name = fields[0];
|
|
23
25
|
assert!(
|
|
24
26
|
name.bytes()
|
|
25
27
|
.all(|c| c.is_ascii_uppercase() || c.is_ascii_digit() || c == b'_')
|
|
26
28
|
);
|
|
27
|
-
let value: i32 = fields[if
|
|
29
|
+
let value: i32 = fields[if prefix.is_empty() { 1 } else { 2 }]
|
|
28
30
|
.parse()
|
|
29
31
|
.expect("protocol integer value");
|
|
30
|
-
let prefix = if opcode { "OP_" } else { "" };
|
|
31
32
|
output.push_str(&format!("pub const {prefix}{name}: i32 = {value};\n"));
|
|
32
33
|
}
|
|
33
34
|
let directory = PathBuf::from(env::var_os("OUT_DIR").expect("Cargo output directory"));
|
|
@@ -2,6 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
require "mkmf"
|
|
4
4
|
|
|
5
|
+
# Platform gems resolve Ruby symbols from the running interpreter so the same
|
|
6
|
+
# binary also works with Rubies built without a shared libruby.
|
|
7
|
+
$LIBRUBYARG = "" unless enable_config("link-ruby", true) # rubocop:disable Style/GlobalVars -- mkmf build configuration.
|
|
8
|
+
|
|
5
9
|
unless find_executable("cargo")
|
|
6
10
|
warn "cargo was not found; skipping the optional native backend (Ruby and VM remain available)"
|
|
7
11
|
File.write("Makefile", dummy_makefile(".").join)
|
data/ext/schemurai_native/glue.c
CHANGED
|
@@ -344,13 +344,18 @@ static VALUE build_runner(VALUE self, size_t index) {
|
|
|
344
344
|
r->caches = NULL;
|
|
345
345
|
r->matches = NULL;
|
|
346
346
|
r->frame_indices = NULL;
|
|
347
|
-
r->frame_indices = st_init_numtable();
|
|
348
|
-
r->matches = st_init_numtable();
|
|
349
|
-
r->caches = sn_caches_new();
|
|
350
|
-
sn_caches_runtime(r->caches, r->matches);
|
|
351
347
|
RB_GC_GUARD(self);
|
|
352
348
|
return object;
|
|
353
349
|
}
|
|
350
|
+
/* Child factories and direct Boolean/type callbacks do not evaluate. Keep
|
|
351
|
+
* their observation state absent until an execution or invocation scope needs it. */
|
|
352
|
+
static void prepare_runner(runner_holder *r) {
|
|
353
|
+
if (r->caches) return;
|
|
354
|
+
if (!r->frame_indices) r->frame_indices = st_init_numtable();
|
|
355
|
+
if (!r->matches) r->matches = st_init_numtable();
|
|
356
|
+
r->caches = sn_caches_new();
|
|
357
|
+
sn_caches_runtime(r->caches, r->matches);
|
|
358
|
+
}
|
|
354
359
|
typedef struct { uint32_t kind; size_t attempted; } native_outcome;
|
|
355
360
|
extern int sn_valid_with_limits(const void *, const void *, size_t, VALUE, size_t, const native_limits *, native_outcome *);
|
|
356
361
|
static native_limits read_limits(VALUE budget) {
|
|
@@ -671,14 +676,23 @@ int sn_same(VALUE a, VALUE b) { return a == b; }
|
|
|
671
676
|
int sn_same_class(VALUE a, VALUE b) { return CLASS_OF(a) == CLASS_OF(b); }
|
|
672
677
|
int sn_string_equal(VALUE a, VALUE b) { return rb_str_cmp(a, b) == 0; }
|
|
673
678
|
|
|
674
|
-
struct
|
|
679
|
+
static struct { ID id; const char *symbol; } fields[] = {
|
|
680
|
+
#define SN_OPCODE(name, symbol, value)
|
|
681
|
+
#define SN_CONSTANT(name, value)
|
|
682
|
+
#define SN_FIELD(name, symbol, value) [SN_FIELD_##name] = {0, symbol},
|
|
683
|
+
#include "protocol.def"
|
|
684
|
+
#undef SN_OPCODE
|
|
685
|
+
#undef SN_CONSTANT
|
|
686
|
+
#undef SN_FIELD
|
|
687
|
+
};
|
|
688
|
+
struct access { VALUE object; ID name; };
|
|
675
689
|
static VALUE get_field(VALUE arg) {
|
|
676
690
|
struct access *a = (struct access *)arg;
|
|
677
|
-
return rb_funcall(a->object,
|
|
691
|
+
return rb_funcall(a->object, a->name, 0);
|
|
678
692
|
}
|
|
679
|
-
/* Rust supplies
|
|
680
|
-
int sn_field(VALUE object,
|
|
681
|
-
struct access a = {object, name};
|
|
693
|
+
/* Rust supplies an accessor index from the shared protocol. */
|
|
694
|
+
int sn_field(VALUE object, int name, VALUE *value) {
|
|
695
|
+
struct access a = {object, fields[name].id};
|
|
682
696
|
int state = 0;
|
|
683
697
|
*value = rb_protect(get_field, (VALUE)&a, &state);
|
|
684
698
|
if (state) { *value = rb_errinfo(); rb_set_errinfo(Qnil); }
|
|
@@ -687,9 +701,11 @@ int sn_field(VALUE object, const char *name, VALUE *value) {
|
|
|
687
701
|
static struct { ID id; const char *symbol; int opcode; } opcodes[] = {
|
|
688
702
|
#define SN_OPCODE(name, symbol, value) {0, #symbol, SN_OP_##name},
|
|
689
703
|
#define SN_CONSTANT(name, value)
|
|
704
|
+
#define SN_FIELD(name, symbol, value)
|
|
690
705
|
#include "protocol.def"
|
|
691
706
|
#undef SN_OPCODE
|
|
692
707
|
#undef SN_CONSTANT
|
|
708
|
+
#undef SN_FIELD
|
|
693
709
|
};
|
|
694
710
|
int sn_opcode(VALUE symbol) {
|
|
695
711
|
ID id = SYM2ID(symbol);
|
|
@@ -898,6 +914,7 @@ static VALUE callback_scope(evaluation_call *call) {
|
|
|
898
914
|
scope->active_budget = call->budget;
|
|
899
915
|
scope->active_limits = call->limits;
|
|
900
916
|
copy_states(scope, runner, 1, 0);
|
|
917
|
+
prepare_runner(scope);
|
|
901
918
|
sn_caches_child(scope->caches, runner->caches, !NIL_P(call->budget), 1);
|
|
902
919
|
runner->callback_scope = object;
|
|
903
920
|
}
|
|
@@ -1814,7 +1831,8 @@ static VALUE evaluate_runner_body(VALUE context) {
|
|
|
1814
1831
|
h->report = NULL;
|
|
1815
1832
|
call.report = h;
|
|
1816
1833
|
}
|
|
1817
|
-
|
|
1834
|
+
/* New runners are empty; finish_evaluation clears every completed or
|
|
1835
|
+
* interrupted execution before it makes the runner available again. */
|
|
1818
1836
|
r->active_budget = budget;
|
|
1819
1837
|
r->report_sink_done = 0;
|
|
1820
1838
|
if (!NIL_P(r->report_seed)) {
|
|
@@ -1949,6 +1967,7 @@ static VALUE finish_evaluation(VALUE context) {
|
|
|
1949
1967
|
static VALUE evaluate_runner(VALUE self, VALUE instance, VALUE budget, int diagnostics) {
|
|
1950
1968
|
runner_holder *runner;
|
|
1951
1969
|
TypedData_Get_Struct(self, runner_holder, &runner_type, runner);
|
|
1970
|
+
prepare_runner(runner);
|
|
1952
1971
|
if (runner->busy) {
|
|
1953
1972
|
VALUE nested = build_runner(runner->owner, runner->root);
|
|
1954
1973
|
runner_holder *child;
|
|
@@ -2168,15 +2187,19 @@ void Init_schemurai_native(void) {
|
|
|
2168
2187
|
binary_index = rb_ascii8bit_encindex();
|
|
2169
2188
|
for (size_t i = 0; i < sizeof(opcodes) / sizeof(opcodes[0]); i++)
|
|
2170
2189
|
opcodes[i].id = rb_intern_const(opcodes[i].symbol);
|
|
2190
|
+
for (size_t i = 0; i < sizeof(fields) / sizeof(fields[0]); i++)
|
|
2191
|
+
fields[i].id = rb_intern_const(fields[i].symbol);
|
|
2171
2192
|
rb_ext_ractor_safe(true);
|
|
2172
2193
|
VALUE schemurai = rb_define_module("Schemurai");
|
|
2173
2194
|
VALUE native = rb_define_module_under(schemurai, "Native");
|
|
2174
2195
|
VALUE protocol = rb_define_module_under(native, "Protocol");
|
|
2175
2196
|
#define SN_OPCODE(name, symbol, value) rb_define_const(protocol, "OP_" #name, INT2NUM(SN_OP_##name));
|
|
2176
2197
|
#define SN_CONSTANT(name, value) rb_define_const(protocol, #name, INT2NUM(SN_##name));
|
|
2198
|
+
#define SN_FIELD(name, symbol, value)
|
|
2177
2199
|
#include "protocol.def"
|
|
2178
2200
|
#undef SN_OPCODE
|
|
2179
2201
|
#undef SN_CONSTANT
|
|
2202
|
+
#undef SN_FIELD
|
|
2180
2203
|
VALUE klass = rb_define_class_under(native, "Program", rb_cObject);
|
|
2181
2204
|
rb_define_alloc_func(klass, allocate);
|
|
2182
2205
|
rb_define_method(klass, "initialize", initialize, 4);
|
|
@@ -106,3 +106,55 @@ SN_CONSTANT(ENCODING_SEVEN_BIT, 3)
|
|
|
106
106
|
SN_CONSTANT(CALLBACK_RUNTIME, 0)
|
|
107
107
|
SN_CONSTANT(CALLBACK_BOOLEAN, 1)
|
|
108
108
|
SN_CONSTANT(CALLBACK_TYPE, 2)
|
|
109
|
+
|
|
110
|
+
/* Fixed native constructor accessors; IDs are initialized before Ractor use. */
|
|
111
|
+
SN_FIELD(ADDITIONAL, "additional", 0)
|
|
112
|
+
SN_FIELD(CODE, "code", 1)
|
|
113
|
+
SN_FIELD(CONDITION, "condition", 2)
|
|
114
|
+
SN_FIELD(CONTAINS, "contains", 3)
|
|
115
|
+
SN_FIELD(COUNT_CONTAINS, "count_contains", 4)
|
|
116
|
+
SN_FIELD(DENOMINATOR, "denominator", 5)
|
|
117
|
+
SN_FIELD(DEPENDENCIES, "dependencies", 6)
|
|
118
|
+
SN_FIELD(DEPENDENT_REQUIRED, "dependent_required", 7)
|
|
119
|
+
SN_FIELD(DEPENDENT_SCHEMAS, "dependent_schemas", 8)
|
|
120
|
+
SN_FIELD(DYNAMIC_ANCHOR, "dynamic_anchor", 9)
|
|
121
|
+
SN_FIELD(ELSE_BRANCH, "else_branch", 10)
|
|
122
|
+
SN_FIELD(EXCLUSIVE_MAXIMUM, "exclusive_maximum", 11)
|
|
123
|
+
SN_FIELD(EXCLUSIVE_MINIMUM, "exclusive_minimum", 12)
|
|
124
|
+
SN_FIELD(FORMAT, "format", 13)
|
|
125
|
+
SN_FIELD(FORMAT_ASSERTION, "format_assertion", 14)
|
|
126
|
+
SN_FIELD(FRAGMENT, "fragment", 15)
|
|
127
|
+
SN_FIELD(ITEMS, "items", 16)
|
|
128
|
+
SN_FIELD(ITEMS_LIST, "items_list", 17)
|
|
129
|
+
SN_FIELD(MASK, "mask", 18)
|
|
130
|
+
SN_FIELD(MAX_CONTAINS, "max_contains", 19)
|
|
131
|
+
SN_FIELD(MAX_ITEMS, "max_items", 20)
|
|
132
|
+
SN_FIELD(MAX_LENGTH, "max_length", 21)
|
|
133
|
+
SN_FIELD(MAX_PROPERTIES, "max_properties", 22)
|
|
134
|
+
SN_FIELD(MAXIMUM, "maximum", 23)
|
|
135
|
+
SN_FIELD(MESSAGE, "message", 24)
|
|
136
|
+
SN_FIELD(MIN_CONTAINS, "min_contains", 25)
|
|
137
|
+
SN_FIELD(MIN_ITEMS, "min_items", 26)
|
|
138
|
+
SN_FIELD(MIN_LENGTH, "min_length", 27)
|
|
139
|
+
SN_FIELD(MIN_PROPERTIES, "min_properties", 28)
|
|
140
|
+
SN_FIELD(MINIMUM, "minimum", 29)
|
|
141
|
+
SN_FIELD(MULTIPLE_OF, "multiple_of", 30)
|
|
142
|
+
SN_FIELD(NAME, "name", 31)
|
|
143
|
+
SN_FIELD(NEGATIVE_P, "negative?", 32)
|
|
144
|
+
SN_FIELD(NODE, "node", 33)
|
|
145
|
+
SN_FIELD(NUMERATOR, "numerator", 34)
|
|
146
|
+
SN_FIELD(PARSE_JSON, "parse_json", 35)
|
|
147
|
+
SN_FIELD(PATTERN, "pattern", 36)
|
|
148
|
+
SN_FIELD(PATTERNS, "patterns", 37)
|
|
149
|
+
SN_FIELD(PREFIX_ITEMS, "prefix_items", 38)
|
|
150
|
+
SN_FIELD(PROPERTIES, "properties", 39)
|
|
151
|
+
SN_FIELD(PROPERTY_NAMES, "property_names", 40)
|
|
152
|
+
SN_FIELD(RECURSIVE_ANCHOR_P, "recursive_anchor?", 41)
|
|
153
|
+
SN_FIELD(REQUIRED, "required", 42)
|
|
154
|
+
SN_FIELD(RESOURCE, "resource", 43)
|
|
155
|
+
SN_FIELD(THEN_BRANCH, "then_branch", 44)
|
|
156
|
+
SN_FIELD(TO_F, "to_f", 45)
|
|
157
|
+
SN_FIELD(UNEVALUATED, "unevaluated", 46)
|
|
158
|
+
SN_FIELD(UNIQUE, "unique", 47)
|
|
159
|
+
SN_FIELD(VALUE, "value", 48)
|
|
160
|
+
SN_FIELD(DECODE_BASE64, "decode_base64", 49)
|
|
@@ -3,8 +3,10 @@
|
|
|
3
3
|
enum {
|
|
4
4
|
#define SN_OPCODE(name, symbol, value) SN_OP_##name = value,
|
|
5
5
|
#define SN_CONSTANT(name, value) SN_##name = value,
|
|
6
|
+
#define SN_FIELD(name, symbol, value) SN_FIELD_##name = value,
|
|
6
7
|
#include "protocol.def"
|
|
7
8
|
#undef SN_OPCODE
|
|
8
9
|
#undef SN_CONSTANT
|
|
10
|
+
#undef SN_FIELD
|
|
9
11
|
};
|
|
10
12
|
#endif
|
|
@@ -305,8 +305,8 @@ impl Engine {
|
|
|
305
305
|
pub(super) fn container_header(
|
|
306
306
|
&mut self,
|
|
307
307
|
source: Value,
|
|
308
|
-
maximum:
|
|
309
|
-
minimum:
|
|
308
|
+
maximum: i32,
|
|
309
|
+
minimum: i32,
|
|
310
310
|
object: bool,
|
|
311
311
|
tail: u8,
|
|
312
312
|
) -> Result<Header, Value> {
|
|
@@ -314,7 +314,7 @@ impl Engine {
|
|
|
314
314
|
let minimum = self.field(source, minimum)?;
|
|
315
315
|
let native_bounds = self.native_count(maximum)? && self.native_count(minimum)?;
|
|
316
316
|
let required = if object {
|
|
317
|
-
self.field(source,
|
|
317
|
+
self.field(source, FIELD_REQUIRED)?
|
|
318
318
|
} else {
|
|
319
319
|
unsafe { sn_nil() }
|
|
320
320
|
};
|
|
@@ -324,8 +324,8 @@ impl Engine {
|
|
|
324
324
|
let mut native_dependencies = true;
|
|
325
325
|
if object {
|
|
326
326
|
for (field, keyword) in [
|
|
327
|
-
(
|
|
328
|
-
(
|
|
327
|
+
(FIELD_DEPENDENCIES, "dependencies"),
|
|
328
|
+
(FIELD_DEPENDENT_REQUIRED, "dependentRequired"),
|
|
329
329
|
] {
|
|
330
330
|
let entries = self.field(source, field)?;
|
|
331
331
|
for pair in self.values(entries) {
|
|
@@ -375,7 +375,7 @@ impl Engine {
|
|
|
375
375
|
if unsafe { sn_is_float(value) } != 0 {
|
|
376
376
|
return Ok(unsafe { sn_float(value) } >= 0.0);
|
|
377
377
|
}
|
|
378
|
-
let negative = self.field(value,
|
|
378
|
+
let negative = self.field(value, FIELD_NEGATIVE_P)?;
|
|
379
379
|
Ok(unsafe { sn_true(negative) } == 0)
|
|
380
380
|
}
|
|
381
381
|
}
|
|
@@ -1,14 +1,71 @@
|
|
|
1
1
|
//! Content validation borrows Ruby string storage. Base64 decoding uses bounded
|
|
2
|
-
//!
|
|
2
|
+
//! scratch; native JSON scanning never constructs a tree. Other source
|
|
3
3
|
//! encodings request Ruby parsing after Rust evaluation returns.
|
|
4
4
|
use super::*;
|
|
5
5
|
use std::mem::MaybeUninit;
|
|
6
6
|
|
|
7
|
-
const STACK_BYTES: usize =
|
|
7
|
+
const STACK_BYTES: usize = 256;
|
|
8
8
|
// Full refills must end on a three-byte Base64 group boundary.
|
|
9
|
-
const STREAM_BYTES: usize =
|
|
9
|
+
const STREAM_BYTES: usize = 8190;
|
|
10
10
|
const DIRECT_BYTES: usize = 8192;
|
|
11
11
|
|
|
12
|
+
// Runner-local derived content storage. The decoder overwrites the requested
|
|
13
|
+
// prefix before parsing; previous decoded bytes are never used as input.
|
|
14
|
+
#[derive(Default)]
|
|
15
|
+
pub(super) struct Scratch(std::cell::RefCell<Vec<u8>>);
|
|
16
|
+
impl Scratch {
|
|
17
|
+
fn decode(&self, bytes: &[u8], length: usize) -> Verdict {
|
|
18
|
+
let mut scratch = self.0.borrow_mut();
|
|
19
|
+
if scratch.is_empty() {
|
|
20
|
+
scratch.resize(DIRECT_BYTES + 2, 0);
|
|
21
|
+
}
|
|
22
|
+
if length <= DIRECT_BYTES {
|
|
23
|
+
decode_json_turbo(bytes, length, &mut scratch)
|
|
24
|
+
} else {
|
|
25
|
+
let mut input = BufferedBase64 {
|
|
26
|
+
bytes,
|
|
27
|
+
decoded: &mut scratch,
|
|
28
|
+
at: 0,
|
|
29
|
+
len: 0,
|
|
30
|
+
remaining: length,
|
|
31
|
+
valid: true,
|
|
32
|
+
};
|
|
33
|
+
input.fill();
|
|
34
|
+
json(input)
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
pub(super) fn memory_usage(&self) -> usize {
|
|
38
|
+
self.0.try_borrow().map_or(0, |scratch| scratch.capacity())
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
#[cfg(test)]
|
|
43
|
+
mod scratch_tests {
|
|
44
|
+
use super::*;
|
|
45
|
+
|
|
46
|
+
#[test]
|
|
47
|
+
fn reused_decoding_ignores_old_bytes_and_reports_bounded_memory() {
|
|
48
|
+
let caches = runtime::Caches::default();
|
|
49
|
+
let before = unsafe { runtime::sn_caches_size(&caches) };
|
|
50
|
+
assert_eq!(caches.content.memory_usage(), 0);
|
|
51
|
+
for length in [8192, 385, 32768, 4096, 386, 8191, 387] {
|
|
52
|
+
let json = format!("\"{}\"", "x".repeat(length - 2));
|
|
53
|
+
let encoded = base64_simd::STANDARD.encode_to_string(&json);
|
|
54
|
+
assert_eq!(caches.content.decode(encoded.as_bytes(), length), Valid);
|
|
55
|
+
let mut malformed = encoded.into_bytes();
|
|
56
|
+
malformed[0] = b'!';
|
|
57
|
+
assert_eq!(caches.content.decode(&malformed, length), Invalid);
|
|
58
|
+
let bad_json = base64_simd::STANDARD.encode_to_string("x".repeat(length));
|
|
59
|
+
assert_eq!(caches.content.decode(bad_json.as_bytes(), length), Invalid);
|
|
60
|
+
assert_eq!(caches.content.memory_usage(), DIRECT_BYTES + 2);
|
|
61
|
+
}
|
|
62
|
+
assert_eq!(
|
|
63
|
+
unsafe { runtime::sn_caches_size(&caches) } - before,
|
|
64
|
+
DIRECT_BYTES + 2
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
12
69
|
#[derive(Debug)]
|
|
13
70
|
pub(super) struct Content {
|
|
14
71
|
pub(super) base64: bool,
|
|
@@ -156,10 +213,7 @@ impl Content {
|
|
|
156
213
|
if length <= STACK_BYTES {
|
|
157
214
|
return decode_json_medium(bytes, length);
|
|
158
215
|
}
|
|
159
|
-
|
|
160
|
-
return decode_json_direct(bytes, length);
|
|
161
|
-
}
|
|
162
|
-
decode_json_stream(bytes, length)
|
|
216
|
+
caches.content.decode(bytes, length)
|
|
163
217
|
} else {
|
|
164
218
|
if JSON {
|
|
165
219
|
let encoding = match (BORROWED, encoding) {
|
|
@@ -187,13 +241,6 @@ fn decode_json_medium(bytes: &[u8], length: usize) -> Verdict {
|
|
|
187
241
|
decode_json_turbo(bytes, length, &mut scratch)
|
|
188
242
|
}
|
|
189
243
|
|
|
190
|
-
// Keep the 8 KiB scratch frame out of shorter decoded-content paths.
|
|
191
|
-
#[inline(never)]
|
|
192
|
-
fn decode_json_direct(bytes: &[u8], length: usize) -> Verdict {
|
|
193
|
-
let mut scratch = [0; DIRECT_BYTES + 2];
|
|
194
|
-
decode_json_turbo(bytes, length, &mut scratch)
|
|
195
|
-
}
|
|
196
|
-
|
|
197
244
|
#[cfg(target_arch = "aarch64")]
|
|
198
245
|
fn decode_json_turbo(bytes: &[u8], length: usize, scratch: &mut [u8]) -> Verdict {
|
|
199
246
|
let capacity = base64_turbo::STANDARD.decoded_len_estimate(bytes.len());
|
|
@@ -216,22 +263,7 @@ fn decode_json_turbo(bytes: &[u8], length: usize, scratch: &mut [u8]) -> Verdict
|
|
|
216
263
|
}
|
|
217
264
|
}
|
|
218
265
|
|
|
219
|
-
//
|
|
220
|
-
#[inline(never)]
|
|
221
|
-
fn decode_json_stream(bytes: &[u8], length: usize) -> Verdict {
|
|
222
|
-
let mut input = BufferedBase64 {
|
|
223
|
-
bytes,
|
|
224
|
-
decoded: [0; STREAM_BYTES + 2],
|
|
225
|
-
at: 0,
|
|
226
|
-
len: 0,
|
|
227
|
-
remaining: length,
|
|
228
|
-
valid: true,
|
|
229
|
-
};
|
|
230
|
-
input.fill();
|
|
231
|
-
json(input)
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
// Decode borrowed bytes into the caller's fixed stack scratch. The STANDARD
|
|
266
|
+
// Decode borrowed bytes into the caller's bounded scratch. The STANDARD
|
|
235
267
|
// variant also checks each chunk's alphabet, padding and unused trailing bits.
|
|
236
268
|
#[cfg(target_arch = "aarch64")]
|
|
237
269
|
fn decode_groups(bytes: &[u8], output: &mut [u8], expected: usize) -> bool {
|
|
@@ -398,7 +430,7 @@ unsafe fn string_run_avx2(bytes: &[u8]) -> (usize, bool) {
|
|
|
398
430
|
}
|
|
399
431
|
struct BufferedBase64<'a> {
|
|
400
432
|
bytes: &'a [u8],
|
|
401
|
-
decoded: [u8
|
|
433
|
+
decoded: &'a mut [u8],
|
|
402
434
|
at: usize,
|
|
403
435
|
len: usize,
|
|
404
436
|
remaining: usize,
|