compiler: Remove package metadata and lockfiles
47cdde9e0fc33d39d17b6813f73e9ff512a3026aee3230f6046b36fea21f7b67
Each package artifact carried a typed `package.metadata` descriptor, and each exported graph a `package.lock`. Nearly every descriptor field was already in the image or could be recomputed: the name, dependencies and pins, and metadata identity are in the image header; module, file, and image hashes are digests of the stored bytes; the request key is the cache directory name; and the compiler string was never checked. The lockfile repeated each image's metadata identity, and safety rests on dependency pins, not on exact image selection. Reading these documents required an embedded schema and a typed value reader. Store each package as `name.ril.bin`, `name.interface/`, and an optional `name.native/`. The interface tree holds the `.rid` modules and the embedded files they read, and a directory listing finds them. The native tree holds the assembly files `0.ras` and so on, `startup.ras`, and `locations` at fixed names. Explicit artifacts recompute the metadata identity from the image's exports and the interface files, hashed in path order; cache entries trust the image's identity. The image now lists every declared dependency, not only those it imports from, because the interface of a package such as a re-export facade depends on providers whose code it does not call. Remove the descriptor writer and reader, the schema, the value reader and its tests, and the lockfile. Assembly origins are no longer stored, so renaming an assembly source file reuses its cache entry. Assisted-by: Claude:opus-5.5 Change-Id: Ie126faeb85885919bef015c2aadb929c0f61c03b
1 parent
831c8d45
Makefile
+3 -8
| 27 | 27 | ||
| 28 | 28 | SEED := seed/radiance.rv64 |
|
| 29 | 29 | COMPILER_SRC := compiler/radiance.rad compiler/radiance/codegen.rad \ |
|
| 30 | 30 | compiler/radiance/testgen.rad compiler/radiance/link.rad \ |
|
| 31 | 31 | compiler/radiance/artifact.rad compiler/radiance/metadata.rad \ |
|
| 32 | - | compiler/radiance/build.rad compiler/radiance/cache.rad compiler/radiance/locations.rad \ |
|
| 33 | - | compiler/radiance/artifact/value.rad |
|
| 34 | - | # Files that the compiler reads with `@embed`. |
|
| 35 | - | COMPILER_EMBED := compiler/radiance/artifact/schema.rad |
|
| 32 | + | compiler/radiance/build.rad compiler/radiance/cache.rad compiler/radiance/locations.rad |
|
| 36 | 33 | COMPILER_MANIFEST := $(STD_MANIFEST) compiler/radiance.manifest |
|
| 37 | - | SEED_OPTS := $(STD) -pkg radiance $(patsubst %,-mod %,$(COMPILER_SRC)) \ |
|
| 38 | - | $(patsubst %,-embed %,$(COMPILER_EMBED)) -entry radiance |
|
| 34 | + | SEED_OPTS := $(STD) -pkg radiance $(patsubst %,-mod %,$(COMPILER_SRC)) -entry radiance |
|
| 39 | 35 | ||
| 40 | - | $(RAD_BIN): Makefile $(SEED) std.lib $(STD_LIB) $(COMPILER_SRC) $(COMPILER_EMBED) \ |
|
| 41 | - | $(COMPILER_MANIFEST) | $(BIN_DIR) |
|
| 36 | + | $(RAD_BIN): Makefile $(SEED) std.lib $(STD_LIB) $(COMPILER_SRC) $(COMPILER_MANIFEST) | $(BIN_DIR) |
|
| 42 | 37 | @echo "radiance $(SEED) => $@" |
|
| 43 | 38 | @$(HOSTED) $(SEED) $(SEED_OPTS) -o $@ |
|
| 44 | 39 | ||
| 45 | 40 | # Compiler inputs shared by the normal build and bootstrap stages. |
|
| 46 | 41 | compiler-options: |
compiler/radiance/artifact.rad
+244 -411
| 1 | - | //! Complete compiler interface trees paired with exact executable package images. |
|
| 2 | - | ||
| 3 | - | mod value; |
|
| 1 | + | //! Package images with their compiler interface and native input trees. |
|
| 2 | + | //! |
|
| 3 | + | //! A package `name` is stored below an artifact root as three entries: |
|
| 4 | + | //! |
|
| 5 | + | //! * `name.ril.bin`, the binary RIL image. Its header names the dependencies, |
|
| 6 | + | //! their metadata pins, and the package's own metadata identity. |
|
| 7 | + | //! * `name.interface/`, the interface modules `name.rid` and `name/**.rid`, and the |
|
| 8 | + | //! embedded files that they read, at their paths from the package root. |
|
| 9 | + | //! * `name.native/`, the optional native inputs: assembly files `0.ras`, `1.ras` |
|
| 10 | + | //! and so on, the startup assembly `startup.ras`, and the source location table |
|
| 11 | + | //! `locations`. |
|
| 12 | + | //! |
|
| 13 | + | //! The image's metadata identity covers the interface tree, so the trees need no |
|
| 14 | + | //! descriptor. |
|
| 4 | 15 | ||
| 5 | 16 | use std::char; |
|
| 17 | + | use std::fmt; |
|
| 6 | 18 | use std::fs; |
|
| 7 | - | use std::io; |
|
| 8 | 19 | use std::hash::blake3; |
|
| 20 | + | use std::io; |
|
| 9 | 21 | use std::lang::alloc; |
|
| 10 | - | use std::lang::ast; |
|
| 11 | 22 | use std::lang::il::binary; |
|
| 12 | - | use std::lang::il::binary::linkage; |
|
| 13 | 23 | use std::lang::il::binary::program; |
|
| 14 | 24 | use std::lang::il::binary::reader; |
|
| 15 | - | use std::lang::layout; |
|
| 16 | - | use std::lang::pretty; |
|
| 17 | - | use std::lang::sexpr; |
|
| 18 | 25 | use std::mem; |
|
| 19 | 26 | use std::mem::heap; |
|
| 20 | 27 | use std::sys; |
|
| 28 | + | use super::metadata; |
|
| 21 | 29 | ||
| 22 | - | /// Version of the artifact descriptor and lockfile formats. The image header |
|
| 23 | - | /// carries the RIL format and ABI versions. |
|
| 24 | - | constant VERSION: u32 = 1; |
|
| 25 | - | /// Declarations that artifact modules import, and that their typed consumer names. |
|
| 26 | - | constant SCHEMA: *[u8] = @embed("radiance/artifact/schema.rad"); |
|
| 27 | - | /// Largest artifact metadata, including module and dependency descriptors. |
|
| 28 | - | constant MAX_METADATA: u32 = 1048576; |
|
| 30 | + | /// Suffix of the interface tree of a package. |
|
| 31 | + | constant INTERFACE: *[u8] = ".interface/"; |
|
| 32 | + | /// Suffix of the native input tree of a package. |
|
| 33 | + | constant NATIVE: *[u8] = ".native/"; |
|
| 34 | + | /// Name of the startup assembly in a native tree. |
|
| 35 | + | export constant STARTUP: *[u8] = "startup.ras"; |
|
| 36 | + | /// Name of the source location table in a native tree. |
|
| 37 | + | export constant LOCATIONS: *[u8] = "locations"; |
|
| 38 | + | /// Initial storage for one directory listing. |
|
| 39 | + | constant LISTING_BYTES: u32 = 65536; |
|
| 29 | 40 | ||
| 30 | 41 | /// Content validation required when loading a package artifact set. |
|
| 31 | 42 | export union Validation: Copy { |
|
| 32 | - | /// Trust content identities recorded in a compiler cache entry. |
|
| 43 | + | /// Trust the metadata identity in a compiler cache entry. |
|
| 33 | 44 | Trusted, |
|
| 34 | - | /// Compare every recorded content identity with its file bytes. |
|
| 45 | + | /// Compare the image's metadata identity with the interface tree. |
|
| 35 | 46 | Full, |
|
| 36 | 47 | } |
|
| 37 | 48 | ||
| 38 | - | /// One generated interface module in logical registration order. |
|
| 49 | + | /// One generated interface module. |
|
| 39 | 50 | export record Module: Copy { |
|
| 40 | 51 | /// Relative path in the interface tree, including the `.rid` extension. |
|
| 41 | 52 | path: *[u8], |
|
| 42 | 53 | /// Complete immutable interface text. |
|
| 43 | 54 | text: *[u8], |
|
| 44 | - | /// Digest of the complete text snapshot. |
|
| 45 | - | hash: binary::Id, |
|
| 46 | 55 | } |
|
| 47 | 56 | ||
| 48 | 57 | /// Compiler metadata required from a dependency. |
|
| 49 | 58 | export record Dependency: Copy { |
|
| 50 | 59 | /// Provider package name. |
|
| 51 | 60 | name: *[u8], |
|
| 52 | 61 | /// Hash of its compiler declarations and exported runtime contracts. |
|
| 53 | 62 | declarations: binary::Id, |
|
| 54 | 63 | } |
|
| 55 | 64 | ||
| 56 | - | /// Additional native-link input retained with a package image. |
|
| 65 | + | /// Use of a file stored with an artifact. |
|
| 57 | 66 | export union FileRole: Copy { |
|
| 58 | 67 | /// Assembly instructions linked after generated functions. |
|
| 59 | 68 | Assembly, |
|
| 60 | 69 | /// Entry assembly linked before all generated functions. |
|
| 61 | 70 | Startup, |
| 63 | 72 | Debug, |
|
| 64 | 73 | /// File contents that modules read with `@embed`. |
|
| 65 | 74 | Embed, |
|
| 66 | 75 | } |
|
| 67 | 76 | ||
| 68 | - | /// File roles in the order of the schema's `FileRole` variants. |
|
| 69 | - | constant ROLES: [FileRole; 4] = [ |
|
| 70 | - | FileRole::Assembly, FileRole::Startup, FileRole::Debug, FileRole::Embed |
|
| 71 | - | ]; |
|
| 72 | - | /// Schema variant names of `ROLES`. |
|
| 73 | - | constant ROLE_NAMES: [*[u8]; 4] = ["Assembly", "Startup", "Debug", "Embed"]; |
|
| 74 | - | ||
| 75 | 77 | /// An immutable non-Radiance artifact input. |
|
| 76 | 78 | export record File: Copy { |
|
| 77 | - | /// Relative storage path inside the interface tree. |
|
| 79 | + | /// Path in the native tree, or the package-relative path of an embedded file. |
|
| 78 | 80 | path: *[u8], |
|
| 79 | - | /// Relative diagnostic source path in a stored artifact. |
|
| 81 | + | /// Path of the file that was read, for diagnostics. |
|
| 80 | 82 | origin: *[u8], |
|
| 81 | 83 | /// Complete input contents. |
|
| 82 | 84 | bytes: *[u8], |
|
| 83 | - | /// Use of these bytes during native linking. |
|
| 85 | + | /// Use of these bytes. |
|
| 84 | 86 | role: FileRole, |
|
| 85 | 87 | } |
|
| 86 | 88 | ||
| 87 | - | /// Content identities and text for a stored package artifact. |
|
| 88 | - | export record Publication: Copy { |
|
| 89 | - | /// Complete binary RIL image. |
|
| 90 | - | image: *[u8], |
|
| 91 | - | /// Complete source compilation request key. |
|
| 92 | - | inputs: binary::Id, |
|
| 93 | - | /// Identity of the complete binary RIL image. |
|
| 94 | - | identity: binary::Id, |
|
| 95 | - | /// Encoded metadata which binds the complete set. |
|
| 96 | - | metadata: *[u8], |
|
| 97 | - | } |
|
| 98 | - | ||
| 99 | 89 | /// Package artifact retained in immutable compiler storage. |
|
| 100 | 90 | export record Package: Copy { |
|
| 101 | 91 | /// Package name and root module identity. |
|
| 102 | 92 | name: *[u8], |
|
| 103 | - | /// Identity of the compiler that produced this artifact. |
|
| 104 | - | compiler: *[u8], |
|
| 105 | 93 | /// Text modules in parent-before-child order. |
|
| 106 | 94 | modules: *[Module], |
|
| 107 | 95 | /// Assembly, interface embeds, and source locations retained with the package. |
|
| 108 | 96 | files: *[File], |
|
| 109 | 97 | /// Dependency names and required compiler metadata. |
|
| 110 | 98 | dependencies: *[Dependency], |
|
| 111 | - | /// Canonical hash of exported runtime contracts. |
|
| 112 | - | exports: binary::Id, |
|
| 113 | 99 | /// Digest of compiler declarations and dependency metadata. |
|
| 114 | 100 | declarations: binary::Id, |
|
| 115 | - | /// Storage identities and text for cache or artifact publication. |
|
| 116 | - | publication: ?Publication, |
|
| 101 | + | /// Complete binary RIL image, for cache or artifact publication. |
|
| 102 | + | image: ?*[u8], |
|
| 117 | 103 | } |
|
| 118 | 104 | ||
| 119 | 105 | /// Join path components or report exhausted permanent storage. |
|
| 120 | 106 | export fn join(arena: &heap::Heap, parts: &[*[u8]]) -> *[u8] throws (sys::Error) { |
|
| 121 | 107 | let bytes = try alloc::concatBytes(arena, parts) catch { |
| 135 | 121 | mem::store32Le(&mut size[..], 0, bytes.len); |
|
| 136 | 122 | blake3::update(state, &size[..]); |
|
| 137 | 123 | blake3::update(state, bytes); |
|
| 138 | 124 | } |
|
| 139 | 125 | ||
| 140 | - | /// Hash exported contracts, RID declarations, interface embeds, and dependency |
|
| 141 | - | /// metadata. RID declarations can read embedded files. Build provenance, source |
|
| 142 | - | /// locations, and native-link files are separate inputs. |
|
| 143 | - | export fn declarations(package: &Package) -> binary::Id { |
|
| 144 | - | let mut state = blake3::new(); |
|
| 145 | - | field(&mut state, package.name); |
|
| 146 | - | field(&mut state, &package.exports.bytes[..]); |
|
| 147 | - | let mut embeds: u32 = 0; |
|
| 126 | + | /// Return the positions of `paths` in increasing byte order. |
|
| 127 | + | fn sorted(paths: &[*[u8]], arena: &heap::Heap) -> *[u32] throws (sys::Error) { |
|
| 128 | + | let mut order: *mut [u32] = &mut []; |
|
| 129 | + | for path, i in paths { |
|
| 130 | + | try order.append(i, arena) catch { |
|
| 131 | + | throw sys::Error::TooLarge; |
|
| 132 | + | }; |
|
| 133 | + | let mut at = order.len - 1; |
|
| 134 | + | while at > 0 and mem::cmp(paths[order[at - 1]], path) > 0 { |
|
| 135 | + | set order[at] = order[at - 1]; |
|
| 136 | + | set at -= 1; |
|
| 137 | + | } |
|
| 138 | + | set order[at] = i; |
|
| 139 | + | } |
|
| 140 | + | return &order[..]; |
|
| 141 | + | } |
|
| 142 | + | ||
| 143 | + | /// Hash exported contracts, interface modules, the embedded files they read, |
|
| 144 | + | /// and dependency metadata. Modules and files are hashed in path order, so the |
|
| 145 | + | /// order of their registration does not matter. Build provenance, source |
|
| 146 | + | /// locations, and native inputs are separate inputs. |
|
| 147 | + | export fn declarations(package: &Package, exports: binary::Id, arena: &heap::Heap) -> binary::Id |
|
| 148 | + | throws (sys::Error) |
|
| 149 | + | { |
|
| 150 | + | let mut modules: *mut [*[u8]] = &mut []; |
|
| 151 | + | let mut embeds: *mut [*[u8]] = &mut []; |
|
| 152 | + | let mut contents: *mut [*[u8]] = &mut []; |
|
| 153 | + | for module in package.modules { |
|
| 154 | + | try modules.append(module.path, arena) catch { |
|
| 155 | + | throw sys::Error::TooLarge; |
|
| 156 | + | }; |
|
| 157 | + | } |
|
| 148 | 158 | for file in package.files { |
|
| 149 | 159 | if file.role == FileRole::Embed { |
|
| 150 | - | set embeds += 1; |
|
| 160 | + | try embeds.append(file.path, arena) catch { |
|
| 161 | + | throw sys::Error::TooLarge; |
|
| 162 | + | }; |
|
| 163 | + | try contents.append(file.bytes, arena) catch { |
|
| 164 | + | throw sys::Error::TooLarge; |
|
| 165 | + | }; |
|
| 151 | 166 | } |
|
| 152 | 167 | } |
|
| 168 | + | let mut state = blake3::new(); |
|
| 169 | + | field(&mut state, package.name); |
|
| 170 | + | field(&mut state, &exports.bytes[..]); |
|
| 153 | 171 | let mut counts: [u8; 16] = [0; 16]; |
|
| 154 | - | mem::store32Le(&mut counts[..], 0, VERSION); |
|
| 155 | - | mem::store32Le(&mut counts[..], 4, package.modules.len); |
|
| 156 | - | mem::store32Le(&mut counts[..], 8, embeds); |
|
| 172 | + | mem::store32Le(&mut counts[..], 0, binary::VERSION); |
|
| 173 | + | mem::store32Le(&mut counts[..], 4, modules.len); |
|
| 174 | + | mem::store32Le(&mut counts[..], 8, embeds.len); |
|
| 157 | 175 | mem::store32Le(&mut counts[..], 12, package.dependencies.len); |
|
| 158 | 176 | field(&mut state, &counts[..]); |
|
| 159 | - | for module in package.modules { |
|
| 160 | - | field(&mut state, module.path); |
|
| 161 | - | field(&mut state, &module.hash.bytes[..]); |
|
| 177 | + | for i in try sorted(&modules[..], arena) { |
|
| 178 | + | field(&mut state, modules[i]); |
|
| 179 | + | field(&mut state, &digest(package.modules[i].text).bytes[..]); |
|
| 162 | 180 | } |
|
| 163 | - | for file in package.files { |
|
| 164 | - | if file.role == FileRole::Embed { |
|
| 165 | - | field(&mut state, file.path); |
|
| 166 | - | field(&mut state, &digest(file.bytes).bytes[..]); |
|
| 167 | - | } |
|
| 181 | + | for i in try sorted(&embeds[..], arena) { |
|
| 182 | + | field(&mut state, embeds[i]); |
|
| 183 | + | field(&mut state, &digest(contents[i]).bytes[..]); |
|
| 168 | 184 | } |
|
| 169 | 185 | for dependency in package.dependencies { |
|
| 170 | 186 | field(&mut state, dependency.name); |
|
| 171 | 187 | field(&mut state, &dependency.declarations.bytes[..]); |
|
| 172 | 188 | } |
| 191 | 207 | } catch error as sys::Error { |
|
| 192 | 208 | throw error; |
|
| 193 | 209 | }; |
|
| 194 | 210 | } |
|
| 195 | 211 | ||
| 212 | + | /// Read a file if it exists. |
|
| 213 | + | fn readOptional(path: *[u8], arena: &heap::Heap) -> ?*[u8] throws (sys::Error) { |
|
| 214 | + | if try fs::stat(path) == nil { |
|
| 215 | + | return nil; |
|
| 216 | + | } |
|
| 217 | + | return try readFile(path, arena); |
|
| 218 | + | } |
|
| 219 | + | ||
| 196 | 220 | /// Require a relative identifier path bounded by its artifact tree. |
|
| 197 | 221 | export fn validPath(path: &[u8]) -> bool { |
|
| 198 | 222 | if not fs::isNormalRelativePath(path) { |
|
| 199 | 223 | return false; |
|
| 200 | 224 | } |
| 211 | 235 | } |
|
| 212 | 236 | } |
|
| 213 | 237 | return true; |
|
| 214 | 238 | } |
|
| 215 | 239 | ||
| 240 | + | /// Whether `path` names an interface module of package `name` rather than an |
|
| 241 | + | /// embedded file. |
|
| 242 | + | export fn isModule(name: &[u8], path: &[u8]) -> bool { |
|
| 243 | + | if not mem::endsWith(path, ".rid") or not mem::startsWith(path, name) { |
|
| 244 | + | return false; |
|
| 245 | + | } |
|
| 246 | + | let rest = &path[name.len ..]; |
|
| 247 | + | return mem::eq(rest, ".rid") or (rest.len > 0 and rest[0] == '/'); |
|
| 248 | + | } |
|
| 249 | + | ||
| 216 | 250 | /// Make each directory component, accepting an existing directory only. |
|
| 217 | 251 | export fn directories(path: *[u8]) throws (sys::Error) { |
|
| 218 | 252 | if let slash = mem::rfind(path, "/") { |
|
| 219 | 253 | try fs::createDirAll(&path[.. slash]); |
|
| 220 | 254 | } |
|
| 221 | 255 | } |
|
| 222 | 256 | ||
| 223 | - | /// Read a 256-bit content identity from a hexadecimal string field. |
|
| 224 | - | fn readId(data: &value::Image, descriptor: value::Value, name: *[u8]) -> binary::Id |
|
| 225 | - | throws (binary::Error, sys::Error) |
|
| 226 | - | { |
|
| 227 | - | let hash = try blake3::parse(try data.bytes(descriptor, name)) catch { |
|
| 228 | - | throw binary::Error::Invalid; |
|
| 229 | - | }; |
|
| 230 | - | return binary::Id { bytes: hash.bytes }; |
|
| 231 | - | } |
|
| 232 | - | ||
| 233 | - | /// Build a slice of constant element nodes. |
|
| 234 | - | fn sliceValue(nodes: &mut ast::NodeArena, items: *[*ast::Node]) -> ast::NodeValue |
|
| 235 | - | throws (alloc::AllocError) |
|
| 236 | - | { |
|
| 237 | - | let target = try ast::synthNode(nodes, ast::arrayValue(items)); |
|
| 238 | - | return ast::NodeValue::AddressOf(ast::AddressOf { target, mutable: false }); |
|
| 239 | - | } |
|
| 240 | - | ||
| 241 | - | /// Format an artifact document, which is the value `root`. |
|
| 242 | - | fn render(root: *ast::Node, buffer: *mut [u8], arena: *heap::Heap) -> *[u8] |
|
| 243 | - | throws (alloc::AllocError, binary::Error) |
|
| 244 | - | { |
|
| 245 | - | let tree = try pretty::expression(root, arena); |
|
| 246 | - | let mut out = sexpr::Buffer { buf: buffer, pos: 0 }; |
|
| 247 | - | { |
|
| 248 | - | let sink: &mut opaque sexpr::Output = &mut out; |
|
| 249 | - | layout::text::render(tree, layout::text::Options { width: 80, indent: 4 }, sink); |
|
| 250 | - | } |
|
| 251 | - | if out.pos >= out.buf.len { |
|
| 252 | - | throw binary::Error::Capacity; |
|
| 257 | + | /// List the directory at `path`, growing the listing storage as needed. Returns |
|
| 258 | + | /// the names, each followed by a zero byte. |
|
| 259 | + | export fn list(path: *[u8], arena: &heap::Heap) -> *[u8] throws (sys::Error) { |
|
| 260 | + | let mut capacity = LISTING_BYTES; |
|
| 261 | + | loop { |
|
| 262 | + | let saved = heap::mark(arena); |
|
| 263 | + | let listing = try fs::listDirAlloc(path, capacity, arena) catch error as sys::Error { |
|
| 264 | + | if error <> sys::Error::TooLarge or capacity > mem::U32_MAX / 2 { |
|
| 265 | + | throw error; |
|
| 266 | + | } |
|
| 267 | + | // A failed listing retains no pointers into the temporary buffer. |
|
| 268 | + | unsafe { |
|
| 269 | + | heap::rewind(arena, saved); |
|
| 270 | + | } |
|
| 271 | + | set capacity *= 2; |
|
| 272 | + | continue; |
|
| 273 | + | } catch failure as alloc::AllocError { |
|
| 274 | + | throw sys::Error::TooLarge; |
|
| 275 | + | }; |
|
| 276 | + | return listing; |
|
| 253 | 277 | } |
|
| 254 | - | return &out.buf[.. out.pos]; |
|
| 255 | 278 | } |
|
| 256 | 279 | ||
| 257 | - | /// Format package metadata as a record literal. |
|
| 258 | - | /// The arena holds temporary AST nodes, literal text, and layout data. |
|
| 259 | - | export fn metadata( |
|
| 260 | - | package: &Package, |
|
| 261 | - | inputs: binary::Id, |
|
| 262 | - | identity: binary::Id, |
|
| 263 | - | buffer: *mut [u8], |
|
| 264 | - | arena: *heap::Heap |
|
| 265 | - | ) -> *[u8] |
|
| 266 | - | throws (alloc::AllocError, binary::Error) |
|
| 280 | + | /// Append the paths of the regular files below directory `prefix` of `tree`, |
|
| 281 | + | /// relative to `tree`. The prefix is empty or ends with `/`. |
|
| 282 | + | fn walk(tree: *[u8], prefix: *[u8], paths: &mut *mut [*[u8]], arena: &heap::Heap) |
|
| 283 | + | throws (sys::Error) |
|
| 267 | 284 | { |
|
| 268 | - | let mut nodes = ast::NodeArena { heap: arena, nextId: 0 }; |
|
| 269 | - | let mut modules: *mut [*ast::Node] = &mut []; |
|
| 270 | - | let mut files: *mut [*ast::Node] = &mut []; |
|
| 271 | - | let mut dependencies: *mut [*ast::Node] = &mut []; |
|
| 272 | - | for module in package.modules { |
|
| 273 | - | let node = try ast::synthRecord( |
|
| 274 | - | &mut nodes, |
|
| 275 | - | "Module", |
|
| 276 | - | &[ |
|
| 277 | - | ast::field("path", ast::stringValue(module.path)), |
|
| 278 | - | ast::field("hash", try ast::hexStringValue(&module.hash.bytes[..], arena)), |
|
| 279 | - | ] |
|
| 280 | - | ); |
|
| 281 | - | try modules.append(node, arena); |
|
| 282 | - | } |
|
| 283 | - | for file in package.files { |
|
| 284 | - | let role = ROLE_NAMES[file.role as u32]; |
|
| 285 | - | let roleType = try ast::synthNode(&mut nodes, ast::NodeValue::Ident("FileRole")); |
|
| 286 | - | let variant = try ast::synthNode(&mut nodes, ast::NodeValue::Ident(role)); |
|
| 287 | - | let node = try ast::synthRecord( |
|
| 288 | - | &mut nodes, |
|
| 289 | - | "File", |
|
| 290 | - | &[ |
|
| 291 | - | ast::field("path", ast::stringValue(file.path)), |
|
| 292 | - | ast::field("origin", ast::stringValue(file.origin)), |
|
| 293 | - | ast::field("role", ast::NodeValue::ScopeAccess(ast::Access { |
|
| 294 | - | parent: roleType, child: variant |
|
| 295 | - | })), |
|
| 296 | - | ast::field("hash", try ast::hexStringValue(&digest(file.bytes).bytes[..], arena)), |
|
| 297 | - | ] |
|
| 298 | - | ); |
|
| 299 | - | try files.append(node, arena); |
|
| 300 | - | } |
|
| 301 | - | for dependency in package.dependencies { |
|
| 302 | - | let node = try ast::synthRecord( |
|
| 303 | - | &mut nodes, |
|
| 304 | - | "Dependency", |
|
| 305 | - | &[ |
|
| 306 | - | ast::field("name", ast::stringValue(dependency.name)), |
|
| 307 | - | ast::field( |
|
| 308 | - | "declarations", |
|
| 309 | - | try ast::hexStringValue(&dependency.declarations.bytes[..], arena) |
|
| 310 | - | ), |
|
| 311 | - | ] |
|
| 312 | - | ); |
|
| 313 | - | try dependencies.append(node, arena); |
|
| 285 | + | let listing = try list(try join(arena, &[tree, prefix]), arena); |
|
| 286 | + | let mut names = mem::split(listing, 0); |
|
| 287 | + | while let name = names.next() { |
|
| 288 | + | let path = try join(arena, &[prefix, name]); |
|
| 289 | + | if not validPath(path) { |
|
| 290 | + | throw sys::Error::Invalid; |
|
| 291 | + | } |
|
| 292 | + | let info = try fs::stat(try join(arena, &[tree, path])) else throw sys::Error::NotFound; |
|
| 293 | + | match info.kind { |
|
| 294 | + | case fs::Kind::Directory => try walk(tree, try join(arena, &[path, "/"]), paths, arena), |
|
| 295 | + | case fs::Kind::File => try paths.append(path, arena) catch { |
|
| 296 | + | throw sys::Error::TooLarge; |
|
| 297 | + | }, |
|
| 298 | + | else => throw sys::Error::Invalid, |
|
| 299 | + | } |
|
| 314 | 300 | } |
|
| 315 | - | let moduleValues = try sliceValue(&mut nodes, &modules[..]); |
|
| 316 | - | let fileValues = try sliceValue(&mut nodes, &files[..]); |
|
| 317 | - | let dependencyValues = try sliceValue(&mut nodes, &dependencies[..]); |
|
| 318 | - | let root = try ast::synthRecord( |
|
| 319 | - | &mut nodes, |
|
| 320 | - | "Package", |
|
| 321 | - | &[ |
|
| 322 | - | ast::field("version", try ast::numberValue(VERSION as u64, arena)), |
|
| 323 | - | ast::field("name", ast::stringValue(package.name)), |
|
| 324 | - | ast::field("compiler", ast::stringValue(package.compiler)), |
|
| 325 | - | ast::field("exports", try ast::hexStringValue(&package.exports.bytes[..], arena)), |
|
| 326 | - | ast::field("inputs", try ast::hexStringValue(&inputs.bytes[..], arena)), |
|
| 327 | - | ast::field("image", try ast::hexStringValue(&identity.bytes[..], arena)), |
|
| 328 | - | ast::field("modules", moduleValues), |
|
| 329 | - | ast::field("files", fileValues), |
|
| 330 | - | ast::field("dependencies", dependencyValues), |
|
| 331 | - | ] |
|
| 332 | - | ); |
|
| 333 | - | return try render(root, buffer, arena); |
|
| 334 | 301 | } |
|
| 335 | 302 | ||
| 336 | - | /// Read package files and check their metadata with the selected content validation. |
|
| 303 | + | /// Read package files. Full validation checks the interface tree against the |
|
| 304 | + | /// image's metadata identity; trusted validation accepts that identity. |
|
| 337 | 305 | export fn read( |
|
| 338 | 306 | root: *[u8], |
|
| 339 | 307 | name: *[u8], |
|
| 340 | 308 | validation: Validation, |
|
| 341 | 309 | scratch: *heap::Heap, |
|
| 342 | 310 | arena: &heap::Heap |
|
| 343 | 311 | ) -> Package throws (sys::Error) { |
|
| 344 | - | let tree = try join(arena, &[root, "/", name, ".interface/"]); |
|
| 345 | - | let path = try join(arena, &[tree, "package.metadata"]); |
|
| 346 | - | let info = try fs::stat(path) else throw sys::Error::NotFound; |
|
| 347 | - | if info.size > MAX_METADATA as u64 { |
|
| 312 | + | let image = try readFile(try join(arena, &[root, "/", name, ".ril.bin"]), arena); |
|
| 313 | + | let tree = try join(arena, &[root, "/", name, INTERFACE]); |
|
| 314 | + | let mut paths: *mut [*[u8]] = &mut []; |
|
| 315 | + | try walk(tree, "", &mut paths, arena); |
|
| 316 | + | let mut modules: *mut [Module] = &mut []; |
|
| 317 | + | let mut files: *mut [File] = &mut []; |
|
| 318 | + | // Sorted paths list each parent module before its children. |
|
| 319 | + | for i in try sorted(&paths[..], arena) { |
|
| 320 | + | let path = paths[i]; |
|
| 321 | + | let origin = try join(arena, &[tree, path]); |
|
| 322 | + | let bytes = try readFile(origin, arena); |
|
| 323 | + | if isModule(name, path) { |
|
| 324 | + | try modules.append(Module { path, text: bytes }, arena) catch { |
|
| 325 | + | throw sys::Error::TooLarge; |
|
| 326 | + | }; |
|
| 327 | + | } else { |
|
| 328 | + | try files.append(File { path, origin, bytes, role: FileRole::Embed }, arena) catch { |
|
| 329 | + | throw sys::Error::TooLarge; |
|
| 330 | + | }; |
|
| 331 | + | } |
|
| 332 | + | } |
|
| 333 | + | if modules.len == 0 or not mem::eq(modules[0].path, try join(arena, &[name, ".rid"])) { |
|
| 348 | 334 | throw sys::Error::Invalid; |
|
| 349 | 335 | } |
|
| 350 | - | let encoded = try readFile(path, arena); |
|
| 351 | - | // Decoded artifact fields own copies outside the temporary frontend storage. |
|
| 336 | + | let native = try join(arena, &[root, "/", name, NATIVE]); |
|
| 337 | + | for i in 0 .. mem::U32_MAX { |
|
| 338 | + | let mut digits: [u8; fmt::U32_STR_LEN] = undefined; |
|
| 339 | + | let number = try alloc::copyBytes(arena, fmt::formatU32(i, &mut digits[..])) catch { |
|
| 340 | + | throw sys::Error::TooLarge; |
|
| 341 | + | }; |
|
| 342 | + | let path = try join(arena, &[&number[..], ".ras"]); |
|
| 343 | + | let origin = try join(arena, &[native, path]); |
|
| 344 | + | let bytes = try readOptional(origin, arena) else break; |
|
| 345 | + | try files.append(File { path, origin, bytes, role: FileRole::Assembly }, arena) catch { |
|
| 346 | + | throw sys::Error::TooLarge; |
|
| 347 | + | }; |
|
| 348 | + | } |
|
| 349 | + | for path, i in &[STARTUP, LOCATIONS] { |
|
| 350 | + | let origin = try join(arena, &[native, path]); |
|
| 351 | + | if let bytes = try readOptional(origin, arena) { |
|
| 352 | + | let role = FileRole::Startup if i == 0 else FileRole::Debug; |
|
| 353 | + | try files.append(File { path, origin, bytes, role }, arena) catch { |
|
| 354 | + | throw sys::Error::TooLarge; |
|
| 355 | + | }; |
|
| 356 | + | } |
|
| 357 | + | } |
|
| 358 | + | // Decoded artifact fields own copies outside the temporary decoder storage. |
|
| 352 | 359 | let mark = heap::mark(scratch); |
|
| 353 | - | let result = try locked( |
|
| 354 | - | root, tree, name, encoded, validation, scratch, arena |
|
| 360 | + | let result = try decode( |
|
| 361 | + | name, image, &modules[..], &files[..], validation, scratch, arena |
|
| 355 | 362 | ) catch err as binary::Error { |
|
| 356 | 363 | unsafe { |
|
| 357 | 364 | heap::rewind(scratch, mark); |
|
| 358 | 365 | } |
|
| 359 | 366 | if err == binary::Error::Storage { |
| 370 | 377 | heap::rewind(scratch, mark); |
|
| 371 | 378 | } |
|
| 372 | 379 | return result; |
|
| 373 | 380 | } |
|
| 374 | 381 | ||
| 375 | - | /// Decode metadata, check file structure, and apply the selected content validation. |
|
| 382 | + | /// Read the image header, and check the interface tree with the selected validation. |
|
| 376 | 383 | fn decode( |
|
| 377 | - | root: *[u8], |
|
| 378 | - | tree: *[u8], |
|
| 379 | 384 | name: *[u8], |
|
| 380 | - | encoded: *[u8], |
|
| 385 | + | image: *[u8], |
|
| 386 | + | modules: *[Module], |
|
| 387 | + | files: *[File], |
|
| 381 | 388 | validation: Validation, |
|
| 382 | 389 | scratch: *heap::Heap, |
|
| 383 | 390 | arena: &heap::Heap |
|
| 384 | 391 | ) -> Package throws (binary::Error, sys::Error) { |
|
| 385 | - | let data = try value::read( |
|
| 386 | - | SCHEMA, encoded, try join(arena, &[tree, "package.metadata"]), "Package", scratch |
|
| 387 | - | ); |
|
| 388 | - | let rootValue = data.root; |
|
| 389 | - | if try data.number(rootValue, "version") <> VERSION |
|
| 390 | - | or not mem::eq(try data.bytes(rootValue, "name"), name) |
|
| 391 | - | { |
|
| 392 | - | throw binary::Error::Invalid; |
|
| 393 | - | } |
|
| 394 | - | let exports = try readId(&data, rootValue, "exports"); |
|
| 395 | - | let compiler = try join(arena, &[try data.bytes(rootValue, "compiler")]); |
|
| 396 | - | if compiler.len == 0 { |
|
| 397 | - | throw binary::Error::Invalid; |
|
| 398 | - | } |
|
| 399 | - | let inputs = try readId(&data, rootValue, "inputs"); |
|
| 400 | - | let identity = try readId(&data, rootValue, "image"); |
|
| 401 | - | let image = try readFile(try join(arena, &[root, "/", name, ".ril.bin"]), arena); |
|
| 402 | - | if image.len < 8 |
|
| 403 | - | or mem::load32Le(image, 0) <> binary::MAGIC |
|
| 404 | - | or mem::load32Le(image, 4) <> binary::VERSION |
|
| 405 | - | or (validation == Validation::Full and linkage::imageId(image) <> identity) |
|
| 406 | - | { |
|
| 407 | - | throw binary::Error::Invalid; |
|
| 408 | - | } |
|
| 409 | - | let moduleValues = try data.items(rootValue, "modules"); |
|
| 410 | - | if moduleValues.len == 0 { |
|
| 392 | + | let mut input = reader::new(image); |
|
| 393 | + | let header = try program::header(&mut input, scratch); |
|
| 394 | + | if not mem::eq(header.name, name) { |
|
| 411 | 395 | throw binary::Error::Invalid; |
|
| 412 | 396 | } |
|
| 413 | - | let mut modules: *mut [Module] = &mut []; |
|
| 414 | - | for i in 0 .. moduleValues.len { |
|
| 415 | - | let descriptor = try data.at(&moduleValues, i); |
|
| 416 | - | let path = try join(arena, &[try data.bytes(descriptor, "path")]); |
|
| 417 | - | let hash = try readId(&data, descriptor, "hash"); |
|
| 418 | - | if not validPath(path) or not mem::endsWith(path, ".rid") { |
|
| 419 | - | throw binary::Error::Invalid; |
|
| 420 | - | } |
|
| 421 | - | if i == 0 { |
|
| 422 | - | if not mem::eq(path, try join(arena, &[name, ".rid"])) { |
|
| 423 | - | throw binary::Error::Invalid; |
|
| 424 | - | } |
|
| 425 | - | } else if not mem::startsWith(path, try join(arena, &[name, "/"])) { |
|
| 426 | - | throw binary::Error::Invalid; |
|
| 427 | - | } |
|
| 428 | - | let text = try readFile(try join(arena, &[tree, path]), arena); |
|
| 429 | - | if validation == Validation::Full and digest(text) <> hash { |
|
| 430 | - | throw binary::Error::Invalid; |
|
| 431 | - | } |
|
| 432 | - | try modules.append(Module { path, text, hash }, arena) catch { |
|
| 433 | - | throw binary::Error::Storage; |
|
| 434 | - | }; |
|
| 435 | - | } |
|
| 436 | - | let fileValues = try data.items(rootValue, "files"); |
|
| 437 | - | let mut files: *mut [File] = &mut []; |
|
| 438 | - | for i in 0 .. fileValues.len { |
|
| 439 | - | let descriptor = try data.at(&fileValues, i); |
|
| 440 | - | let path = try join(arena, &[try data.bytes(descriptor, "path")]); |
|
| 441 | - | let origin = try join(arena, &[try data.bytes(descriptor, "origin")]); |
|
| 442 | - | if origin.len > 0 and origin[0] == '/' { |
|
| 443 | - | throw binary::Error::Invalid; |
|
| 444 | - | } |
|
| 445 | - | let kind = try data.variant(descriptor, "role"); |
|
| 446 | - | if not validPath(path) or kind >= ROLES.len { |
|
| 447 | - | throw binary::Error::Invalid; |
|
| 448 | - | } |
|
| 449 | - | let hash = try readId(&data, descriptor, "hash"); |
|
| 450 | - | let bytes = try readFile(try join(arena, &[tree, path]), arena); |
|
| 451 | - | if validation == Validation::Full and digest(bytes) <> hash { |
|
| 452 | - | throw binary::Error::Invalid; |
|
| 453 | - | } |
|
| 454 | - | try files.append(File { path, origin, bytes, role: ROLES[kind] }, arena) catch { |
|
| 455 | - | throw binary::Error::Storage; |
|
| 456 | - | }; |
|
| 457 | - | } |
|
| 458 | - | let dependencyValues = try data.items(rootValue, "dependencies"); |
|
| 459 | 397 | let mut dependencies: *mut [Dependency] = &mut []; |
|
| 460 | - | for i in 0 .. dependencyValues.len { |
|
| 461 | - | let descriptor = try data.at(&dependencyValues, i); |
|
| 462 | - | let name = try join(arena, &[try data.bytes(descriptor, "name")]); |
|
| 463 | - | if not validPath(name) { |
|
| 398 | + | for dependency, i in header.dependencies { |
|
| 399 | + | if not validPath(dependency) |
|
| 400 | + | or mem::find(dependency, "/") <> nil |
|
| 401 | + | or mem::find(dependency, ".") <> nil |
|
| 402 | + | { |
|
| 464 | 403 | throw binary::Error::Invalid; |
|
| 465 | 404 | } |
|
| 466 | - | for j in 0 .. dependencies.len { |
|
| 467 | - | if mem::eq(dependencies[j].name, name) { |
|
| 468 | - | throw binary::Error::Invalid; |
|
| 469 | - | } |
|
| 470 | - | } |
|
| 471 | - | for byte in name { |
|
| 472 | - | if byte == '/' or byte == '.' { |
|
| 405 | + | for earlier in &header.dependencies[.. i] { |
|
| 406 | + | if mem::eq(earlier, dependency) { |
|
| 473 | 407 | throw binary::Error::Invalid; |
|
| 474 | 408 | } |
|
| 475 | 409 | } |
|
| 476 | - | let declarations = try readId(&data, descriptor, "declarations"); |
|
| 477 | - | try dependencies.append(Dependency { name, declarations }, arena) catch { |
|
| 410 | + | try dependencies.append( |
|
| 411 | + | Dependency { |
|
| 412 | + | name: try join(arena, &[dependency]), declarations: header.links.pins[i] |
|
| 413 | + | }, |
|
| 414 | + | arena |
|
| 415 | + | ) catch { |
|
| 478 | 416 | throw binary::Error::Storage; |
|
| 479 | 417 | }; |
|
| 480 | 418 | } |
|
| 481 | - | let mut package = Package { |
|
| 419 | + | let package = Package { |
|
| 482 | 420 | name, |
|
| 483 | - | compiler, |
|
| 484 | - | modules: &modules[..], |
|
| 485 | - | files: &files[..], |
|
| 421 | + | modules, |
|
| 422 | + | files, |
|
| 486 | 423 | dependencies: &dependencies[..], |
|
| 487 | - | exports, |
|
| 488 | - | declarations: binary::Id { bytes: [0; 32] }, |
|
| 489 | - | publication: Publication { image, inputs, identity, metadata: encoded }, |
|
| 424 | + | declarations: header.links.metadata, |
|
| 425 | + | image, |
|
| 490 | 426 | }; |
|
| 491 | - | if conflict(&package) <> nil { |
|
| 492 | - | throw binary::Error::Invalid; |
|
| 493 | - | } |
|
| 494 | - | set package.declarations = declarations(&package); |
|
| 495 | - | let mut input = reader::new(image); |
|
| 496 | - | let header = try program::header(&mut input, arena); |
|
| 497 | - | if header.links.metadata <> package.declarations { |
|
| 498 | - | throw binary::Error::Invalid; |
|
| 427 | + | if validation == Validation::Full { |
|
| 428 | + | let contract = try program::declarations(image, scratch); |
|
| 429 | + | let exports = try metadata::exports(&contract, scratch); |
|
| 430 | + | if try declarations(&package, exports, scratch) <> package.declarations { |
|
| 431 | + | throw binary::Error::Invalid; |
|
| 432 | + | } |
|
| 499 | 433 | } |
|
| 500 | 434 | return package; |
|
| 501 | 435 | } |
|
| 502 | 436 | ||
| 503 | - | /// Path `index` of the interface tree: the modules, then the files, then the metadata. |
|
| 504 | - | fn storedPath(package: &Package, index: u32) -> *[u8] { |
|
| 437 | + | /// Path `index` of the interface tree: the modules, then the embedded files. |
|
| 438 | + | /// Other files have no interface path. |
|
| 439 | + | fn interfacePath(package: &Package, index: u32) -> ?*[u8] { |
|
| 505 | 440 | if index < package.modules.len { |
|
| 506 | 441 | return package.modules[index].path; |
|
| 507 | 442 | } |
|
| 508 | - | if index - package.modules.len < package.files.len { |
|
| 509 | - | return package.files[index - package.modules.len].path; |
|
| 443 | + | let file = package.files[index - package.modules.len]; |
|
| 444 | + | if file.role <> FileRole::Embed { |
|
| 445 | + | return nil; |
|
| 510 | 446 | } |
|
| 511 | - | return "package.metadata"; |
|
| 447 | + | return file.path; |
|
| 512 | 448 | } |
|
| 513 | 449 | ||
| 514 | - | /// Find a path of the interface tree that repeats another one, or that names a file |
|
| 515 | - | /// where another path needs a directory. |
|
| 450 | + | /// Find an interface path that repeats another one, that names a file where |
|
| 451 | + | /// another path needs a directory, or that a reader would take for a module |
|
| 452 | + | /// although it names an embedded file. |
|
| 516 | 453 | fn conflict(package: &Package) -> ?*[u8] { |
|
| 517 | - | let count = package.modules.len + package.files.len + 1; |
|
| 454 | + | let count = package.modules.len + package.files.len; |
|
| 518 | 455 | for i in 0 .. count { |
|
| 456 | + | let a = interfacePath(package, i) else continue; |
|
| 457 | + | if i >= package.modules.len and isModule(package.name, a) { |
|
| 458 | + | return a; |
|
| 459 | + | } |
|
| 519 | 460 | for j in i + 1 .. count { |
|
| 520 | - | let a = storedPath(package, i); |
|
| 521 | - | let b = storedPath(package, j); |
|
| 461 | + | let b = interfacePath(package, j) else continue; |
|
| 522 | 462 | let short = a if a.len <= b.len else b; |
|
| 523 | 463 | let long = b if a.len <= b.len else a; |
|
| 524 | 464 | if mem::startsWith(long, short) and (long.len == short.len or long[short.len] == '/') { |
|
| 525 | 465 | return b; |
|
| 526 | 466 | } |
| 529 | 469 | return nil; |
|
| 530 | 470 | } |
|
| 531 | 471 | ||
| 532 | 472 | /// Write the complete set inside a private artifact root. The caller publishes that root. |
|
| 533 | 473 | export fn write(package: &Package, root: *[u8], arena: &heap::Heap) throws (sys::Error) { |
|
| 534 | - | let publication = package.publication else panic "write: missing publication"; |
|
| 474 | + | let image = package.image else panic "write: missing package image"; |
|
| 535 | 475 | if let path = conflict(package) { |
|
| 536 | 476 | io::printError("radiance: artifact path conflicts with another stored path: "); |
|
| 537 | 477 | io::printError(path); |
|
| 538 | 478 | io::printError("\n"); |
|
| 539 | 479 | throw sys::Error::Invalid; |
|
| 540 | 480 | } |
|
| 541 | - | let tree = try join(arena, &[root, "/", package.name, ".interface/"]); |
|
| 481 | + | let tree = try join(arena, &[root, "/", package.name, INTERFACE]); |
|
| 482 | + | let native = try join(arena, &[root, "/", package.name, NATIVE]); |
|
| 542 | 483 | for module in package.modules { |
|
| 543 | 484 | let path = try join(arena, &[tree, module.path]); |
|
| 544 | 485 | try directories(path); |
|
| 545 | 486 | try fs::writeFile(path, module.text); |
|
| 546 | 487 | } |
|
| 547 | 488 | for file in package.files { |
|
| 548 | - | let path = try join(arena, &[tree, file.path]); |
|
| 489 | + | let path = try join(arena, &[tree if file.role == FileRole::Embed else native, file.path]); |
|
| 549 | 490 | try directories(path); |
|
| 550 | 491 | try fs::writeFile(path, file.bytes); |
|
| 551 | 492 | } |
|
| 552 | - | let image = try join(arena, &[root, "/", package.name, ".ril.bin"]); |
|
| 553 | - | try directories(image); |
|
| 554 | - | try fs::writeFile(image, publication.image); |
|
| 555 | - | try fs::writeFile(try join(arena, &[tree, "package.metadata"]), publication.metadata); |
|
| 556 | - | } |
|
| 557 | - | ||
| 558 | - | /// Read an artifact and check its exact selection when a lockfile is present. |
|
| 559 | - | fn locked( |
|
| 560 | - | root: *[u8], |
|
| 561 | - | tree: *[u8], |
|
| 562 | - | name: *[u8], |
|
| 563 | - | encoded: *[u8], |
|
| 564 | - | validation: Validation, |
|
| 565 | - | scratch: *heap::Heap, |
|
| 566 | - | arena: &heap::Heap |
|
| 567 | - | ) -> Package throws (binary::Error, sys::Error) { |
|
| 568 | - | let package = try decode(root, tree, name, encoded, validation, scratch, arena); |
|
| 569 | - | let publication = package.publication else panic "locked: missing publication"; |
|
| 570 | - | if validation == Validation::Trusted { |
|
| 571 | - | return package; |
|
| 572 | - | } |
|
| 573 | - | let path = try join(arena, &[root, "/package.lock"]); |
|
| 574 | - | let info = try fs::stat(path) else return package; |
|
| 575 | - | if info.size > MAX_METADATA as u64 { |
|
| 576 | - | throw binary::Error::Invalid; |
|
| 577 | - | } |
|
| 578 | - | let encoded = try readFile(path, arena); |
|
| 579 | - | let data = try value::read(SCHEMA, encoded, path, "Lock", scratch); |
|
| 580 | - | if try data.number(data.root, "version") <> VERSION { |
|
| 581 | - | throw binary::Error::Invalid; |
|
| 582 | - | } |
|
| 583 | - | let packages = try data.items(data.root, "packages"); |
|
| 584 | - | let mut names: *mut [*[u8]] = &mut []; |
|
| 585 | - | let mut found = false; |
|
| 586 | - | for i in 0 .. packages.len { |
|
| 587 | - | let entry = try data.at(&packages, i); |
|
| 588 | - | let selected = try data.bytes(entry, "name"); |
|
| 589 | - | let image = try readId(&data, entry, "image"); |
|
| 590 | - | let metadata = try readId(&data, entry, "metadata"); |
|
| 591 | - | for j in 0 .. names.len { |
|
| 592 | - | if mem::eq(names[j], selected) { |
|
| 593 | - | throw binary::Error::Invalid; |
|
| 594 | - | } |
|
| 595 | - | } |
|
| 596 | - | try names.append(selected, scratch) catch { |
|
| 597 | - | throw binary::Error::Storage; |
|
| 598 | - | }; |
|
| 599 | - | if mem::eq(selected, name) { |
|
| 600 | - | if image <> publication.identity or metadata <> package.declarations { |
|
| 601 | - | throw binary::Error::Invalid; |
|
| 602 | - | } |
|
| 603 | - | set found = true; |
|
| 604 | - | } |
|
| 605 | - | } |
|
| 606 | - | if not found { |
|
| 607 | - | throw binary::Error::Invalid; |
|
| 608 | - | } |
|
| 609 | - | return package; |
|
| 610 | - | } |
|
| 611 | - | ||
| 612 | - | /// Write the exact images and metadata selected for an exported package graph. |
|
| 613 | - | export fn writeLock(packages: &[Package], root: *[u8], arena: *heap::Heap) |
|
| 614 | - | throws (sys::Error, alloc::AllocError) |
|
| 615 | - | { |
|
| 616 | - | let mut nodes = ast::NodeArena { heap: arena, nextId: 0 }; |
|
| 617 | - | let mut entries: *mut [*ast::Node] = &mut []; |
|
| 618 | - | for package in packages { |
|
| 619 | - | let publication = package.publication else panic "writeLock: missing publication"; |
|
| 620 | - | let identity = publication.identity; |
|
| 621 | - | let image = try ast::hexStringValue(&identity.bytes[..], arena) catch { |
|
| 622 | - | throw sys::Error::TooLarge; |
|
| 623 | - | }; |
|
| 624 | - | let metadata = try ast::hexStringValue(&package.declarations.bytes[..], arena) catch { |
|
| 625 | - | throw sys::Error::TooLarge; |
|
| 626 | - | }; |
|
| 627 | - | let node = try ast::synthRecord( |
|
| 628 | - | &mut nodes, |
|
| 629 | - | "LockedPackage", |
|
| 630 | - | &[ |
|
| 631 | - | ast::field("name", ast::stringValue(package.name)), |
|
| 632 | - | ast::field("image", image), |
|
| 633 | - | ast::field("metadata", metadata), |
|
| 634 | - | ] |
|
| 635 | - | ) catch { |
|
| 636 | - | throw sys::Error::TooLarge; |
|
| 637 | - | }; |
|
| 638 | - | try entries.append(node, arena) catch { |
|
| 639 | - | throw sys::Error::TooLarge; |
|
| 640 | - | }; |
|
| 641 | - | } |
|
| 642 | - | let packages = try sliceValue(&mut nodes, &entries[..]); |
|
| 643 | - | let rootNode = try ast::synthRecord( |
|
| 644 | - | &mut nodes, |
|
| 645 | - | "Lock", |
|
| 646 | - | &[ |
|
| 647 | - | ast::field("version", try ast::numberValue(VERSION as u64, arena)), |
|
| 648 | - | ast::field("packages", packages), |
|
| 649 | - | ] |
|
| 650 | - | ) catch { |
|
| 651 | - | throw sys::Error::TooLarge; |
|
| 652 | - | }; |
|
| 653 | - | let mut buffer: *mut [u8] = &mut []; |
|
| 654 | - | try buffer.resize(MAX_METADATA, 0, arena) catch { |
|
| 655 | - | throw sys::Error::TooLarge; |
|
| 656 | - | }; |
|
| 657 | - | let encoded = try render(rootNode, buffer.take(), arena) catch { |
|
| 658 | - | throw sys::Error::TooLarge; |
|
| 659 | - | }; |
|
| 660 | - | try fs::writeFile(try join(arena, &[root, "/package.lock"]), encoded); |
|
| 493 | + | try fs::writeFile(try join(arena, &[root, "/", package.name, ".ril.bin"]), image); |
|
| 661 | 494 | } |
compiler/radiance/artifact/schema.rad
deleted
+0 -84
| 1 | - | //! Compiler artifact descriptors and exact package selections. |
|
| 2 | - | //! |
|
| 3 | - | //! The compiler embeds this module and resolves package metadata and |
|
| 4 | - | //! lockfiles against it. |
|
| 5 | - | ||
| 6 | - | /// Use of a file stored with an artifact. |
|
| 7 | - | export union FileRole: Copy { |
|
| 8 | - | /// Instructions after generated functions. |
|
| 9 | - | Assembly, |
|
| 10 | - | /// Entry instructions before generated functions. |
|
| 11 | - | Startup, |
|
| 12 | - | /// Source location tables. |
|
| 13 | - | Debug, |
|
| 14 | - | /// Contents read with `@embed`. |
|
| 15 | - | Embed, |
|
| 16 | - | } |
|
| 17 | - | ||
| 18 | - | /// One compiler interface module. |
|
| 19 | - | export record Module: Copy { |
|
| 20 | - | /// Relative interface path. |
|
| 21 | - | path: *[u8], |
|
| 22 | - | /// Content identity in hexadecimal. |
|
| 23 | - | hash: *[u8], |
|
| 24 | - | } |
|
| 25 | - | ||
| 26 | - | /// One file stored with an artifact. |
|
| 27 | - | export record File: Copy { |
|
| 28 | - | /// Relative storage path. |
|
| 29 | - | path: *[u8], |
|
| 30 | - | /// Relative diagnostic source path. |
|
| 31 | - | origin: *[u8], |
|
| 32 | - | /// Use of the contents. |
|
| 33 | - | role: FileRole, |
|
| 34 | - | /// Content identity in hexadecimal. |
|
| 35 | - | hash: *[u8], |
|
| 36 | - | } |
|
| 37 | - | ||
| 38 | - | /// Required provider declarations. |
|
| 39 | - | export record Dependency: Copy { |
|
| 40 | - | /// Provider package name. |
|
| 41 | - | name: *[u8], |
|
| 42 | - | /// Compiler metadata identity in hexadecimal. |
|
| 43 | - | declarations: *[u8], |
|
| 44 | - | } |
|
| 45 | - | ||
| 46 | - | /// Complete artifact descriptor. |
|
| 47 | - | export record Package: Copy { |
|
| 48 | - | /// Descriptor format version. |
|
| 49 | - | version: u32, |
|
| 50 | - | /// Root package name. |
|
| 51 | - | name: *[u8], |
|
| 52 | - | /// Identity of the compiler that produced the artifact. |
|
| 53 | - | compiler: *[u8], |
|
| 54 | - | /// Exported contract identity in hexadecimal. |
|
| 55 | - | exports: *[u8], |
|
| 56 | - | /// Compilation request identity in hexadecimal. |
|
| 57 | - | inputs: *[u8], |
|
| 58 | - | /// Executable image identity in hexadecimal. |
|
| 59 | - | image: *[u8], |
|
| 60 | - | /// Interfaces in registration order. |
|
| 61 | - | modules: *[Module], |
|
| 62 | - | /// Native-link inputs and embedded files. |
|
| 63 | - | files: *[File], |
|
| 64 | - | /// Required provider declarations. |
|
| 65 | - | dependencies: *[Dependency], |
|
| 66 | - | } |
|
| 67 | - | ||
| 68 | - | /// Exact selection of one package. |
|
| 69 | - | export record LockedPackage: Copy { |
|
| 70 | - | /// Package name. |
|
| 71 | - | name: *[u8], |
|
| 72 | - | /// Executable image identity in hexadecimal. |
|
| 73 | - | image: *[u8], |
|
| 74 | - | /// Compiler metadata identity in hexadecimal. |
|
| 75 | - | metadata: *[u8], |
|
| 76 | - | } |
|
| 77 | - | ||
| 78 | - | /// Exact selections for an exported package graph. |
|
| 79 | - | export record Lock: Copy { |
|
| 80 | - | /// Lockfile format version. |
|
| 81 | - | version: u32, |
|
| 82 | - | /// Selected packages in compilation order. |
|
| 83 | - | packages: *[LockedPackage], |
|
| 84 | - | } |
compiler/radiance/artifact/value.rad
deleted
+0 -264
| 1 | - | //! Typed constant values read from artifact documents. |
|
| 2 | - | ||
| 3 | - | use std::lang::alloc; |
|
| 4 | - | use std::lang::ast; |
|
| 5 | - | use std::lang::module; |
|
| 6 | - | use std::lang::parser; |
|
| 7 | - | use std::lang::resolver; |
|
| 8 | - | use std::lang::scanner; |
|
| 9 | - | use std::lang::strings; |
|
| 10 | - | use std::mem; |
|
| 11 | - | use std::mem::heap; |
|
| 12 | - | use std::sys; |
|
| 13 | - | ||
| 14 | - | /// Constant expression with its schema type. |
|
| 15 | - | export record Value: Copy { |
|
| 16 | - | /// Expression after constant aliases and projections are followed. |
|
| 17 | - | node: *ast::Node, |
|
| 18 | - | /// Semantic type required by the schema. |
|
| 19 | - | type: resolver::Type, |
|
| 20 | - | } |
|
| 21 | - | ||
| 22 | - | /// Ordered elements of a constant slice or array. |
|
| 23 | - | export record Elements: Copy { |
|
| 24 | - | /// Semantic element type. |
|
| 25 | - | type: resolver::Type, |
|
| 26 | - | /// Array initializer holding the elements. |
|
| 27 | - | array: resolver::ConstArray, |
|
| 28 | - | /// Number of elements. |
|
| 29 | - | len: u32, |
|
| 30 | - | } |
|
| 31 | - | ||
| 32 | - | /// Resolved artifact document and its value checked against the schema. |
|
| 33 | - | export record Image { |
|
| 34 | - | /// Semantic information about the resolved modules. |
|
| 35 | - | resolver: resolver::Resolver, |
|
| 36 | - | /// Document value checked against the schema. |
|
| 37 | - | root: Value, |
|
| 38 | - | /// Storage for byte strings written as byte arrays. |
|
| 39 | - | arena: *heap::Heap, |
|
| 40 | - | } |
|
| 41 | - | ||
| 42 | - | /// Parse with `parse` and preserve allocation errors as storage failures. |
|
| 43 | - | fn parse( |
|
| 44 | - | syntax: &mut parser::Parser, |
|
| 45 | - | path: *[u8], |
|
| 46 | - | text: *[u8], |
|
| 47 | - | parse: fn(&mut parser::Parser, scanner::SourceLoc, *[u8]) -> *ast::Node |
|
| 48 | - | throws (parser::ParseError) |
|
| 49 | - | ) -> *ast::Node throws (sys::Error) { |
|
| 50 | - | return try parse(syntax, scanner::SourceLoc::File(path), text) catch error { |
|
| 51 | - | if error == parser::ParseError::AllocationFailed { |
|
| 52 | - | throw sys::Error::TooLarge; |
|
| 53 | - | } |
|
| 54 | - | throw sys::Error::Invalid; |
|
| 55 | - | }; |
|
| 56 | - | } |
|
| 57 | - | ||
| 58 | - | /// Build the document module around its value `value`: an import of every |
|
| 59 | - | /// `schema` declaration and the constant `VALUE` of the type named `expected`. |
|
| 60 | - | fn document(syntax: &mut parser::Parser, expected: *[u8], value: *ast::Node) -> *ast::Node |
|
| 61 | - | throws (alloc::AllocError) |
|
| 62 | - | { |
|
| 63 | - | let nodes = &mut syntax.arena; |
|
| 64 | - | let schema = try ast::synthNode(nodes, ast::NodeValue::Ident( |
|
| 65 | - | strings::intern(&mut syntax.pool, "schema") |
|
| 66 | - | )); |
|
| 67 | - | let import = try ast::synthNode(nodes, ast::NodeValue::Use(ast::Use { |
|
| 68 | - | path: schema, wildcard: true, attrs: nil |
|
| 69 | - | })); |
|
| 70 | - | let name = try ast::synthNode(nodes, ast::NodeValue::Ident( |
|
| 71 | - | strings::intern(&mut syntax.pool, expected) |
|
| 72 | - | )); |
|
| 73 | - | let type = try ast::synthNode(nodes, ast::NodeValue::TypeSig(ast::TypeSig::Nominal { |
|
| 74 | - | path: name, regions: &[] |
|
| 75 | - | })); |
|
| 76 | - | let ident = try ast::synthNode(nodes, ast::NodeValue::Ident( |
|
| 77 | - | strings::intern(&mut syntax.pool, "VALUE") |
|
| 78 | - | )); |
|
| 79 | - | let decl = try ast::synthNode(nodes, ast::NodeValue::ConstDecl(ast::ConstDecl { |
|
| 80 | - | ident, type, value, attrs: nil |
|
| 81 | - | })); |
|
| 82 | - | let mut statements = try ast::nodeSlice(nodes, 2); |
|
| 83 | - | try statements.append(import, nodes.heap); |
|
| 84 | - | try statements.append(decl, nodes.heap); |
|
| 85 | - | return try ast::synthNode(nodes, ast::NodeValue::Module(ast::Module { statements })); |
|
| 86 | - | } |
|
| 87 | - | ||
| 88 | - | /// Resolve the value in `text`, typed as the declaration `expected` of the |
|
| 89 | - | /// `schema` module source, and read it from the resolved tree. The text is one |
|
| 90 | - | /// expression, which names `schema` declarations directly. All returned storage |
|
| 91 | - | /// belongs to `arena`. |
|
| 92 | - | export fn read( |
|
| 93 | - | schema: *[u8], text: *[u8], path: *[u8], expected: *[u8], arena: *heap::Heap |
|
| 94 | - | ) -> Image throws (sys::Error) { |
|
| 95 | - | let mut names: *mut [*[u8]] = &mut []; |
|
| 96 | - | try names.resize(strings::TABLE_SIZE, "", arena) catch { |
|
| 97 | - | throw sys::Error::TooLarge; |
|
| 98 | - | }; |
|
| 99 | - | let mut syntax = parser::newParser( |
|
| 100 | - | ast::NodeArena { heap: arena, nextId: 0 }, strings::freshPool(names.take()) |
|
| 101 | - | ); |
|
| 102 | - | let mut entries: *mut [module::ModuleEntry] = &mut []; |
|
| 103 | - | try entries.reserve(2, arena) catch { |
|
| 104 | - | throw sys::Error::TooLarge; |
|
| 105 | - | }; |
|
| 106 | - | // Module registration initializes each reserved entry before it is read. |
|
| 107 | - | unsafe { |
|
| 108 | - | set entries.len = 2; |
|
| 109 | - | } |
|
| 110 | - | let mut graph = module::moduleGraph(entries.take()); |
|
| 111 | - | let schemaId = try module::registerRootWithName( |
|
| 112 | - | &mut graph, &mut syntax.pool, 0, "schema", "schema.rad" |
|
| 113 | - | ) catch { |
|
| 114 | - | throw sys::Error::Invalid; |
|
| 115 | - | }; |
|
| 116 | - | let documentId = try module::registerRootWithName( |
|
| 117 | - | &mut graph, &mut syntax.pool, 1, "document", path |
|
| 118 | - | ) catch { |
|
| 119 | - | throw sys::Error::Invalid; |
|
| 120 | - | }; |
|
| 121 | - | let schemaRoot = try parse(&mut syntax, "schema.rad", schema, parser::parseModule); |
|
| 122 | - | let value = try parse(&mut syntax, path, text, parser::parseValue); |
|
| 123 | - | let documentRoot = try document(&mut syntax, expected, value) catch { |
|
| 124 | - | throw sys::Error::TooLarge; |
|
| 125 | - | }; |
|
| 126 | - | let sources = [schema, text]; |
|
| 127 | - | let roots = [schemaRoot, documentRoot]; |
|
| 128 | - | for id, i in [schemaId, documentId] { |
|
| 129 | - | set graph.entries[id as u32].ast = roots[i]; |
|
| 130 | - | set graph.entries[id as u32].source = sources[i]; |
|
| 131 | - | set graph.entries[id as u32].state = module::ModuleState::Parsed; |
|
| 132 | - | } |
|
| 133 | - | let modules = module::freeze(graph); |
|
| 134 | - | let mut packages: *mut [resolver::Pkg] = &mut []; |
|
| 135 | - | for entry in modules.entries { |
|
| 136 | - | let root = entry.ast else panic "read: missing module AST"; |
|
| 137 | - | let owner = module::get(modules, entry.id) else panic "read: missing module"; |
|
| 138 | - | try packages.append(resolver::Pkg { rootEntry: owner, rootAst: root }, arena) catch { |
|
| 139 | - | throw sys::Error::TooLarge; |
|
| 140 | - | }; |
|
| 141 | - | } |
|
| 142 | - | let nodeData = try resolver::nodeDataStorage(arena, syntax.arena.nextId) catch { |
|
| 143 | - | throw sys::Error::TooLarge; |
|
| 144 | - | }; |
|
| 145 | - | let mut errors: *mut [resolver::Error] = &mut []; |
|
| 146 | - | try errors.resize( |
|
| 147 | - | resolver::MAX_ERRORS, |
|
| 148 | - | resolver::Error { |
|
| 149 | - | kind: resolver::ErrorKind::DuplicateBinding(""), node: nil, moduleId: 0 |
|
| 150 | - | }, |
|
| 151 | - | arena |
|
| 152 | - | ) catch { |
|
| 153 | - | throw sys::Error::TooLarge; |
|
| 154 | - | }; |
|
| 155 | - | let mut res = resolver::resolver( |
|
| 156 | - | resolver::ResolverStorage { arena, nodeData, errors: errors.take() }, |
|
| 157 | - | resolver::Config { buildTest: false } |
|
| 158 | - | ); |
|
| 159 | - | let diagnostics = try resolver::resolve(&mut res, modules, &packages[..]) catch { |
|
| 160 | - | let diagnostics = resolver::diagnostics(&mut res); |
|
| 161 | - | resolver::printer::printDiagnostics(&diagnostics, &res); |
|
| 162 | - | throw sys::Error::Invalid; |
|
| 163 | - | }; |
|
| 164 | - | if not resolver::success(&diagnostics) { |
|
| 165 | - | resolver::printer::printDiagnostics(&diagnostics, &res); |
|
| 166 | - | throw sys::Error::Invalid; |
|
| 167 | - | } |
|
| 168 | - | let case ast::NodeValue::Module(block) = documentRoot.value else panic "read: expected module"; |
|
| 169 | - | let declaration = block.statements[1]; |
|
| 170 | - | let type = resolver::typeFor(&res, declaration) else panic "read: missing value type"; |
|
| 171 | - | let node = res.constantNode(value) else throw sys::Error::Invalid; |
|
| 172 | - | return Image { resolver: res, root: Value { node, type }, arena }; |
|
| 173 | - | } |
|
| 174 | - | ||
| 175 | - | /// Select a record field. Record literals list fields in declaration order. |
|
| 176 | - | fn (image: &Image) field(value: Value, name: *[u8]) -> Value throws (sys::Error) { |
|
| 177 | - | let info = resolver::getRecord(&image.resolver, value.type) else { |
|
| 178 | - | throw sys::Error::Invalid; |
|
| 179 | - | }; |
|
| 180 | - | for field, i in info.fields { |
|
| 181 | - | if let label = field.name; mem::eq(label, name) { |
|
| 182 | - | let node = image.resolver.constantField(value.node, i) else { |
|
| 183 | - | throw sys::Error::Invalid; |
|
| 184 | - | }; |
|
| 185 | - | return Value { node, type: field.fieldType }; |
|
| 186 | - | } |
|
| 187 | - | } |
|
| 188 | - | throw sys::Error::Invalid; |
|
| 189 | - | } |
|
| 190 | - | ||
| 191 | - | /// Read the elements of a constant slice or array value. |
|
| 192 | - | fn (image: &Image) elements(value: Value) -> Elements throws (sys::Error) { |
|
| 193 | - | let mut type = resolver::Type::Unknown; |
|
| 194 | - | match value.type { |
|
| 195 | - | case resolver::Type::Slice { item, .. } => set type = *item, |
|
| 196 | - | case resolver::Type::Array(info) => set type = *info.item, |
|
| 197 | - | else => throw sys::Error::Invalid, |
|
| 198 | - | } |
|
| 199 | - | let array = image.resolver.constantArray(value.node) else throw sys::Error::Invalid; |
|
| 200 | - | return Elements { type, array, len: array.len }; |
|
| 201 | - | } |
|
| 202 | - | ||
| 203 | - | /// Read a slice field. |
|
| 204 | - | export fn (image: &Image) items(value: Value, name: *[u8]) -> Elements throws (sys::Error) { |
|
| 205 | - | return try image.elements(try image.field(value, name)); |
|
| 206 | - | } |
|
| 207 | - | ||
| 208 | - | /// Select one element from a constant slice. |
|
| 209 | - | export fn (image: &Image) at(elements: &Elements, index: u32) -> Value throws (sys::Error) { |
|
| 210 | - | assert index < elements.len; |
|
| 211 | - | let node = image.resolver.constantElement(elements.array, index) else { |
|
| 212 | - | throw sys::Error::Invalid; |
|
| 213 | - | }; |
|
| 214 | - | return Value { node, type: elements.type }; |
|
| 215 | - | } |
|
| 216 | - | ||
| 217 | - | /// Read an unsigned integer from a constant expression. |
|
| 218 | - | fn (image: &Image) unsigned(value: Value) -> u32 throws (sys::Error) { |
|
| 219 | - | let case resolver::ConstValue::Int(number) = resolver::constValueEntry( |
|
| 220 | - | &image.resolver, value.node |
|
| 221 | - | ) else throw sys::Error::Invalid; |
|
| 222 | - | if number.negative or number.magnitude > mem::U32_MAX as u64 { |
|
| 223 | - | throw sys::Error::Invalid; |
|
| 224 | - | } |
|
| 225 | - | return number.magnitude as u32; |
|
| 226 | - | } |
|
| 227 | - | ||
| 228 | - | /// Read a byte-slice or byte-array field from a string or from byte constants. |
|
| 229 | - | export fn (image: &Image) bytes(value: Value, name: *[u8]) -> *[u8] throws (sys::Error) { |
|
| 230 | - | let field = try image.field(value, name); |
|
| 231 | - | if let case resolver::ConstValue::String(text) = resolver::constValueEntry( |
|
| 232 | - | &image.resolver, field.node |
|
| 233 | - | ) { |
|
| 234 | - | return text; |
|
| 235 | - | } |
|
| 236 | - | let elements = try image.elements(field); |
|
| 237 | - | if elements.type <> resolver::Type::U8 { |
|
| 238 | - | throw sys::Error::Invalid; |
|
| 239 | - | } |
|
| 240 | - | let mut bytes: *mut [u8] = &mut []; |
|
| 241 | - | for i in 0 .. elements.len { |
|
| 242 | - | let byte = try image.unsigned(try image.at(&elements, i)); |
|
| 243 | - | try bytes.append(byte as u8, image.arena) catch { |
|
| 244 | - | throw sys::Error::TooLarge; |
|
| 245 | - | }; |
|
| 246 | - | } |
|
| 247 | - | return &bytes[..]; |
|
| 248 | - | } |
|
| 249 | - | ||
| 250 | - | /// Read an unsigned integer field. |
|
| 251 | - | export fn (image: &Image) number(value: Value, name: *[u8]) -> u32 throws (sys::Error) { |
|
| 252 | - | return try image.unsigned(try image.field(value, name)); |
|
| 253 | - | } |
|
| 254 | - | ||
| 255 | - | /// Read the index of a union variant field. |
|
| 256 | - | export fn (image: &Image) variant(value: Value, name: *[u8]) -> u32 throws (sys::Error) { |
|
| 257 | - | let field = try image.field(value, name); |
|
| 258 | - | let symbol = resolver::symbolFor(&image.resolver, field.node) else { |
|
| 259 | - | throw sys::Error::Invalid; |
|
| 260 | - | }; |
|
| 261 | - | let case resolver::SymbolData::Variant { index, .. } = image.resolver.symbols[*symbol].data |
|
| 262 | - | else throw sys::Error::Invalid; |
|
| 263 | - | return index; |
|
| 264 | - | } |
compiler/radiance/build.rad
+50 -96
| 281 | 281 | let dependencies = try dependencyNames(manifest, arena); |
|
| 282 | 282 | let mut files: *mut [artifact::File] = &mut []; |
|
| 283 | 283 | let count = options.asmPathCount + 1; |
|
| 284 | 284 | for i in 0 .. count { |
|
| 285 | 285 | let mut origin: *[u8] = ""; |
|
| 286 | + | let mut path = artifact::STARTUP; |
|
| 286 | 287 | let mut role = artifact::FileRole::Assembly; |
|
| 287 | 288 | if i == options.asmPathCount { |
|
| 288 | - | let path = options.startupPath else continue; |
|
| 289 | - | set origin = path; |
|
| 289 | + | let startup = options.startupPath else continue; |
|
| 290 | + | set origin = startup; |
|
| 290 | 291 | set role = artifact::FileRole::Startup; |
|
| 291 | 292 | } else { |
|
| 292 | 293 | set origin = options.asmPaths[i]; |
|
| 294 | + | let mut digits: [u8; fmt::U32_STR_LEN] = undefined; |
|
| 295 | + | let number = try keep(arena, fmt::formatU32(i, &mut digits[..])); |
|
| 296 | + | set path = try artifact::join(arena, &[number, ".ras"]); |
|
| 293 | 297 | } |
|
| 294 | - | let mut digits: [u8; fmt::U32_STR_LEN] = undefined; |
|
| 295 | - | let value = fmt::formatU32(i, &mut digits[..]); |
|
| 296 | - | let number = try keep(arena, value); |
|
| 297 | - | let path = try artifact::join(arena, &["asm/", number, ".ras"]); |
|
| 298 | 298 | let bytes = try artifact::readFile(origin, arena); |
|
| 299 | 299 | try files.append(artifact::File { path, origin, bytes, role }, arena) catch { |
|
| 300 | 300 | throw sys::Error::TooLarge; |
|
| 301 | 301 | }; |
|
| 302 | 302 | } |
| 317 | 317 | let first = sourcePath(options.radPaths[0]); |
|
| 318 | 318 | let slash = mem::rfind(first, "/") else return ""; |
|
| 319 | 319 | return &first[.. slash + 1]; |
|
| 320 | 320 | } |
|
| 321 | 321 | ||
| 322 | - | /// Return a relative diagnostic origin for a captured artifact file. |
|
| 323 | - | fn fileOrigin(root: *[u8], file: &artifact::File) -> *[u8] { |
|
| 324 | - | if file.origin.len == 0 { |
|
| 325 | - | return ""; |
|
| 326 | - | } |
|
| 327 | - | if mem::startsWith(file.origin, root) { |
|
| 328 | - | let relative = &file.origin[root.len ..]; |
|
| 329 | - | if relative.len > 0 and relative[0] <> '/' { |
|
| 330 | - | return relative; |
|
| 331 | - | } |
|
| 332 | - | } |
|
| 333 | - | return file.path; |
|
| 334 | - | } |
|
| 335 | - | ||
| 336 | 322 | /// Read the files that a source package declares for `@embed`. Each one must lie |
|
| 337 | 323 | /// under the package root, and is stored at its path from the root in the interface |
|
| 338 | 324 | /// tree. |
|
| 339 | 325 | export fn embeds(options: super::PackageInput, arena: &heap::Heap) -> *[artifact::File] |
|
| 340 | 326 | throws (sys::Error) |
| 600 | 586 | artifact::field(&mut state, &mapped[..]); |
|
| 601 | 587 | artifact::field(&mut state, module.logical); |
|
| 602 | 588 | let hash = artifact::digest(module.text); |
|
| 603 | 589 | artifact::field(&mut state, &hash.bytes[..]); |
|
| 604 | 590 | } |
|
| 605 | - | let root = rootDirectory(*options); |
|
| 606 | 591 | for file in input.files { |
|
| 607 | 592 | artifact::field(&mut state, file.path); |
|
| 608 | - | artifact::field(&mut state, fileOrigin(root, &file)); |
|
| 609 | 593 | let role: [u8; 1] = [file.role as u8]; |
|
| 610 | 594 | artifact::field(&mut state, &role[..]); |
|
| 611 | 595 | artifact::field(&mut state, file.bytes); |
|
| 612 | 596 | } |
|
| 613 | 597 | for id in declared { |
| 667 | 651 | ctx, graph, work, selected, entryName, driverArena, compiler |
|
| 668 | 652 | ); |
|
| 669 | 653 | let case BuiltPackages { packages, native, data, sourcePaths, asmData } = built |
|
| 670 | 654 | else panic "run: expected package results"; |
|
| 671 | 655 | if let directory = ctx.rilDirectory { |
|
| 672 | - | try writePackages(packages, directory, driverArena, work.functions); |
|
| 656 | + | try writePackages(packages, directory, driverArena); |
|
| 673 | 657 | } |
|
| 674 | 658 | if let output = ctx.outputPath { |
|
| 675 | 659 | resetFrontend(ctx, graph, work.main); |
|
| 676 | 660 | let case locations::Native::Program(native) = native |
|
| 677 | 661 | else panic "run: missing native generator"; |
| 807 | 791 | throw super::Error::Other; |
|
| 808 | 792 | }; |
|
| 809 | 793 | resetFrontend(ctx, graph, work.main); |
|
| 810 | 794 | if let root = ctx.cacheDirectory { |
|
| 811 | 795 | // Function storage has no live IL after its sink consumed each function. |
|
| 812 | - | try cache::publish(root, &built, work.main, work.functions) catch { |
|
| 796 | + | let cacheKey = request else panic "buildPackages: missing cache key"; |
|
| 797 | + | try cache::publish( |
|
| 798 | + | root, cacheKey, &built, work.main, work.functions |
|
| 799 | + | ) catch { |
|
| 813 | 800 | throw super::error(&["cannot publish package cache", built.name]); |
|
| 814 | 801 | }; |
|
| 815 | 802 | unsafe { |
|
| 816 | 803 | heap::reset(work.functions); |
|
| 817 | 804 | } |
| 846 | 833 | /// Copy a completed provider's exports into persistent emitter storage. |
|
| 847 | 834 | fn readExports(provider: &artifact::Package, work: &mut Workspace) -> link::Provider |
|
| 848 | 835 | throws (super::Error) |
|
| 849 | 836 | { |
|
| 850 | 837 | let checkpoint = heap::mark(work.functions); |
|
| 851 | - | let publication = provider.publication else panic "readExports: missing package image"; |
|
| 852 | - | let mut input = reader::new(publication.image); |
|
| 838 | + | let image = provider.image else panic "readExports: missing package image"; |
|
| 839 | + | let mut input = reader::new(image); |
|
| 853 | 840 | let head = try program::header(&mut input, work.functions) catch { |
|
| 854 | 841 | throw super::error(&[ |
|
| 855 | 842 | "package encoding storage exhausted", provider.name |
|
| 856 | 843 | ]); |
|
| 857 | 844 | }; |
| 906 | 893 | ctx, graph, i, &modules[..], module::Role::Interface, deps, embeds |
|
| 907 | 894 | ); |
|
| 908 | 895 | } |
|
| 909 | 896 | } |
|
| 910 | 897 | ||
| 911 | - | /// Write package artifact trees and the exact graph lockfile. |
|
| 912 | - | fn writePackages( |
|
| 913 | - | compiled: &[artifact::Package], |
|
| 914 | - | directory: *[u8], |
|
| 915 | - | driverArena: &heap::Heap, |
|
| 916 | - | functions: *heap::Heap |
|
| 917 | - | ) throws (super::Error) { |
|
| 918 | - | for i in 0 .. compiled.len { |
|
| 919 | - | let package = compiled[i]; |
|
| 898 | + | /// Write package artifact trees. |
|
| 899 | + | fn writePackages(compiled: &[artifact::Package], directory: *[u8], driverArena: &heap::Heap) |
|
| 900 | + | throws (super::Error) |
|
| 901 | + | { |
|
| 902 | + | for package in compiled { |
|
| 920 | 903 | try artifact::write(&package, directory, driverArena) catch { |
|
| 921 | 904 | throw super::error(&["cannot write package artifacts", package.name]); |
|
| 922 | 905 | }; |
|
| 923 | 906 | } |
|
| 924 | - | try artifact::writeLock(&compiled[..], directory, functions) catch { |
|
| 925 | - | throw super::error(&["cannot write package lockfile"]); |
|
| 926 | - | }; |
|
| 927 | 907 | } |
|
| 928 | 908 | ||
| 929 | 909 | /// Begin native generation and assemble the selected startup input. |
|
| 930 | 910 | fn nativeOutput( |
|
| 931 | 911 | ctx: &mut super::CompileContext, |
| 1151 | 1131 | }; |
|
| 1152 | 1132 | } |
|
| 1153 | 1133 | set path = try artifact::join(work.artifacts, &[path, ".rid"]) catch { |
|
| 1154 | 1134 | throw super::error(&["package encoding storage exhausted", pkg.name]); |
|
| 1155 | 1135 | }; |
|
| 1156 | - | try declarations.append( |
|
| 1157 | - | artifact::Module { path, text: bytes, hash: artifact::digest(bytes) }, |
|
| 1158 | - | work.artifacts |
|
| 1159 | - | ) catch { |
|
| 1136 | + | try declarations.append(artifact::Module { path, text: bytes }, work.artifacts) catch { |
|
| 1160 | 1137 | throw super::error(&["package encoding storage exhausted", pkg.name]); |
|
| 1161 | 1138 | }; |
|
| 1162 | 1139 | } |
|
| 1163 | 1140 | } |
|
| 1164 | 1141 | unsafe { |
| 1233 | 1210 | work: &mut Workspace, |
|
| 1234 | 1211 | selection: Request['providers] |
|
| 1235 | 1212 | ) -> Compilation throws (super::Error) { |
|
| 1236 | 1213 | let mut lowering = low; |
|
| 1237 | 1214 | let pkg = ctx.packages[index]; |
|
| 1238 | - | let case Request { declared, key: request, files, .. } = selection |
|
| 1239 | - | else panic "finishPackage: expected source request"; |
|
| 1215 | + | let case Request { files, .. } = selection else panic "finishPackage: expected source request"; |
|
| 1240 | 1216 | let mut packageFiles: *mut [artifact::File] = &mut []; |
|
| 1241 | - | let root = rootDirectory(ctx.inputs[index]); |
|
| 1242 | - | for file in files { |
|
| 1243 | - | let mut stored = file; |
|
| 1244 | - | set stored.origin = fileOrigin(root, &file); |
|
| 1245 | - | try packageFiles.append(stored, work.artifacts) catch { |
|
| 1246 | - | throw super::Error::Other; |
|
| 1247 | - | }; |
|
| 1248 | - | } |
|
| 1217 | + | try packageFiles.extend(files, work.artifacts) catch { |
|
| 1218 | + | throw super::Error::Other; |
|
| 1219 | + | }; |
|
| 1249 | 1220 | if sink.locations.bytes.len > 0 { |
|
| 1250 | 1221 | let bytes = try keep( |
|
| 1251 | 1222 | work.artifacts, &sink.locations.bytes[.. sink.locations.offset] |
|
| 1252 | 1223 | ) catch { |
|
| 1253 | 1224 | throw super::Error::Other; |
|
| 1254 | 1225 | }; |
|
| 1255 | 1226 | try packageFiles.append( |
|
| 1256 | 1227 | artifact::File { |
|
| 1257 | - | path: "locations", origin: "", bytes, role: artifact::FileRole::Debug |
|
| 1228 | + | path: artifact::LOCATIONS, origin: "", bytes, role: artifact::FileRole::Debug |
|
| 1258 | 1229 | }, |
|
| 1259 | 1230 | work.artifacts |
|
| 1260 | 1231 | ) catch { |
|
| 1261 | 1232 | throw super::Error::Other; |
|
| 1262 | 1233 | }; |
| 1288 | 1259 | else panic "finishPackage: expected function export"; |
|
| 1289 | 1260 | set declaredIndices[source] = i; |
|
| 1290 | 1261 | } |
|
| 1291 | 1262 | } |
|
| 1292 | 1263 | let mut plan = try link::prepare( |
|
| 1293 | - | &mut spool, &whole, &dataOrigins[..], selection.providers, &references[..], index as u16 |
|
| 1264 | + | &mut spool, |
|
| 1265 | + | &whole, |
|
| 1266 | + | &dataOrigins[..], |
|
| 1267 | + | selection.providers, |
|
| 1268 | + | selection.declared, |
|
| 1269 | + | &references[..], |
|
| 1270 | + | index as u16 |
|
| 1294 | 1271 | ) catch { |
|
| 1295 | 1272 | throw super::error(&["cannot resolve package imports"]); |
|
| 1296 | 1273 | }; |
|
| 1297 | 1274 | super::pkgLog(&pkg, &["encoding package tables"]); |
|
| 1298 | 1275 | let functionCount = spool.count; |
| 1322 | 1299 | set contract.program.fns = &fns[..]; |
|
| 1323 | 1300 | set contract.exports = &contractExports[..]; |
|
| 1324 | 1301 | let exports = try metadata::exports(&contract, work.functions) catch { |
|
| 1325 | 1302 | throw super::error(&["cannot hash exported contracts", pkg.name]); |
|
| 1326 | 1303 | }; |
|
| 1327 | - | // Only the hash survives the temporary declaration graph. |
|
| 1328 | - | unsafe { |
|
| 1329 | - | heap::rewind(work.functions, checkpoint); |
|
| 1330 | - | } |
|
| 1331 | 1304 | let mut dependencies: *mut [artifact::Dependency] = &mut []; |
|
| 1332 | - | for provider, i in providers { |
|
| 1333 | - | let mut used = false; |
|
| 1334 | - | for id in declared { |
|
| 1335 | - | set used = used or id as u32 == i; |
|
| 1336 | - | } |
|
| 1337 | - | for id in plan.dependencies { |
|
| 1338 | - | set used = used or id == i; |
|
| 1339 | - | } |
|
| 1340 | - | if used { |
|
| 1341 | - | try dependencies.append( |
|
| 1342 | - | artifact::Dependency { |
|
| 1343 | - | name: provider.name, |
|
| 1344 | - | declarations: provider.declarations, |
|
| 1345 | - | }, |
|
| 1346 | - | work.artifacts |
|
| 1347 | - | ) catch { |
|
| 1348 | - | throw super::error(&["package encoding storage exhausted", pkg.name]); |
|
| 1349 | - | }; |
|
| 1350 | - | } |
|
| 1305 | + | for id in plan.dependencies { |
|
| 1306 | + | let provider = providers[id]; |
|
| 1307 | + | try dependencies.append( |
|
| 1308 | + | artifact::Dependency { name: provider.name, declarations: provider.declarations }, |
|
| 1309 | + | work.artifacts |
|
| 1310 | + | ) catch { |
|
| 1311 | + | throw super::error(&["package encoding storage exhausted", pkg.name]); |
|
| 1312 | + | }; |
|
| 1351 | 1313 | } |
|
| 1352 | - | super::pkgLog(&pkg, &["encoding interface metadata"]); |
|
| 1353 | 1314 | let mut result = artifact::Package { |
|
| 1354 | 1315 | name: pkg.name, |
|
| 1355 | - | compiler: super::IDENTITY, |
|
| 1356 | 1316 | modules: declarations, |
|
| 1357 | 1317 | files: &packageFiles[..], |
|
| 1358 | 1318 | dependencies: &dependencies[..], |
|
| 1359 | - | exports, |
|
| 1360 | 1319 | declarations: binary::Id { bytes: [0; linkage::ID_BYTES] }, |
|
| 1361 | - | publication: nil, |
|
| 1320 | + | image: nil, |
|
| 1362 | 1321 | }; |
|
| 1363 | - | set result.declarations = artifact::declarations(&result); |
|
| 1322 | + | set result.declarations = try artifact::declarations(&result, exports, work.functions) catch { |
|
| 1323 | + | throw super::error(&["package encoding storage exhausted", pkg.name]); |
|
| 1324 | + | }; |
|
| 1325 | + | // Only the hashes survive the temporary declaration graph. |
|
| 1326 | + | unsafe { |
|
| 1327 | + | heap::rewind(work.functions, checkpoint); |
|
| 1328 | + | } |
|
| 1364 | 1329 | set plan.image.links.metadata = result.declarations; |
|
| 1365 | 1330 | let case codegen::RilSpool { encoding, names: collectedNames, .. } = spool |
|
| 1366 | 1331 | else panic "finishPackage: expected package spool"; |
|
| 1367 | 1332 | let mut names = collectedNames; |
|
| 1368 | 1333 | if let case codegen::Encoding::Image { bytes, used } = encoding { |
|
| 1369 | - | let inputs = request else panic "finishPackage: missing publication key"; |
|
| 1370 | 1334 | let mut head = try buffer(work.functions, HEADER_BYTES); |
|
| 1371 | 1335 | let length = try program::encodeHead( |
|
| 1372 | 1336 | &mut head, &plan.image, &mut names.index, functionCount |
|
| 1373 | 1337 | ) catch { |
|
| 1374 | 1338 | throw super::error(&["cannot encode package image"]); |
| 1384 | 1348 | throw super::error(&["package encoding storage exhausted", pkg.name]); |
|
| 1385 | 1349 | }; |
|
| 1386 | 1350 | try finalImage.extend(&bytes[.. used], work.artifacts) catch { |
|
| 1387 | 1351 | throw super::error(&["package encoding storage exhausted", pkg.name]); |
|
| 1388 | 1352 | }; |
|
| 1389 | - | let image: *[u8] = &finalImage[..]; |
|
| 1390 | - | let identity = linkage::imageId(image); |
|
| 1391 | - | let metadata = try artifact::metadata( |
|
| 1392 | - | &result, inputs, identity, head.take(), work.functions |
|
| 1393 | - | ) catch { |
|
| 1394 | - | throw super::error(&["package encoding storage exhausted", pkg.name]); |
|
| 1395 | - | }; |
|
| 1396 | - | let text = try keep(work.artifacts, metadata) catch { |
|
| 1397 | - | throw super::error(&["package encoding storage exhausted", pkg.name]); |
|
| 1398 | - | }; |
|
| 1399 | - | set result.publication = artifact::Publication { image, inputs, identity, metadata: text }; |
|
| 1353 | + | set result.image = &finalImage[..]; |
|
| 1400 | 1354 | set work.spool = bytes; |
|
| 1401 | 1355 | } |
|
| 1402 | 1356 | let mut nativeData: *[il::Data] = &[]; |
|
| 1403 | 1357 | if let case locations::Native::Program(_) = &native { |
|
| 1404 | 1358 | set nativeData = try retainData(whole.data, work.emitter) catch { |
| 1532 | 1486 | try sourcePaths.append(module.path, work.emitter) catch { |
|
| 1533 | 1487 | throw super::Error::Other; |
|
| 1534 | 1488 | }; |
|
| 1535 | 1489 | } |
|
| 1536 | 1490 | } |
|
| 1537 | - | let publication = package.publication else panic "decode: missing package image"; |
|
| 1538 | - | let mut input = reader::new(publication.image); |
|
| 1491 | + | let image = package.image else panic "decode: missing package image"; |
|
| 1492 | + | let mut input = reader::new(image); |
|
| 1539 | 1493 | let head = try program::header(&mut input, work.emitter) catch { |
|
| 1540 | 1494 | throw super::error(&["invalid native package image", package.name]); |
|
| 1541 | 1495 | }; |
|
| 1542 | 1496 | if not mem::eq(head.name, package.name) { |
|
| 1543 | 1497 | throw super::error(&["package image name mismatch", package.name]); |
compiler/radiance/cache.rad
+14 -41
| 10 | 10 | use std::sys; |
|
| 11 | 11 | use super::artifact; |
|
| 12 | 12 | ||
| 13 | 13 | /// Bound on temporary directory reservations left by concurrent or interrupted writers. |
|
| 14 | 14 | constant TEMPORARY_SLOTS: u32 = 4096; |
|
| 15 | - | /// Initial storage for the cache root directory listing. |
|
| 16 | - | constant DIRECTORY_BYTES: u32 = 65536; |
|
| 17 | 15 | /// Hexadecimal characters in a 256-bit cache key. |
|
| 18 | 16 | constant KEY_LENGTH: u32 = 64; |
|
| 19 | 17 | /// Suffix that identifies a private cache publication directory. |
|
| 20 | 18 | constant TEMPORARY_MARKER: *[u8] = ".tmp."; |
|
| 21 | 19 |
| 27 | 25 | throw sys::Error::TooLarge; |
|
| 28 | 26 | }; |
|
| 29 | 27 | return try artifact::join(arena, &[root, "/", &name[..]]); |
|
| 30 | 28 | } |
|
| 31 | 29 | ||
| 32 | - | /// Load a cache entry using its recorded content identities. |
|
| 33 | - | /// Missing files and invalid metadata are misses; unrelated I/O errors propagate. |
|
| 30 | + | /// Load the cache entry of request `key`, trusting its metadata identity. |
|
| 31 | + | /// Missing files and invalid images are misses; unrelated I/O errors propagate. |
|
| 34 | 32 | export fn lookup( |
|
| 35 | 33 | root: *[u8], |
|
| 36 | 34 | name: *[u8], |
|
| 37 | 35 | key: binary::Id, |
|
| 38 | 36 | scratch: *heap::Heap, |
| 50 | 48 | if error == sys::Error::NotFound or error == sys::Error::Invalid { |
|
| 51 | 49 | return nil; |
|
| 52 | 50 | } |
|
| 53 | 51 | throw error; |
|
| 54 | 52 | }; |
|
| 55 | - | let publication = result.publication else panic "lookup: missing publication"; |
|
| 56 | - | if publication.inputs <> key { |
|
| 57 | - | unsafe { |
|
| 58 | - | heap::rewind(arena, saved); |
|
| 59 | - | } |
|
| 60 | - | return nil; |
|
| 61 | - | } |
|
| 62 | 53 | return result; |
|
| 63 | 54 | } |
|
| 64 | 55 | ||
| 65 | 56 | /// Reserve a private directory with an atomic creation operation. |
|
| 66 | 57 | fn temporary(base: *[u8], arena: &heap::Heap) -> *[u8] throws (sys::Error) { |
| 90 | 81 | export fn clean(root: *[u8], arena: &heap::Heap) throws (sys::Error) { |
|
| 91 | 82 | let info = try fs::stat(root) else return; |
|
| 92 | 83 | if info.kind <> fs::Kind::Directory { |
|
| 93 | 84 | throw sys::Error::Invalid; |
|
| 94 | 85 | } |
|
| 95 | - | let mut capacity = DIRECTORY_BYTES; |
|
| 96 | - | let mut listing: *[u8] = ""; |
|
| 97 | - | loop { |
|
| 98 | - | let saved = heap::mark(arena); |
|
| 99 | - | let mut bytes: *mut [u8] = &mut []; |
|
| 100 | - | try bytes.resize(capacity, 0, arena) catch { |
|
| 101 | - | throw sys::Error::TooLarge; |
|
| 102 | - | }; |
|
| 103 | - | set listing = try fs::listDir(root, bytes.take()) catch error { |
|
| 104 | - | if error <> sys::Error::TooLarge or capacity > mem::U32_MAX / 2 { |
|
| 105 | - | throw error; |
|
| 106 | - | } |
|
| 107 | - | // A failed listing retains no pointers into the temporary buffer. |
|
| 108 | - | unsafe { |
|
| 109 | - | heap::rewind(arena, saved); |
|
| 110 | - | } |
|
| 111 | - | set capacity *= 2; |
|
| 112 | - | continue; |
|
| 113 | - | }; |
|
| 114 | - | break; |
|
| 115 | - | } |
|
| 86 | + | let listing = try artifact::list(root, arena); |
|
| 116 | 87 | let mut names = mem::split(listing, 0); |
|
| 117 | 88 | while let name = names.next() { |
|
| 118 | 89 | if name.len < KEY_LENGTH { |
|
| 119 | 90 | continue; |
|
| 120 | 91 | } |
| 143 | 114 | heap::rewind(arena, saved); |
|
| 144 | 115 | } |
|
| 145 | 116 | } |
|
| 146 | 117 | } |
|
| 147 | 118 | ||
| 148 | - | /// Write a private complete set, then make it visible with one directory rename. |
|
| 119 | + | /// Write a private complete set for request `key`, then make it visible with one |
|
| 120 | + | /// directory rename. |
|
| 149 | 121 | export fn publish( |
|
| 150 | - | root: *[u8], package: &artifact::Package, scratch: *heap::Heap, arena: &heap::Heap |
|
| 122 | + | root: *[u8], |
|
| 123 | + | key: binary::Id, |
|
| 124 | + | package: &artifact::Package, |
|
| 125 | + | scratch: *heap::Heap, |
|
| 126 | + | arena: &heap::Heap |
|
| 151 | 127 | ) |
|
| 152 | 128 | throws (sys::Error) |
|
| 153 | 129 | { |
|
| 154 | - | let publication = package.publication else panic "publish: missing publication"; |
|
| 155 | - | let path = try entry(root, publication.inputs, arena); |
|
| 130 | + | let path = try entry(root, key, arena); |
|
| 156 | 131 | try artifact::directories(try artifact::join(arena, &[root, "/"])); |
|
| 157 | 132 | let stage = try temporary(path, arena); |
|
| 158 | 133 | try artifact::write(package, stage, arena) catch error { |
|
| 159 | 134 | try fs::removeTree(stage, arena) catch {}; |
|
| 160 | 135 | throw error; |
|
| 161 | 136 | }; |
|
| 162 | - | try install(root, path, stage, package, scratch, arena) catch error { |
|
| 137 | + | try install(root, key, path, stage, package, scratch, arena) catch error { |
|
| 163 | 138 | try fs::removeTree(stage, arena) catch {}; |
|
| 164 | 139 | throw error; |
|
| 165 | 140 | }; |
|
| 166 | 141 | } |
|
| 167 | 142 | ||
| 168 | 143 | /// Install one complete set, retrying a concurrent repair or publication. |
|
| 169 | 144 | fn install( |
|
| 170 | 145 | root: *[u8], |
|
| 146 | + | key: binary::Id, |
|
| 171 | 147 | path: *[u8], |
|
| 172 | 148 | stage: *[u8], |
|
| 173 | 149 | package: &artifact::Package, |
|
| 174 | 150 | scratch: *heap::Heap, |
|
| 175 | 151 | arena: &heap::Heap |
|
| 176 | 152 | ) throws (sys::Error) { |
|
| 177 | - | let publication = package.publication else panic "install: missing publication"; |
|
| 178 | 153 | for _ in 0 .. TEMPORARY_SLOTS { |
|
| 179 | 154 | try fs::rename(stage, path) catch error { |
|
| 180 | 155 | if error <> sys::Error::NotEmpty |
|
| 181 | 156 | and error <> sys::Error::Exists |
|
| 182 | 157 | and error <> sys::Error::Invalid |
| 184 | 159 | throw error; |
|
| 185 | 160 | } |
|
| 186 | 161 | if try fs::stat(path) == nil { |
|
| 187 | 162 | continue; |
|
| 188 | 163 | } |
|
| 189 | - | let existing = try lookup( |
|
| 190 | - | root, package.name, publication.inputs, scratch, arena |
|
| 191 | - | ); |
|
| 164 | + | let existing = try lookup(root, package.name, key, scratch, arena); |
|
| 192 | 165 | if existing <> nil { |
|
| 193 | 166 | try fs::removeTree(stage, arena); |
|
| 194 | 167 | return; |
|
| 195 | 168 | } |
|
| 196 | 169 | // Quarantine the damaged set before installing its complete replacement. |
compiler/radiance/link.rad
+38 -27
| 24 | 24 | image: binary::Package, |
|
| 25 | 25 | /// Provider indices in the image's dependency order. |
|
| 26 | 26 | dependencies: *[u32], |
|
| 27 | 27 | } |
|
| 28 | 28 | ||
| 29 | - | /// Construct one package head with typed aliases and dependency metadata hashes. |
|
| 30 | - | /// Function records remain in the spool. |
|
| 29 | + | /// Construct one package head with typed imports and dependency metadata hashes. |
|
| 30 | + | /// The dependencies are the declared and the referenced providers, in provider |
|
| 31 | + | /// order. Function records remain in the spool. |
|
| 31 | 32 | export fn prepare( |
|
| 32 | 33 | spool: &mut codegen::RilSpool, |
|
| 33 | 34 | whole: &il::Program, |
|
| 34 | 35 | origins: &[lower::Origin], |
|
| 35 | 36 | providers: &[Provider], |
|
| 37 | + | declared: &[u16], |
|
| 36 | 38 | references: &[lower::Reference], |
|
| 37 | 39 | active: u16 |
|
| 38 | 40 | ) -> Plan throws (binary::Error) { |
|
| 39 | 41 | let arena = spool.arena; |
|
| 40 | 42 | for item, i in whole.data { |
| 50 | 52 | ) catch { |
|
| 51 | 53 | throw binary::Error::Storage; |
|
| 52 | 54 | }; |
|
| 53 | 55 | } |
|
| 54 | 56 | } |
|
| 55 | - | let mut dependencies: *mut [*[u8]] = &mut []; |
|
| 56 | - | let mut indices: *mut [u32] = &mut []; |
|
| 57 | - | let mut pins: *mut [binary::Id] = &mut []; |
|
| 58 | - | let mut imports: *mut [binary::Import] = &mut []; |
|
| 57 | + | // Position of each provider in the dependency table, once it is used. |
|
| 58 | + | let mut positions: *mut [?u32] = &mut []; |
|
| 59 | + | try positions.resize(providers.len, nil, arena) catch { |
|
| 60 | + | throw binary::Error::Storage; |
|
| 61 | + | }; |
|
| 62 | + | for id in declared { |
|
| 63 | + | if id as u32 >= providers.len { |
|
| 64 | + | throw binary::Error::Symbol; |
|
| 65 | + | } |
|
| 66 | + | set positions[id as u32] = 0; |
|
| 67 | + | } |
|
| 59 | 68 | for reference in references { |
|
| 60 | 69 | if reference.package <> active { |
|
| 61 | 70 | throw binary::Error::Invalid; |
|
| 62 | 71 | } |
|
| 63 | 72 | let owner = reference.provider as u32; |
|
| 64 | 73 | if owner >= providers.len { |
|
| 65 | 74 | throw binary::Error::Symbol; |
|
| 66 | 75 | } |
|
| 67 | - | let provider = &providers[owner]; |
|
| 68 | - | if binary::find(provider.exports, reference.name) == nil { |
|
| 76 | + | if binary::find(providers[owner].exports, reference.name) == nil { |
|
| 69 | 77 | throw binary::Error::Symbol; |
|
| 70 | 78 | } |
|
| 71 | - | let mut dependency: ?u32 = nil; |
|
| 72 | - | for k in 0 .. indices.len { |
|
| 73 | - | if indices[k] == owner { |
|
| 74 | - | set dependency = k; |
|
| 75 | - | break; |
|
| 76 | - | } |
|
| 77 | - | } |
|
| 78 | - | if dependency == nil { |
|
| 79 | - | set dependency = dependencies.len; |
|
| 80 | - | try dependencies.append(provider.name, arena) catch { |
|
| 81 | - | throw binary::Error::Storage; |
|
| 82 | - | }; |
|
| 83 | - | try pins.append(provider.metadata, arena) catch { |
|
| 84 | - | throw binary::Error::Storage; |
|
| 85 | - | }; |
|
| 86 | - | try indices.append(owner, arena) catch { |
|
| 87 | - | throw binary::Error::Storage; |
|
| 88 | - | }; |
|
| 79 | + | set positions[owner] = 0; |
|
| 80 | + | } |
|
| 81 | + | let mut dependencies: *mut [*[u8]] = &mut []; |
|
| 82 | + | let mut indices: *mut [u32] = &mut []; |
|
| 83 | + | let mut pins: *mut [binary::Id] = &mut []; |
|
| 84 | + | for provider, i in providers { |
|
| 85 | + | if positions[i] == nil { |
|
| 86 | + | continue; |
|
| 89 | 87 | } |
|
| 90 | - | let selected = dependency else panic "prepare: dependency index"; |
|
| 88 | + | set positions[i] = indices.len; |
|
| 89 | + | try dependencies.append(provider.name, arena) catch { |
|
| 90 | + | throw binary::Error::Storage; |
|
| 91 | + | }; |
|
| 92 | + | try pins.append(provider.metadata, arena) catch { |
|
| 93 | + | throw binary::Error::Storage; |
|
| 94 | + | }; |
|
| 95 | + | try indices.append(i, arena) catch { |
|
| 96 | + | throw binary::Error::Storage; |
|
| 97 | + | }; |
|
| 98 | + | } |
|
| 99 | + | let mut imports: *mut [binary::Import] = &mut []; |
|
| 100 | + | for reference in references { |
|
| 101 | + | let selected = positions[reference.provider as u32] else panic "prepare: dependency index"; |
|
| 91 | 102 | try imports.append( |
|
| 92 | 103 | binary::Import { |
|
| 93 | 104 | dependency: selected, |
|
| 94 | 105 | exported: reference.name, |
|
| 95 | 106 | kind: reference.kind, |
test/Makefile
+2 -13
| 62 | 62 | @$(call integration,host) |
|
| 63 | 63 | ||
| 64 | 64 | package-test: $(INTEGRATION) |
|
| 65 | 65 | @$(call integration,packages) |
|
| 66 | 66 | ||
| 67 | - | module-test: $(RAD_BIN) test/compiler/discovery.rv64 test/compiler/metadata.rv64 \ |
|
| 68 | - | test/compiler/values.rv64 |
|
| 67 | + | module-test: $(RAD_BIN) test/compiler/discovery.rv64 test/compiler/metadata.rv64 |
|
| 69 | 68 | @$(HOSTED) test/compiler/discovery.rv64 |
|
| 70 | 69 | @$(HOSTED) test/compiler/metadata.rv64 |
|
| 71 | - | @$(HOSTED) test/compiler/values.rv64 |
|
| 72 | 70 | ||
| 73 | 71 | # Standard Library Tests |
|
| 74 | 72 | ||
| 75 | 73 | STD_LIB_TEST := lib/std.test.rv64 |
|
| 76 | 74 |
| 135 | 133 | test/tests/%.rv64: test/tests/%.ras test/runner.rv64 test/Makefile |
|
| 136 | 134 | @$(HOSTED) test/runner.rv64 assemble $< $@ |
|
| 137 | 135 | ||
| 138 | 136 | clean-bin-test: |
|
| 139 | 137 | @rm -f $(TEST_TOOLS) $(INTEGRATION) $(BIN_TEST_BIN) \ |
|
| 140 | - | test/compiler/discovery.rv64 test/compiler/metadata.rv64 test/compiler/values.rv64 \ |
|
| 138 | + | test/compiler/discovery.rv64 test/compiler/metadata.rv64 \ |
|
| 141 | 139 | $(wildcard test/tests/*.rv64.debug test/tests/*.rv64.s) \ |
|
| 142 | 140 | $(TEST_TOOLS:.rv64=.rv64.debug) $(TEST_TOOLS:.rv64=.rv64.s) \ |
|
| 143 | 141 | $(TEST_TOOLS:.rv64=.rv64.o) |
|
| 144 | 142 | ||
| 145 | 143 | # Integer trap contexts and nested machine interrupts. |
| 230 | 228 | test/compiler/metadata.rv64: test/compiler/metadata.rad compiler/radiance/metadata.rad \ |
|
| 231 | 229 | test/compiler/metachecks.manifest $(STD_LIB) $(STD_MANIFEST) test/std.manifest $(RAD_BIN) test/Makefile |
|
| 232 | 230 | @$(RADIANCE) $(STD) -pkg metadata -manifest test/std.manifest -mod compiler/radiance/metadata.rad \ |
|
| 233 | 231 | -pkg metachecks -mod test/compiler/metadata.rad -entry metachecks -o $@ |
|
| 234 | 232 | ||
| 235 | - | VALUE_FIXTURES := $(wildcard test/compiler/values/*) |
|
| 236 | - | ||
| 237 | - | # Read typed artifact documents through the production reader module. |
|
| 238 | - | test/compiler/values.rv64: $(VALUE_FIXTURES) test/compiler/values.rad compiler/radiance/artifact/value.rad \ |
|
| 239 | - | test/compiler/valuechecks.manifest $(STD_LIB) $(STD_MANIFEST) test/std.manifest $(RAD_BIN) test/Makefile |
|
| 240 | - | @$(RADIANCE) $(STD) -pkg value -manifest test/std.manifest -mod compiler/radiance/artifact/value.rad \ |
|
| 241 | - | -pkg valuechecks -mod test/compiler/values.rad \ |
|
| 242 | - | $(patsubst %,-embed %,$(VALUE_FIXTURES)) -entry valuechecks -o $@ |
|
| 243 | - | ||
| 244 | 233 | .PHONY: test $(TESTS) $(SMP_TESTS) $(CYCLES_TESTS) clean-std-test clean-bin-test |
test/compiler/valuechecks.manifest
deleted
+0 -2
| 1 | - | std |
|
| 2 | - | value |
test/compiler/values.rad
deleted
+0 -55
| 1 | - | //! Artifact document reader checks. |
|
| 2 | - | ||
| 3 | - | use std::mem; |
|
| 4 | - | use std::mem::heap; |
|
| 5 | - | use value; |
|
| 6 | - | ||
| 7 | - | /// Declarations of the test documents. |
|
| 8 | - | constant SCHEMA: *[u8] = @embed("values/schema.rad"); |
|
| 9 | - | ||
| 10 | - | /// Documents that type-check as `Root` with count 3, name `ab`, and items named `a`. |
|
| 11 | - | constant ACCEPTED: [*[u8]; 4] = [ |
|
| 12 | - | @embed("values/typed.metadata"), |
|
| 13 | - | @embed("values/inferred.metadata"), |
|
| 14 | - | @embed("values/byte_arrays.metadata"), |
|
| 15 | - | @embed("values/repeat.metadata"), |
|
| 16 | - | ]; |
|
| 17 | - | ||
| 18 | - | /// Documents that do not type-check as `Root`, or hold more than one expression. |
|
| 19 | - | constant REJECTED: [*[u8]; 8] = [ |
|
| 20 | - | @embed("values/string_count.metadata"), |
|
| 21 | - | @embed("values/negative_count.metadata"), |
|
| 22 | - | @embed("values/overflow_count.metadata"), |
|
| 23 | - | @embed("values/duplicate_field.metadata"), |
|
| 24 | - | @embed("values/extra_field.metadata"), |
|
| 25 | - | @embed("values/wrong_record.metadata"), |
|
| 26 | - | @embed("values/scalar.metadata"), |
|
| 27 | - | @embed("values/trailing_semicolon.metadata"), |
|
| 28 | - | ]; |
|
| 29 | - | ||
| 30 | - | /// The reader follows byte arrays and repeat literals to typed values, and rejects |
|
| 31 | - | /// documents that do not type-check against the schema. |
|
| 32 | - | @default unsafe fn main() -> u32 { |
|
| 33 | - | static storage: [u8; 8388608] = [0; 8388608]; |
|
| 34 | - | let arena = heap::over(&mut storage[..]); |
|
| 35 | - | for document, i in ACCEPTED { |
|
| 36 | - | let mark = heap::mark(arena); |
|
| 37 | - | let image = try! value::read(SCHEMA, document, "document.rad", "Root", arena); |
|
| 38 | - | assert try! image.number(image.root, "count") == 3; |
|
| 39 | - | assert mem::eq(try! image.bytes(image.root, "name"), "ab"); |
|
| 40 | - | let items = try! image.items(image.root, "items"); |
|
| 41 | - | assert items.len == (2 if i == 3 else 1); |
|
| 42 | - | for j in 0 .. items.len { |
|
| 43 | - | let item = try! image.at(&items, j); |
|
| 44 | - | assert mem::eq(try! image.bytes(item, "name"), "a"); |
|
| 45 | - | assert try! image.variant(item, "role") == 1; |
|
| 46 | - | } |
|
| 47 | - | heap::rewind(arena, mark); |
|
| 48 | - | } |
|
| 49 | - | for document in REJECTED { |
|
| 50 | - | let mark = heap::mark(arena); |
|
| 51 | - | assert try? value::read(SCHEMA, document, "document.rad", "Root", arena) == nil; |
|
| 52 | - | heap::rewind(arena, mark); |
|
| 53 | - | } |
|
| 54 | - | return 0; |
|
| 55 | - | } |
test/compiler/values/byte_arrays.metadata
deleted
+0 -1
| 1 | - | Root { count: 3, name: &[97, 98], items: &[Item { name: &[97], role: Role::Second }] } |
test/compiler/values/duplicate_field.metadata
deleted
+0 -1
| 1 | - | Root { count: 3, count: 3, name: "ab", items: &[] } |
test/compiler/values/extra_field.metadata
deleted
+0 -1
| 1 | - | Root { count: 3, name: "ab", items: &[], extra: 1 } |
test/compiler/values/inferred.metadata
deleted
+0 -1
| 1 | - | { count: 4 - 2 + 1, name: "ab", items: &[Item { name: "a", role: Role::Second }] } |
test/compiler/values/negative_count.metadata
deleted
+0 -1
| 1 | - | Root { count: -1, name: "ab", items: &[] } |
test/compiler/values/overflow_count.metadata
deleted
+0 -1
| 1 | - | Root { count: 4294967296, name: "ab", items: &[] } |
test/compiler/values/repeat.metadata
deleted
+0 -1
| 1 | - | Root { count: 3, name: "ab", items: &[Item { name: "a", role: Role::Second }; 2] } |
test/compiler/values/scalar.metadata
deleted
+0 -1
| 1 | - | 42 |
test/compiler/values/schema.rad
deleted
+0 -5
| 1 | - | export union Role: Copy { |
|
| 2 | - | First, Second, |
|
| 3 | - | } |
|
| 4 | - | export record Item: Copy { name: *[u8], role: Role } |
|
| 5 | - | export record Root: Copy { count: u32, name: *[u8], items: *[Item] } |
test/compiler/values/string_count.metadata
deleted
+0 -1
| 1 | - | Root { count: "3", name: "ab", items: &[] } |
test/compiler/values/trailing_semicolon.metadata
deleted
+0 -1
| 1 | - | Root { count: 3, name: "ab", items: &[] }; |
test/compiler/values/typed.metadata
deleted
+0 -1
| 1 | - | Root { count: 3, name: "ab", items: &[Item { name: "a", role: Role::Second }] } |
test/compiler/values/wrong_record.metadata
deleted
+0 -1
| 1 | - | Item { name: "a", role: Role::Second } |
test/integration/packages.rad
+30 -138
| 347 | 347 | h.add(&["clean", "-cache"]); |
|
| 348 | 348 | let invalid = h.cc(h.take(), h.out(&["clean-invalid.log"])); |
|
| 349 | 349 | h.run("reject a missing cache path", invalid, process::Outcome::Exited(1), &[]); |
|
| 350 | 350 | } |
|
| 351 | 351 | ||
| 352 | - | /// Relocated source packages share cache entries and portable debug information. |
|
| 352 | + | /// Relocated and renamed source files share cache entries and portable debug information. |
|
| 353 | 353 | fn relocation(h: &mut Harness) throws (sys::Error) { |
|
| 354 | 354 | let root = h.path(&["relocation"]); |
|
| 355 | 355 | let first = h.join(&[root, "/0/exported"]); |
|
| 356 | 356 | let second = h.join(&[root, "/1/exported"]); |
|
| 357 | 357 |
| 403 | 403 | "debug locations use current source paths", |
|
| 404 | 404 | h.join(&[native, ".debug"]), |
|
| 405 | 405 | harness::Expect::Contains(path) |
|
| 406 | 406 | ); |
|
| 407 | 407 | } |
|
| 408 | - | for path in &["portable.interface/package.metadata", "portable.interface/locations"] { |
|
| 409 | - | h.expect( |
|
| 410 | - | "artifacts omit checkout paths", |
|
| 411 | - | h.join(&[output, "/", path]), |
|
| 412 | - | harness::Expect::Matching { text: root, count: 0 } |
|
| 413 | - | ); |
|
| 414 | - | } |
|
| 415 | - | if i == 1 { |
|
| 408 | + | h.expect( |
|
| 409 | + | "artifacts omit checkout paths", |
|
| 410 | + | h.join(&[output, "/portable.native/locations"]), |
|
| 411 | + | harness::Expect::Matching { text: root, count: 0 } |
|
| 412 | + | ); |
|
| 413 | + | if i > 0 { |
|
| 414 | + | // Renamed root and assembly files keep their logical module and storage paths. |
|
| 416 | 415 | h.expect("relocation reuses compiled packages", report, harness::Expect::Contains( |
|
| 417 | 416 | "cache hit portable" |
|
| 418 | 417 | )); |
|
| 419 | 418 | for path in &[ |
|
| 420 | - | "portable.ril.bin", "portable.interface/package.metadata", |
|
| 421 | - | "portable.interface/locations", |
|
| 419 | + | "portable.ril.bin", "portable.native/locations", "portable.native/0.ras", |
|
| 420 | + | "portable.interface/asset.bin", |
|
| 422 | 421 | ] { |
|
| 423 | 422 | h.expect( |
|
| 424 | 423 | "relocation preserves package artifacts", |
|
| 425 | - | h.join(&[second, "/", path]), |
|
| 424 | + | h.join(&[output, "/", path]), |
|
| 426 | 425 | harness::Expect::Same(h.join(&[first, "/", path])) |
|
| 427 | 426 | ); |
|
| 428 | 427 | } |
|
| 429 | - | } else if i == 2 { |
|
| 430 | - | h.expect( |
|
| 431 | - | "relative origin edits rebuild artifact metadata", |
|
| 432 | - | report, |
|
| 433 | - | harness::Expect::Matching { text: "cache hit portable", count: 0 } |
|
| 434 | - | ); |
|
| 435 | - | h.expect( |
|
| 436 | - | "retain relative source origins", |
|
| 437 | - | h.join(&[output, "/portable.interface/package.metadata"]), |
|
| 438 | - | harness::Expect::Contains("origin: \"renamed.ras\"") |
|
| 439 | - | ); |
|
| 440 | 428 | } |
|
| 441 | 429 | } |
|
| 442 | 430 | let image = h.join(&[root, "/portable.rv64"]); |
|
| 443 | 431 | h.add(&["-no-cache", "-debug", "-pkg", "portable", "-artifact", second, "-o", image]); |
|
| 444 | 432 | if h.compile("link a relocated artifact", h.out(&["relocation-link.log"])) { |
|
| 445 | 433 | execute(h, image); |
|
| 446 | 434 | } |
|
| 447 | 435 | } |
|
| 448 | 436 | ||
| 449 | - | /// Record compiler provenance without making it an artifact compatibility check. |
|
| 450 | - | fn producer(h: &mut Harness, exported: *[u8]) throws (sys::Error) { |
|
| 451 | - | let path = h.join(&[exported, "/dep.interface/package.metadata"]); |
|
| 452 | - | let original = try h.load(path); |
|
| 453 | - | let field = "compiler: \"radiance-1.0.0\""; |
|
| 454 | - | if not h.expect("record the artifact compiler", path, harness::Expect::Contains(field)) { |
|
| 455 | - | return; |
|
| 456 | - | } |
|
| 457 | - | let start = mem::find(original, field) else panic "producer: missing compiler field"; |
|
| 458 | - | try fs::writeFileParts(path, &[ |
|
| 459 | - | &original[.. start], "compiler: \"another-compiler-2.0.0\"", &original[start + field.len ..] |
|
| 460 | - | ]); |
|
| 461 | - | let output = h.path(&["producer-exported"]); |
|
| 462 | - | h.add(&["-no-cache", "-pkg", "dep", "-artifact", exported, "-ril", output]); |
|
| 463 | - | if h.compile("read compatible artifacts from another producer", h.out(&["producer.log"])) { |
|
| 464 | - | h.expect( |
|
| 465 | - | "preserve original artifact provenance", |
|
| 466 | - | h.join(&[output, "/dep.interface/package.metadata"]), |
|
| 467 | - | harness::Expect::Contains("compiler: \"another-compiler-2.0.0\"") |
|
| 468 | - | ); |
|
| 469 | - | } |
|
| 470 | - | try fs::writeFile(path, original); |
|
| 437 | + | /// Print the compiler identity. |
|
| 438 | + | fn version(h: &mut Harness) { |
|
| 471 | 439 | h.add(&["-version"]); |
|
| 472 | 440 | h.compile("print the compiler identity", h.out(&["compiler-version.log"])); |
|
| 473 | 441 | h.expect( |
|
| 474 | 442 | "compiler identity is readable", |
|
| 475 | 443 | h.path(&["compiler-version.log"]), |
| 522 | 490 | ); |
|
| 523 | 491 | let private = h.cc(h.take(), h.out(&["private-alias.log"])); |
|
| 524 | 492 | h.run("preserve private package alias visibility", private, process::Outcome::Exited(1), &[]); |
|
| 525 | 493 | } |
|
| 526 | 494 | ||
| 527 | - | /// Preserve assembly source paths through string escapes in metadata. |
|
| 528 | - | fn paths(h: &mut Harness) throws (sys::Error) { |
|
| 529 | - | let code = h.path(&["paths.rad"]); |
|
| 530 | - | let root = h.path(&["paths-exported"]); |
|
| 531 | - | let mut name: *mut [u8] = &mut []; |
|
| 532 | - | try! name.extend(h.path(&["assembly \"\\"]), h.arena); |
|
| 533 | - | try! name.append(255, h.arena); |
|
| 534 | - | try! name.append(31, h.arena); |
|
| 535 | - | try! name.extend("\n.ras", h.arena); |
|
| 536 | - | let assembly = &name[..]; |
|
| 537 | - | try fs::writeFile(code, "@default fn main() -> u32 { return 0; }\n"); |
|
| 538 | - | try fs::writeFile(assembly, ""); |
|
| 539 | - | h.add( |
|
| 540 | - | &[ |
|
| 541 | - | "-no-cache", "-pkg", "paths", "-mod", code, "-mod", assembly, |
|
| 542 | - | "-entry", "paths", "-ril", root, |
|
| 543 | - | ] |
|
| 544 | - | ); |
|
| 545 | - | if not h.compile("export escaped assembly paths", h.out(&["paths-export.log"])) { |
|
| 546 | - | return; |
|
| 547 | - | } |
|
| 548 | - | h.expect( |
|
| 549 | - | "metadata uses hex escapes", |
|
| 550 | - | h.join(&[root, "/paths.interface/package.metadata"]), |
|
| 551 | - | harness::Expect::Contains("\\xff\\x1f\\n.ras") |
|
| 552 | - | ); |
|
| 553 | - | let metadataPath = h.join(&[root, "/paths.interface/package.metadata"]); |
|
| 554 | - | let original = try h.load(metadataPath); |
|
| 555 | - | let role = mem::find(original, "FileRole::Assembly") else panic; |
|
| 556 | - | for selected in ["FileRole::Startup", "FileRole::Debug"] { |
|
| 557 | - | try fs::writeFileParts(metadataPath, &[ |
|
| 558 | - | &original[.. role], selected, &original[role + "FileRole::Assembly".len ..] |
|
| 559 | - | ]); |
|
| 560 | - | h.add(&["-no-cache", "-pkg", "paths", "-artifact", root]); |
|
| 561 | - | h.compile("read typed metadata file roles", h.out(&["role-value.log"])); |
|
| 562 | - | } |
|
| 563 | - | try fs::writeFile(metadataPath, original); |
|
| 564 | - | let image = h.path(&["paths.rv64"]); |
|
| 565 | - | h.add(&["-no-cache", "-pkg", "paths", "-artifact", root, "-o", image]); |
|
| 566 | - | if h.compile("load escaped assembly paths", h.out(&["paths-load.log"])) { |
|
| 567 | - | execute(h, image); |
|
| 568 | - | } |
|
| 569 | - | } |
|
| 570 | - | ||
| 571 | - | /// Reject malformed metadata and content identities before loading an artifact. |
|
| 572 | - | /// The reader's typing rules are checked in `test/compiler/values.rad`. |
|
| 573 | - | fn metadata(h: &mut Harness, root: *[u8]) throws (sys::Error) { |
|
| 574 | - | let path = h.join(&[root, "/dep.interface/package.metadata"]); |
|
| 575 | - | let original = try h.load(path); |
|
| 576 | - | for byte in original { |
|
| 577 | - | assert byte == '\n' or (byte >= 32 and byte <= 126); |
|
| 578 | - | } |
|
| 579 | - | assert mem::startsWith(original, "Package {\n"); |
|
| 580 | - | let version = mem::find(original, " version: 1,") else panic; |
|
| 581 | - | try fs::writeFileParts(path, &[ |
|
| 582 | - | &original[.. version], " version: \"1\",", &original[version + " version: 1,".len ..] |
|
| 583 | - | ]); |
|
| 495 | + | /// Reject explicit artifacts whose interface tree differs from the one that the image's |
|
| 496 | + | /// metadata identity covers. |
|
| 497 | + | fn integrity(h: &mut Harness, root: *[u8]) throws (sys::Error) { |
|
| 498 | + | let extra = h.join(&[root, "/dep.interface/extra.txt"]); |
|
| 499 | + | try fs::writeFile(extra, ""); |
|
| 584 | 500 | h.add(&["-no-cache", "-pkg", "dep", "-artifact", root]); |
|
| 585 | - | let mistyped = h.cc(h.take(), h.out(&["invalid-metadata.log"])); |
|
| 586 | - | h.run("reject mistyped metadata fields", mistyped, process::Outcome::Exited(1), &[ |
|
| 587 | - | harness::Expect::Contains("type mismatch") |
|
| 588 | - | ]); |
|
| 589 | - | try fs::writeFileParts(path, &[original, "extra field\n"]); |
|
| 501 | + | let added = h.cc(h.take(), h.out(&["extra-interface.log"])); |
|
| 502 | + | h.run("reject unlisted interface files", added, process::Outcome::Exited(1), &[]); |
|
| 503 | + | try fs::remove(extra); |
|
| 504 | + | let module = h.join(&[root, "/dep.interface/dep.rid"]); |
|
| 505 | + | let original = try h.load(module); |
|
| 506 | + | try fs::remove(module); |
|
| 590 | 507 | h.add(&["-no-cache", "-pkg", "dep", "-artifact", root]); |
|
| 591 | - | let command = h.cc(h.take(), h.out(&["trailing-metadata.log"])); |
|
| 592 | - | h.run("reject trailing metadata text", command, process::Outcome::Exited(1), &[]); |
|
| 593 | - | let image = mem::find(original, "image: \"") else panic; |
|
| 594 | - | let digit = image + "image: \"".len; |
|
| 595 | - | // An identity has exactly 64 hexadecimal digits. |
|
| 596 | - | for replaced in ["g", "-", ""] { |
|
| 597 | - | try fs::writeFileParts(path, &[&original[.. digit], replaced, &original[digit + 1 ..]]); |
|
| 598 | - | h.add(&["-no-cache", "-pkg", "dep", "-artifact", root]); |
|
| 599 | - | let command = h.cc(h.take(), h.out(&["invalid-hex.log"])); |
|
| 600 | - | h.run("reject invalid metadata identities", command, process::Outcome::Exited(1), &[]); |
|
| 601 | - | } |
|
| 602 | - | try fs::writeFile(path, original); |
|
| 508 | + | let missing = h.cc(h.take(), h.out(&["missing-interface.log"])); |
|
| 509 | + | h.run("reject a missing root interface", missing, process::Outcome::Exited(1), &[]); |
|
| 510 | + | try fs::writeFile(module, original); |
|
| 603 | 511 | } |
|
| 604 | 512 | ||
| 605 | 513 | /// Implementation edits preserve client images across a transitive dependency graph. |
|
| 606 | 514 | fn metadataReuse(h: &mut Harness) throws (sys::Error) { |
|
| 607 | 515 | let provider = h.path(&["reuse.rad"]); |
| 652 | 560 | ); |
|
| 653 | 561 | } |
|
| 654 | 562 | } |
|
| 655 | 563 | } |
|
| 656 | 564 | let root = h.path(&["reuse-second"]); |
|
| 657 | - | let lock = h.join(&[root, "/package.lock"]); |
|
| 658 | - | let original = try h.load(lock); |
|
| 659 | - | h.expect("export exact package selections", lock, harness::Expect::Contains("LockedPackage")); |
|
| 660 | - | let start = mem::find(original, "image: \"") else panic "metadataReuse: locked image"; |
|
| 661 | - | let digit = start + "image: \"".len; |
|
| 662 | - | let changed = "0" if original[digit] <> '0' else "1"; |
|
| 663 | - | try fs::writeFileParts(lock, &[&original[.. digit], changed, &original[digit + 1 ..]]); |
|
| 664 | - | h.add(&["-no-cache", "-pkg", "consumer", "-artifact", root]); |
|
| 665 | - | let rejected = h.cc(h.take(), h.out(&["lock-mismatch.log"])); |
|
| 666 | - | h.run("reject an image outside its lockfile", rejected, process::Outcome::Exited(1), &[]); |
|
| 667 | - | try fs::writeFile(lock, original); |
|
| 668 | 565 | h.add(&["-no-cache", "-pkg", "consumer", "-artifact", root, "-o", h.path(&["reuse.rv64"])]); |
|
| 669 | 566 | if h.compile("link an unchanged client to the selected provider", h.out(&["reuse-link.log"])) { |
|
| 670 | 567 | execute(h, h.path(&["reuse.rv64"])); |
|
| 671 | 568 | } |
|
| 672 | 569 | } |
| 746 | 643 | } |
|
| 747 | 644 | h.expect("cache bypass creates no cache", h.path(&["disabled-cache"]), harness::Expect::Absent); |
|
| 748 | 645 | h.expect( |
|
| 749 | 646 | "uncached graph uses package interfaces", |
|
| 750 | 647 | h.path(&["uncached.log"]), |
|
| 751 | - | harness::Expect::Contains("encoding interface metadata") |
|
| 648 | + | harness::Expect::Contains("encoding package tables") |
|
| 752 | 649 | ); |
|
| 753 | 650 | execute(h, h.path(&["uncached.rv64"])); |
|
| 754 | 651 | source(h, image, "-o"); |
|
| 755 | 652 | if not h.compile("compile source package graph", h.out(&["cold.log"])) { |
|
| 756 | 653 | return; |
| 774 | 671 | source(h, exported, "-ril"); |
|
| 775 | 672 | if not h.compile("export complete package closure", h.out(&["export.log"])) { |
|
| 776 | 673 | return; |
|
| 777 | 674 | } |
|
| 778 | 675 | try compilerImage(h, exported); |
|
| 779 | - | try producer(h, exported); |
|
| 780 | - | try metadata(h, exported); |
|
| 781 | - | let metadataPath = h.join(&[exported, "/dep.interface/package.metadata"]); |
|
| 782 | - | h.expect("artifact metadata is text", metadataPath, harness::Expect::Contains( |
|
| 783 | - | "version: 1," |
|
| 784 | - | )); |
|
| 676 | + | version(h); |
|
| 677 | + | try integrity(h, exported); |
|
| 785 | 678 | let declarations = h.join(&[exported, "/dep.interface/dep.rid"]); |
|
| 786 | 679 | h.expect("interface omits implementation helpers", declarations, harness::Expect::Matching { |
|
| 787 | 680 | text: "fn helper(", count: 0 |
|
| 788 | 681 | }); |
|
| 789 | 682 | h.expect( |
| 900 | 793 | try reexports(h); |
|
| 901 | 794 | try embeds(h); |
|
| 902 | 795 | try privateEmbeds(h); |
|
| 903 | 796 | try cleanCache(h); |
|
| 904 | 797 | try relocation(h); |
|
| 905 | - | try paths(h); |
|
| 906 | 798 | try large(h); |
|
| 907 | 799 | h.done(mark, "packages: source-free linking, cache reuse, and integrity checks passed"); |
|
| 908 | 800 | } |