Here are two patterns you can use in angr to get a truly read-only region in your state’s memory:
- Use
add_backer
- Manually map → write → protect
1) Using state.memory.add_backer
This is the simplest: you hand angr a blob of bytes, it maps it in as a backer (read-only) and you can’t accidentally store into it.
import angr
# load your binary (or create a blank project)
proj = angr.Project('your_binary', auto_load_libs=False)
# create a blank state (or an entry_state, etc.)
state = proj.factory.blank_state()
# Suppose you want a read-only region at 0x600000 of 0x1000 bytes:
ADDR = 0x600000
DATA = b"HELLO, this is read-only!\x00" # up to 0x1000
# This call:
# - maps 0x1000 bytes at ADDR with PROT_READ
# - initializes it with DATA
# - further `state.memory.store` calls into that region will fail
state.memory.add_backer(ADDR, DATA)
# Test: a plain load works
val = state.memory.load(ADDR, len(DATA))
print(state.solver.eval(val, cast_to=bytes))
# b"HELLO, this is read-only!\x00"
# But a write will now raise:
# state.memory.store(ADDR, b"OUCH") # => angr.errors.SimMemoryError
Pros
• Only one call, automatically read-only
• Under the hood it uses a “backer” so it’s very efficient
2) Manually map → write → protect
If you really want to see the “map / store / protect” steps yourself, do this:
import angr
import mmap
# PROT flags (same as POSIX)
PROT_READ = 1
PROT_WRITE = 2
PROT_EXEC = 4
proj = angr.Project('your_binary', auto_load_libs=False)
state = proj.factory.blank_state()
ADDR = 0x700000
SIZE = 0x1000
DATA = b"MY RO CONTENTS" # <= SIZE
# 1) Map it in as RW so you can initialize it
state.memory.map(ADDR, SIZE, PROT_READ | PROT_WRITE)
# 2) Write your contents
state.memory.store(ADDR, DATA)
# 3) Strip off the write bit, leaving read-only
state.memory.protect(ADDR, SIZE, PROT_READ)
# Test
print(state.solver.eval(state.memory.load(ADDR, len(DATA)), cast_to=bytes))
# b"MY RO CONTENTS"
#
# Any further store() here would now fail:
# state.memory.store(ADDR, b"X") # => SimMemoryError
Notes
map(addr, size, perms) → maps an anonymous region with the given POSIX-style permission bits
protect(addr, size, perms) → change permissions on an already-mapped region
Which approach fits your use-case?
• If you only need to seed a blob you’ll never overwrite → add_backer is your friend.
• If you want fine control over permissions at runtime → go with map/store/protect.