Skip to content

Commit

Permalink
Merge pull request #238 from iadavis/extract-null-byte-string
Browse files Browse the repository at this point in the history
extract_byte_string will return none when reading i8* null byte strings.
  • Loading branch information
idavis authored May 2, 2023
2 parents acaeb54 + 881d576 commit 43de824
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
6 changes: 3 additions & 3 deletions eng/psakefile.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Properties {
task default -depends build, run-examples
task build -depends qirlib, pyqir
task checks -depends cargo-fmt, cargo-clippy, black, mypy
task manylinux -depends build-manylinux-container-image, run-manylinux-container-image, run-examples-in-containers
task manylinux -depends build-manylinux-container-image, run-manylinux-container-image, run-examples-in-containers

task run-manylinux-container-image -preaction { Write-CacheStats } -postaction { Write-CacheStats } {
$llvmDir = Resolve-InstallationDirectory
Expand Down Expand Up @@ -125,7 +125,7 @@ task check-environment {
}

Assert ((Test-InVirtualEnvironment) -eq $true) ($env_message -Join ' ')
exec { & $Python -m pip install pip~=22.3 }
exec { & $Python -m pip install pip~=23.1 }
}

task init -depends check-environment {
Expand Down Expand Up @@ -320,7 +320,7 @@ task run-examples {
& $Python bernstein_vazirani.py | Tee-Object -Variable bz_output
$bz_lines = $bz_output -join ", "
$bz_expected = "x(5), h(0), h(1), h(2), h(3), h(4), h(5), cnot(1, 5), cnot(3, 5), cnot(4, 5), h(0), h(1), h(2), h(3), h(4), mz(0, 0), mz(1, 1), mz(2, 2), mz(3, 3), mz(4, 4)"

Assert (@(Compare-Object $bz_lines $bz_expected).Length -eq 0) "Expected $bz_expected found $bz_lines"

& $Python teleport.py | Tee-Object -Variable teleport_output
Expand Down
15 changes: 15 additions & 0 deletions pyqir/tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,21 @@ def test_global_string() -> None:
assert value.decode("utf-8") == "Hello World!\0"


def test_null_i8ptr_string() -> None:
llvm_ir = """
define void @main() {
call void @a(i8* null)
ret void
}
declare void @a(i8*)
"""

module = Module.from_ir(Context(), llvm_ir, "module")
null_ptr_value = module.functions[0].basic_blocks[0].instructions[0].operands[0]
string = extract_byte_string(null_ptr_value)
assert string is None


def test_parser_zext_support() -> None:
bitcode = Path("tests/select.bc").read_bytes()
mod = Module.from_bitcode(Context(), bitcode)
Expand Down
4 changes: 4 additions & 0 deletions qirlib/src/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ pub unsafe fn global_string(module: LLVMModuleRef, value: &[u8]) -> LLVMValueRef
}

pub unsafe fn extract_string(value: LLVMValueRef) -> Option<Vec<u8>> {
if LLVMIsNull(value) != 0 {
return None;
}

if !is_byte_string(LLVMTypeOf(value)) {
return None;
}
Expand Down

0 comments on commit 43de824

Please sign in to comment.