Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to start interactive server for Parser dialect conversion #768

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/vast/Conversion/Parser/Passes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "vast/Util/Warnings.hpp"

#include "vast/server/server.hpp"

VAST_RELAX_WARNINGS
#include <mlir/Pass/Pass.h>
#include <mlir/Pass/PassManager.h>
Expand All @@ -17,6 +19,6 @@

// Generate the code for registering passes.
#define GEN_PASS_REGISTRATION
#include "vast/Conversion/Parser/Passes.h.inc"

Check failure on line 22 in include/vast/Conversion/Parser/Passes.hpp

View workflow job for this annotation

GitHub Actions / cpp-linter (19, 22.04)

include/vast/Conversion/Parser/Passes.hpp:22:14 [clang-diagnostic-error]

'vast/Conversion/Parser/Passes.h.inc' file not found

} // namespace vast
3 changes: 3 additions & 0 deletions include/vast/Conversion/Parser/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ def HLToParser : Pass<"vast-hl-to-parser", "core::ModuleOp"> {
let options = [
Option< "config", "config", "std::string", "",
"Configuration file for parser transformation."
>,
Option< "socket", "socket", "std::string", "",
"Unix socket path to use for server"
>
];

Expand Down
103 changes: 103 additions & 0 deletions include/vast/server/io.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Copyright (c) 2024-present, Trail of Bits, Inc.

#pragma once

#include <memory>
#include <span>
#include <stdexcept>
#include <string>

#include <stdio.h>

#include <gap/core/crtp.hpp>

#include "vast/Util/Warnings.hpp"

namespace vast::server {
class connection_closed : public std::runtime_error
{
public:
connection_closed() : std::runtime_error("Connection closed") {}

connection_closed(const char *what) : std::runtime_error(what) {}
};

struct io_adapter
{
virtual ~io_adapter() = default;

virtual void close() {}

virtual size_t read_some(std::span< char > dst) = 0;
virtual size_t write_some(std::span< const char > dst) = 0;

// Upon completion, `dst` is filled with data.
void read_all(std::span< char > dst) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not work as dst is not output parameter and you overwrite it.

You are only lucky it is used with single character span.

Also why to introduce unnecessary complexity when only thing you use is read_byte? read_some is also nowhere else used then in read of single byte.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the thing about dst being overwritten

re read_some: read_some is the only semantic that most APIs offer, but for convenience sake I also want read_all semantics, for example when reading a request's body.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you expect to be in dst at the end of read_all? You rewrite it at every iteration what is invisible from outside.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dst is a span -- it's essentially a pointer char*. It's equivalent to

void read_all(char *start, char *end) {
  while(start != end) {
    do_something(start);
    ++start;
  }
}

dst.subspan returns a new span that points to the same data but nread characters ahead

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I misunderstood it. I guess it works :)

while (!dst.empty()) {
size_t nread = read_some(dst);
dst = dst.subspan(nread);
}
}

// Upon completion, all of the data in `src` is written to the client..
void write_all(std::span< const char > src) {
while (!src.empty()) {
size_t nwritten = write_some(src);
src = src.subspan(nwritten);
}
}

char read() {
char res[1];
read_all(res);
return res[0];
}
};

class file_adapter final : public io_adapter
{
FILE *ifd;
FILE *ofd;

public:
file_adapter(FILE *ifd = stdin, FILE *ofd = stdout) : ifd(ifd), ofd(ofd) {
VAST_ASSERT(ifd != nullptr);
VAST_ASSERT(ofd != nullptr);

setvbuf(ofd, NULL, _IONBF, 0);
}

size_t read_some(std::span< char > dst) override {
size_t nread = fread(dst.data(), 1, dst.size_bytes(), ifd);
if (nread == 0 && (feof(ifd) || ferror(ifd))) {
throw connection_closed{};
}
return nread;
}

size_t write_some(std::span< const char > src) override {
size_t nwritten = fwrite(src.data(), 1, src.size_bytes(), ofd);
if (src.size() != 0 && nwritten == 0) {
throw connection_closed{};
}
return nwritten;
}
};

class sock_adapter final : public io_adapter
{
public:
struct impl;

size_t read_some(std::span< char > dst) override;
size_t write_some(std::span< const char > src) override;
~sock_adapter();
void close() override;

static std::unique_ptr< sock_adapter > create_unix_socket(const std::string &path);

private:
std::unique_ptr< struct impl > pimpl;
sock_adapter(std::unique_ptr< impl > pimpl);
};
} // namespace vast::server
Loading
Loading