Skip to content

Commit

Permalink
Help with Linux only functions
Browse files Browse the repository at this point in the history
  • Loading branch information
charmoniumQ committed Nov 8, 2024
1 parent cf3bda8 commit a3289c3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 23 deletions.
4 changes: 2 additions & 2 deletions probe_src/libprobe/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
UNAME_S := $(shell uname -s)
UNAME_S := $(shell uname -s)

SOURCE_VERSION ?= $(shell git rev-parse --short HEAD)
CFLAGS_COMMON ?= -DSOURCE_VERSION=\"$(SOURCE_VERSION)\" -Wall -Wextra -pthread -fPIC $(NIX_CFLAGS_COMPILE)
Expand Down Expand Up @@ -37,7 +37,7 @@ build/lib%-dbg.$(LIB_EXTENSION): $(SOURCE_FILES) $(GENERATED_FILES)
gcc $(CFLAGS_COMMON) $(DBGCFLAGS) $(SHARED_LIB_FLAGS) $(EXTRA_LIBS) -o $@ src/lib.c

$(GENERATED_FILES): $(wildcard generator/*)
./generator/gen_libc_hooks.py
./generator/gen_libc_hooks.py $(UNAME_S)

install:
$(INSTALL_CMD)
Expand Down
63 changes: 42 additions & 21 deletions probe_src/libprobe/generator/gen_libc_hooks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3

from __future__ import annotations
import sys
import dataclasses
import pycparser # type: ignore
import pycparser.c_generator # type: ignore
Expand Down Expand Up @@ -160,6 +161,24 @@ def ptr_type(type: Node) -> pycparser.c_ast.PtrDecl:
)


def find_decl(
block: typing.Sequence[Node],
name: str,
comment: typing.Any,
) -> Decl | None:
relevant_stmts = [
stmt
for stmt in block
if isinstance(stmt, Decl) and stmt.name == name
]
if not relevant_stmts:
return None
elif len(relevant_stmts) > 1:
raise ValueError(f"Multiple definitions of {name}" + " ({})".format(comment) if comment else "")
else:
return relevant_stmts[0]


@dataclasses.dataclass(frozen=True)
class ParsedFunc:
name: str
Expand Down Expand Up @@ -248,6 +267,26 @@ def definition(self) -> pycparser.c_ast.FuncDef:
if isinstance(node, Decl) and isinstance(node.type, pycparser.c_ast.TypeDecl) and node.type.type.names == ["fn"]
},
}
if len(sys.argv) != 2:
raise RuntimeError("Need to pass exactly 1 arg")
match sys.argv[1]:
case "Darwin":
compiling_for_linux = False
case "Linux":
compiling_for_linux = True
case _:
raise RuntimeError("First argument should be uname -s")
platform_funcs = {}
for func_name, func in funcs.items():
linux_only_field = find_decl(func.stmts, "linux_only", func.name)
linux_only_field_init = None if linux_only_field is None else linux_only_field.init
linux_only = linux_only_field_init is not None and linux_only_field_init.name == "true" # type: ignore
if linux_only and compiling_for_linux:
platform_funcs[func_name] = func
elif linux_only and not compiling_for_linux:
pass
else:
platform_funcs[func_name] = func
# funcs = {
# key: val
# for key, val in list(funcs.items())
Expand All @@ -267,7 +306,7 @@ def definition(self) -> pycparser.c_ast.FuncDef:
init=None,
bitsize=None,
)
for func_name, func in funcs.items()
for func_name, func in platform_funcs.items()
]
init_function_pointers = ParsedFunc(
name="init_function_pointers",
Expand All @@ -288,7 +327,7 @@ def definition(self) -> pycparser.c_ast.FuncDef:
),
),
)
for func_name, func in funcs.items()
for func_name, func in platform_funcs.items()
],
).definition()

Expand All @@ -302,24 +341,6 @@ def raise_thunk(exception: Exception) -> typing.Callable[..., typing.NoReturn]:
return lambda *args, **kwarsg: raise_(exception)


def find_decl(
block: typing.Sequence[Node],
name: str,
comment: typing.Any,
) -> Decl | None:
relevant_stmts = [
stmt
for stmt in block
if isinstance(stmt, Decl) and stmt.name == name
]
if not relevant_stmts:
return None
elif len(relevant_stmts) > 1:
raise ValueError(f"Multiple definitions of {name}" + " ({})".format(comment) if comment else "")
else:
return relevant_stmts[0]


def wrapper_func_body(func: ParsedFunc) -> typing.Sequence[Node]:
pre_call_stmts = [
pycparser.c_ast.FuncCall(
Expand Down Expand Up @@ -419,7 +440,7 @@ def wrapper_func_body(func: ParsedFunc) -> typing.Sequence[Node]:
func,
stmts=wrapper_func_body(func),
).definition()
for _, func in funcs.items()
for _, func in platform_funcs.items()
]
pathlib.Path("generated/libc_hooks.h").write_text(
GccCGenerator().visit(
Expand Down
1 change: 1 addition & 0 deletions probe_src/libprobe/generator/libc_hooks_source.c
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ int close (int filedes) {
});
}
int close_range (unsigned int lowfd, unsigned int maxfd, int flags) {
bool linux_only = true;
void* pre_call = ({
if (flags != 0) {
NOT_IMPLEMENTED("I don't know how to handle close_rnage flags yet");
Expand Down

0 comments on commit a3289c3

Please sign in to comment.