Skip to content

Commit 89eb46a

Browse files
dvyukovcopybara-github
authored andcommitted
Make clang tool generate fake main
PiperOrigin-RevId: 809025458 Change-Id: I25e8c330d542b695811103c4df6d3c7d1163e8b3
1 parent 4e71a92 commit 89eb46a

4 files changed

Lines changed: 49 additions & 11 deletions

File tree

sandboxed_api/bazel/sapi.bzl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,13 +532,15 @@ def cc_sandboxed_library(
532532
lib_name = wrapper_name,
533533
sandboxee_hdr_out = name + ".sapi.sandboxee.h.unformatted",
534534
sandboxee_src_out = name + ".sapi.sandboxee.cc.unformatted",
535+
sandboxee_main_out = name + ".sapi.sandboxee_main.cc.unformatted",
535536
host_src_out = name + ".sapi.host.cc.unformatted",
536537
sapi_hdr = native.package_name() + "/_sapi_" + name + ".sapi.h",
537538
**common
538539
)
539540

540541
_clang_format_file(name + ".sapi.sandboxee.h.unformatted", name + ".sapi.sandboxee.h", **common)
541542
_clang_format_file(name + ".sapi.sandboxee.cc.unformatted", name + ".sapi.sandboxee.cc", **common)
543+
_clang_format_file(name + ".sapi.sandboxee_main.cc.unformatted", name + ".sapi.sandboxee_main.cc", **common)
542544
_clang_format_file(name + ".sapi.host.cc.unformatted", name + ".sapi.host.cc", **common)
543545

