File: link-open-file.test

package info (click to toggle)
llvm-toolchain-11 1%3A11.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 995,808 kB
  • sloc: cpp: 4,767,656; ansic: 760,916; asm: 477,436; python: 170,940; objc: 69,804; lisp: 29,914; sh: 23,855; f90: 18,173; pascal: 7,551; perl: 7,471; ml: 5,603; awk: 3,489; makefile: 2,573; xml: 915; cs: 573; fortran: 503; javascript: 452
file content (71 lines) | stat: -rw-r--r-- 2,226 bytes parent folder | download | duplicates (9)
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
## On Windows co-operative applications can be expected to open LLD's output
## with FILE_SHARE_DELETE included in the sharing mode. This allows us to link
## over the top of an existing file even if it is in use by another application.

# REQUIRES: system-windows, x86
# RUN: echo '.globl _start; _start:' > %t.s
# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-unknown %t.s -o %t.o

## FILE_SHARE_READ   = 1
## FILE_SHARE_WRITE  = 2
## FILE_SHARE_DELETE = 4

# RUN:     %python %s %t.o 7
# RUN: not %python %s %t.o 3 2>&1 | FileCheck %s
# CHECK: error: failed to write to the output file

import contextlib
import ctypes
from ctypes import wintypes as w
import os
import shutil
import subprocess
import platform
import sys
import time

object_file = sys.argv[1]
share_flags = int(sys.argv[2])

@contextlib.contextmanager
def open_with_share_flags(filename, share_flags):
    GENERIC_READ          = 0x80000000
    FILE_ATTRIBUTE_NORMAL = 0x80
    OPEN_EXISTING         = 0x3
    INVALID_HANDLE_VALUE = w.HANDLE(-1).value

    CreateFileA = ctypes.windll.kernel32.CreateFileA
    CreateFileA.restype = w.HANDLE
    h = CreateFileA(filename.encode('mbcs'), GENERIC_READ, share_flags,
                    None, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, None)

    assert h != INVALID_HANDLE_VALUE, 'Failed to open ' + filename
    try:
        yield
    finally:
        ctypes.windll.kernel32.CloseHandle(h)

## Ensure we have an empty directory for the output.
outdir = os.path.basename(__file__) + '.dir'
if os.path.exists(outdir):
    shutil.rmtree(outdir)
os.makedirs(outdir)

## Link on top of an open file.
elf = os.path.join(outdir, 'output_file.elf')
open(elf, 'wb').close()
with open_with_share_flags(elf, share_flags):
    subprocess.check_call(['ld.lld.exe', object_file, '-o', elf])

## Check the linker wrote the output file.
with open(elf, 'rb') as f:
    assert f.read(4) == b'\x7fELF', "linker did not write output file correctly"

## Check no temp files are left around.
## It might take a while for Windows to remove them, so loop.
deleted = lambda: len(os.listdir(outdir)) == 1
for _ in range(10):
    if not deleted():
        time.sleep (1)

assert deleted(), "temp file(s) not deleted after grace period"