Use C string literals not cstr!
Since Rust 1.77.0 the language has supported C string literals, so the
`cstr!` macro is no longer needed. Replace existing usages with the
equivalent literal.
See https://doc.rust-lang.org/reference/tokens.html#c-string-literals.
I believe that the two are equivalent:
- Escapes are handled the same way;
- Both allow arbitrary Unicode, which is mapped to UTF-8 (I don't
think we made any use of this);
- Both treat any embedded NUL character as a compile time error.
This is of no significance whatsoever, but it does make the code a
tiny bit simpler. It should not change the compiled code at all, so
no flagging should be needed.
I'm not deleting the macro in this CL; I'll do a follow-up for that,
since there may be usages I can't see, and it has greater chance of
accidental conflict.
Test: TH
Change-Id: I4354b3b0a0c53fbec0c2d78b4182786e4e2d0ce8
diff --git a/guest/pvmfw/src/device_assignment.rs b/guest/pvmfw/src/device_assignment.rs
index f37f443..bb2e6ce 100644
--- a/guest/pvmfw/src/device_assignment.rs
+++ b/guest/pvmfw/src/device_assignment.rs
@@ -37,18 +37,6 @@
use zerocopy::byteorder::big_endian::U32;
use zerocopy::FromBytes as _;
-// TODO(b/308694211): Use cstr! from vmbase instead.
-macro_rules! cstr {
- ($str:literal) => {{
- const S: &str = concat!($str, "\0");
- const C: &::core::ffi::CStr = match ::core::ffi::CStr::from_bytes_with_nul(S.as_bytes()) {
- Ok(v) => v,
- Err(_) => panic!("string contains interior NUL"),
- };
- C
- }};
-}
-
// TODO(b/277993056): Keep constants derived from platform.dts in one place.
const CELLS_PER_INTERRUPT: usize = 3; // from /intc node in platform.dts
@@ -332,7 +320,7 @@
// };
// };
//
- // Then locate_overlay_target_path(cstr!("/fragment@rng/__overlay__/rng")) is Ok("/rng")
+ // Then locate_overlay_target_path(c"/fragment@rng/__overlay__/rng") is Ok("/rng")
//
// Contrary to fdt_overlay_target_offset(), this API enforces overlay target property
// 'target-path = "/"', so the overlay doesn't modify and/or append platform DT's existing
@@ -343,10 +331,9 @@
dtbo_node: &FdtNode,
) -> Result<CString> {
let fragment_node = dtbo_node.supernode_at_depth(1)?;
- let target_path = fragment_node
- .getprop_str(cstr!("target-path"))?
- .ok_or(DeviceAssignmentError::InvalidDtbo)?;
- if target_path != cstr!("/") {
+ let target_path =
+ fragment_node.getprop_str(c"target-path")?.ok_or(DeviceAssignmentError::InvalidDtbo)?;
+ if target_path != c"/" {
return Err(DeviceAssignmentError::UnsupportedOverlayTarget);
}
@@ -415,7 +402,7 @@
/// Parses Physical devices in VM DTBO
fn parse_physical_devices(&self) -> Result<BTreeMap<Phandle, PhysicalDeviceInfo>> {
- let Some(physical_node) = self.as_ref().node(cstr!("/host"))? else {
+ let Some(physical_node) = self.as_ref().node(c"/host")? else {
return Ok(BTreeMap::new());
};
@@ -459,7 +446,7 @@
let vm_dtbo = self.as_ref();
let mut phandle_map = BTreeMap::new();
- let Some(local_fixups) = vm_dtbo.node(cstr!("/__local_fixups__"))? else {
+ let Some(local_fixups) = vm_dtbo.node(c"/__local_fixups__")? else {
return Ok(phandle_map);
};
@@ -615,15 +602,14 @@
impl PvIommu {
fn parse(node: &FdtNode) -> Result<Self> {
- let iommu_cells = node
- .getprop_u32(cstr!("#iommu-cells"))?
- .ok_or(DeviceAssignmentError::InvalidPvIommu)?;
+ let iommu_cells =
+ node.getprop_u32(c"#iommu-cells")?.ok_or(DeviceAssignmentError::InvalidPvIommu)?;
// Ensures #iommu-cells = <1>. It means that `<iommus>` entry contains pair of
// (pvIOMMU ID, vSID)
if iommu_cells != 1 {
return Err(DeviceAssignmentError::InvalidPvIommu);
}
- let id = node.getprop_u32(cstr!("id"))?.ok_or(DeviceAssignmentError::InvalidPvIommu)?;
+ let id = node.getprop_u32(c"id")?.ok_or(DeviceAssignmentError::InvalidPvIommu)?;
Ok(Self { id })
}
}
@@ -687,10 +673,10 @@
impl PhysIommu {
fn parse(node: &FdtNode) -> Result<Option<Self>> {
- let Some(token) = node.getprop_u64(cstr!("android,pvmfw,token"))? else {
+ let Some(token) = node.getprop_u64(c"android,pvmfw,token")? else {
return Ok(None);
};
- let Some(iommu_cells) = node.getprop_u32(cstr!("#iommu-cells"))? else {
+ let Some(iommu_cells) = node.getprop_u32(c"#iommu-cells")? else {
return Err(DeviceAssignmentError::InvalidPhysIommu);
};
// Currently only supports #iommu-cells = <1>.
@@ -715,7 +701,7 @@
phys_iommus: &BTreeMap<Phandle, PhysIommu>,
) -> Result<Vec<(PhysIommu, Sid)>> {
let mut iommus = vec![];
- let Some(mut cells) = node.getprop_cells(cstr!("iommus"))? else {
+ let Some(mut cells) = node.getprop_cells(c"iommus")? else {
return Ok(iommus);
};
while let Some(cell) = cells.next() {
@@ -735,7 +721,7 @@
}
fn parse(node: &FdtNode, phys_iommus: &BTreeMap<Phandle, PhysIommu>) -> Result<Option<Self>> {
- let Some(phandle) = node.getprop_u32(cstr!("android,pvmfw,target"))? else {
+ let Some(phandle) = node.getprop_u32(c"android,pvmfw,target")? else {
return Ok(None);
};
let target = Phandle::try_from(phandle)?;
@@ -812,7 +798,7 @@
// Validation: Validate if interrupts cell numbers are multiple of #interrupt-cells.
// We can't know how many interrupts would exist.
let interrupts_cells = node
- .getprop_cells(cstr!("interrupts"))?
+ .getprop_cells(c"interrupts")?
.ok_or(DeviceAssignmentError::InvalidInterrupts)?
.count();
if interrupts_cells % CELLS_PER_INTERRUPT != 0 {
@@ -820,7 +806,7 @@
}
// Once validated, keep the raw bytes so patch can be done with setprop()
- Ok(node.getprop(cstr!("interrupts")).unwrap().unwrap().into())
+ Ok(node.getprop(c"interrupts").unwrap().unwrap().into())
}
// TODO(b/277993056): Also validate /__local_fixups__ to ensure that <iommus> has phandle.
@@ -829,7 +815,7 @@
pviommus: &BTreeMap<Phandle, PvIommu>,
) -> Result<Vec<(PvIommu, Vsid)>> {
let mut iommus = vec![];
- let Some(mut cells) = node.getprop_cells(cstr!("iommus"))? else {
+ let Some(mut cells) = node.getprop_cells(c"iommus")? else {
return Ok(iommus);
};
while let Some(cell) = cells.next() {
@@ -917,15 +903,15 @@
fn patch(&self, fdt: &mut Fdt, pviommu_phandles: &BTreeMap<PvIommu, Phandle>) -> Result<()> {
let mut dst = fdt.node_mut(&self.node_path)?.unwrap();
- dst.setprop(cstr!("reg"), &to_be_bytes(&self.reg))?;
- dst.setprop(cstr!("interrupts"), &self.interrupts)?;
+ dst.setprop(c"reg", &to_be_bytes(&self.reg))?;
+ dst.setprop(c"interrupts", &self.interrupts)?;
let mut iommus = Vec::with_capacity(8 * self.iommus.len());
for (pviommu, vsid) in &self.iommus {
let phandle = pviommu_phandles.get(pviommu).unwrap();
iommus.extend_from_slice(&u32::from(*phandle).to_be_bytes());
iommus.extend_from_slice(&vsid.0.to_be_bytes());
}
- dst.setprop(cstr!("iommus"), &iommus)?;
+ dst.setprop(c"iommus", &iommus)?;
Ok(())
}
@@ -939,7 +925,7 @@
}
impl DeviceAssignmentInfo {
- const PVIOMMU_COMPATIBLE: &'static CStr = cstr!("pkvm,pviommu");
+ const PVIOMMU_COMPATIBLE: &'static CStr = c"pkvm,pviommu";
/// Parses pvIOMMUs in fdt
// Note: This will validate pvIOMMU ids' uniqueness, even when unassigned.
@@ -1046,8 +1032,8 @@
Self::validate_pviommu_topology(&assigned_devices)?;
let mut vm_dtbo_mask = vm_dtbo.build_mask(assigned_device_paths)?;
- vm_dtbo_mask.mask_all(&DtPathTokens::new(cstr!("/__local_fixups__"))?);
- vm_dtbo_mask.mask_all(&DtPathTokens::new(cstr!("/__symbols__"))?);
+ vm_dtbo_mask.mask_all(&DtPathTokens::new(c"/__local_fixups__")?);
+ vm_dtbo_mask.mask_all(&DtPathTokens::new(c"/__symbols__")?);
// Note: Any node without __overlay__ will be ignored by fdt_apply_overlay,
// so doesn't need to be filtered.
@@ -1060,7 +1046,7 @@
let vm_dtbo = vm_dtbo.as_mut();
// Filter unused references in /__local_fixups__
- if let Some(local_fixups) = vm_dtbo.node_mut(cstr!("/__local_fixups__"))? {
+ if let Some(local_fixups) = vm_dtbo.node_mut(c"/__local_fixups__")? {
filter_with_mask(local_fixups, &self.vm_dtbo_mask)?;
}
@@ -1078,7 +1064,7 @@
for pviommu in &self.pviommus {
let mut node = compatible.ok_or(DeviceAssignmentError::TooManyPvIommu)?;
let phandle = node.as_node().get_phandle()?.ok_or(DeviceAssignmentError::Internal)?;
- node.setprop_inplace(cstr!("id"), &pviommu.id.to_be_bytes())?;
+ node.setprop_inplace(c"id", &pviommu.id.to_be_bytes())?;
if pviommu_phandles.insert(*pviommu, phandle).is_some() {
return Err(DeviceAssignmentError::Internal);
}
@@ -1108,10 +1094,10 @@
/// Cleans device trees not to contain any pre-populated nodes/props for device assignment.
pub fn clean(fdt: &mut Fdt) -> Result<()> {
- let mut compatible = fdt.root_mut().next_compatible(cstr!("pkvm,pviommu"))?;
+ let mut compatible = fdt.root_mut().next_compatible(c"pkvm,pviommu")?;
// Filters pre-populated
while let Some(filtered_pviommu) = compatible {
- compatible = filtered_pviommu.delete_and_next_compatible(cstr!("pkvm,pviommu"))?;
+ compatible = filtered_pviommu.delete_and_next_compatible(c"pkvm,pviommu")?;
}
// Removes any dangling references in __symbols__ (e.g. removed pvIOMMUs)
@@ -1239,24 +1225,23 @@
return Err(FdtError::NotFound.into());
};
- let reg = node.getprop(cstr!("reg"))?.ok_or(DeviceAssignmentError::MalformedReg)?;
- let interrupts = node
- .getprop(cstr!("interrupts"))?
- .ok_or(DeviceAssignmentError::InvalidInterrupts)?;
+ let reg = node.getprop(c"reg")?.ok_or(DeviceAssignmentError::MalformedReg)?;
+ let interrupts =
+ node.getprop(c"interrupts")?.ok_or(DeviceAssignmentError::InvalidInterrupts)?;
let mut iommus = vec![];
- if let Some(mut cells) = node.getprop_cells(cstr!("iommus"))? {
+ if let Some(mut cells) = node.getprop_cells(c"iommus")? {
while let Some(pviommu_id) = cells.next() {
// pvIOMMU id
let phandle = Phandle::try_from(pviommu_id)?;
let pviommu = fdt
.node_with_phandle(phandle)?
.ok_or(DeviceAssignmentError::MalformedIommus)?;
- let compatible = pviommu.getprop_str(cstr!("compatible"));
- if compatible != Ok(Some(cstr!("pkvm,pviommu"))) {
+ let compatible = pviommu.getprop_str(c"compatible");
+ if compatible != Ok(Some(c"pkvm,pviommu")) {
return Err(DeviceAssignmentError::MalformedIommus);
}
let id = pviommu
- .getprop_u32(cstr!("id"))?
+ .getprop_u32(c"id")?
.ok_or(DeviceAssignmentError::MalformedIommus)?;
iommus.push(id);
@@ -1273,8 +1258,8 @@
fn collect_pviommus(fdt: &Fdt) -> Result<Vec<u32>> {
let mut pviommus = BTreeSet::new();
- for pviommu in fdt.compatible_nodes(cstr!("pkvm,pviommu"))? {
- if let Ok(Some(id)) = pviommu.getprop_u32(cstr!("id")) {
+ for pviommu in fdt.compatible_nodes(c"pkvm,pviommu")? {
+ if let Ok(Some(id)) = pviommu.getprop_u32(c"id") {
pviommus.insert(id);
}
}
@@ -1395,24 +1380,24 @@
let symbols = vm_dtbo.symbols().unwrap().unwrap();
- let rng = vm_dtbo.node(cstr!("/fragment@0/__overlay__/rng")).unwrap();
+ let rng = vm_dtbo.node(c"/fragment@0/__overlay__/rng").unwrap();
assert_ne!(rng, None);
- let rng_symbol = symbols.getprop_str(cstr!("rng")).unwrap();
- assert_eq!(Some(cstr!("/fragment@0/__overlay__/rng")), rng_symbol);
+ let rng_symbol = symbols.getprop_str(c"rng").unwrap();
+ assert_eq!(Some(c"/fragment@0/__overlay__/rng"), rng_symbol);
- let light = vm_dtbo.node(cstr!("/fragment@0/__overlay__/light")).unwrap();
+ let light = vm_dtbo.node(c"/fragment@0/__overlay__/light").unwrap();
assert_eq!(light, None);
- let light_symbol = symbols.getprop_str(cstr!("light")).unwrap();
+ let light_symbol = symbols.getprop_str(c"light").unwrap();
assert_eq!(None, light_symbol);
- let led = vm_dtbo.node(cstr!("/fragment@0/__overlay__/led")).unwrap();
+ let led = vm_dtbo.node(c"/fragment@0/__overlay__/led").unwrap();
assert_eq!(led, None);
- let led_symbol = symbols.getprop_str(cstr!("led")).unwrap();
+ let led_symbol = symbols.getprop_str(c"led").unwrap();
assert_eq!(None, led_symbol);
- let backlight = vm_dtbo.node(cstr!("/fragment@0/__overlay__/bus0/backlight")).unwrap();
+ let backlight = vm_dtbo.node(c"/fragment@0/__overlay__/bus0/backlight").unwrap();
assert_eq!(backlight, None);
- let backlight_symbol = symbols.getprop_str(cstr!("backlight")).unwrap();
+ let backlight_symbol = symbols.getprop_str(c"backlight").unwrap();
assert_eq!(None, backlight_symbol);
}
@@ -1440,19 +1425,19 @@
}
device_info.patch(platform_dt).unwrap();
- let rng_node = platform_dt.node(cstr!("/bus0/backlight")).unwrap().unwrap();
- let phandle = rng_node.getprop_u32(cstr!("phandle")).unwrap();
+ let rng_node = platform_dt.node(c"/bus0/backlight").unwrap().unwrap();
+ let phandle = rng_node.getprop_u32(c"phandle").unwrap();
assert_ne!(None, phandle);
// Note: Intentionally not using AssignedDeviceNode for matching all props.
type FdtResult<T> = libfdt::Result<T>;
let expected: Vec<(FdtResult<&CStr>, FdtResult<Vec<u8>>)> = vec![
- (Ok(cstr!("android,backlight,ignore-gctrl-reset")), Ok(Vec::new())),
- (Ok(cstr!("compatible")), Ok(Vec::from(*b"android,backlight\0"))),
- (Ok(cstr!("interrupts")), Ok(into_fdt_prop(vec![0x0, 0xF, 0x4]))),
- (Ok(cstr!("iommus")), Ok(Vec::new())),
- (Ok(cstr!("phandle")), Ok(into_fdt_prop(vec![phandle.unwrap()]))),
- (Ok(cstr!("reg")), Ok(into_fdt_prop(vec![0x0, 0x9, 0x0, 0xFF]))),
+ (Ok(c"android,backlight,ignore-gctrl-reset"), Ok(Vec::new())),
+ (Ok(c"compatible"), Ok(Vec::from(*b"android,backlight\0"))),
+ (Ok(c"interrupts"), Ok(into_fdt_prop(vec![0x0, 0xF, 0x4]))),
+ (Ok(c"iommus"), Ok(Vec::new())),
+ (Ok(c"phandle"), Ok(into_fdt_prop(vec![phandle.unwrap()]))),
+ (Ok(c"reg"), Ok(into_fdt_prop(vec![0x0, 0x9, 0x0, 0xFF]))),
];
let mut properties: Vec<_> = rng_node
@@ -1493,7 +1478,7 @@
}
device_info.patch(platform_dt).unwrap();
- let compatible = platform_dt.root().next_compatible(cstr!("pkvm,pviommu")).unwrap();
+ let compatible = platform_dt.root().next_compatible(c"pkvm,pviommu").unwrap();
assert_eq!(None, compatible);
if let Some(symbols) = platform_dt.symbols().unwrap() {
@@ -1794,12 +1779,12 @@
let mut platform_dt_data = pvmfw_fdt_template::RAW.to_vec();
let platform_dt = Fdt::from_mut_slice(&mut platform_dt_data).unwrap();
- let compatible = platform_dt.root().next_compatible(cstr!("pkvm,pviommu"));
+ let compatible = platform_dt.root().next_compatible(c"pkvm,pviommu");
assert_ne!(None, compatible.unwrap());
clean(platform_dt).unwrap();
- let compatible = platform_dt.root().next_compatible(cstr!("pkvm,pviommu"));
+ let compatible = platform_dt.root().next_compatible(c"pkvm,pviommu");
assert_eq!(Ok(None), compatible);
}