544546
cc_library(
@@ -639,6 +641,7 @@ def _sandboxed_library_gen_impl(ctx):
639641
args.append("--sapi_name={}".format(ctx.attr.lib_name))
640642
args.append("--sandboxee_hdr_out={}".format(ctx.outputs.sandboxee_hdr_out.path))
641643
args.append("--sandboxee_src_out={}".format(ctx.outputs.sandboxee_src_out.path))
644+
args.append("--sandboxee_main_out={}".format(ctx.outputs.sandboxee_main_out.path))
642645
args.append("--host_src_out={}".format(ctx.outputs.host_src_out.path))
643646
args.append("--sapi_out={}".format(ctx.attr.sapi_hdr))
644647
args.append("--sapi_limit_scan_depth")
@@ -649,7 +652,12 @@ def _sandboxed_library_gen_impl(ctx):
649652
progress_msg = "Generating sandboxed library {}.".format(ctx.attr.lib_name)
650653
ctx.actions.run(
651654
inputs = cc_ctx.headers.to_list() + cpp_toolchain.all_files.to_list(),
652-
outputs = [ctx.outputs.sandboxee_hdr_out, ctx.outputs.sandboxee_src_out, ctx.outputs.host_src_out],
655+
outputs = [
656+
ctx.outputs.sandboxee_hdr_out,
657+
ctx.outputs.sandboxee_src_out,
658+
ctx.outputs.sandboxee_main_out,
659+
ctx.outputs.host_src_out,
660+
],
653661
arguments = args,
654662
mnemonic = "SandboxedLibraryGen",
655663
progress_message = progress_msg,
@@ -665,6 +673,7 @@ _sandboxed_library_gen = rule(
665673
"lib_name": attr.string(),
666674
"sandboxee_hdr_out": attr.output(),
667675
"sandboxee_src_out": attr.output(),
676+
"sandboxee_main_out": attr.output(),
668677
"host_src_out": attr.output(),
669678
"sapi_hdr": attr.string(),
670679
"_generator": make_exec_label(

sandboxed_api/tools/clang_generator/generator_tool.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,13 @@ absl::NoDestructor<llvm::cl::opt<std::string>> g_sandboxee_src_out(
122122
"source file."),
123123
llvm::cl::cat(*g_tool_category));
124124

125+
absl::NoDestructor<llvm::cl::opt<std::string>> g_sandboxee_main_out(
126+
"sandboxee_main_out",
127+
llvm::cl::desc("Output path of the generated source file containing the "
128+
"stub main function for binary used for system call policy "
129+
"extraction."),
130+
llvm::cl::cat(*g_tool_category));
131+
125132
absl::NoDestructor<llvm::cl::opt<std::string>> g_host_src_out(
126133
"host_src_out",
127134
llvm::cl::desc(
@@ -214,6 +221,10 @@ absl::Status GeneratorMain(int argc, char* argv[]) {
214221
emitter.EmitSandboxeeSrc(options));
215222
SAPI_RETURN_IF_ERROR(file::SetContents(*g_sandboxee_src_out, sandboxee_src,
216223
file::Defaults()));
224+
SAPI_ASSIGN_OR_RETURN(std::string sandboxee_main,
225+
emitter.EmitSandboxeeMain(options));
226+
SAPI_RETURN_IF_ERROR(file::SetContents(*g_sandboxee_main_out,
227+
sandboxee_main, file::Defaults()));
217228
SAPI_ASSIGN_OR_RETURN(std::string host_src, emitter.EmitHostSrc(options));
218229
SAPI_RETURN_IF_ERROR(
219230
file::SetContents(*g_host_src_out, host_src, file::Defaults()));

sandboxed_api/tools/clang_generator/sandboxed_library_emitter.cc

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ absl::StatusOr<std::string> SandboxedLibraryEmitter::EmitSandboxeeHdr(
281281
EmitWrapperDecl(out, *func);
282282
out += ";\n\n";
283283
}
284-
return Finalize(out, true);
284+
return Finalize(out, /*is_header=*/true, /*add_includes=*/true);
285285
}
286286

287287
absl::StatusOr<std::string> SandboxedLibraryEmitter::EmitSandboxeeSrc(
@@ -315,7 +315,21 @@ absl::StatusOr<std::string> SandboxedLibraryEmitter::EmitSandboxeeSrc(
315315
}
316316
out += "}\n\n";
317317
}
318-
return Finalize(out, false);
318+
return Finalize(out, /*is_header=*/false, /*add_includes=*/true);
319+
}
320+
321+
absl::StatusOr<std::string> SandboxedLibraryEmitter::EmitSandboxeeMain(
322+
const GeneratorOptions& options) const {
323+
std::string out;
324+
for (const auto* func : SortedFuncs()) {
325+
out += absl::Substitute("extern \"C\" void $0();\n", func->name);
326+
}
327+
out += "\nint main() {\n";
328+
for (const auto* func : SortedFuncs()) {
329+
out += absl::Substitute("$0();\n", func->name);
330+
}
331+
out += "}\n";
332+
return Finalize(out, /*is_header=*/false, /*add_includes=*/false);
319333
}
320334

321335
absl::StatusOr<std::string> SandboxedLibraryEmitter::EmitHostSrc(
@@ -359,7 +373,7 @@ absl::StatusOr<std::string> SandboxedLibraryEmitter::EmitHostSrc(
359373
}
360374
out += "}\n\n";
361375
}
362-
return Finalize(out, false);
376+
return Finalize(out, /*is_header=*/false, /*add_includes=*/true);
363377
}
364378

365379
void SandboxedLibraryEmitter::EmitFuncDecl(std::string& out, const Func& func) {
@@ -401,16 +415,18 @@ void SandboxedLibraryEmitter::EmitWrapperDecl(std::string& out,
401415
}
402416

403417
absl::StatusOr<std::string> SandboxedLibraryEmitter::Finalize(
404-
const std::string& body, bool is_header) const {
418+
const std::string& body, bool is_header, bool add_includes) const {
405419
std::string out;
406420
if (is_header) {
407421
out.append(kHeaderHeader.data(), kHeaderHeader.size());
408422
}
409423
out.append(kCommonHeader.data(), kCommonHeader.size());
410-
std::vector<std::string> includes(includes_.begin(), includes_.end());
411-
std::sort(includes.begin(), includes.end());
412-
for (const auto& inc : includes) {
413-
out += absl::Substitute("#include $0\n", inc);
424+
if (add_includes) {
425+
std::vector<std::string> includes(includes_.begin(), includes_.end());
426+
std::sort(includes.begin(), includes.end());
427+
for (const auto& inc : includes) {
428+
out += absl::Substitute("#include $0\n", inc);
429+
}
414430
}
415431
out += "\n";
416432
out += body;

sandboxed_api/tools/clang_generator/sandboxed_library_emitter.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class SandboxedLibraryEmitter : public EmitterBase {
3838
const GeneratorOptions& options) const;
3939
absl::StatusOr<std::string> EmitSandboxeeSrc(
4040
const GeneratorOptions& options) const;
41+
absl::StatusOr<std::string> EmitSandboxeeMain(
42+
const GeneratorOptions& options) const;
4143
absl::StatusOr<std::string> EmitHostSrc(
4244
const GeneratorOptions& options) const;
4345

@@ -57,8 +59,8 @@ class SandboxedLibraryEmitter : public EmitterBase {
5759
absl::Status AddFunction(clang::FunctionDecl* decl) override;
5860
static void EmitFuncDecl(std::string& out, const Func& func);
5961
static void EmitWrapperDecl(std::string& out, const Func& func);
60-
absl::StatusOr<std::string> Finalize(const std::string& body,
61-
bool is_header) const;
62+
absl::StatusOr<std::string> Finalize(const std::string& body, bool is_header,
63+
bool add_includes) const;
6264
ArgPtr Convert(absl::string_view name, clang::QualType type);
6365
std::vector<const Func*> SortedFuncs() const;
6466

0 commit comments

Comments
 (0)