Skip to content

Commit

Permalink
fix: unsound schema runtime check.
Browse files Browse the repository at this point in the history
  • Loading branch information
Peefy committed Jun 25, 2023
1 parent ddf9a4f commit 7a1367d
Show file tree
Hide file tree
Showing 13 changed files with 143 additions and 5 deletions.
8 changes: 4 additions & 4 deletions kclvm/runtime/src/value/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1823,20 +1823,20 @@ pub unsafe extern "C" fn kclvm_value_union(
let mut entry = b.dict_get_entry(k).unwrap().deep_copy();
entry.dict_update_key_value(k, v);
result = a
.union_entry(&entry, true, false, false, false)
.union_entry(&entry, true, false, false, true)
.clone()
.into_raw();
} else {
let entry = b.dict_get_entry(k).unwrap();
result = a
.union_entry(&entry, true, false, false, false)
.union_entry(&entry, true, false, false, true)
.clone()
.into_raw();
}
}
result
} else {
a.union_entry(b, true, false, false, false).into_raw()
a.union_entry(b, true, false, false, true).into_raw()
}
}

Expand Down Expand Up @@ -2186,7 +2186,7 @@ pub unsafe extern "C" fn kclvm_schema_value_check(
schema_value.dict_update_entry(key.as_str(), &value.clone(), op, &-1);
} else if !should_add_attr && is_not_in_schema {
let schema_name = c2str(schema_name);
panic!("{key}: No such member in the schema '{schema_name}'");
panic!("No attribute named '{key}' in the schema '{schema_name}'");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion kclvm/runtime/src/value/val_dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ impl ValueRef {
true,
false,
should_idempotent_check,
false,
true,
);
} else {
panic!("invalid dict insert value: {}", self.type_str())
Expand Down
2 changes: 2 additions & 0 deletions kclvm/runtime/src/value/val_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ pub fn resolve_schema(schema: &ValueRef, keys: &[String]) -> ValueRef {
ptr_as_ref(value)
};
ctx.panic_info = now_meta_info;
value.schema_check_attr_optional(true);
return value.clone();
}
ctx.panic_info = now_meta_info;
Expand Down Expand Up @@ -337,6 +338,7 @@ pub fn convert_collection_value(value: &ValueRef, tpe: &str) -> ValueRef {
ptr_as_ref(value)
};
ctx.panic_info = now_meta_info;
value.schema_check_attr_optional(true);
return value.clone();
}
ctx.panic_info = now_meta_info;
Expand Down
4 changes: 4 additions & 0 deletions test/grammar/schema/optional_attr/fail_4/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
schema Values:
a: int

values: Values = {}
18 changes: 18 additions & 0 deletions test/grammar/schema/optional_attr/fail_4/stderr.golden.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import sys
import os

import kclvm.kcl.error as kcl_error

cwd = os.path.dirname(os.path.realpath(__file__))

kcl_error.print_kcl_error_message(
kcl_error.get_exception(err_type=kcl_error.ErrType.EvaluationError_TYPE,
file_msgs=[
kcl_error.ErrFileMsg(
filename=os.path.join(cwd, "main.k"),
line_no=4,
),
],
arg_msg="attribute 'a' of Version is required and can't be None or Undefined")
, file=sys.stdout
)
4 changes: 4 additions & 0 deletions test/grammar/schema/optional_attr/fail_5/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
schema Values:
a: int

values: Values = Values {a = 1} | {a = Undefined}
18 changes: 18 additions & 0 deletions test/grammar/schema/optional_attr/fail_5/stderr.golden.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import sys
import os

import kclvm.kcl.error as kcl_error

cwd = os.path.dirname(os.path.realpath(__file__))

kcl_error.print_kcl_error_message(
kcl_error.get_exception(err_type=kcl_error.ErrType.EvaluationError_TYPE,
file_msgs=[
kcl_error.ErrFileMsg(
filename=os.path.join(cwd, "main.k"),
line_no=4,
),
],
arg_msg="attribute 'a' of Version is required and can't be None or Undefined")
, file=sys.stdout
)
4 changes: 4 additions & 0 deletions test/grammar/schema/optional_attr/fail_6/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
schema Values:
a: int

values: Values = Values {a = 1} | {a = 2} | {a = None}
18 changes: 18 additions & 0 deletions test/grammar/schema/optional_attr/fail_6/stderr.golden.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import sys
import os

import kclvm.kcl.error as kcl_error

cwd = os.path.dirname(os.path.realpath(__file__))

kcl_error.print_kcl_error_message(
kcl_error.get_exception(err_type=kcl_error.ErrType.EvaluationError_TYPE,
file_msgs=[
kcl_error.ErrFileMsg(
filename=os.path.join(cwd, "main.k"),
line_no=4,
),
],
arg_msg="attribute 'a' of Version is required and can't be None or Undefined")
, file=sys.stdout
)
17 changes: 17 additions & 0 deletions test/grammar/schema/relaxed/fail_0/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
values = {
res.cpu = 2
res.no_such_attr = 2
}

schema Config:
res: Res

schema Res:
cpu: int

c: Config {
res: values.res
res: Res {
cpu = 1
}
}
18 changes: 18 additions & 0 deletions test/grammar/schema/relaxed/fail_0/stderr.golden.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import sys
import os

import kclvm.kcl.error as kcl_error

cwd = os.path.dirname(os.path.realpath(__file__))

kcl_error.print_kcl_error_message(
kcl_error.get_exception(err_type=kcl_error.ErrType.EvaluationError_TYPE,
file_msgs=[
kcl_error.ErrFileMsg(
filename=os.path.join(cwd, "main.k"),
line_no=10,
),
],
arg_msg="No attribute named 'no_such_attr' in the schema 'Res'")
, file=sys.stdout
)
17 changes: 17 additions & 0 deletions test/grammar/schema/relaxed/fail_1/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
values = {
res.cpu = 2
res.no_such_attr = 2
}

schema Config:
res: Res

schema Res:
cpu: int

c: Config {
res: Res {
cpu = 1
}
res: values.res
}
18 changes: 18 additions & 0 deletions test/grammar/schema/relaxed/fail_1/stderr.golden.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import sys
import os

import kclvm.kcl.error as kcl_error

cwd = os.path.dirname(os.path.realpath(__file__))

kcl_error.print_kcl_error_message(
kcl_error.get_exception(err_type=kcl_error.ErrType.EvaluationError_TYPE,
file_msgs=[
kcl_error.ErrFileMsg(
filename=os.path.join(cwd, "main.k"),
line_no=10,
),
],
arg_msg="No attribute named 'no_such_attr' in the schema 'Res'")
, file=sys.stdout
)

0 comments on commit 7a1367d

Please sign in to comment.