• Top
    • Documentation
    • Books
    • Recursion-and-induction
    • Boolean-reasoning
    • Debugging
    • Projects
    • Std
    • Proof-automation
    • Macro-libraries
    • ACL2
    • Interfacing-tools
    • Hardware-verification
    • Software-verification
      • Kestrel-books
      • X86isa
        • Program-execution
        • Introduction
        • X86isa-build-instructions
        • Publications
        • Contributors
        • Machine
          • Syscalls
          • Cpuid
          • X86isa-state
          • Linear-memory
          • Rflag-specifications
          • Characterizing-undefined-behavior
          • Top-level-memory
          • App-view
          • X86-decoder
          • Physical-memory
          • Decoding-and-spec-utils
          • Instructions
          • X86-modes
          • Segmentation
          • Register-readers-and-writers
          • Other-non-deterministic-computations
          • Environment
            • Components-of-the-environment-field
            • Reading-memory-as-strings-or-bytes
              • Read-bytes-from-memory
              • Read-memory-zero-terminated
              • Read-string-zero-terminated
              • Read-string-from-memory
            • Converting-between-strings-and-bytes
            • Writing-strings-or-bytes-to-memory
          • Paging
        • Implemented-opcodes
        • Proof-utilities
        • To-do
        • Concrete-simulation-examples
        • Model-validation
        • Utils
        • Debugging-code-proofs
      • Axe
      • Execloader
    • Testing-utilities
    • Math
  • Environment

Reading-memory-as-strings-or-bytes

Definitions and Theorems

Function: read-memory-zero-terminated

