Skip to content

Commit f8b97f3

Browse files
committed
Check that bytecode source buffer is sufficiently aligned.
Adjust test to make a copy of the source buffer that is sufficiently aligned.
1 parent a59870a commit f8b97f3

File tree

3 files changed

+60
-12
lines changed

3 files changed

+60
-12
lines changed

mlir/lib/Bytecode/Reader/BytecodeReader.cpp

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,33 @@
1111
#include "mlir/Bytecode/BytecodeImplementation.h"
1212
#include "mlir/Bytecode/BytecodeOpInterface.h"
1313
#include "mlir/Bytecode/Encoding.h"
14-
#include "mlir/IR/BuiltinDialect.h"
14+
#include "mlir/IR/AsmState.h"
1515
#include "mlir/IR/BuiltinOps.h"
1616
#include "mlir/IR/Diagnostics.h"
17+
#include "mlir/IR/Dialect.h"
1718
#include "mlir/IR/OpImplementation.h"
1819
#include "mlir/IR/Verifier.h"
1920
#include "mlir/IR/Visitors.h"
2021
#include "mlir/Support/LLVM.h"
2122
#include "mlir/Support/LogicalResult.h"
2223
#include "llvm/ADT/ArrayRef.h"
23-
#include "llvm/ADT/MapVector.h"
2424
#include "llvm/ADT/ScopeExit.h"
25-
#include "llvm/ADT/SmallString.h"
2625
#include "llvm/ADT/StringExtras.h"
2726
#include "llvm/ADT/StringRef.h"
2827
#include "llvm/Support/Endian.h"
28+
#include "llvm/Support/ErrorHandling.h"
2929
#include "llvm/Support/MemoryBufferRef.h"
30-
#include "llvm/Support/SaveAndRestore.h"
3130
#include "llvm/Support/SourceMgr.h"
31+
32+
#include <cassert>
3233
#include <cstddef>
34+
#include <cstdint>
35+
#include <cstring>
3336
#include <list>
3437
#include <memory>
3538
#include <numeric>
3639
#include <optional>
40+
#include <string>
3741

3842
#define DEBUG_TYPE "mlir-bytecode-reader"
3943

@@ -93,23 +97,31 @@ namespace {
9397
class EncodingReader {
9498
public:
9599
explicit EncodingReader(ArrayRef<uint8_t> contents, Location fileLoc)
96-
: dataIt(contents.data()), dataEnd(contents.end()), fileLoc(fileLoc) {}
100+
: buffer(contents), dataIt(buffer.begin()), fileLoc(fileLoc) {}
97101
explicit EncodingReader(StringRef contents, Location fileLoc)
98102
: EncodingReader({reinterpret_cast<const uint8_t *>(contents.data()),
99103
contents.size()},
100104
fileLoc) {}
101105

102106
/// Returns true if the entire section has been read.
103-
bool empty() const { return dataIt == dataEnd; }
107+
bool empty() const { return dataIt == buffer.end(); }
104108

105109
/// Returns the remaining size of the bytecode.
106-
size_t size() const { return dataEnd - dataIt; }
110+
size_t size() const { return buffer.end() - dataIt; }
107111

108112
/// Align the current reader position to the specified alignment.
109113
LogicalResult alignTo(unsigned alignment) {
110114
if (!llvm::isPowerOf2_32(alignment))
111115
return emitError("expected alignment to be a power-of-two");
112116

117+
// Ensure the data buffer was sufficiently aligned in the first place.
118+
if (LLVM_UNLIKELY(
119+
!llvm::isAddrAligned(llvm::Align(alignment), buffer.begin()))) {
120+
return emitError("expected bytecode buffer to be aligned to ", alignment,
121+
", but got pointer: '0x" +
122+
llvm::utohexstr((uintptr_t)buffer.begin()) + "'");
123+
}
124+
113125
// Shift the reader position to the next alignment boundary.
114126
while (uintptr_t(dataIt) & (uintptr_t(alignment) - 1)) {
115127
uint8_t padding;
@@ -320,8 +332,11 @@ class EncodingReader {
320332
return success();
321333
}
322334

323-
/// The current data iterator, and an iterator to the end of the buffer.
324-
const uint8_t *dataIt, *dataEnd;
335+
/// The bytecode buffer.
336+
ArrayRef<uint8_t> buffer;
337+
338+
/// The current iterator within the 'buffer'.
339+
const uint8_t *dataIt;
325340

326341
/// A location for the bytecode used to report errors.
327342
Location fileLoc;

mlir/unittests/Bytecode/BytecodeTest.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "mlir/Bytecode/BytecodeReader.h"
109
#include "mlir/Bytecode/BytecodeWriter.h"
1110
#include "mlir/IR/AsmState.h"
1211
#include "mlir/IR/BuiltinAttributes.h"
@@ -19,6 +18,10 @@
1918
#include "gmock/gmock.h"
2019
#include "gtest/gtest.h"
2120

21+
#include <algorithm>
22+
#include <cstdlib>
23+
#include <memory>
24+
2225
using namespace llvm;
2326
using namespace mlir;
2427

@@ -50,9 +53,18 @@ TEST(Bytecode, MultiModuleWithResource) {
5053
llvm::raw_string_ostream ostream(buffer);
5154
ASSERT_TRUE(succeeded(writeBytecodeToFile(module.get(), ostream)));
5255

56+
// Make a sufficiently aligned copy of the buffer for reading it back.
57+
ostream.flush();
58+
constexpr std::size_t kAlignment = 16; // AsmResourceBlob alignment.
59+
auto deleter = [](char *ptr) { std::free(ptr); };
60+
std::unique_ptr<char, decltype(deleter)> aligned_buffer(
61+
static_cast<char *>(std::aligned_alloc(kAlignment, buffer.size())),
62+
deleter);
63+
std::copy(buffer.begin(), buffer.end(), aligned_buffer.get());
64+
5365
// Parse it back
54-
OwningOpRef<Operation *> roundTripModule =
55-
parseSourceString<Operation *>(ostream.str(), parseConfig);
66+
OwningOpRef<Operation *> roundTripModule = parseSourceString<Operation *>(
67+
{aligned_buffer.get(), buffer.size()}, parseConfig);
5668
ASSERT_TRUE(roundTripModule);
5769

5870
// FIXME: Parsing external resources does not work on big-endian

utils/bazel/llvm-project-overlay/mlir/unittests/BUILD.bazel

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,27 @@ cc_test(
359359
],
360360
)
361361

362+
cc_test(
363+
name = "bytecode_tests",
364+
size = "small",
365+
srcs = glob([
366+
"Bytecode/*.cpp",
367+
"Bytecode/*.h",
368+
"Bytecode/*/*.cpp",
369+
"Bytecode/*/*.h",
370+
]),
371+
deps = [
372+
"//llvm:Support",
373+
"//mlir:BytecodeReader",
374+
"//mlir:BytecodeWriter",
375+
"//mlir:IR",
376+
"//mlir:Parser",
377+
"//third-party/unittest:gmock",
378+
"//third-party/unittest:gtest",
379+
"//third-party/unittest:gtest_main",
380+
],
381+
)
382+
362383
cc_test(
363384
name = "conversion_tests",
364385
size = "small",

0 commit comments

Comments
 (0)