2023-01-15 14:52:37 +03:00
|
|
|
|
//! This module implements `builtins.toXML`, which is a serialisation
|
|
|
|
|
//! of value information as well as internal tvix state that several
|
|
|
|
|
//! things in nixpkgs rely on.
|
|
|
|
|
|
fix(tvix): Represent strings as byte arrays
C++ nix uses C-style zero-terminated char pointers to represent strings
internally - however, up to this point, tvix has used Rust `String` and
`str` for string values. Since those are required to be valid utf-8, we
haven't been able to properly represent all the string values that Nix
supports.
To fix that, this change converts the internal representation of the
NixString struct from `Box<str>` to `BString`, from the `bstr` crate -
this is a wrapper around a `Vec<u8>` with extra functions for treating
that byte vector as a "morally string-like" value, which is basically
exactly what we need.
Since this changes a pretty fundamental assumption about a pretty core
type, there are a *lot* of changes in a lot of places to make this work,
but I've tried to keep the general philosophy and intent of most of the
code in most places intact. Most notably, there's nothing that's been
done to make the derivation stuff in //tvix/glue work with non-utf8
strings everywhere, instead opting to just convert to String/str when
passing things into that - there *might* be something to be done there,
but I don't know what the rules should be and I don't want to figure
them out in this change.
To deal with OS-native paths in a way that also works in WASM for
tvixbolt, this also adds a dependency on the "os_str_bytes" crate.
Fixes: b/189
Fixes: b/337
Change-Id: I5e6eb29c62f47dd91af954f5e12bfc3d186f5526
Reviewed-on: https://cl.tvl.fyi/c/depot/+/10200
Reviewed-by: tazjin <tazjin@tvl.su>
Reviewed-by: flokli <flokli@flokli.de>
Reviewed-by: sterni <sternenseemann@systemli.org>
Autosubmit: aspen <root@gws.fyi>
Tested-by: BuildkiteCI
2023-12-05 17:25:52 -05:00
|
|
|
|
use bstr::ByteSlice;
|
2024-05-20 15:50:33 +02:00
|
|
|
|
use std::borrow::Cow;
|
2023-01-15 14:52:37 +03:00
|
|
|
|
use std::{io::Write, rc::Rc};
|
|
|
|
|
|
2024-05-23 11:06:30 +02:00
|
|
|
|
use crate::{ErrorKind, NixContext, NixContextElement, Value};
|
2023-01-15 14:52:37 +03:00
|
|
|
|
|
|
|
|
|
/// Recursively serialise a value to XML. The value *must* have been
|
|
|
|
|
/// deep-forced before being passed to this function.
|
2024-05-23 11:06:30 +02:00
|
|
|
|
/// On success, returns the NixContext.
|
|
|
|
|
pub fn value_to_xml<W: Write>(mut writer: W, value: &Value) -> Result<NixContext, ErrorKind> {
|
2023-01-15 14:52:37 +03:00
|
|
|
|
// Write a literal document declaration, using C++-Nix-style
|
|
|
|
|
// single quotes.
|
2023-02-02 20:07:25 +05:30
|
|
|
|
writeln!(writer, "<?xml version='1.0' encoding='utf-8'?>")?;
|
2023-01-15 14:52:37 +03:00
|
|
|
|
|
2024-05-20 15:50:33 +02:00
|
|
|
|
let mut emitter = XmlEmitter::new(writer);
|
2023-01-15 14:52:37 +03:00
|
|
|
|
|
2024-05-20 15:50:33 +02:00
|
|
|
|
emitter.write_open_tag("expr", &[])?;
|
|
|
|
|
value_variant_to_xml(&mut emitter, value)?;
|
|
|
|
|
emitter.write_closing_tag("expr")?;
|
2023-01-15 14:52:37 +03:00
|
|
|
|
|
2024-05-23 11:06:30 +02:00
|
|
|
|
Ok(emitter.into_context())
|
2023-01-15 14:52:37 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn write_typed_value<W: Write, V: ToString>(
|
2024-05-20 15:50:33 +02:00
|
|
|
|
w: &mut XmlEmitter<W>,
|
|
|
|
|
name_unescaped: &str,
|
2023-01-15 14:52:37 +03:00
|
|
|
|
value: V,
|
|
|
|
|
) -> Result<(), ErrorKind> {
|
2024-05-20 15:50:33 +02:00
|
|
|
|
w.write_self_closing_tag(name_unescaped, &[("value", &value.to_string())])?;
|
2023-01-15 14:52:37 +03:00
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-20 15:50:33 +02:00
|
|
|
|
fn value_variant_to_xml<W: Write>(w: &mut XmlEmitter<W>, value: &Value) -> Result<(), ErrorKind> {
|
2023-01-15 14:52:37 +03:00
|
|
|
|
match value {
|
|
|
|
|
Value::Thunk(t) => return value_variant_to_xml(w, &t.value()),
|
|
|
|
|
|
|
|
|
|
Value::Null => {
|
2024-05-20 15:50:33 +02:00
|
|
|
|
w.write_open_tag("null", &[])?;
|
|
|
|
|
w.write_closing_tag("null")?;
|
2023-01-15 14:52:37 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Value::Bool(b) => return write_typed_value(w, "bool", b),
|
|
|
|
|
Value::Integer(i) => return write_typed_value(w, "int", i),
|
|
|
|
|
Value::Float(f) => return write_typed_value(w, "float", f),
|
2024-05-23 11:06:30 +02:00
|
|
|
|
Value::String(s) => {
|
|
|
|
|
if let Some(context) = s.context() {
|
|
|
|
|
w.extend_context(context.iter().cloned());
|
|
|
|
|
}
|
|
|
|
|
return write_typed_value(w, "string", s.to_str()?);
|
|
|
|
|
}
|
2023-01-15 14:52:37 +03:00
|
|
|
|
Value::Path(p) => return write_typed_value(w, "path", p.to_string_lossy()),
|
|
|
|
|
|
|
|
|
|
Value::List(list) => {
|
2024-05-20 15:50:33 +02:00
|
|
|
|
w.write_open_tag("list", &[])?;
|
2023-01-15 14:52:37 +03:00
|
|
|
|
|
|
|
|
|
for elem in list.into_iter() {
|
2023-02-26 18:22:08 +03:00
|
|
|
|
value_variant_to_xml(w, elem)?;
|
2023-01-15 14:52:37 +03:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-20 15:50:33 +02:00
|
|
|
|
w.write_closing_tag("list")?;
|
2023-01-15 14:52:37 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Value::Attrs(attrs) => {
|
2024-05-20 15:50:33 +02:00
|
|
|
|
w.write_open_tag("attrs", &[])?;
|
2023-01-15 14:52:37 +03:00
|
|
|
|
|
|
|
|
|
for elem in attrs.iter() {
|
2024-05-20 15:50:33 +02:00
|
|
|
|
w.write_open_tag("attr", &[("name", &elem.0.to_str_lossy())])?;
|
2023-02-26 18:22:08 +03:00
|
|
|
|
value_variant_to_xml(w, elem.1)?;
|
2024-05-20 15:50:33 +02:00
|
|
|
|
w.write_closing_tag("attr")?;
|
2023-01-15 14:52:37 +03:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-20 15:50:33 +02:00
|
|
|
|
w.write_closing_tag("attrs")?;
|
2023-01-15 14:52:37 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Value::Closure(c) => {
|
2024-05-20 15:50:33 +02:00
|
|
|
|
w.write_open_tag("function", &[])?;
|
2023-01-15 14:52:37 +03:00
|
|
|
|
|
|
|
|
|
match &c.lambda.formals {
|
|
|
|
|
Some(formals) => {
|
2024-05-20 15:50:33 +02:00
|
|
|
|
let mut attrs: Vec<(&str, &str)> = Vec::with_capacity(2);
|
2023-01-15 14:52:37 +03:00
|
|
|
|
if formals.ellipsis {
|
2024-05-20 15:50:33 +02:00
|
|
|
|
attrs.push(("ellipsis", "1"));
|
2024-01-23 15:34:38 +02:00
|
|
|
|
}
|
|
|
|
|
if let Some(ref name) = &formals.name {
|
2024-05-20 15:50:33 +02:00
|
|
|
|
attrs.push(("name", name.as_str()));
|
2023-01-15 14:52:37 +03:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-20 15:50:33 +02:00
|
|
|
|
w.write_open_tag("attrspat", &attrs)?;
|
2023-01-15 14:52:37 +03:00
|
|
|
|
for arg in formals.arguments.iter() {
|
2024-05-20 15:50:33 +02:00
|
|
|
|
w.write_self_closing_tag("attr", &[("name", &arg.0.to_str_lossy())])?;
|
2023-01-15 14:52:37 +03:00
|
|
|
|
}
|
2024-01-23 15:34:38 +02:00
|
|
|
|
|
2024-05-20 15:50:33 +02:00
|
|
|
|
w.write_closing_tag("attrspat")?;
|
2023-01-15 14:52:37 +03:00
|
|
|
|
}
|
2023-01-31 11:21:35 +03:00
|
|
|
|
None => {
|
|
|
|
|
// TODO(tazjin): tvix does not currently persist function
|
|
|
|
|
// argument names anywhere (whereas we do for formals, as
|
|
|
|
|
// that is required for other runtime behaviour). Because of
|
|
|
|
|
// this the implementation here is fake, always returning
|
|
|
|
|
// the same argument name.
|
|
|
|
|
//
|
|
|
|
|
// If we don't want to persist the data, we can re-parse the
|
|
|
|
|
// AST from the spans of the lambda's bytecode and figure it
|
|
|
|
|
// out that way, but it needs some investigating.
|
2024-05-20 15:50:33 +02:00
|
|
|
|
w.write_self_closing_tag("varpat", &[("name", /* fake: */ "x")])?;
|
2023-01-31 11:21:35 +03:00
|
|
|
|
}
|
2023-01-15 14:52:37 +03:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-20 15:50:33 +02:00
|
|
|
|
w.write_closing_tag("function")?;
|
2023-01-15 14:52:37 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Value::Builtin(_) => {
|
2024-05-20 15:50:33 +02:00
|
|
|
|
w.write_open_tag("unevaluated", &[])?;
|
|
|
|
|
w.write_closing_tag("unevaluated")?;
|
2023-01-15 14:52:37 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Value::AttrNotFound
|
|
|
|
|
| Value::Blueprint(_)
|
|
|
|
|
| Value::DeferredUpvalue(_)
|
2023-03-04 02:12:06 +03:00
|
|
|
|
| Value::UnresolvedPath(_)
|
2024-03-25 03:36:32 +01:00
|
|
|
|
| Value::Json(..)
|
fix(tvix/eval): only finalise formal arguments if defaulting
When dealing with a formal argument in a function argument pattern that
has a default expression, there are two different things that can happen
at runtime: Either we select its value from the passed attribute
successfully or we need to use the default expression. Both of these may
be thunks and both of these may need finalisers. However, in the former
case this is taken care of elsewhere, the value will always be finalised
already if necessary. In the latter case we may need to finalise the
thunk resulting from the default expression. However, the thunk
corresponding to the expression may never end up in the local's stack
slot. Since finalisation goes by stack slot (and not constants), we need
to prevent a case where we don't fall back to the default expression,
but finalise anyways.
Previously, we worked around this by making `OpFinalise` ignore
non-thunks. Since finalisation of already evaluated thunks still
crashed, the faulty compilation of function pattern arguments could
still cause a crash.
As a new approach, we reinstate the old behavior of `OpFinalise` to
crash whenever encountering something that is either not a thunk or
doesn't need finalisation. This can also help catching (similar)
miscompilations in the future. To then prevent the crash, we need to
track whether we have fallen back or not at runtime. This is done using
an additional phantom on the stack that holds a new `FinaliseRequest`
value. When it comes to finalisation we check this value and
conditionally execute `OpFinalise` based on its value.
Resolves b/261 and b/265 (partially).
Change-Id: Ic04fb80ec671a2ba11fa645090769c335fb7f58b
Reviewed-on: https://cl.tvl.fyi/c/depot/+/8705
Reviewed-by: tazjin <tazjin@tvl.su>
Tested-by: BuildkiteCI
Autosubmit: sterni <sternenseemann@systemli.org>
2023-06-03 02:10:31 +02:00
|
|
|
|
| Value::FinaliseRequest(_) => {
|
2023-01-15 14:52:37 +03:00
|
|
|
|
return Err(ErrorKind::TvixBug {
|
|
|
|
|
msg: "internal value variant encountered in builtins.toXML",
|
|
|
|
|
metadata: Some(Rc::new(value.clone())),
|
|
|
|
|
})
|
|
|
|
|
}
|
2023-09-09 22:02:56 -07:00
|
|
|
|
|
|
|
|
|
Value::Catchable(_) => {
|
|
|
|
|
panic!("tvix bug: value_to_xml() called on a value which had not been deep-forced")
|
|
|
|
|
}
|
2024-05-20 15:50:33 +02:00
|
|
|
|
};
|
2023-01-15 14:52:37 +03:00
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2024-05-20 15:50:33 +02:00
|
|
|
|
|
|
|
|
|
/// A simple-stupid XML emitter, which implements only the subset needed for byte-by-byte compat with C++ nix’ `builtins.toXML`.
|
|
|
|
|
struct XmlEmitter<W> {
|
|
|
|
|
/// The current indentation
|
|
|
|
|
cur_indent: usize,
|
|
|
|
|
writer: W,
|
2024-05-23 11:06:30 +02:00
|
|
|
|
context: NixContext,
|
2024-05-20 15:50:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<W: Write> XmlEmitter<W> {
|
|
|
|
|
pub fn new(writer: W) -> Self {
|
|
|
|
|
XmlEmitter {
|
|
|
|
|
cur_indent: 0,
|
|
|
|
|
writer,
|
2024-05-23 11:06:30 +02:00
|
|
|
|
context: Default::default(),
|
2024-05-20 15:50:33 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Write an open tag with the given name (which is not escaped!)
|
|
|
|
|
/// and attributes (Keys are not escaped! Only attribute values are.)
|
|
|
|
|
pub fn write_open_tag(
|
|
|
|
|
&mut self,
|
|
|
|
|
name_unescaped: &str,
|
|
|
|
|
attrs: &[(&str, &str)],
|
|
|
|
|
) -> std::io::Result<()> {
|
|
|
|
|
self.add_indent()?;
|
|
|
|
|
self.writer.write_all(b"<")?;
|
|
|
|
|
self.writer.write_all(name_unescaped.as_bytes())?;
|
|
|
|
|
self.write_attrs_escape_vals(attrs)?;
|
|
|
|
|
self.writer.write_all(b">\n")?;
|
|
|
|
|
self.cur_indent += 2;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Write a self-closing open tag with the given name (which is not escaped!)
|
|
|
|
|
/// and attributes (Keys are not escaped! Only attribute values are.)
|
|
|
|
|
pub fn write_self_closing_tag(
|
|
|
|
|
&mut self,
|
|
|
|
|
name_unescaped: &str,
|
|
|
|
|
attrs: &[(&str, &str)],
|
|
|
|
|
) -> std::io::Result<()> {
|
|
|
|
|
self.add_indent()?;
|
|
|
|
|
self.writer.write_all(b"<")?;
|
|
|
|
|
self.writer.write_all(name_unescaped.as_bytes())?;
|
|
|
|
|
self.write_attrs_escape_vals(attrs)?;
|
|
|
|
|
self.writer.write_all(b" />\n")?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Write a closing tag with the given name (which is not escaped!)
|
|
|
|
|
pub fn write_closing_tag(&mut self, name_unescaped: &str) -> std::io::Result<()> {
|
|
|
|
|
self.cur_indent -= 2;
|
|
|
|
|
self.add_indent()?;
|
|
|
|
|
self.writer.write_all(b"</")?;
|
|
|
|
|
self.writer.write_all(name_unescaped.as_bytes())?;
|
|
|
|
|
self.writer.write_all(b">\n")?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn add_indent(&mut self) -> std::io::Result<()> {
|
|
|
|
|
self.writer.write_all(&b" ".repeat(self.cur_indent))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Write an attribute list
|
|
|
|
|
fn write_attrs_escape_vals(&mut self, attrs: &[(&str, &str)]) -> std::io::Result<()> {
|
|
|
|
|
for (name, val) in attrs {
|
|
|
|
|
self.writer.write_all(b" ")?;
|
|
|
|
|
self.writer.write_all(name.as_bytes())?;
|
|
|
|
|
self.writer.write_all(br#"=""#)?;
|
|
|
|
|
self.writer
|
|
|
|
|
.write_all(Self::escape_attr_value(val).as_bytes())?;
|
|
|
|
|
self.writer.write_all(b"\"")?;
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Escape the given attribute value, making sure we only actually clone the string if we needed to replace something.
|
|
|
|
|
fn escape_attr_value(s: &str) -> Cow<str> {
|
|
|
|
|
let mut last_escape: usize = 0;
|
|
|
|
|
let mut res: Cow<str> = Cow::Borrowed("");
|
|
|
|
|
// iterating via char_indices gives us the ability to index the original string slice at character boundaries
|
|
|
|
|
for (idx, c) in s.char_indices() {
|
|
|
|
|
match Self::should_escape_char(c) {
|
|
|
|
|
None => {}
|
|
|
|
|
Some(new) => {
|
|
|
|
|
// add characters since the last escape we did
|
|
|
|
|
res += &s[last_escape..idx];
|
|
|
|
|
// add the escaped value
|
|
|
|
|
res += new;
|
|
|
|
|
last_escape = idx + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// we did not need to escape anything, so borrow original string
|
|
|
|
|
if last_escape == 0 {
|
|
|
|
|
Cow::Borrowed(s)
|
|
|
|
|
} else {
|
|
|
|
|
// add the remaining characters
|
|
|
|
|
res += &s[last_escape..];
|
|
|
|
|
res
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn should_escape_char(c: char) -> Option<&'static str> {
|
|
|
|
|
match c {
|
|
|
|
|
'<' => Some("<"),
|
|
|
|
|
'>' => Some(">"),
|
|
|
|
|
'"' => Some("""),
|
|
|
|
|
'\'' => Some("'"),
|
|
|
|
|
'&' => Some("&"),
|
|
|
|
|
'\n' => Some("
"),
|
|
|
|
|
'\r' => Some("
"),
|
|
|
|
|
_ => None,
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-23 11:06:30 +02:00
|
|
|
|
|
|
|
|
|
/// Extends the existing context with more context elements.
|
|
|
|
|
fn extend_context<T>(&mut self, iter: T)
|
|
|
|
|
where
|
|
|
|
|
T: IntoIterator<Item = NixContextElement>,
|
|
|
|
|
{
|
|
|
|
|
self.context.extend(iter)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Consumes [Self] and returns the [NixContext] collected.
|
|
|
|
|
fn into_context(self) -> NixContext {
|
|
|
|
|
self.context
|
|
|
|
|
}
|
2024-05-20 15:50:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use bytes::buf::Writer;
|
|
|
|
|
use pretty_assertions::assert_eq;
|
|
|
|
|
|
|
|
|
|
use crate::builtins::to_xml::XmlEmitter;
|
|
|
|
|
use std::borrow::Cow;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn xml_gen() {
|
|
|
|
|
let mut buf = Vec::new();
|
|
|
|
|
let mut x = XmlEmitter::new(&mut buf);
|
|
|
|
|
x.write_open_tag("hello", &[("hi", "it’s me"), ("no", "<escape>")])
|
|
|
|
|
.unwrap();
|
|
|
|
|
x.write_self_closing_tag("self-closing", &[("tag", "yay")])
|
|
|
|
|
.unwrap();
|
|
|
|
|
x.write_closing_tag("hello").unwrap();
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
std::str::from_utf8(&buf).unwrap(),
|
|
|
|
|
r##"<hello hi="it’s me" no="<escape>">
|
|
|
|
|
<self-closing tag="yay" />
|
|
|
|
|
</hello>
|
|
|
|
|
"##
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn xml_escape() {
|
|
|
|
|
match XmlEmitter::<Writer<Vec<u8>>>::escape_attr_value("ab<>c&de") {
|
|
|
|
|
Cow::Owned(s) => assert_eq!(s, "ab<>c&de".to_string(), "escape stuff"),
|
|
|
|
|
Cow::Borrowed(s) => panic!("s should be owned {}", s),
|
|
|
|
|
}
|
|
|
|
|
match XmlEmitter::<Writer<Vec<u8>>>::escape_attr_value("") {
|
|
|
|
|
Cow::Borrowed(s) => assert_eq!(s, "", "empty escape is borrowed"),
|
|
|
|
|
Cow::Owned(s) => panic!("s should be borrowed {}", s),
|
|
|
|
|
}
|
|
|
|
|
match XmlEmitter::<Writer<Vec<u8>>>::escape_attr_value("hi!ŷbla") {
|
|
|
|
|
Cow::Borrowed(s) => assert_eq!(s, "hi!ŷbla", "no escape is borrowed"),
|
|
|
|
|
Cow::Owned(s) => panic!("s should be borrowed {}", s),
|
|
|
|
|
}
|
|
|
|
|
match XmlEmitter::<Writer<Vec<u8>>>::escape_attr_value("hi!<ŷ>bla") {
|
|
|
|
|
Cow::Owned(s) => assert_eq!(
|
|
|
|
|
s,
|
|
|
|
|
"hi!<ŷ>bla".to_string(),
|
|
|
|
|
"multi-byte chars are correctly used"
|
|
|
|
|
),
|
|
|
|
|
Cow::Borrowed(s) => panic!("s should be owned {}", s),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|