Skip to content

Commit

Permalink
Fix wiggle issues
Browse files Browse the repository at this point in the history
  • Loading branch information
elliottt committed May 7, 2024
1 parent 8039de5 commit 0b9c0c7
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 71 deletions.
2 changes: 2 additions & 0 deletions lib/src/wiggle_abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ where
// If there's not enough room to write even a single value, that's an error.
// Write out the number of bytes necessary to fit this header value, or zero
// on overflow to signal an error condition.
drop(buf);
nwritten_out.write(value_len_with_term.try_into().unwrap_or(0))?;
return Err(Error::BufferLengthError {
buf: "buf",
Expand Down Expand Up @@ -263,6 +264,7 @@ where
types::MultiValueCursorResult::from(cursor as i64)
};

drop(buf);
nwritten_out.write(buf_offset.try_into().unwrap_or(0))?;

Ok(ending_cursor)
Expand Down
30 changes: 14 additions & 16 deletions lib/src/wiggle_abi/device_detection_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,18 @@ impl FastlyDeviceDetection for Session {
buf_len: u32,
nwritten_out: &GuestPtr<u32>,
) -> Result<(), Error> {
let user_agent_slice = user_agent
.as_bytes()
.as_slice()?
.ok_or(Error::SharedMemory)?;
let user_agent_str = std::str::from_utf8(&user_agent_slice)?;

let result = self
.device_detection_lookup(user_agent_str)
.ok_or_else(|| {
DeviceDetectionError::NoDeviceDetectionData(user_agent_str.to_string())
})?;
let result = {
let user_agent_slice = user_agent
.as_bytes()
.as_slice()?
.ok_or(Error::SharedMemory)?;
let user_agent_str = std::str::from_utf8(&user_agent_slice)?;

self.device_detection_lookup(user_agent_str)
.ok_or_else(|| {
DeviceDetectionError::NoDeviceDetectionData(user_agent_str.to_string())
})?
};

if result.len() > buf_len as usize {
return Err(Error::BufferLengthError {
Expand All @@ -52,12 +53,9 @@ impl FastlyDeviceDetection for Session {
let result_len =
u32::try_from(result.len()).expect("smaller than buf_len means it must fit");

let mut buf_ptr = buf
.as_array(result_len)
.as_slice_mut()?
.ok_or(Error::SharedMemory)?;
buf.as_array(result_len)
.copy_from_slice(result.as_bytes())?;

buf_ptr.copy_from_slice(result.as_bytes());
nwritten_out.write(result_len)?;
Ok(())
}
Expand Down
17 changes: 7 additions & 10 deletions lib/src/wiggle_abi/dictionary_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ impl FastlyDictionary for Session {
.contents()
.map_err(|err| Error::Other(err.into()))?;

let key: &str = &key.as_str()?.ok_or(Error::SharedMemory)?;
let item_bytes = dict
.get(key)
.ok_or_else(|| DictionaryError::UnknownDictionaryItem(key.to_owned()))?
.as_bytes();
let item_bytes = {
let key: &str = &key.as_str()?.ok_or(Error::SharedMemory)?;
dict.get(key)
.ok_or_else(|| DictionaryError::UnknownDictionaryItem(key.to_owned()))?
.as_bytes()
};

if item_bytes.len() > buf_len as usize {
return Err(Error::BufferLengthError {
Expand All @@ -65,11 +66,7 @@ impl FastlyDictionary for Session {
let item_len = u32::try_from(item_bytes.len())
.expect("smaller than dictionary_item_max_len means it must fit");

let mut buf_slice = buf
.as_array(item_len)
.as_slice_mut()?
.ok_or(Error::SharedMemory)?;
buf_slice.copy_from_slice(item_bytes);
buf.as_array(item_len).copy_from_slice(item_bytes)?;
Ok(item_len)
}
}
7 changes: 2 additions & 5 deletions lib/src/wiggle_abi/geo_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,8 @@ impl FastlyGeo for Session {
let result_len =
u32::try_from(result.len()).expect("smaller than value_max_len means it must fit");

let mut buf_ptr = buf
.as_array(result_len)
.as_slice_mut()?
.ok_or(Error::SharedMemory)?;
buf_ptr.copy_from_slice(result.as_bytes());
buf.as_array(result_len)
.copy_from_slice(result.as_bytes())?;
nwritten_out.write(result_len)?;
Ok(())
}
Expand Down
6 changes: 1 addition & 5 deletions lib/src/wiggle_abi/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,7 @@ impl HttpHeaders for HeaderMap<HeaderValue> {
}
let value_len =
u32::try_from(value_bytes.len()).expect("smaller than value_max_len means it must fit");
let mut value_out = value_ptr
.as_array(value_len)
.as_slice_mut()?
.ok_or(Error::SharedMemory)?;
value_out.copy_from_slice(value_bytes);
value_ptr.as_array(value_len).copy_from_slice(value_bytes)?;
nwritten_out.write(value_len)?;

Ok(())
Expand Down
53 changes: 22 additions & 31 deletions lib/src/wiggle_abi/req_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,18 @@ impl FastlyHttpReq for Session {
let octets = addr.octets();
let octets_bytes = octets.len() as u32;
debug_assert_eq!(octets_bytes, 4);
let mut octets_slice = addr_octets_ptr
addr_octets_ptr
.as_array(octets_bytes)
.as_slice_mut()?
.ok_or(Error::SharedMemory)?;
octets_slice.copy_from_slice(&octets);
.copy_from_slice(&octets)?;
Ok(octets_bytes)
}
IpAddr::V6(addr) => {
let octets = addr.octets();
let octets_bytes = octets.len() as u32;
debug_assert_eq!(octets_bytes, 16);
let mut octets_slice = addr_octets_ptr
addr_octets_ptr
.as_array(octets_bytes)
.as_slice_mut()?
.ok_or(Error::SharedMemory)?;
octets_slice.copy_from_slice(&octets);
.copy_from_slice(&octets)?;
Ok(octets_bytes)
}
}
Expand Down Expand Up @@ -125,11 +121,9 @@ impl FastlyHttpReq for Session {
let reqid_len =
u32::try_from(reqid_bytes.len()).expect("smaller u32::MAX means it must fit");

let mut reqid_slice = reqid_out
reqid_out
.as_array(reqid_len)
.as_slice_mut()?
.ok_or(Error::SharedMemory)?;
reqid_slice.copy_from_slice(&reqid_bytes);
.copy_from_slice(&reqid_bytes)?;
nwritten_out.write(reqid_len)?;
Ok(())
}
Expand Down Expand Up @@ -257,13 +251,17 @@ impl FastlyHttpReq for Session {
backend_info_mask: BackendConfigOptions,
backend_info: &GuestPtr<'a, DynamicBackendConfig<'a>>,
) -> Result<(), Error> {
let name_slice = name.as_bytes().as_slice()?.ok_or(Error::SharedMemory)?;
let name = std::str::from_utf8(&name_slice)?;
let origin_name_slice = upstream_dynamic
.as_bytes()
.as_slice()?
.ok_or(Error::SharedMemory)?;
let origin_name = std::str::from_utf8(&origin_name_slice)?;
let name = {
let name_slice = name.as_bytes().as_slice()?.ok_or(Error::SharedMemory)?;
std::str::from_utf8(&name_slice)?.to_owned()
};
let origin_name = {
let origin_name_slice = upstream_dynamic
.as_bytes()
.as_slice()?
.ok_or(Error::SharedMemory)?;
std::str::from_utf8(&origin_name_slice)?.to_owned()
};
let config = backend_info.read()?;

// If someone set our reserved bit, error. We might need it, and we don't
Expand Down Expand Up @@ -427,8 +425,8 @@ impl FastlyHttpReq for Session {
ca_certs,
};

if !self.add_backend(name, new_backend) {
return Err(Error::BackendNameRegistryError(name.to_string()));
if !self.add_backend(&name, new_backend) {
return Err(Error::BackendNameRegistryError(name));
}

Ok(())
Expand Down Expand Up @@ -570,11 +568,8 @@ impl FastlyHttpReq for Session {
let req_method_len = u32::try_from(req_method_bytes.len())
.expect("smaller than method_max_len means it must fit");

let mut buf_slice = buf
.as_array(req_method_len)
.as_slice_mut()?
.ok_or(Error::SharedMemory)?;
buf_slice.copy_from_slice(&req_method_bytes);
buf.as_array(req_method_len)
.copy_from_slice(&req_method_bytes)?;
nwritten_out.write(req_method_len)?;

Ok(())
Expand Down Expand Up @@ -615,11 +610,7 @@ impl FastlyHttpReq for Session {
let req_uri_len =
u32::try_from(req_uri_bytes.len()).expect("smaller than uri_max_len means it must fit");

let mut buf_slice = buf
.as_array(req_uri_len)
.as_slice_mut()?
.ok_or(Error::SharedMemory)?;
buf_slice.copy_from_slice(&req_uri_bytes);
buf.as_array(req_uri_len).copy_from_slice(&req_uri_bytes)?;
nwritten_out.write(req_uri_len)?;

Ok(())
Expand Down
6 changes: 2 additions & 4 deletions lib/src/wiggle_abi/secret_store_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,9 @@ impl FastlySecretStore for Session {
let plaintext_len = u32::try_from(plaintext.len())
.expect("smaller than plaintext_max_len means it must fit");

let mut plaintext_out = plaintext_buf
plaintext_buf
.as_array(plaintext_len)
.as_slice_mut()?
.ok_or(Error::SharedMemory)?;
plaintext_out.copy_from_slice(plaintext);
.copy_from_slice(plaintext)?;
nwritten_out.write(plaintext_len)?;

Ok(())
Expand Down

0 comments on commit 0b9c0c7

Please sign in to comment.