1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
// SPDX-FileCopyrightText: 2026 takubokudori
// SPDX-License-Identifier: MIT OR Apache-2.0
//! IDebugDataSpaces
use crate::dbgeng::*;
use bitflags::bitflags;
use std::{ffi::c_void, mem::MaybeUninit};
use windows::{
Win32::{
Foundation::E_UNEXPECTED, System::Memory::MEMORY_BASIC_INFORMATION64,
},
core::{GUID, PSTR, PWSTR},
};
use windy::{ACPString, WString};
impl_debug_interface!(
DebugDataSpaces,
DebugDataSpacesRef,
IDebugDataSpaces4,
IDebugDataSpaces
);
/// This handle is not released automatically. Release it explicitly with [`DebugDataSpaces::end_enum_tagged`].
#[derive(Debug, Eq, PartialEq)]
pub struct EnumTaggedHandle(u64);
impl EnumTaggedHandle {
/// Returns the wrapped raw value.
pub fn as_raw(&self) -> u64 { self.0 }
}
bitflags! {
pub struct DebugVSearchFlags: u32 {
const Default = DEBUG_VSEARCH_DEFAULT;
const WritableOnly = DEBUG_VSEARCH_WRITABLE_ONLY;
}
}
/// IDebugDataSpaces
impl DebugDataSpaces {
pub fn read_virtual(
&self,
offset: DebuggeeOffset,
buffer: &mut [u8],
) -> WinResult<u32> {
let buffer = to_uninit_u8_slice(buffer);
self._read_virtual_uninit(offset, buffer)
}
pub fn _read_virtual_uninit(
&self,
offset: DebuggeeOffset,
buffer: &mut [MaybeUninit<u8>],
) -> WinResult<u32> {
let mut bytes_read = 0;
unsafe {
self.0.ReadVirtual(
offset,
buffer.as_mut_ptr() as *mut c_void,
buffer.len().try_into()?,
Some(&mut bytes_read),
)?;
assert!(
bytes_read as usize <= buffer.len(),
"DbgEng returned a read count larger than the supplied buffer"
);
Ok(bytes_read)
}
}
pub fn write_virtual(
&self,
offset: DebuggeeOffset,
buffer: &[u8],
) -> WinResult<u32> {
let mut bytes_written = 0;
unsafe {
self.0.WriteVirtual(
offset,
buffer.as_ptr() as *const c_void,
buffer.len().try_into()?,
Some(&mut bytes_written),
)?;
Ok(bytes_written)
}
}
pub fn search_virtual(
&self,
offset: DebuggeeOffset,
length: u64,
pattern: &[u8],
pattern_granularity: u32,
) -> WinResult<u64> {
unsafe {
self.0.SearchVirtual(
offset,
length,
pattern.as_ptr() as *const c_void,
pattern.len().try_into()?,
pattern_granularity,
)
}
}
pub fn read_virtual_uncached(
&self,
offset: DebuggeeOffset,
buffer: &mut [u8],
) -> WinResult<u32> {
let buffer = to_uninit_u8_slice(buffer);
self._read_virtual_uncached_uninit(offset, buffer)
}
pub fn _read_virtual_uncached_uninit(
&self,
offset: DebuggeeOffset,
buffer: &mut [MaybeUninit<u8>],
) -> WinResult<u32> {
let mut bytes_read = 0;
unsafe {
self.0.ReadVirtualUncached(
offset,
buffer.as_mut_ptr() as *mut c_void,
buffer.len().try_into()?,
Some(&mut bytes_read),
)?;
assert!(
bytes_read as usize <= buffer.len(),
"DbgEng returned a read count larger than the supplied buffer"
);
Ok(bytes_read)
}
}
pub fn write_virtual_uncached(
&self,
offset: DebuggeeOffset,
buffer: &[u8],
) -> WinResult<u32> {
let mut bytes_written = 0;
unsafe {
self.0.WriteVirtualUncached(
offset,
buffer.as_ptr() as *const c_void,
buffer.len().try_into()?,
Some(&mut bytes_written),
)?;
Ok(bytes_written)
}
}
pub fn read_pointers_virtual(
&self,
offset: DebuggeeOffset,
ptrs: &mut [DebuggeeOffset],
) -> WinResult<()> {
unsafe { self.0.ReadPointersVirtual(offset, ptrs) }
}
pub fn write_pointers_virtual(
&self,
offset: DebuggeeOffset,
ptrs: &[DebuggeeOffset],
) -> WinResult<()> {
unsafe { self.0.WritePointersVirtual(offset, ptrs) }
}
pub fn read_physical(
&self,
offset: DebuggeeOffset,
buffer: &mut [u8],
) -> WinResult<u32> {
let mut bytes_read = 0;
unsafe {
self.0.ReadPhysical(
offset,
buffer.as_mut_ptr() as *mut c_void,
buffer.len().try_into()?,
Some(&mut bytes_read),
)?;
assert!(
bytes_read as usize <= buffer.len(),
"DbgEng returned a read count larger than the supplied buffer"
);
Ok(bytes_read)
}
}
pub fn write_physical(
&self,
offset: DebuggeeOffset,
buffer: &[u8],
) -> WinResult<u32> {
let mut bytes_written = 0;
unsafe {
self.0.WritePhysical(
offset,
buffer.as_ptr() as *const c_void,
buffer.len().try_into()?,
Some(&mut bytes_written),
)?;
Ok(bytes_written)
}
}
pub fn read_control(
&self,
processor: u32,
offset: u64,
buffer: &mut [u8],
) -> WinResult<u32> {
let buffer = to_uninit_u8_slice(buffer);
self._read_control_uninit(processor, offset, buffer)
}
pub(crate) fn _read_control_uninit(
&self,
processor: u32,
offset: u64,
buffer: &mut [MaybeUninit<u8>],
) -> WinResult<u32> {
let mut bytes_read = 0;
unsafe {
self.0.ReadControl(
processor,
offset,
buffer.as_mut_ptr() as *mut c_void,
buffer.len().try_into()?,
Some(&mut bytes_read),
)?;
assert!(
bytes_read as usize <= buffer.len(),
"DbgEng returned a read count larger than the supplied buffer"
);
Ok(bytes_read)
}
}
pub fn write_control(
&self,
processor: u32,
offset: u64,
buffer: &[u8],
) -> WinResult<u32> {
let mut bytes_written = 0;
unsafe {
self.0.WriteControl(
processor,
offset,
buffer.as_ptr() as *const c_void,
buffer.len().try_into()?,
Some(&mut bytes_written),
)?;
Ok(bytes_written)
}
}
pub fn read_io(
&self,
interface_type: InterfaceType,
bus_number: u32,
address_space: u32, // is always 1
offset: DebuggeeOffset,
buffer: &mut [u8],
) -> WinResult<u32> {
let buffer = to_uninit_u8_slice(buffer);
self._read_io_uninit(
interface_type,
bus_number,
address_space,
offset,
buffer,
)
}
pub fn _read_io_uninit(
&self,
interface_type: InterfaceType,
bus_number: u32,
address_space: u32, // is always 1
offset: DebuggeeOffset,
buffer: &mut [MaybeUninit<u8>],
) -> WinResult<u32> {
let mut bytes_read = 0;
unsafe {
self.0.ReadIo(
interface_type.into(),
bus_number,
address_space,
offset,
buffer.as_mut_ptr() as *mut c_void,
buffer.len().try_into()?,
Some(&mut bytes_read),
)?;
assert!(
bytes_read as usize <= buffer.len(),
"DbgEng returned a read count larger than the supplied buffer"
);
Ok(bytes_read)
}
}
pub fn write_io(
&self,
interface_type: InterfaceType,
bus_number: u32,
address_space: u32, // is always 1
offset: DebuggeeOffset,
buffer: &[u8],
) -> WinResult<u32> {
let mut bytes_written = 0;
unsafe {
self.0.WriteIo(
interface_type.into(),
bus_number,
address_space,
offset,
buffer.as_ptr() as *const c_void,
buffer.len().try_into()?,
Some(&mut bytes_written),
)?;
}
Ok(bytes_written)
}
pub fn read_msr(&self, msr: u32) -> WinResult<u64> {
unsafe { self.0.ReadMsr(msr) }
}
pub fn write_msr(&self, msr: u32, value: u64) -> WinResult<()> {
unsafe { self.0.WriteMsr(msr, value) }
}
pub fn read_bus_data(
&self,
bus_data_type: BusDataType,
bus_number: u32,
slot_number: u32,
offset: u32,
buffer: &mut [u8],
) -> WinResult<u32> {
let buffer = to_uninit_u8_slice(buffer);
self._read_bus_data_uninit(
bus_data_type,
bus_number,
slot_number,
offset,
buffer,
)
}
pub fn _read_bus_data_uninit(
&self,
bus_data_type: BusDataType,
bus_number: u32,
slot_number: u32,
offset: u32,
buffer: &mut [MaybeUninit<u8>],
) -> WinResult<u32> {
let mut bytes_read = 0;
unsafe {
self.0.ReadBusData(
bus_data_type as u32,
bus_number,
slot_number,
offset,
buffer.as_mut_ptr() as *mut c_void,
buffer.len().try_into()?,
Some(&mut bytes_read),
)?;
assert!(
bytes_read as usize <= buffer.len(),
"DbgEng returned a read count larger than the supplied buffer"
);
Ok(bytes_read)
}
}
pub fn write_bus_data(
&self,
bus_data_type: BusDataType,
bus_number: u32,
slot_number: u32,
offset: u32,
buffer: &[u8],
) -> WinResult<u32> {
let mut bytes_written = 0;
unsafe {
self.0.WriteBusData(
bus_data_type as u32,
bus_number,
slot_number,
offset,
buffer.as_ptr() as *const c_void,
buffer.len().try_into()?,
Some(&mut bytes_written),
)?;
Ok(bytes_written)
}
}
pub fn check_low_memory(&self) -> WinResult<()> {
unsafe { self.0.CheckLowMemory() }
}
pub fn read_debugger_data(&self, index: u32) -> WinResult<u64> {
let mut buffer: u64 = 0;
let mut data_size = 0;
unsafe {
self.0.ReadDebuggerData(
index,
&mut buffer as *mut u64 as *mut _,
size_of_val(&buffer) as u32,
Some(&mut data_size),
)?;
match data_size {
1 => Ok((buffer as u8) as u64),
2 => Ok((buffer as u16) as u64),
4 => Ok((buffer as u32) as u64),
8 => Ok(buffer),
_ => Err(E_UNEXPECTED.into()),
}
}
}
pub fn read_processor_system_data(
&self,
processor: u32,
index: DebugDataType,
buffer: &mut [u8],
) -> WinResult<u32> {
let mut bytes_read = 0;
unsafe {
self.0.ReadProcessorSystemData(
processor,
index as u32,
buffer.as_mut_ptr() as *mut c_void,
buffer.len().try_into()?,
Some(&mut bytes_read),
)?;
assert!(
bytes_read as usize <= buffer.len(),
"DbgEng returned a read count larger than the supplied buffer"
);
Ok(bytes_read)
}
}
}
/// IDebugDataSpaces2
impl DebugDataSpaces {
pub fn virtual_to_physical(
&self,
virtual_address: DebuggeeOffset,
) -> WinResult<DebuggeeOffset> {
unsafe { self.0.VirtualToPhysical(virtual_address) }
}
pub fn get_virtual_translation_physical_offsets(
&self,
virtual_address: DebuggeeOffset,
offsets: &mut [DebuggeeOffset],
) -> WinResult<u32> {
let mut levels = 0;
unsafe {
self.0.GetVirtualTranslationPhysicalOffsets(
virtual_address,
Some(offsets),
Some(&mut levels),
)?;
Ok(levels)
}
}
// ReadHandleData
pub fn fill_virtual(
&self,
start: DebuggeeOffset,
size: u32,
pattern: &[u8],
) -> WinResult<u32> {
let mut sz = 0;
unsafe {
self.0.FillVirtual(
start,
size,
pattern.as_ptr() as *mut c_void,
pattern.len().try_into()?,
Some(&mut sz),
)?;
Ok(sz)
}
}
pub fn fill_physical(
&self,
start: DebuggeeOffset,
size: u32,
pattern: &[u8],
) -> WinResult<u32> {
let mut filled = 0;
unsafe {
self.0.FillPhysical(
start,
size,
pattern.as_ptr() as *mut c_void,
pattern.len().try_into()?,
Some(&mut filled),
)?;
Ok(filled)
}
}
// QueryVirtual
pub fn query_virtual(
&self,
offset: DebuggeeOffset,
) -> WinResult<MEMORY_BASIC_INFORMATION64> {
unsafe {
let mut info = MaybeUninit::<MEMORY_BASIC_INFORMATION64>::uninit();
self.0.QueryVirtual(offset, info.as_mut_ptr())?;
Ok(info.assume_init())
}
}
}
/// IDebugDataSpaces3
impl DebugDataSpaces {
// ReadImageNtHeaders
pub fn read_tagged(
&self,
tag: &GUID,
offset: u32,
buffer: &mut [u8],
) -> WinResult<u32> {
let mut size = 0;
unsafe {
self.0.ReadTagged(
tag as *const _,
offset,
Some(buffer.as_mut_ptr() as *mut c_void),
buffer.len().try_into()?,
Some(&mut size),
)?;
Ok(size)
}
}
pub fn start_enum_tagged(&self) -> WinResult<EnumTaggedHandle> {
unsafe { Ok(EnumTaggedHandle(self.0.StartEnumTagged()?)) }
}
pub fn get_next_tagged(
&self,
handle: &EnumTaggedHandle,
) -> WinResult<Option<(GUID, u32)>> {
let mut guid = GUID::zeroed();
let mut size = 0;
unsafe {
if hr!(vcall!(self, GetNextTagged, handle.0, &mut guid, &mut size))?
{
Ok(Some((guid, size)))
} else {
Ok(None)
}
}
}
pub fn end_enum_tagged(&self, handle: EnumTaggedHandle) -> WinResult<()> {
unsafe { self.0.EndEnumTagged(handle.0) }
}
}
// IDebugDataSpaces4
impl DebugDataSpaces {
// GetOffsetInformation
pub fn get_next_differently_valid_offset_virtual(
&self,
offset: DebuggeeOffset,
) -> WinResult<DebuggeeOffset> {
unsafe { self.0.GetNextDifferentlyValidOffsetVirtual(offset) }
}
pub fn get_valid_region_virtual(
&self,
base: u64,
size: u32,
) -> WinResult<(DebuggeeOffset, u32)> {
let mut valid_base = 0;
let mut valid_size = 0;
unsafe {
self.0.GetValidRegionVirtual(
base,
size,
&mut valid_base,
&mut valid_size,
)?;
Ok((valid_base, valid_size))
}
}
pub fn search_virtual2(
&self,
offset: DebuggeeOffset,
length: u64,
flags: DebugVSearchFlags,
pattern: &[u8],
pattern_granularity: u32,
) -> WinResult<u64> {
unsafe {
self.0.SearchVirtual2(
offset,
length,
flags.bits(),
pattern.as_ptr() as *const c_void,
pattern.len().try_into()?,
pattern_granularity,
)
}
}
pub fn read_multi_byte_string_virtual(
&self,
offset: DebuggeeOffset,
max_bytes: u32,
) -> WinResult<(ACPString, bool)> {
let mut v = Vec::with_capacity(max_bytes as usize);
unsafe {
let (hr, string_bytes) = self
.__read_multi_byte_string_virtual_inner(
offset,
max_bytes,
v.spare_capacity_mut(),
)?;
match hr {
S_OK => {
let string_bytes = string_bytes.unwrap();
// Successfully read including the null byte.
assert!(max_bytes >= string_bytes);
v.set_len(string_bytes as usize);
assert_eq!(v[v.len() - 1], 0);
Ok((
ACPString::new(v).map_err(|e| {
HRESULT::from_win32(e.as_error_code())
})?,
true,
))
}
S_FALSE | E_INVALIDARG => Ok((
ACPString::new(v)
.map_err(|e| HRESULT::from_win32(e.as_error_code()))?,
false,
)),
_ => unreachable!(),
}
}
}
pub(crate) fn __read_multi_byte_string_virtual_inner(
&self,
offset: DebuggeeOffset,
max_bytes: u32,
buffer: &mut [MaybeUninit<u8>],
) -> WinResult<(HRESULT, Option<u32>)> {
if max_bytes == 0 && buffer.is_empty() {
// When MaxBytes == 0 && BufferSize == 0, ReadMultiByteStringVirtual returns `S_OK`
// and stores the string length in bytes in StringBytes.
// On the other hand, ReadUnicodeStringVirtual returns E_INVALIDARG
// and does not store anything in StringBytes.
// Since this behavior is inconsistent, treat this condition as `E_INVALIDARG`.
return Ok((E_INVALIDARG, None));
}
let mut string_bytes = 0;
unsafe {
let hr = vcall!(
self,
ReadMultiByteStringVirtual,
offset,
max_bytes,
PSTR(buffer.as_mut_ptr() as *mut u8),
// MaxBytes and StringBytes is in bytes, but BufferSize is in characters.
buffer.len().try_into()?,
&mut string_bytes,
);
match hr {
S_OK => {
// Successfully read including the null byte.
Ok((S_OK, Some(string_bytes)))
}
S_FALSE => {
// The BufferSize limit was reached before the MaxBytes limit.
// In this case, StringBytes contains the number of bytes required to store the string.
Ok((S_FALSE, Some(string_bytes)))
}
E_INVALIDARG => {
// The MaxBytes limit was reached before the BufferSize limit. Note that the MaxBytes limit is reached earlier.
// The string was too long and was truncated.
// In this case, `string_bytes` is 0, and the byte length of `v` is `max_bytes`.
Ok((E_INVALIDARG, None))
}
hr => Err(hr.into()),
}
}
}
//ReadMultiByteStringVirtualWide
// ReadUnicodeStringVirtual
pub fn read_unicode_string_virtual_wide(
&self,
offset: DebuggeeOffset,
mut max_bytes: u32,
) -> WinResult<(WString, bool)> {
if max_bytes & 1 == 1 {
// If `max_bytes` is odd, it may try to read one extra byte.
max_bytes = max_bytes
.checked_add(1)
.ok_or_else(|| windows::core::Error::from(E_INVALIDARG))?;
}
let max_cap = max_bytes / size_of::<u16>() as u32;
let mut v = Vec::with_capacity(max_cap as usize);
unsafe {
let (hr, string_bytes) = self
.__read_unicode_string_virtual_wide_inner(
offset,
max_bytes,
v.spare_capacity_mut(),
)?;
match hr {
S_OK => {
let string_bytes = string_bytes.unwrap();
// Successfully read including the null byte.
assert!(max_bytes >= string_bytes);
v.set_len(string_bytes as usize / size_of::<u16>());
assert_eq!(v[v.len() - 1], 0);
Ok((
WString::new(v).map_err(|e| {
HRESULT::from_win32(e.as_error_code())
})?,
true,
))
}
S_FALSE | E_INVALIDARG => Ok((
WString::new(v)
.map_err(|e| HRESULT::from_win32(e.as_error_code()))?,
false,
)),
_ => unreachable!(),
}
}
}
pub(crate) fn __read_unicode_string_virtual_wide_inner(
&self,
offset: DebuggeeOffset,
max_bytes: u32,
buffer: &mut [MaybeUninit<u16>],
) -> WinResult<(HRESULT, Option<u32>)> {
if max_bytes == 1 {
// An infinite loop occurs when max_bytes is 1. Is this DbgEng's bug?
return Ok((E_INVALIDARG, None));
}
if max_bytes == 0 && buffer.is_empty() {
// When MaxBytes == 0 && BufferSize == 0, ReadMultiByteStringVirtual returns `S_OK`
// and stores the string length in bytes in StringBytes.
// On the other hand, ReadUnicodeStringVirtual returns E_INVALIDARG
// and does not store anything in StringBytes.
// Since this behavior is inconsistent, treat this condition as `E_INVALIDARG`.
return Ok((E_INVALIDARG, None));
}
let mut string_bytes = 0;
unsafe {
// SAFETY: When `max_bytes` is odd, up to `max_bytes + 1` bytes may be read.
// However, `buffer` is 2 * N bytes, and if the buffer is too small,
// DbgEng handles it, so this is not an issue.
// For surrogate pairs, data is read 2 bytes at a time;
// it is not read as 4 bytes at once.
let hr = vcall!(
self,
ReadUnicodeStringVirtualWide,
offset,
max_bytes,
PWSTR(buffer.as_mut_ptr() as *mut u16),
// MaxBytes and StringBytes is in bytes, but BufferSize is in characters.
buffer.len().try_into()?,
&mut string_bytes,
);
match hr {
S_OK => {
// Successfully read including the null byte.
Ok((S_OK, Some(string_bytes)))
}
S_FALSE => {
// The BufferSize limit was reached before the MaxBytes limit.
// In this case, StringBytes contains the number of bytes required to store the string.
// When the `BufferSize` limit is reached before the `MaxBytes` limit,
// `StringBytes` contains the number of bytes required to store the string.
Ok((S_FALSE, Some(string_bytes)))
}
E_INVALIDARG => {
// The MaxBytes limit was reached before the BufferSize limit.
// Note that the MaxBytes limit is reached earlier.
// The string was too long and was truncated.
// In this case, `string_bytes` is 0, and the byte length of `v` is `max_bytes`.
Ok((E_INVALIDARG, None))
}
hr => Err(hr.into()),
}
}
}
pub fn read_physical2(
&self,
offset: DebuggeeOffset,
flags: DebugPhysicalFlags,
buffer: &mut [u8],
) -> WinResult<u32> {
let buffer = to_uninit_u8_slice(buffer);
self._read_physical2_uninit(offset, flags, buffer)
}
pub fn _read_physical2_uninit(
&self,
offset: DebuggeeOffset,
flags: DebugPhysicalFlags,
buffer: &mut [MaybeUninit<u8>],
) -> WinResult<u32> {
let mut bytes_read = 0;
unsafe {
self.0.ReadPhysical2(
offset,
flags as u32,
buffer.as_mut_ptr() as *mut c_void,
buffer.len().try_into()?,
Some(&mut bytes_read),
)?;
assert!(
bytes_read as usize <= buffer.len(),
"DbgEng returned a read count larger than the supplied buffer"
);
Ok(bytes_read)
}
}
pub fn write_physical2(
&self,
offset: DebuggeeOffset,
flags: DebugPhysicalFlags,
buffer: &[u8],
) -> WinResult<u32> {
let mut sz = 0;
unsafe {
self.0.WritePhysical2(
offset,
flags as u32,
buffer.as_ptr() as *const c_void,
buffer.len().try_into()?,
Some(&mut sz),
)?;
Ok(sz)
}
}
}