(defun read-memory-zero-terminated (ptr x86 acc)
 (declare (xargs :stobjs (x86)))
 (declare (type (signed-byte 49) ptr))
 (declare (xargs :guard (and (integerp ptr)
                             (<= (- *2^47*) ptr)
                             (<= ptr *2^47*)
                             (nat-listp acc))))
 (let ((__function__ 'read-memory-zero-terminated))
   (declare (ignorable __function__))
   (if
    (signed-byte-p 48 ptr)
    (b* (((mv flg (the (unsigned-byte 8) mem-val)
              x86)
          (rml08 ptr :r x86))
         ((when flg) (mv flg acc x86)))
      (if (equal 0 mem-val)
          (mv nil (append (reverse acc) '(0)) x86)
        (read-memory-zero-terminated (the (signed-byte 49) (1+ ptr))
                                     x86 (cons mem-val acc))))
    (mv t (reverse acc) x86))))

Theorem: byte-listp-mv-nth-1-read-memory-zero-terminated

(defthm byte-listp-mv-nth-1-read-memory-zero-terminated
 (implies
    (and (x86p x86) (byte-listp acc))
    (byte-listp (mv-nth 1
                        (read-memory-zero-terminated ptr x86 acc))))
 :rule-classes :type-prescription)

Theorem: x86p-mv-nth-2-read-memory-zero-terminated

(defthm x86p-mv-nth-2-read-memory-zero-terminated
  (implies
       (x86p x86)
       (x86p (mv-nth 2
                     (read-memory-zero-terminated ptr x86 acc)))))

Function: read-string-zero-terminated

(defun read-string-zero-terminated (ptr x86)
  (declare (xargs :stobjs (x86)))
  (declare (type (signed-byte 49) ptr))
  (declare (xargs :guard (and (integerp ptr)
                              (<= (- *2^47*) ptr)
                              (<= ptr *2^47*))))
  (let ((__function__ 'read-string-zero-terminated))
    (declare (ignorable __function__))
    (b* (((mv flg bytes x86)
          (read-memory-zero-terminated ptr x86 nil))
         ((when flg) (mv flg "nil" x86))
         (charlist (bytes-to-charlist bytes)))
      (mv nil (coerce charlist 'string)
          x86))))

Theorem: stringp-mv-nth-1-read-string-zero-terminated

(defthm stringp-mv-nth-1-read-string-zero-terminated
  (implies (force (x86p x86))
           (stringp (mv-nth 1
                            (read-string-zero-terminated ptr x86))))
  :rule-classes :type-prescription)

Theorem: x86p-mv-nth-2-read-string-zero-terminated

(defthm x86p-mv-nth-2-read-string-zero-terminated
  (implies (x86p x86)
           (x86p (mv-nth 2
                         (read-string-zero-terminated ptr x86)))))

Function: read-bytes-from-memory

(defun read-bytes-from-memory (ptr nbytes x86 acc)
 (declare (xargs :stobjs (x86)))
 (declare (type (signed-byte 49) ptr)
          (type (unsigned-byte 48) nbytes))
 (declare (xargs :split-types t
                 :guard (and (integerp nbytes)
                             (<= 0 nbytes)
                             (integerp ptr)
                             (<= (- *2^47*) ptr)
                             (< (+ ptr nbytes) *2^47*)
                             (nat-listp acc))))
 (let ((__function__ 'read-bytes-from-memory))
   (declare (ignorable __function__))
   (if
    (mbt (and (integerp nbytes)
              (integerp ptr)
              (<= (- *2^47*) ptr)
              (< (+ ptr nbytes) *2^47*)))
    (if (zp nbytes)
        (mv nil (reverse acc) x86)
      (b* (((mv flg (the (unsigned-byte 8) byte)
                x86)
            (rml08 ptr :r x86))
           ((when flg) (mv flg nil x86)))
        (read-bytes-from-memory (the (signed-byte 49) (1+ ptr))
                                (the (unsigned-byte 48) (1- nbytes))
                                x86 (cons byte acc))))
    (mv t (reverse acc) x86))))

Theorem: byte-listp-mv-nth-1-read-bytes-from-memory

(defthm byte-listp-mv-nth-1-read-bytes-from-memory
 (implies
  (forced-and (x86p x86) (byte-listp acc))
  (byte-listp (mv-nth 1
                      (read-bytes-from-memory ptr nbytes x86 acc))))
 :rule-classes :type-prescription)

Theorem: x86p-mv-nth-2-read-bytes-from-memory

(defthm x86p-mv-nth-2-read-bytes-from-memory
  (implies
       (x86p x86)
       (x86p (mv-nth 2
                     (read-bytes-from-memory ptr nbytes x86 acc)))))

Function: read-string-from-memory

(defun read-string-from-memory (ptr nbytes x86)
  (declare (xargs :stobjs (x86)))
  (declare (type (signed-byte 49) ptr)
           (type (signed-byte 48) nbytes))
  (declare (xargs :guard (and (integerp nbytes)
                              (<= 0 nbytes)
                              (integerp ptr)
                              (<= (- *2^47*) ptr)
                              (< (+ ptr nbytes) *2^47*))))
  (let ((__function__ 'read-string-from-memory))
    (declare (ignorable __function__))
    (b* (((mv flg bytes x86)
          (read-bytes-from-memory ptr nbytes x86 nil))
         ((when flg) (mv flg "nil" x86))
         (charlist (bytes-to-charlist bytes)))
      (mv nil (coerce charlist 'string)
          x86))))

Theorem: stringp-mv-nth-1-read-string-from-memory

(defthm stringp-mv-nth-1-read-string-from-memory
  (implies
       (force (x86p x86))
       (stringp (mv-nth 1
                        (read-string-from-memory ptr nbytes x86))))
  :rule-classes :type-prescription)

Theorem: x86p-mv-nth-2-read-string-from-memory

(defthm x86p-mv-nth-2-read-string-from-memory
 (implies (x86p x86)
          (x86p (mv-nth 2
                        (read-string-from-memory ptr nbytes x86)))))

Subtopics

Read-bytes-from-memory
Read nbytes bytes from memory
Read-memory-zero-terminated
Read memory as bytes till a zero is encountered
Read-string-zero-terminated
Read memory as a string till a zero is encountered
Read-string-from-memory
Read nbytes bytes from memory as a string